| 1 | /* | 
|---|
| 2 | * format_portability_unit.c | 
|---|
| 3 | * | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | #include <assert.h> | 
|---|
| 7 | #include <stdio.h> | 
|---|
| 8 | #include <stdlib.h> | 
|---|
| 9 | #include <string.h> | 
|---|
| 10 |  | 
|---|
| 11 | #include <roaring/roaring.h> | 
|---|
| 12 |  | 
|---|
| 13 | #include "config.h" | 
|---|
| 14 | #include "test.h" | 
|---|
| 15 |  | 
|---|
| 16 | long filesize(char const* path) { | 
|---|
| 17 | FILE* fp = fopen(path, "rb"); | 
|---|
| 18 | assert_non_null(fp); | 
|---|
| 19 |  | 
|---|
| 20 | assert_int_not_equal(fseek(fp, 0L, SEEK_END), -1); | 
|---|
| 21 |  | 
|---|
| 22 | return ftell(fp); | 
|---|
| 23 | } | 
|---|
| 24 |  | 
|---|
| 25 | char* readfile(char const* path) { | 
|---|
| 26 | FILE* fp = fopen(path, "rb"); | 
|---|
| 27 | assert_non_null(fp); | 
|---|
| 28 |  | 
|---|
| 29 | assert_int_not_equal(fseek(fp, 0L, SEEK_END), -1); | 
|---|
| 30 |  | 
|---|
| 31 | long bytes = ftell(fp); | 
|---|
| 32 | char* buf = malloc(bytes); | 
|---|
| 33 | assert_non_null(buf); | 
|---|
| 34 |  | 
|---|
| 35 | rewind(fp); | 
|---|
| 36 | assert_int_equal(bytes, fread(buf, 1, bytes, fp)); | 
|---|
| 37 |  | 
|---|
| 38 | fclose(fp); | 
|---|
| 39 | return buf; | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | int compare(char* x, char* y, size_t size) { | 
|---|
| 43 | for (size_t i = 0; i < size; ++i) { | 
|---|
| 44 | if (x[i] != y[i]) { | 
|---|
| 45 | return i + 1; | 
|---|
| 46 | } | 
|---|
| 47 | } | 
|---|
| 48 | return 0; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | void test_deserialize(char* filename) { | 
|---|
| 52 | char* input_buffer = readfile(filename); | 
|---|
| 53 | assert_non_null(input_buffer); | 
|---|
| 54 |  | 
|---|
| 55 | roaring_bitmap_t* bitmap = | 
|---|
| 56 | roaring_bitmap_portable_deserialize(input_buffer); | 
|---|
| 57 | assert_non_null(bitmap); | 
|---|
| 58 |  | 
|---|
| 59 | size_t expected_size = roaring_bitmap_portable_size_in_bytes(bitmap); | 
|---|
| 60 |  | 
|---|
| 61 | assert_int_equal(expected_size, filesize(filename)); | 
|---|
| 62 |  | 
|---|
| 63 | char* output_buffer = malloc(expected_size); | 
|---|
| 64 | size_t actual_size = | 
|---|
| 65 | roaring_bitmap_portable_serialize(bitmap, output_buffer); | 
|---|
| 66 |  | 
|---|
| 67 | assert_int_equal(actual_size, expected_size); | 
|---|
| 68 | assert_false(compare(input_buffer, output_buffer, actual_size)); | 
|---|
| 69 |  | 
|---|
| 70 | free(output_buffer); | 
|---|
| 71 | free(input_buffer); | 
|---|
| 72 | roaring_bitmap_free(bitmap); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void test_deserialize_portable_norun() { | 
|---|
| 76 | char filename[1024]; | 
|---|
| 77 |  | 
|---|
| 78 | strcpy(filename, TEST_DATA_DIR); | 
|---|
| 79 | strcat(filename, "bitmapwithoutruns.bin"); | 
|---|
| 80 |  | 
|---|
| 81 | test_deserialize(filename); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void test_deserialize_portable_wrun() { | 
|---|
| 85 | char filename[1024]; | 
|---|
| 86 |  | 
|---|
| 87 | strcpy(filename, TEST_DATA_DIR); | 
|---|
| 88 | strcat(filename, "bitmapwithruns.bin"); | 
|---|
| 89 |  | 
|---|
| 90 | test_deserialize(filename); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | int main() { | 
|---|
| 94 | const struct CMUnitTest tests[] = { | 
|---|
| 95 | cmocka_unit_test(test_deserialize_portable_norun), | 
|---|
| 96 | cmocka_unit_test(test_deserialize_portable_wrun), | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 | return cmocka_run_group_tests(tests, NULL, NULL); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|