1 // SPDX-License-Identifier: GPL-2.0-only
14 #include <linux/kernel.h>
15 #include <linux/zalloc.h>
17 #define TEMPL "/tmp/perf-test-XXXXXX"
19 #define EXPECT_EQUAL(val, expected) \
21 if (val != expected) { \
22 pr_debug("%s:%d: %d != %d\n", \
23 __FILE__, __LINE__, val, expected); \
28 #define EXPECT_EQUAL64(val, expected) \
30 if (val != expected) { \
31 pr_debug("%s:%d: %lld != %lld\n", \
32 __FILE__, __LINE__, val, expected); \
37 static int make_test_file(char path[PATH_MAX], const char *contents)
39 ssize_t contents_len = strlen(contents);
45 pr_debug("mkstemp failed");
48 if (write(fd, contents, contents_len) < contents_len) {
49 pr_debug("short write");
58 static int setup_test(char path[PATH_MAX], const char *contents,
59 size_t buf_size, struct io *io)
61 if (make_test_file(path, contents))
64 io->fd = open(path, O_RDONLY);
66 pr_debug("Failed to open '%s'\n", path);
70 io->buf = malloc(buf_size);
71 if (io->buf == NULL) {
72 pr_debug("Failed to allocate memory");
77 io__init(io, io->fd, io->buf, buf_size);
81 static void cleanup_test(char path[PATH_MAX], struct io *io)
88 static int do_test_get_char(const char *test_string, size_t buf_size)
95 if (setup_test(path, test_string, buf_size, &io))
98 for (i = 0; i < strlen(test_string); i++) {
99 ch = io__get_char(&io);
101 EXPECT_EQUAL(ch, test_string[i]);
102 EXPECT_EQUAL(io.eof, false);
104 ch = io__get_char(&io);
105 EXPECT_EQUAL(ch, -1);
106 EXPECT_EQUAL(io.eof, true);
108 cleanup_test(path, &io);
112 static int test_get_char(void)
117 static const char *const test_strings[] = {
122 for (i = 0; i <= 10; i++) {
123 for (j = 0; j < ARRAY_SIZE(test_strings); j++) {
124 if (do_test_get_char(test_strings[j], 1 << i))
131 static int do_test_get_hex(const char *test_string,
142 if (setup_test(path, test_string, 4, &io))
145 ch = io__get_hex(&io, &hex);
146 EXPECT_EQUAL64(hex, val1);
147 EXPECT_EQUAL(ch, ch1);
149 ch = io__get_hex(&io, &hex);
150 EXPECT_EQUAL64(hex, val2);
151 EXPECT_EQUAL(ch, ch2);
153 ch = io__get_hex(&io, &hex);
154 EXPECT_EQUAL64(hex, val3);
155 EXPECT_EQUAL(ch, ch3);
157 EXPECT_EQUAL(io.eof, end_eof);
159 cleanup_test(path, &io);
163 static int test_get_hex(void)
167 if (do_test_get_hex("12345678abcdef90",
168 0x12345678abcdef90, -1,
174 if (do_test_get_hex("1\n2\n3\n",
181 if (do_test_get_hex("12345678ABCDEF90;a;b",
182 0x12345678abcdef90, ';',
188 if (do_test_get_hex("0x1x2x",
195 if (do_test_get_hex("x1x",
202 if (do_test_get_hex("10000000000000000000000000000abcdefgh99i",
212 static int do_test_get_dec(const char *test_string,
223 if (setup_test(path, test_string, 4, &io))
226 ch = io__get_dec(&io, &dec);
227 EXPECT_EQUAL64(dec, val1);
228 EXPECT_EQUAL(ch, ch1);
230 ch = io__get_dec(&io, &dec);
231 EXPECT_EQUAL64(dec, val2);
232 EXPECT_EQUAL(ch, ch2);
234 ch = io__get_dec(&io, &dec);
235 EXPECT_EQUAL64(dec, val3);
236 EXPECT_EQUAL(ch, ch3);
238 EXPECT_EQUAL(io.eof, end_eof);
240 cleanup_test(path, &io);
244 static int test_get_dec(void)
248 if (do_test_get_dec("12345678abcdef90",
255 if (do_test_get_dec("1\n2\n3\n",
262 if (do_test_get_dec("12345678;1;2",
269 if (do_test_get_dec("0x1x2x",
276 if (do_test_get_dec("x1x",
283 if (do_test_get_dec("10000000000000000000000000000000000000000000000000000000000123456789ab99c",
293 static int test_get_line(void)
297 char test_string[1024];
299 size_t i, line_len = 0;
300 size_t buf_size = 128;
303 for (i = 0; i < 512; i++)
304 test_string[i] = 'a';
305 test_string[512] = '\n';
306 for (i = 513; i < 1023; i++)
307 test_string[i] = 'b';
308 test_string[1023] = '\0';
310 if (setup_test(path, test_string, buf_size, &io))
313 EXPECT_EQUAL((int)io__getline(&io, &line, &line_len), 513);
314 EXPECT_EQUAL((int)strlen(line), 513);
315 for (i = 0; i < 512; i++)
316 EXPECT_EQUAL(line[i], 'a');
317 EXPECT_EQUAL(line[512], '\n');
318 EXPECT_EQUAL((int)io__getline(&io, &line, &line_len), 510);
319 for (i = 0; i < 510; i++)
320 EXPECT_EQUAL(line[i], 'b');
323 cleanup_test(path, &io);
327 static int test__api_io(struct test_suite *test __maybe_unused,
328 int subtest __maybe_unused)
343 DEFINE_SUITE("Test api io", api_io);