summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2021-06-16 10:54:38 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-06-16 10:54:38 +0200
commitf1bfe711a283f2d98618c5eec73f2ba173c061c8 (patch)
tree0509e2432688d1f3ef596cf0a770a657e7ed2e9c /include
parent30d6ac7c240e224287f495e9be203bc7d2c4bc74 (diff)
parente06a8f0bf3151eafce695954109defbcfc4033f4 (diff)
downloadbarebox-f1bfe711a283f2d98618c5eec73f2ba173c061c8.tar.gz
Merge branch 'for-next/testing'
Diffstat (limited to 'include')
-rw-r--r--include/bselftest.h74
-rw-r--r--include/stdlib.h5
2 files changed, 79 insertions, 0 deletions
diff --git a/include/bselftest.h b/include/bselftest.h
new file mode 100644
index 0000000000..21eeba0526
--- /dev/null
+++ b/include/bselftest.h
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+#ifndef __BSELFTEST_H
+#define __BSELFTEST_H
+
+#include <linux/compiler.h>
+#include <linux/list.h>
+#include <init.h>
+
+enum bselftest_group {
+ BSELFTEST_core
+};
+
+struct selftest {
+ enum bselftest_group group;
+ const char *name;
+ int (*func)(void);
+ struct list_head list;
+};
+
+static inline int selftest_report(unsigned int total_tests, unsigned int failed_tests,
+ unsigned int skipped_tests)
+{
+ if (failed_tests == 0) {
+ if (skipped_tests) {
+ pr_info("skipped %u tests\n", skipped_tests);
+ pr_info("remaining %u tests passed\n", total_tests);
+ } else
+ pr_info("all %u tests passed\n", total_tests);
+ } else
+ pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
+
+ return failed_tests ? -EINVAL : 0;
+}
+
+extern struct list_head selftests;
+
+#define BSELFTEST_GLOBALS() \
+static unsigned int total_tests __initdata; \
+static unsigned int failed_tests __initdata; \
+static unsigned int skipped_tests __initdata
+
+#ifdef CONFIG_SELFTEST
+#define __bselftest_initcall(func) late_initcall(func)
+void selftests_run(void);
+#else
+#define __bselftest_initcall(func)
+static inline void selftests_run(void)
+{
+}
+#endif
+
+#define bselftest(_group, _func) \
+ static int __bselftest_##_func(void) \
+ { \
+ total_tests = failed_tests = skipped_tests = 0; \
+ _func(); \
+ return selftest_report(total_tests, \
+ failed_tests, \
+ skipped_tests); \
+ } \
+ static __maybe_unused \
+ int __init _func##_bselftest_register(void) \
+ { \
+ static struct selftest this = { \
+ .group = BSELFTEST_##_group, \
+ .name = KBUILD_MODNAME, \
+ .func = __bselftest_##_func, \
+ }; \
+ list_add_tail(&this.list, &selftests); \
+ return 0; \
+ } \
+ __bselftest_initcall(_func##_bselftest_register);
+
+#endif
diff --git a/include/stdlib.h b/include/stdlib.h
index d405608724..8eb419e111 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -25,4 +25,9 @@ static inline u32 random32(void)
return ret;
}
+static inline u32 prandom_u32_max(u32 ep_ro)
+{
+ return (u32)(((u64) random32() * ep_ro) >> 32);
+}
+
#endif /* __STDLIB_H */