fix problems in Queue.c / Stack.c ; still issues: to investigate
[cgds.git] / test / t.Queue.c
CommitLineData
a7868768
BA
1#include <stdlib.h>
2#include "cgds/Queue.h"
3#include "test/helpers.h"
4#include "test/lut.h"
5
6void t_queue_clear() //FTEST
7{
8 Queue* q = queue_new(int);
9
10 queue_push(q, 0);
11 queue_push(q, 0);
12 queue_push(q, 0);
13
14 queue_clear(q);
15 lu_assert(queue_empty(q));
16
17 queue_destroy(q);
18}
19
20void t_queue_size() //FTEST
21{
22 Queue* q = queue_new(int);
23
24 queue_push(q, 0);
25 queue_push(q, 0);
26 queue_push(q, 0);
27 lu_assert_int_eq(queue_size(q), 3);
28
29 queue_push(q, 0);
30 queue_push(q, 0);
31 lu_assert_int_eq(queue_size(q), 5);
32
33 queue_push(q, 0);
34 queue_push(q, 0);
35 queue_push(q, 0);
36 lu_assert_int_eq(queue_size(q), 8);
37
38 queue_destroy(q);
39}
40
41void t_queue_push_pop_basic() //FTEST
42{
43 int n = 10;
44
45 Queue* q = queue_new(double);
46 for (int i = 0; i < n; i++) queue_push(q, (double) i);
47 // iterate and check values
48 double ckValue = 0.0;
49 while (!queue_empty(q))
50 {
51 double d;
52 queue_peek(q, d);
53 lu_assert_dbl_eq(d, ckValue);
54 ckValue += 1.0;
55 queue_pop(q);
56 }
57
58 lu_assert(queue_empty(q));
59 queue_destroy(q);
60}
61
62void t_queue_push_pop_evolved() //FTEST
63{
64 int n = 10;
65
66 Queue* q = queue_new(StructTest1);
67 StructTest1* st1 = (StructTest1*) safe_malloc(n * sizeof (StructTest1));
68 for (int i = 0; i < n; i++)
69 {
70 st1[i].a = rand() % 42;
71 st1[i].b = (double) rand() / RAND_MAX;
72 queue_push(q, *(st1 + i));
73 }
74 for (int i = 0; i < n; i++)
75 {
76 StructTest1 st1Cell;
77 queue_peek(q, st1Cell);
78 lu_assert_int_eq(st1Cell.a, st1[i].a);
79 lu_assert_dbl_eq(st1Cell.b, st1[i].b);
80 queue_pop(q);
81 }
82 safe_free(st1);
83 queue_destroy(q);
84
85 q = queue_new(StructTest2*);
86 StructTest2* st2 = (StructTest2*) safe_malloc(n * sizeof (StructTest2));
87 for (int i = 0; i < n; i++)
88 {
89 st2[i].a = (float) rand() / RAND_MAX;
90 st2[i].b = (StructTest1*) safe_malloc(sizeof (StructTest1));
91 st2[i].b->a = rand() % 42;
92 st2[i].b->b = (double) rand() / RAND_MAX;
93 queue_push(q, st2 + i);
94 }
95 for (int i = 0; i < n; i++)
96 {
97 StructTest2* st2Cell;
98 queue_peek(q, st2Cell);
99 lu_assert_dbl_eq(st2Cell->a, st2[i].a);
100 lu_assert_int_eq(st2Cell->b->a, st2[i].b->a);
101 lu_assert_dbl_eq(st2Cell->b->b, st2[i].b->b);
102 queue_pop(q);
103 safe_free(st2Cell->b);
104 }
105 safe_free(st2);
106 queue_destroy(q);
107}
108
109void t_queue_copy() //FTEST
110{
111 int n = 10;
112
113 Queue* q = queue_new(int);
114 for (int i = 0; i < n; i++)
115 queue_push(q, rand() % 42);
116 Queue* qc = queue_copy(q);
117
012c97dd 118 lu_assert_int_eq(queue_size(q), queue_size(qc));
a7868768
BA
119 int a, b;
120 for (int i = 0; i < n; i++)
121 {
122 queue_peek(q, a);
123 queue_peek(qc, b);
124 lu_assert_int_eq(a, b);
125 queue_pop(q);
126 queue_pop(qc);
127 }
128 queue_destroy(q);
129 queue_destroy(qc);
130}