-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestStack.c
70 lines (60 loc) · 1.76 KB
/
TestStack.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Stack.h"
int main() {
Stack* S = stack_create();
// test capacity
assert(stack_empty(S));
assert(stack_size(S) == 0);
// test access and modifiers
int count = 5;
char** strArr = malloc(count * sizeof(char*));
strArr[0] = "Hello ";
strArr[1] = "World! ";
strArr[2] = "How ";
strArr[3] = "are ";
strArr[4] = "you?";
stack_push(S, strArr[0]);
assert(!stack_empty(S));
assert(stack_size(S) == 1);
assert(stack_top(S) == strArr[0]);
for (int i = 1; i < count; ++i) {
stack_push(S, strArr[i]);
}
assert(!stack_empty(S));
assert(stack_size(S) == count);
assert(stack_top(S) == strArr[count - 1]);
for (int i = count - 1; i >= 0; --i) {
assert(stack_pop(S) == strArr[i]);
}
assert(stack_empty(S));
assert(stack_size(S) == 0);
int intArr[] = {1, 2, 3};
stack_push(S, strArr[0]);
stack_push(S, &(intArr[0]));
stack_push(S, strArr[2]);
stack_push(S, &(intArr[1]));
stack_push(S, strArr[4]);
stack_push(S, &(intArr[2]));
assert(stack_pop(S) == &(intArr[2]));
assert(stack_pop(S) == strArr[4]);
assert(stack_pop(S) == &(intArr[1]));
assert(stack_pop(S) == strArr[2]);
assert(stack_pop(S) == &(intArr[0]));
assert(stack_pop(S) == strArr[0]);
stack_push(S, strArr[0]);
assert(stack_pop(S) == strArr[0]);
stack_push(S, &(intArr[0]));
assert(stack_top(S) == &(intArr[0]));
stack_push(S, strArr[2]);
assert(stack_top(S) == strArr[2]);
assert(stack_pop(S) == strArr[2]);
assert(!stack_empty(S));
assert(stack_size(S) == 1);
assert(stack_top(S) == &(intArr[0]));
stack_destroy(S);
free(strArr);
S = NULL;
return 0;
}