aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorIvan Orlov <ivan.orlov0322@gmail.com>2024-03-04 21:45:49 +0000
committerAnup Patel <anup@brainfault.org>2024-03-10 10:05:28 +0530
commit874fcefdf5883c042b4b1f61f6265e03e16f2e79 (patch)
treee499c0a570bad230077c367b70a23f1e47d0db3a /include
parentb9e4de0641a0b4e4ed9cb0439d3b7b66fe6ef75f (diff)
downloadopensbi-874fcefdf5883c042b4b1f61f6265e03e16f2e79.tar.gz
opensbi-874fcefdf5883c042b4b1f61f6265e03e16f2e79.tar.bz2
opensbi-874fcefdf5883c042b4b1f61f6265e03e16f2e79.zip
lib: Add SBIUnit testing macros and functions
This patch introduces all of the SBIUnit macros and functions which can be used during the test development process. Also, it defines the 'run_all_tests' function, which is being called during the 'init_coldboot' right after printing the boot hart information. Also, add the CONFIG_SBIUNIT Kconfig entry in order to be able to turn the tests on and off. When the CONFIG_SBIUNIT is disabled, the tests and all related code is excluded completely on the compilation stage. Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Diffstat (limited to 'include')
-rw-r--r--include/sbi/sbi_unit_test.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/include/sbi/sbi_unit_test.h b/include/sbi/sbi_unit_test.h
new file mode 100644
index 00000000..c63d900c
--- /dev/null
+++ b/include/sbi/sbi_unit_test.h
@@ -0,0 +1,71 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Author: Ivan Orlov <ivan.orlov0322@gmail.com>
+ */
+#ifdef CONFIG_SBIUNIT
+#ifndef __SBI_UNIT_H__
+#define __SBI_UNIT_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_string.h>
+
+struct sbiunit_test_case {
+ const char *name;
+ bool failed;
+ void (*test_func)(struct sbiunit_test_case *test);
+};
+
+struct sbiunit_test_suite {
+ const char *name;
+ struct sbiunit_test_case *cases;
+};
+
+#define SBIUNIT_TEST_CASE(func) \
+ { \
+ .name = #func, \
+ .failed = false, \
+ .test_func = (func) \
+ }
+
+#define SBIUNIT_END_CASE { }
+
+#define SBIUNIT_TEST_SUITE(suite_name, cases_arr) \
+ struct sbiunit_test_suite suite_name = { \
+ .name = #suite_name, \
+ .cases = cases_arr \
+ }
+
+#define _sbiunit_msg(test, msg) "[SBIUnit] [%s:%d]: %s: %s", __FILE__, \
+ __LINE__, test->name, msg
+
+#define SBIUNIT_INFO(test, msg) sbi_printf(_sbiunit_msg(test, msg))
+#define SBIUNIT_PANIC(test, msg) sbi_panic(_sbiunit_msg(test, msg))
+
+#define SBIUNIT_EXPECT(test, cond) do { \
+ if (!(cond)) { \
+ test->failed = true; \
+ SBIUNIT_INFO(test, "Condition \"" #cond "\" expected to be true!\n"); \
+ } \
+} while (0)
+
+#define SBIUNIT_ASSERT(test, cond) do { \
+ if (!(cond)) \
+ SBIUNIT_PANIC(test, "Condition \"" #cond "\" must be true!\n"); \
+} while (0)
+
+#define SBIUNIT_EXPECT_EQ(test, a, b) SBIUNIT_EXPECT(test, (a) == (b))
+#define SBIUNIT_ASSERT_EQ(test, a, b) SBIUNIT_ASSERT(test, (a) == (b))
+#define SBIUNIT_EXPECT_NE(test, a, b) SBIUNIT_EXPECT(test, (a) != (b))
+#define SBIUNIT_ASSERT_NE(test, a, b) SBIUNIT_ASSERT(test, (a) != (b))
+#define SBIUNIT_EXPECT_MEMEQ(test, a, b, len) SBIUNIT_EXPECT(test, !sbi_memcmp(a, b, len))
+#define SBIUNIT_ASSERT_MEMEQ(test, a, b, len) SBIUNIT_ASSERT(test, !sbi_memcmp(a, b, len))
+#define SBIUNIT_EXPECT_STREQ(test, a, b, len) SBIUNIT_EXPECT(test, !sbi_strncmp(a, b, len))
+#define SBIUNIT_ASSERT_STREQ(test, a, b, len) SBIUNIT_ASSERT(test, !sbi_strncmp(a, b, len))
+
+void run_all_tests(void);
+#endif
+#else
+#define run_all_tests()
+#endif