11 #define TEST_ASSERT_VAL(text, cond) \
14 pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
19 static char *test_file(int size
)
21 static char buf_templ
[] = "/tmp/test-XXXXXX";
22 char *templ
= buf_templ
;
26 fd
= mkostemp(templ
, O_CREAT
|O_WRONLY
|O_TRUNC
);
34 for (i
= 0; i
< size
; i
++)
35 buf
[i
] = (unsigned char) ((int) i
% 10);
37 if (size
!= write(fd
, buf
, size
))
44 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
46 struct test_data_offset
{
52 struct test_data_offset offsets
[] = {
53 /* Fill first cache page. */
56 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
59 /* Read first cache page. */
62 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
65 /* Fill cache boundary pages. */
67 .offset
= DSO__DATA_CACHE_SIZE
- DSO__DATA_CACHE_SIZE
% 10,
68 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
71 /* Read cache boundary pages. */
73 .offset
= DSO__DATA_CACHE_SIZE
- DSO__DATA_CACHE_SIZE
% 10,
74 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
77 /* Fill final cache page. */
79 .offset
= TEST_FILE_SIZE
- 10,
80 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
83 /* Read final cache page. */
85 .offset
= TEST_FILE_SIZE
- 10,
86 .data
= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
89 /* Read final cache page. */
91 .offset
= TEST_FILE_SIZE
- 3,
92 .data
= { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
97 int dso__test_data(void)
99 struct machine machine
;
101 char *file
= test_file(TEST_FILE_SIZE
);
104 TEST_ASSERT_VAL("No test file", file
);
106 memset(&machine
, 0, sizeof(machine
));
108 dso
= dso__new((const char *)file
);
110 /* Basic 10 bytes tests. */
111 for (i
= 0; i
< ARRAY_SIZE(offsets
); i
++) {
112 struct test_data_offset
*data
= &offsets
[i
];
117 size
= dso__data_read_offset(dso
, &machine
, data
->offset
,
120 TEST_ASSERT_VAL("Wrong size", size
== data
->size
);
121 TEST_ASSERT_VAL("Wrong data", !memcmp(buf
, data
->data
, 10));
124 /* Read cross multiple cache pages. */
130 buf
= malloc(TEST_FILE_SIZE
);
131 TEST_ASSERT_VAL("ENOMEM\n", buf
);
133 /* First iteration to fill caches, second one to read them. */
134 for (c
= 0; c
< 2; c
++) {
135 memset(buf
, 0, TEST_FILE_SIZE
);
136 size
= dso__data_read_offset(dso
, &machine
, 10,
137 buf
, TEST_FILE_SIZE
);
139 TEST_ASSERT_VAL("Wrong size",
140 size
== (TEST_FILE_SIZE
- 10));
142 for (i
= 0; i
< (size_t)size
; i
++)
143 TEST_ASSERT_VAL("Wrong data",