xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/kvm/include/test_util.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * tools/testing/selftests/kvm/include/test_util.h
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2018, Google LLC.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef SELFTEST_KVM_TEST_UTIL_H
9*4882a593Smuzhiyun #define SELFTEST_KVM_TEST_UTIL_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <stdlib.h>
12*4882a593Smuzhiyun #include <stdarg.h>
13*4882a593Smuzhiyun #include <stdbool.h>
14*4882a593Smuzhiyun #include <stdio.h>
15*4882a593Smuzhiyun #include <string.h>
16*4882a593Smuzhiyun #include <inttypes.h>
17*4882a593Smuzhiyun #include <errno.h>
18*4882a593Smuzhiyun #include <unistd.h>
19*4882a593Smuzhiyun #include <fcntl.h>
20*4882a593Smuzhiyun #include "kselftest.h"
21*4882a593Smuzhiyun 
_no_printf(const char * format,...)22*4882a593Smuzhiyun static inline int _no_printf(const char *format, ...) { return 0; }
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #ifdef DEBUG
25*4882a593Smuzhiyun #define pr_debug(...) printf(__VA_ARGS__)
26*4882a593Smuzhiyun #else
27*4882a593Smuzhiyun #define pr_debug(...) _no_printf(__VA_ARGS__)
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun #ifndef QUIET
30*4882a593Smuzhiyun #define pr_info(...) printf(__VA_ARGS__)
31*4882a593Smuzhiyun #else
32*4882a593Smuzhiyun #define pr_info(...) _no_printf(__VA_ARGS__)
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun void print_skip(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun ssize_t test_write(int fd, const void *buf, size_t count);
38*4882a593Smuzhiyun ssize_t test_read(int fd, void *buf, size_t count);
39*4882a593Smuzhiyun int test_seq_read(const char *path, char **bufp, size_t *sizep);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun void test_assert(bool exp, const char *exp_str,
42*4882a593Smuzhiyun 		 const char *file, unsigned int line, const char *fmt, ...)
43*4882a593Smuzhiyun 		__attribute__((format(printf, 5, 6)));
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #define TEST_ASSERT(e, fmt, ...) \
46*4882a593Smuzhiyun 	test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define ASSERT_EQ(a, b) do { \
49*4882a593Smuzhiyun 	typeof(a) __a = (a); \
50*4882a593Smuzhiyun 	typeof(b) __b = (b); \
51*4882a593Smuzhiyun 	TEST_ASSERT(__a == __b, \
52*4882a593Smuzhiyun 		    "ASSERT_EQ(%s, %s) failed.\n" \
53*4882a593Smuzhiyun 		    "\t%s is %#lx\n" \
54*4882a593Smuzhiyun 		    "\t%s is %#lx", \
55*4882a593Smuzhiyun 		    #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \
56*4882a593Smuzhiyun } while (0)
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define TEST_FAIL(fmt, ...) \
59*4882a593Smuzhiyun 	TEST_ASSERT(false, fmt, ##__VA_ARGS__)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun size_t parse_size(const char *size);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun int64_t timespec_to_ns(struct timespec ts);
64*4882a593Smuzhiyun struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
65*4882a593Smuzhiyun struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
66*4882a593Smuzhiyun struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
67*4882a593Smuzhiyun struct timespec timespec_diff_now(struct timespec start);
68*4882a593Smuzhiyun struct timespec timespec_div(struct timespec ts, int divisor);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #endif /* SELFTEST_KVM_TEST_UTIL_H */
71