1 #include "test.h"
2 
3 #include <alloc.h>
4 #include <stack.h>
5 
test_push_pop_top(void)6 static void test_push_pop_top(void)
7 {
8     Stack *stack = StackNew(0, free);
9 
10     StackPush(stack, xstrdup("1"));
11     assert_string_equal("1", StackTop(stack));
12 
13     StackPush(stack, xstrdup("2"));
14     assert_string_equal("2", StackTop(stack));
15 
16     StackPush(stack, xstrdup("3"));
17     assert_string_equal("3", StackTop(stack));
18 
19     char *str1 = StackPop(stack);
20     assert_string_equal("2", StackTop(stack));
21 
22     char *str2 = StackPop(stack);
23     assert_string_equal("1", StackTop(stack));
24 
25     char *str3 = StackPop(stack);
26     assert_int_equal(NULL, StackTop(stack));
27 
28     assert_int_equal(strcmp(str1, "3"), 0);
29     assert_int_equal(strcmp(str2, "2"), 0);
30     assert_int_equal(strcmp(str3, "1"), 0);
31 
32     free(str1);
33     free(str2);
34     free(str3);
35 
36     StackDestroy(stack);
37 }
38 
test_pop_empty_and_push_null(void)39 static void test_pop_empty_and_push_null(void)
40 {
41     Stack *stack = StackNew(1, NULL);
42 
43     assert(StackIsEmpty(stack));
44 
45     void *i_am_null = StackPop(stack);
46     assert(i_am_null == NULL);
47     StackPush(stack, i_am_null);
48     assert(StackPop(stack) == NULL);
49 
50     StackDestroy(stack);
51 }
52 
test_copy(void)53 static void test_copy(void)
54 {
55     Stack *stack = StackNew(4, free);
56 
57     StackPush(stack, xstrdup("1"));
58     StackPush(stack, xstrdup("2"));
59     StackPush(stack, xstrdup("3"));
60 
61     Stack *new_stack = StackCopy(stack);
62 
63     assert(new_stack != NULL);
64     assert_int_equal(StackCount(stack), StackCount(new_stack));
65     assert_int_equal(StackCapacity(stack), StackCapacity(new_stack));
66 
67     char *old_str1 = StackPop(stack); char *new_str1 = StackPop(new_stack);
68     char *old_str2 = StackPop(stack); char *new_str2 = StackPop(new_stack);
69     char *old_str3 = StackPop(stack); char *new_str3 = StackPop(new_stack);
70 
71     assert(old_str1 == new_str1);
72     assert(old_str2 == new_str2);
73     assert(old_str3 == new_str3);
74 
75     free(old_str1);
76     free(old_str2);
77     free(old_str3);
78 
79     StackSoftDestroy(stack);
80 
81     // Tests expanding the copied stack
82     StackPush(new_stack, xstrdup("1"));
83     StackPush(new_stack, xstrdup("2"));
84     StackPush(new_stack, xstrdup("3"));
85     StackPush(new_stack, xstrdup("4"));
86     StackPush(new_stack, xstrdup("5"));
87 
88     assert_int_equal(StackCount(new_stack), 5);
89     assert_int_equal(StackCapacity(new_stack), 8);
90 
91     new_str1 = StackPop(new_stack);
92     new_str2 = StackPop(new_stack);
93     new_str3 = StackPop(new_stack);
94     char *new_str4 = StackPop(new_stack);
95     char *new_str5 = StackPop(new_stack);
96 
97     assert_int_equal(strcmp(new_str1, "5"), 0);
98     assert_int_equal(strcmp(new_str2, "4"), 0);
99     assert_int_equal(strcmp(new_str3, "3"), 0);
100     assert_int_equal(strcmp(new_str4, "2"), 0);
101     assert_int_equal(strcmp(new_str5, "1"), 0);
102 
103     free(new_str1);
104     free(new_str2);
105     free(new_str3);
106     free(new_str4);
107     free(new_str5);
108 
109     StackDestroy(new_stack);
110 }
111 
test_push_report_count(void)112 static void test_push_report_count(void)
113 {
114     Stack *stack = StackNew(0, free);
115 
116     size_t size1 = StackPushReportCount(stack, xstrdup("1"));
117     size_t size2 = StackPushReportCount(stack, xstrdup("2"));
118     size_t size3 = StackPushReportCount(stack, xstrdup("3"));
119     size_t size4 = StackPushReportCount(stack, xstrdup("4"));
120 
121     assert_int_equal(size1, 1);
122     assert_int_equal(size2, 2);
123     assert_int_equal(size3, 3);
124     assert_int_equal(size4, 4);
125 
126     StackDestroy(stack);
127 }
128 
test_expand(void)129 static void test_expand(void)
130 {
131     Stack *stack = StackNew(1, free);
132 
133     StackPush(stack, xstrdup("spam"));
134     StackPush(stack, xstrdup("spam"));
135     StackPush(stack, xstrdup("spam"));
136     StackPush(stack, xstrdup("spam"));
137     StackPush(stack, xstrdup("spam"));
138     StackPush(stack, xstrdup("spam"));
139     StackPush(stack, xstrdup("spam"));
140     StackPush(stack, xstrdup("spam"));
141     StackPush(stack, xstrdup("spam"));
142 
143     assert_int_equal(StackCount(stack), 9);
144     assert_int_equal(StackCapacity(stack), 16);
145 
146     StackDestroy(stack);
147 }
148 
main()149 int main()
150 {
151     PRINT_TEST_BANNER();
152     const UnitTest tests[] =
153     {
154         unit_test(test_push_pop_top),
155         unit_test(test_pop_empty_and_push_null),
156         unit_test(test_copy),
157         unit_test(test_push_report_count),
158         unit_test(test_expand),
159     };
160     return run_tests(tests);
161 }
162