13 static char *test_file(int size
)
15 static char buf_templ
[] = "/tmp/test-XXXXXX";
16 char *templ
= buf_templ
;
22 perror("mkstemp failed");
32 for (i
= 0; i
< size
; i
++)
33 buf
[i
] = (unsigned char) ((int) i
% 10);
35 if (size
!= write(fd
, buf
, size
))
43 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
45 struct test_data_offset
{
51 struct test_data_offset offsets
[] = {
52 /* Fill first cache page. */
55 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
58 /* Read first cache page. */
61 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
64 /* Fill cache boundary pages. */
66 .offset
= DSO__DATA_CACHE_SIZE
- DSO__DATA_CACHE_SIZE
% 10,
67 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
70 /* Read cache boundary pages. */
72 .offset
= DSO__DATA_CACHE_SIZE
- DSO__DATA_CACHE_SIZE
% 10,
73 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
76 /* Fill final cache page. */
78 .offset
= TEST_FILE_SIZE
- 10,
79 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
82 /* Read final cache page. */
84 .offset
= TEST_FILE_SIZE
- 10,
85 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
88 /* Read final cache page. */
90 .offset
= TEST_FILE_SIZE
- 3,
91 .data
= { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
96 int test__dso_data(void)
98 struct machine machine
;
100 char *file
= test_file(TEST_FILE_SIZE
);
103 TEST_ASSERT_VAL("No test file", file
);
105 memset(&machine
, 0, sizeof(machine
));
107 dso
= dso__new((const char *)file
);
109 /* Basic 10 bytes tests. */
110 for (i
= 0; i
< ARRAY_SIZE(offsets
); i
++) {
111 struct test_data_offset
*data
= &offsets
[i
];
116 size
= dso__data_read_offset(dso
, &machine
, data
->offset
,
119 TEST_ASSERT_VAL("Wrong size", size
== data
->size
);
120 TEST_ASSERT_VAL("Wrong data", !memcmp(buf
, data
->data
, 10));
123 /* Read cross multiple cache pages. */
129 buf
= malloc(TEST_FILE_SIZE
);
130 TEST_ASSERT_VAL("ENOMEM\n", buf
);
132 /* First iteration to fill caches, second one to read them. */
133 for (c
= 0; c
< 2; c
++) {
134 memset(buf
, 0, TEST_FILE_SIZE
);
135 size
= dso__data_read_offset(dso
, &machine
, 10,
136 buf
, TEST_FILE_SIZE
);
138 TEST_ASSERT_VAL("Wrong size",
139 size
== (TEST_FILE_SIZE
- 10));
141 for (i
= 0; i
< (size_t)size
; i
++)
142 TEST_ASSERT_VAL("Wrong data",