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))
42 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
44 struct test_data_offset {
50 struct test_data_offset offsets[] = {
51 /* Fill first cache page. */
54 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
57 /* Read first cache page. */
60 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
63 /* Fill cache boundary pages. */
65 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
66 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
69 /* Read cache boundary pages. */
71 .offset = DSO__DATA_CACHE_SIZE - DSO__DATA_CACHE_SIZE % 10,
72 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
75 /* Fill final cache page. */
77 .offset = TEST_FILE_SIZE - 10,
78 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
81 /* Read final cache page. */
83 .offset = TEST_FILE_SIZE - 10,
84 .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
87 /* Read final cache page. */
89 .offset = TEST_FILE_SIZE - 3,
90 .data = { 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 },
95 int test__dso_data(void)
97 struct machine machine;
99 char *file = test_file(TEST_FILE_SIZE);
102 TEST_ASSERT_VAL("No test file", file);
104 memset(&machine, 0, sizeof(machine));
106 dso = dso__new((const char *)file);
108 /* Basic 10 bytes tests. */
109 for (i = 0; i < ARRAY_SIZE(offsets); i++) {
110 struct test_data_offset *data = &offsets[i];
115 size = dso__data_read_offset(dso, &machine, data->offset,
118 TEST_ASSERT_VAL("Wrong size", size == data->size);
119 TEST_ASSERT_VAL("Wrong data", !memcmp(buf, data->data, 10));
122 /* Read cross multiple cache pages. */
128 buf = malloc(TEST_FILE_SIZE);
129 TEST_ASSERT_VAL("ENOMEM\n", buf);
131 /* First iteration to fill caches, second one to read them. */
132 for (c = 0; c < 2; c++) {
133 memset(buf, 0, TEST_FILE_SIZE);
134 size = dso__data_read_offset(dso, &machine, 10,
135 buf, TEST_FILE_SIZE);
137 TEST_ASSERT_VAL("Wrong size",
138 size == (TEST_FILE_SIZE - 10));
140 for (i = 0; i < (size_t)size; i++)
141 TEST_ASSERT_VAL("Wrong data",