|
9 | 9 |
|
10 | 10 | #include <roaring/containers/array.h> |
11 | 11 | #include <roaring/containers/bitset.h> |
| 12 | +#include <roaring/containers/containers.h> |
12 | 13 | #include <roaring/containers/mixed_equal.h> |
13 | 14 | #include <roaring/misc/configreport.h> |
14 | 15 |
|
@@ -193,6 +194,71 @@ DEFINE_TEST(capacity_test) { |
193 | 194 | array_container_free(array); |
194 | 195 | } |
195 | 196 |
|
| 197 | +DEFINE_TEST(iterator_read_into_bool_test) { |
| 198 | + array_container_t* A = array_container_create(); |
| 199 | + assert_non_null(A); |
| 200 | + |
| 201 | + // Variables to use. |
| 202 | + uint16_t initial_value = 0; |
| 203 | + uint16_t value_out = 0; |
| 204 | + uint16_t max_value = 0; |
| 205 | + roaring_container_iterator_t it; |
| 206 | + const uint16_t max_elements = 600; |
| 207 | + bool* ans_array = (bool*)calloc(max_elements, sizeof(bool)); |
| 208 | + bool* bool_array; |
| 209 | + |
| 210 | + // Add values with gaps |
| 211 | + for (uint16_t i = 100; i < 200; i += 5) { |
| 212 | + array_container_add(A, i); |
| 213 | + ans_array[i] = true; |
| 214 | + } |
| 215 | + for (uint16_t i = 500; i < max_elements; i += 3) { |
| 216 | + array_container_add(A, i); |
| 217 | + ans_array[i] = true; |
| 218 | + } |
| 219 | + |
| 220 | + // Test 1: Read without max_value (read all) |
| 221 | + it = container_init_iterator(A, ARRAY_CONTAINER_TYPE, &initial_value); |
| 222 | + bool_array = (bool*)calloc(max_elements - initial_value, sizeof(bool)); |
| 223 | + value_out = initial_value; |
| 224 | + bool has_more = array_container_iterator_read_into_bool(A, &it, bool_array, |
| 225 | + NULL, &value_out); |
| 226 | + assert_false(has_more); // Should read all values |
| 227 | + assert_true(memcmp(ans_array + initial_value, bool_array, |
| 228 | + max_elements - initial_value) == 0); |
| 229 | + free(bool_array); |
| 230 | + |
| 231 | + // Test 2: Read with max_value |
| 232 | + it = container_init_iterator(A, ARRAY_CONTAINER_TYPE, &initial_value); |
| 233 | + max_value = 300; |
| 234 | + bool_array = (bool*)calloc(max_value - initial_value, sizeof(bool)); |
| 235 | + value_out = initial_value; |
| 236 | + has_more = array_container_iterator_read_into_bool(A, &it, bool_array, |
| 237 | + &max_value, &value_out); |
| 238 | + assert_true(has_more); |
| 239 | + assert_true(memcmp(ans_array + initial_value, bool_array, |
| 240 | + max_value - initial_value) == 0); |
| 241 | + free(bool_array); |
| 242 | + |
| 243 | + // Test 3: Read from middle with max_value |
| 244 | + uint32_t consumed; |
| 245 | + it = container_init_iterator(A, ARRAY_CONTAINER_TYPE, &initial_value); |
| 246 | + container_iterator_skip(A, ARRAY_CONTAINER_TYPE, &it, 10, &consumed, |
| 247 | + &initial_value); |
| 248 | + max_value = 550; |
| 249 | + bool_array = (bool*)calloc(max_value - initial_value, sizeof(bool)); |
| 250 | + value_out = initial_value; |
| 251 | + has_more = array_container_iterator_read_into_bool(A, &it, bool_array, |
| 252 | + &max_value, &value_out); |
| 253 | + assert_true(has_more); |
| 254 | + assert_true(memcmp(ans_array + initial_value, bool_array, |
| 255 | + max_value - initial_value) == 0); |
| 256 | + free(bool_array); |
| 257 | + |
| 258 | + array_container_free(A); |
| 259 | + free(ans_array); |
| 260 | +} |
| 261 | + |
196 | 262 | /* This is a fixed-increment version of Java 8's SplittableRandom generator |
197 | 263 | See http://dx.doi.org/10.1145/2714064.2660195 and |
198 | 264 | http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html */ |
@@ -374,7 +440,8 @@ int main() { |
374 | 440 | cmocka_unit_test(and_or_test), |
375 | 441 | cmocka_unit_test(to_uint32_array_test), |
376 | 442 | cmocka_unit_test(select_test), |
377 | | - cmocka_unit_test(capacity_test)}; |
| 443 | + cmocka_unit_test(capacity_test), |
| 444 | + cmocka_unit_test(iterator_read_into_bool_test)}; |
378 | 445 |
|
379 | 446 | return cmocka_run_group_tests(tests, NULL, NULL); |
380 | 447 | } |
0 commit comments