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