forked from btmills/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_test.c
94 lines (81 loc) · 1.89 KB
/
stack_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define STACKSIZE 100
int main()
{
Stack strs;
Stack ints;
char* str = NULL;
int val = 0;
int i;
int *iptr;
stackInit(&strs, STACKSIZE);
stackInit(&ints, STACKSIZE);
// Test strings
printf("Test strings:\n");
str = "this";
printf("Pushing \"%s\"\n", str);
stackPush(&strs, str);
printf("Top of stack: \"%s\"\n", (char*)stackTop(&strs));
str = "is";
printf("Pushing \"%s\"\n", str);
stackPush(&strs, str);
printf("Top of stack: \"%s\"\n", (char*)stackTop(&strs));
str = "a";
printf("Pushing \"%s\"\n", str);
stackPush(&strs, str);
printf("Top of stack: \"%s\"\n", (char*)stackTop(&strs));
str = "stack";
printf("Pushing \"%s\"\n", str);
stackPush(&strs, str);
printf("Top of stack: \"%s\"\n", (char*)stackTop(&strs));
printf("Stack before stackPop():\n");
for(i = stackSize(&strs) - 1; i >= 0; i--)
{
printf("\"%s\"\n", (char*)(strs.content[i]));
}
str = stackPop(&strs);
printf("stackPop() returned \"%s\"\n", str);
printf("Remainder of stack:\n");
while(stackSize(&strs))
{
str = (char*)stackPop(&strs);
printf("\"%s\"\n", str);
}
stackFree(&strs);
// Test ints
printf("\n\nTest ints:\n");
for(i = 1; i <= 4; i++)
{
int *add = (int*)malloc(sizeof(int));
if (add == NULL)
{
printf("Cannot allocate memory, poping stack\n");
goto error_pop;
}
*add = i;
printf("Pushing %d\n", *add);
stackPush(&ints, add);
printf("Top of stack: %d\n", *(int*)stackTop(&ints));
}
printf("Stack before stackPop():\n");
for(i = stackSize(&ints) - 1; i >= 0; i--)
{
printf("%d\n", *((int*)(ints.content[i])));
}
iptr = (int*)stackPop(&ints);
printf("stackPop() returned %d\n", *iptr);
free(iptr);
printf("Remainder of stack:\n");
error_pop:
while(stackSize(&ints))
{
int *rem = (int*)stackPop(&ints);
val = (rem != NULL ? *rem : -1);
printf("%d\n", val);
free(rem);
}
stackFree(&ints);
return 0;
}