18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * tools/testing/selftests/kvm/include/test_util.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018, Google LLC.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef SELFTEST_KVM_TEST_UTIL_H
98c2ecf20Sopenharmony_ci#define SELFTEST_KVM_TEST_UTIL_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <stdlib.h>
128c2ecf20Sopenharmony_ci#include <stdarg.h>
138c2ecf20Sopenharmony_ci#include <stdbool.h>
148c2ecf20Sopenharmony_ci#include <stdio.h>
158c2ecf20Sopenharmony_ci#include <string.h>
168c2ecf20Sopenharmony_ci#include <inttypes.h>
178c2ecf20Sopenharmony_ci#include <errno.h>
188c2ecf20Sopenharmony_ci#include <unistd.h>
198c2ecf20Sopenharmony_ci#include <fcntl.h>
208c2ecf20Sopenharmony_ci#include "kselftest.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic inline int _no_printf(const char *format, ...) { return 0; }
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#ifdef DEBUG
258c2ecf20Sopenharmony_ci#define pr_debug(...) printf(__VA_ARGS__)
268c2ecf20Sopenharmony_ci#else
278c2ecf20Sopenharmony_ci#define pr_debug(...) _no_printf(__VA_ARGS__)
288c2ecf20Sopenharmony_ci#endif
298c2ecf20Sopenharmony_ci#ifndef QUIET
308c2ecf20Sopenharmony_ci#define pr_info(...) printf(__VA_ARGS__)
318c2ecf20Sopenharmony_ci#else
328c2ecf20Sopenharmony_ci#define pr_info(...) _no_printf(__VA_ARGS__)
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_civoid print_skip(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cissize_t test_write(int fd, const void *buf, size_t count);
388c2ecf20Sopenharmony_cissize_t test_read(int fd, void *buf, size_t count);
398c2ecf20Sopenharmony_ciint test_seq_read(const char *path, char **bufp, size_t *sizep);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_civoid test_assert(bool exp, const char *exp_str,
428c2ecf20Sopenharmony_ci		 const char *file, unsigned int line, const char *fmt, ...)
438c2ecf20Sopenharmony_ci		__attribute__((format(printf, 5, 6)));
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define TEST_ASSERT(e, fmt, ...) \
468c2ecf20Sopenharmony_ci	test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define ASSERT_EQ(a, b) do { \
498c2ecf20Sopenharmony_ci	typeof(a) __a = (a); \
508c2ecf20Sopenharmony_ci	typeof(b) __b = (b); \
518c2ecf20Sopenharmony_ci	TEST_ASSERT(__a == __b, \
528c2ecf20Sopenharmony_ci		    "ASSERT_EQ(%s, %s) failed.\n" \
538c2ecf20Sopenharmony_ci		    "\t%s is %#lx\n" \
548c2ecf20Sopenharmony_ci		    "\t%s is %#lx", \
558c2ecf20Sopenharmony_ci		    #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \
568c2ecf20Sopenharmony_ci} while (0)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define TEST_FAIL(fmt, ...) \
598c2ecf20Sopenharmony_ci	TEST_ASSERT(false, fmt, ##__VA_ARGS__)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cisize_t parse_size(const char *size);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciint64_t timespec_to_ns(struct timespec ts);
648c2ecf20Sopenharmony_cistruct timespec timespec_add_ns(struct timespec ts, int64_t ns);
658c2ecf20Sopenharmony_cistruct timespec timespec_add(struct timespec ts1, struct timespec ts2);
668c2ecf20Sopenharmony_cistruct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
678c2ecf20Sopenharmony_cistruct timespec timespec_diff_now(struct timespec start);
688c2ecf20Sopenharmony_cistruct timespec timespec_div(struct timespec ts, int divisor);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#endif /* SELFTEST_KVM_TEST_UTIL_H */
71