2 #include "cgds/Stack.h"
3 #include "test/helpers.h"
6 void t_stack_clear() //FTEST
8 Stack
* s
= stack_new(int);
15 lu_assert(stack_empty(s
));
20 void t_stack_size() //FTEST
22 Stack
* s
= stack_new(int);
27 lu_assert_int_eq(stack_size(s
), 3);
31 lu_assert_int_eq(stack_size(s
), 5);
36 lu_assert_int_eq(stack_size(s
), 8);
41 void t_stack_push_pop_basic() //FTEST
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
))
54 lu_assert_dbl_eq(d
, ckValue
);
59 lu_assert(stack_empty(s
));
63 void t_stack_push_pop_evolved() //FTEST
65 Stack
* s
= stack_new(StructTest1
);
68 StructTest1
* st1
= (StructTest1
*) malloc(n
* sizeof (StructTest1
));
69 for (int i
= n
- 1; i
>= 0; i
--)
71 st1
[i
].a
= rand() % 42;
72 st1
[i
].b
= (double) rand() / RAND_MAX
;
73 stack_push(s
, *(st1
+ i
));
75 for (int i
= 0; i
< n
; i
++)
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
);
86 s
= stack_new(StructTest2
*);
87 StructTest2
* st2
= (StructTest2
*) malloc(n
* sizeof (StructTest2
));
88 for (int i
= n
- 1; i
>= 0; i
--)
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
);
96 for (int i
= 0; i
< n
; i
++)
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
);
104 safe_free(st2Cell
->b
);
110 void t_stack_copy() //FTEST
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
);
119 lu_assert_int_eq(s
->size
, sc
->size
);
121 for (int i
= 0; i
< n
; i
++)
125 lu_assert_int_eq(a
, b
);