1c390cd83d8fa841408b1ca236d27a46c0b81b93
2 #include "cgds/BufferTop.h"
6 void t_buffertop_clear()
8 BufferTop
* bt
= buffertop_new(int, 10, MIN_T
, 2);
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);
17 lu_assert(buffertop_empty(bt
));
19 buffertop_destroy(bt
);
22 void t_buffertop_size()
24 BufferTop
* bt
= buffertop_new(double, 10, MAX_T
, 3);
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);
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);
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
);
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);
54 lu_assert(buffertop_empty(bt
));
55 buffertop_destroy(bt
);
58 void t_buffertop_push_pop_basic()
60 BufferTop
* bt
= buffertop_new(int, 5, MIN_T
, 3);
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);
72 buffertop_first(bt
, a
);
73 lu_assert_int_eq(a
, 7); //7 -> 5.0
75 buffertop_first(bt
, a
);
76 lu_assert_int_eq(a
, 3); //3 -> 4.0
78 buffertop_first(bt
, a
);
79 lu_assert_int_eq(a
, 5); //5 -> 3.0
81 buffertop_first(bt
, a
);
82 lu_assert_int_eq(a
, 1); //1 -> 2.0
84 buffertop_first(bt
, a
);
85 lu_assert_int_eq(a
, 6); //6 has "highest" priority (1.0, MIN_T)
88 lu_assert(buffertop_empty(bt
));
89 buffertop_destroy(bt
);
92 void t_buffertop_push_pop_evolved()
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
--)
101 st1
[i
].b
= (double) rand() / RAND_MAX
;
102 buffertop_tryadd(bt
, *(st1
+ i
), i
); //item i has value i
104 for (int i
= n
- buffertop_size(bt
); i
< n
; i
++)
107 buffertop_first(bt
, st1Cell
);
108 lu_assert_int_eq(st1Cell
.a
, i
);
112 buffertop_destroy(bt
);
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
--)
118 st2
[i
].a
= (float) rand() / RAND_MAX
;
119 st2
[i
].b
= (StructTest1
*) safe_malloc(sizeof (StructTest1
));
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);
126 for (int i
= 0; i
< n
; i
++)
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);
133 safe_free(st2Cell
->b
);
136 buffertop_destroy(bt
);
139 void t_buffertop_copy()
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
);
148 lu_assert_int_eq(buffertop_size(bt
), buffertop_size(btc
));
150 for (int i
= 0; i
< n
; i
++)
152 buffertop_first(bt
, a
);
153 buffertop_first(btc
, b
);
154 lu_assert_int_eq(a
, b
);
158 buffertop_destroy(bt
);
159 buffertop_destroy(btc
);