Some fixes + improvements (Vector) + code reformatting
[cgds.git] / test / t.Stack.c
... / ...
CommitLineData
1#include <stdlib.h>
2#include "cgds/Stack.h"
3#include "helpers.h"
4#include "lut.h"
5
6void t_stack_clear()
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
20void t_stack_size()
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
41void t_stack_push_pop_basic()
42{
43
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);
62}
63
64void t_stack_push_pop_evolved()
65{
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);
109}
110
111void t_stack_copy()
112{
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);
132}