Commit | Line | Data |
---|---|---|
a7868768 BA |
1 | #include <stdlib.h> |
2 | #include "cgds/Stack.h" | |
aef8a996 BA |
3 | #include "helpers.h" |
4 | #include "lut.h" | |
a7868768 | 5 | |
4029c991 | 6 | void t_stack_clear() |
a7868768 | 7 | { |
e45132ac | 8 | Stack* s = stack_new(int); |
a7868768 | 9 | |
e45132ac BA |
10 | stack_push(s, 0); |
11 | stack_push(s, 0); | |
12 | stack_push(s, 0); | |
a7868768 | 13 | |
e45132ac BA |
14 | stack_clear(s); |
15 | lu_assert(stack_empty(s)); | |
a7868768 | 16 | |
e45132ac | 17 | stack_destroy(s); |
a7868768 BA |
18 | } |
19 | ||
4029c991 | 20 | void t_stack_size() |
a7868768 | 21 | { |
e45132ac | 22 | Stack* s = stack_new(int); |
a7868768 | 23 | |
e45132ac BA |
24 | stack_push(s, 0); |
25 | stack_push(s, 0); | |
26 | stack_push(s, 0); | |
27 | lu_assert_int_eq(stack_size(s), 3); | |
a7868768 | 28 | |
e45132ac BA |
29 | stack_push(s, 0); |
30 | stack_push(s, 0); | |
31 | lu_assert_int_eq(stack_size(s), 5); | |
a7868768 | 32 | |
e45132ac BA |
33 | stack_push(s, 0); |
34 | stack_push(s, 0); | |
35 | stack_push(s, 0); | |
36 | lu_assert_int_eq(stack_size(s), 8); | |
a7868768 | 37 | |
e45132ac | 38 | stack_destroy(s); |
a7868768 BA |
39 | } |
40 | ||
4029c991 | 41 | void t_stack_push_pop_basic() |
a7868768 BA |
42 | { |
43 | ||
e45132ac BA |
44 | int n = 10; |
45 | ||
46 | Stack* s = stack_new(double); | |
47 | for (int i = 0; i < n; i++) | |
48 | stack_push(s, (double) i); | |
49 | // iterate and check values | |
50 | double ckValue = n - 1; | |
51 | while (!stack_empty(s)) | |
52 | { | |
53 | double d; | |
54 | stack_top(s, d); | |
55 | lu_assert_dbl_eq(d, ckValue); | |
56 | ckValue -= 1.0; | |
57 | stack_pop(s); | |
58 | } | |
59 | ||
60 | lu_assert(stack_empty(s)); | |
61 | stack_destroy(s); | |
a7868768 BA |
62 | } |
63 | ||
4029c991 | 64 | void t_stack_push_pop_evolved() |
a7868768 | 65 | { |
e45132ac BA |
66 | Stack* s = stack_new(StructTest1); |
67 | ||
68 | int n = 10; | |
69 | StructTest1* st1 = (StructTest1*) malloc(n * sizeof (StructTest1)); | |
70 | for (int i = n - 1; i >= 0; i--) | |
71 | { | |
72 | st1[i].a = rand() % 42; | |
73 | st1[i].b = (double) rand() / RAND_MAX; | |
74 | stack_push(s, *(st1 + i)); | |
75 | } | |
76 | for (int i = 0; i < n; i++) | |
77 | { | |
78 | StructTest1 st1Cell; | |
79 | stack_top(s, st1Cell); | |
80 | lu_assert_int_eq(st1Cell.a, st1[i].a); | |
81 | lu_assert_dbl_eq(st1Cell.b, st1[i].b); | |
82 | stack_pop(s); | |
83 | } | |
84 | safe_free(st1); | |
85 | stack_destroy(s); | |
86 | ||
87 | s = stack_new(StructTest2*); | |
88 | StructTest2* st2 = (StructTest2*) malloc(n * sizeof (StructTest2)); | |
89 | for (int i = n - 1; i >= 0; i--) | |
90 | { | |
91 | st2[i].a = (float) rand() / RAND_MAX; | |
92 | st2[i].b = (StructTest1*) malloc(sizeof (StructTest1)); | |
93 | st2[i].b->a = rand() % 42; | |
94 | st2[i].b->b = (double) rand() / RAND_MAX; | |
95 | stack_push(s, st2 + i); | |
96 | } | |
97 | for (int i = 0; i < n; i++) | |
98 | { | |
99 | StructTest2* st2Cell; | |
100 | stack_top(s, st2Cell); | |
101 | lu_assert_dbl_eq(st2Cell->a, st2[i].a); | |
102 | lu_assert_int_eq(st2Cell->b->a, st2[i].b->a); | |
103 | lu_assert_dbl_eq(st2Cell->b->b, st2[i].b->b); | |
104 | stack_pop(s); | |
105 | safe_free(st2Cell->b); | |
106 | } | |
107 | safe_free(st2); | |
108 | stack_destroy(s); | |
a7868768 BA |
109 | } |
110 | ||
4029c991 | 111 | void t_stack_copy() |
a7868768 | 112 | { |
e45132ac BA |
113 | int n = 10; |
114 | ||
115 | Stack* s = stack_new(int); | |
116 | for (int i = 0; i < n; i++) | |
117 | stack_push(s, rand() % 42); | |
118 | Stack* sc = stack_copy(s); | |
119 | ||
120 | lu_assert_int_eq(stack_size(s), stack_size(sc)); | |
121 | int a, b; | |
122 | for (int i = 0; i < n; i++) | |
123 | { | |
124 | stack_top(s, a); | |
125 | stack_top(sc, b); | |
126 | lu_assert_int_eq(a, b); | |
127 | stack_pop(s); | |
128 | stack_pop(sc); | |
129 | } | |
130 | stack_destroy(s); | |
131 | stack_destroy(sc); | |
a7868768 | 132 | } |