initial commit
[cgds.git] / test / t.BufferTop.c
CommitLineData
a7868768
BA
1#include <stdlib.h>
2#include "cgds/BufferTop.h"
3#include "test/helpers.h"
4#include "test/lut.h"
5
6void t_buffertop_clear() //FTEST
7{
8 BufferTop* bt = buffertop_new(int, 10, MIN_T, 2);
9
10 // NOTE: items with same values are supported;
11 // since it is unused in this test, we arbitrarily choose 0.0
12 buffertop_tryadd(bt, 0, 0.0);
13 buffertop_tryadd(bt, 0, 0.0);
14 buffertop_tryadd(bt, 0, 0.0);
15
16 buffertop_clear(bt);
17 lu_assert(buffertop_empty(bt));
18
19 buffertop_destroy(bt);
20}
21
22void t_buffertop_size() //FTEST
23{
24 BufferTop* bt = buffertop_new(double, 10, MAX_T, 3);
25
26 buffertop_tryadd(bt, 0.0, 0.0);
27 buffertop_tryadd(bt, 0.0, 0.0);
28 buffertop_tryadd(bt, 0.0, 0.0);
29 lu_assert_int_eq(buffertop_size(bt), 3);
30
31 buffertop_tryadd(bt, 0.0, 0.0);
32 buffertop_tryadd(bt, 0.0, 0.0);
33 lu_assert_int_eq(buffertop_size(bt), 5);
34
35 buffertop_tryadd(bt, 0.0, 0.0);
36 buffertop_tryadd(bt, 0.0, 0.0);
37 buffertop_tryadd(bt, 0.0, 0.0);
38 lu_assert_int_eq(buffertop_size(bt), 8);
39 buffertop_destroy(bt);
40
41 // Now try to add beyond capacity, with reorganizing
42 bt = buffertop_new(double, 3, MAX_T, 2);
43 buffertop_tryadd(bt, 0.0, 0.0);
44 buffertop_tryadd(bt, 0.0, 1.0);
45 buffertop_tryadd(bt, 0.0, 2.0);
46 buffertop_tryadd(bt, 0.0, 3.0);
47 buffertop_tryadd(bt, 0.0, 4.0);
48 buffertop_tryadd(bt, 0.0, 5.0);
49 buffertop_tryadd(bt, 0.0, 6.0);
50 buffertop_tryadd(bt, 0.0, 7.0);
51 lu_assert_int_eq(buffertop_size(bt), 3);
52
53 buffertop_clear(bt);
54 lu_assert(buffertop_empty(bt));
55 buffertop_destroy(bt);
56}
57
58void t_buffertop_push_pop_basic() //FTEST
59{
60 BufferTop* bt = buffertop_new(int, 5, MIN_T, 3);
61
62 buffertop_tryadd(bt, 1, 2.0);
63 buffertop_tryadd(bt, 2, 6.0);
64 buffertop_tryadd(bt, 3, 4.0);
65 buffertop_tryadd(bt, 4, 8.0);
66 buffertop_tryadd(bt, 5, 3.0);
67 buffertop_tryadd(bt, 6, 1.0);
68 buffertop_tryadd(bt, 7, 5.0);
69 buffertop_tryadd(bt, 8, 7.0);
70
71 int a;
72 buffertop_first(bt, a);
73 lu_assert_int_eq(a, 7); //7 -> 5.0
74 buffertop_pop(bt);
75 buffertop_first(bt, a);
76 lu_assert_int_eq(a, 3); //3 -> 4.0
77 buffertop_pop(bt);
78 buffertop_first(bt, a);
79 lu_assert_int_eq(a, 5); //5 -> 3.0
80 buffertop_pop(bt);
81 buffertop_first(bt, a);
82 lu_assert_int_eq(a, 1); //1 -> 2.0
83 buffertop_pop(bt);
84 buffertop_first(bt, a);
85 lu_assert_int_eq(a, 6); //6 has "highest" priority (1.0, MIN_T)
86 buffertop_pop(bt);
87
88 lu_assert(buffertop_empty(bt));
89 buffertop_destroy(bt);
90}
91
92void t_buffertop_push_pop_evolved() //FTEST
93{
94 int n = 10;
95
96 BufferTop* bt = buffertop_new(StructTest1, 5, MAX_T, 2);
97 StructTest1* st1 = (StructTest1*) safe_malloc(n * sizeof (StructTest1));
98 for (int i = n - 1; i >= 0; i--)
99 {
100 st1[i].a = i;
101 st1[i].b = (double) rand() / RAND_MAX;
102 buffertop_tryadd(bt, *(st1 + i), i); //item i has value i
103 }
104 for (int i = n - buffertop_size(bt); i < n; i++)
105 {
106 StructTest1 st1Cell;
107 buffertop_first(bt, st1Cell);
108 lu_assert_int_eq(st1Cell.a, i);
109 buffertop_pop(bt);
110 }
111 safe_free(st1);
112 buffertop_destroy(bt);
113
114 bt = buffertop_new(StructTest2*, 15, MAX_T, 4);
115 StructTest2* st2 = (StructTest2*) safe_malloc(n * sizeof (StructTest2));
116 for (int i = n - 1; i >= 0; i--)
117 {
118 st2[i].a = (float) rand() / RAND_MAX;
119 st2[i].b = (StructTest1*) safe_malloc(sizeof (StructTest1));
120 st2[i].b->a = i;
121 st2[i].b->b = (double) rand() / RAND_MAX;
122 // item i has value i+1 if i is even, i-1 if i is odd
123 // that is to say, values are 1 0 3 2 5 4 ...
124 buffertop_tryadd(bt, st2 + i, i % 2 == 0 ? i + 1 : i - 1);
125 }
126 for (int i = 0; i < n; i++)
127 {
128 StructTest2* st2Cell;
129 buffertop_first(bt, st2Cell);
130 // NOTE: i |--> i%2==0 ? i+1 : i-1 is an involution
131 lu_assert_int_eq(st2Cell->b->a, i % 2 == 0 ? i + 1 : i - 1);
132 buffertop_pop(bt);
133 safe_free(st2Cell->b);
134 }
135 safe_free(st2);
136 buffertop_destroy(bt);
137}
138
139void t_buffertop_copy() //FTEST
140{
141 int n = 10;
142
143 BufferTop* bt = buffertop_new(int, n, MIN_T, 3);
144 for (int i = 0; i < n; i++)
145 buffertop_tryadd(bt, rand() % 42, (double) rand() / RAND_MAX);
146 BufferTop* btc = buffertop_copy(bt);
147
148 lu_assert_int_eq(buffertop_size(bt), buffertop_size(btc));
149 int a, b;
150 for (int i = 0; i < n; i++)
151 {
152 buffertop_first(bt, a);
153 buffertop_first(btc, b);
154 lu_assert_int_eq(a, b);
155 buffertop_pop(bt);
156 buffertop_pop(btc);
157 }
158 buffertop_destroy(bt);
159 buffertop_destroy(btc);
160}