1 // SPDX-License-Identifier: GPL-2.0-only
14 #include <linux/kernel.h>
16 #define TEMPL "/tmp/perf-test-XXXXXX"
18 #define EXPECT_EQUAL(val, expected) \
20 if (val != expected) { \
21 pr_debug("%s:%d: %d != %d\n", \
22 __FILE__, __LINE__, val, expected); \
27 #define EXPECT_EQUAL64(val, expected) \
29 if (val != expected) { \
30 pr_debug("%s:%d: %lld != %lld\n", \
31 __FILE__, __LINE__, val, expected); \
36 static int make_test_file(char path
[PATH_MAX
], const char *contents
)
38 ssize_t contents_len
= strlen(contents
);
44 pr_debug("mkstemp failed");
47 if (write(fd
, contents
, contents_len
) < contents_len
) {
48 pr_debug("short write");
57 static int setup_test(char path
[PATH_MAX
], const char *contents
,
58 size_t buf_size
, struct io
*io
)
60 if (make_test_file(path
, contents
))
63 io
->fd
= open(path
, O_RDONLY
);
65 pr_debug("Failed to open '%s'\n", path
);
69 io
->buf
= malloc(buf_size
);
70 if (io
->buf
== NULL
) {
71 pr_debug("Failed to allocate memory");
76 io__init(io
, io
->fd
, io
->buf
, buf_size
);
80 static void cleanup_test(char path
[PATH_MAX
], struct io
*io
)
87 static int do_test_get_char(const char *test_string
, size_t buf_size
)
94 if (setup_test(path
, test_string
, buf_size
, &io
))
97 for (i
= 0; i
< strlen(test_string
); i
++) {
98 ch
= io__get_char(&io
);
100 EXPECT_EQUAL(ch
, test_string
[i
]);
101 EXPECT_EQUAL(io
.eof
, false);
103 ch
= io__get_char(&io
);
104 EXPECT_EQUAL(ch
, -1);
105 EXPECT_EQUAL(io
.eof
, true);
107 cleanup_test(path
, &io
);
111 static int test_get_char(void)
116 static const char *const test_strings
[] = {
121 for (i
= 0; i
<= 10; i
++) {
122 for (j
= 0; j
< ARRAY_SIZE(test_strings
); j
++) {
123 if (do_test_get_char(test_strings
[j
], 1 << i
))
130 static int do_test_get_hex(const char *test_string
,
141 if (setup_test(path
, test_string
, 4, &io
))
144 ch
= io__get_hex(&io
, &hex
);
145 EXPECT_EQUAL64(hex
, val1
);
146 EXPECT_EQUAL(ch
, ch1
);
148 ch
= io__get_hex(&io
, &hex
);
149 EXPECT_EQUAL64(hex
, val2
);
150 EXPECT_EQUAL(ch
, ch2
);
152 ch
= io__get_hex(&io
, &hex
);
153 EXPECT_EQUAL64(hex
, val3
);
154 EXPECT_EQUAL(ch
, ch3
);
156 EXPECT_EQUAL(io
.eof
, end_eof
);
158 cleanup_test(path
, &io
);
162 static int test_get_hex(void)
166 if (do_test_get_hex("12345678abcdef90",
167 0x12345678abcdef90, -1,
173 if (do_test_get_hex("1\n2\n3\n",
180 if (do_test_get_hex("12345678ABCDEF90;a;b",
181 0x12345678abcdef90, ';',
187 if (do_test_get_hex("0x1x2x",
194 if (do_test_get_hex("x1x",
201 if (do_test_get_hex("10000000000000000000000000000abcdefgh99i",
211 static int do_test_get_dec(const char *test_string
,
222 if (setup_test(path
, test_string
, 4, &io
))
225 ch
= io__get_dec(&io
, &dec
);
226 EXPECT_EQUAL64(dec
, val1
);
227 EXPECT_EQUAL(ch
, ch1
);
229 ch
= io__get_dec(&io
, &dec
);
230 EXPECT_EQUAL64(dec
, val2
);
231 EXPECT_EQUAL(ch
, ch2
);
233 ch
= io__get_dec(&io
, &dec
);
234 EXPECT_EQUAL64(dec
, val3
);
235 EXPECT_EQUAL(ch
, ch3
);
237 EXPECT_EQUAL(io
.eof
, end_eof
);
239 cleanup_test(path
, &io
);
243 static int test_get_dec(void)
247 if (do_test_get_dec("12345678abcdef90",
254 if (do_test_get_dec("1\n2\n3\n",
261 if (do_test_get_dec("12345678;1;2",
268 if (do_test_get_dec("0x1x2x",
275 if (do_test_get_dec("x1x",
282 if (do_test_get_dec("10000000000000000000000000000000000000000000000000000000000123456789ab99c",
292 int test__api_io(struct test
*test __maybe_unused
,
293 int subtest __maybe_unused
)