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