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