forked from Mirrors/opensbi
lib: tests: Move tests to a separate directory
Move all of the SBIUnit-related code into the lib/sbi/tests directory. Update 'Makefile' to index objects from the tests subdirectory. I don't think creating the full separate list of Makefile variables (libsbitests-objs-path-y, libsbitests-object-mks, etc. as it is done for libsbiutils) is necessary for the tests because: 1) `lib/sbi/tests/objects.mk` is already indexed into 'libsbi-objects-mks' since the find expression for the libsbi-object-mks variable looks for objects.mk files in the nested directories as well). 2) Tests are tightly coupled with the `lib/sbi/` sources, therefore it may be reasonable to store the list of lib/sbi and lib/sbi/tests object files together in the libsbi-objs-path-y variable. Additionally, update relative paths in the tests where necessary. Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com> Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
8
lib/sbi/tests/objects.mk
Normal file
8
lib/sbi/tests/objects.mk
Normal file
@@ -0,0 +1,8 @@
|
||||
libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_unit_test.o
|
||||
libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_unit_tests.o
|
||||
|
||||
carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += bitmap_test_suite
|
||||
libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_bitmap_test.o
|
||||
|
||||
carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += console_test_suite
|
||||
libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_console_test.o
|
102
lib/sbi/tests/sbi_bitmap_test.c
Normal file
102
lib/sbi/tests/sbi_bitmap_test.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Author: Ivan Orlov <ivan.orlov0322@gmail.com>
|
||||
*/
|
||||
#include <sbi/sbi_bitmap.h>
|
||||
#include <sbi/sbi_unit_test.h>
|
||||
|
||||
#define DATA_SIZE sizeof(data_zero)
|
||||
#define DATA_BIT_SIZE (DATA_SIZE * 8)
|
||||
|
||||
static unsigned long data_a[] = { 0xDEADBEEF, 0x00BAB10C, 0x1BADB002, 0xABADBABE };
|
||||
static unsigned long data_b[] = { 0xC00010FF, 0x00BAB10C, 0xBAAAAAAD, 0xBADDCAFE };
|
||||
static unsigned long data_zero[] = { 0, 0, 0, 0 };
|
||||
|
||||
static void bitmap_and_test(struct sbiunit_test_case *test)
|
||||
{
|
||||
unsigned long res[DATA_SIZE];
|
||||
unsigned long a_and_b[] = { data_a[0] & data_b[0], data_a[1] & data_b[1],
|
||||
data_a[2] & data_b[2], data_a[3] & data_b[3] };
|
||||
|
||||
__bitmap_and(res, data_a, data_b, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, a_and_b, DATA_SIZE);
|
||||
|
||||
/* a & a = a */
|
||||
__bitmap_and(res, data_a, data_a, DATA_BIT_SIZE);
|
||||
SBIUNIT_ASSERT_MEMEQ(test, res, data_a, DATA_SIZE);
|
||||
|
||||
/* a & 0 = 0 */
|
||||
__bitmap_and(res, data_a, data_zero, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
|
||||
/* 0 & 0 = 0 */
|
||||
__bitmap_and(res, data_zero, data_zero, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
|
||||
sbi_memcpy(res, data_zero, DATA_SIZE);
|
||||
/* Cover zero 'bits' argument */
|
||||
__bitmap_and(res, data_a, data_b, 0);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
}
|
||||
|
||||
static void bitmap_or_test(struct sbiunit_test_case *test)
|
||||
{
|
||||
unsigned long res[DATA_SIZE];
|
||||
unsigned long a_or_b[] = { data_a[0] | data_b[0], data_a[1] | data_b[1],
|
||||
data_a[2] | data_b[2], data_a[3] | data_b[3] };
|
||||
|
||||
__bitmap_or(res, data_a, data_b, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, a_or_b, DATA_SIZE);
|
||||
|
||||
/* a | a = a */
|
||||
__bitmap_or(res, data_a, data_a, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_a, DATA_SIZE);
|
||||
|
||||
/* a | 0 = a */
|
||||
__bitmap_or(res, data_a, data_zero, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_a, DATA_SIZE);
|
||||
|
||||
/* 0 | 0 = 0 */
|
||||
__bitmap_or(res, data_zero, data_zero, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
|
||||
sbi_memcpy(res, data_zero, DATA_SIZE);
|
||||
__bitmap_or(res, data_a, data_b, 0);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
}
|
||||
|
||||
static void bitmap_xor_test(struct sbiunit_test_case *test)
|
||||
{
|
||||
unsigned long res[DATA_SIZE];
|
||||
unsigned long a_xor_b[] = { data_a[0] ^ data_b[0], data_a[1] ^ data_b[1],
|
||||
data_a[2] ^ data_b[2], data_a[3] ^ data_b[3] };
|
||||
|
||||
__bitmap_xor(res, data_a, data_b, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, a_xor_b, DATA_SIZE);
|
||||
|
||||
/* a ^ 0 = a */
|
||||
__bitmap_xor(res, data_a, data_zero, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_a, DATA_SIZE);
|
||||
|
||||
/* a ^ a = 0 */
|
||||
__bitmap_xor(res, data_a, data_a, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
|
||||
/* 0 ^ 0 = 0 */
|
||||
__bitmap_xor(res, data_zero, data_zero, DATA_BIT_SIZE);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
|
||||
sbi_memcpy(res, data_zero, DATA_SIZE);
|
||||
__bitmap_xor(res, data_a, data_b, 0);
|
||||
SBIUNIT_EXPECT_MEMEQ(test, res, data_zero, DATA_SIZE);
|
||||
}
|
||||
|
||||
static struct sbiunit_test_case bitmap_test_cases[] = {
|
||||
SBIUNIT_TEST_CASE(bitmap_and_test),
|
||||
SBIUNIT_TEST_CASE(bitmap_or_test),
|
||||
SBIUNIT_TEST_CASE(bitmap_xor_test),
|
||||
SBIUNIT_END_CASE,
|
||||
};
|
||||
|
||||
SBIUNIT_TEST_SUITE(bitmap_test_suite, bitmap_test_cases);
|
107
lib/sbi/tests/sbi_console_test.c
Normal file
107
lib/sbi/tests/sbi_console_test.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Author: Ivan Orlov <ivan.orlov0322@gmail.com>
|
||||
*/
|
||||
#include <sbi/riscv_locks.h>
|
||||
#include <sbi/sbi_console.h>
|
||||
#include <sbi/sbi_unit_test.h>
|
||||
|
||||
#define TEST_CONSOLE_BUF_LEN 1024
|
||||
|
||||
static const struct sbi_console_device *old_dev;
|
||||
static char test_console_buf[TEST_CONSOLE_BUF_LEN];
|
||||
static u32 test_console_buf_pos;
|
||||
static spinlock_t test_console_lock = SPIN_LOCK_INITIALIZER;
|
||||
|
||||
static void test_console_putc(char c)
|
||||
{
|
||||
test_console_buf[test_console_buf_pos] = c;
|
||||
test_console_buf_pos = (test_console_buf_pos + 1) % TEST_CONSOLE_BUF_LEN;
|
||||
}
|
||||
|
||||
static void clear_test_console_buf(void)
|
||||
{
|
||||
test_console_buf_pos = 0;
|
||||
test_console_buf[0] = '\0';
|
||||
}
|
||||
|
||||
static const struct sbi_console_device test_console_dev = {
|
||||
.name = "Test console device",
|
||||
.console_putc = test_console_putc,
|
||||
};
|
||||
|
||||
/* Mock the console device */
|
||||
static inline void test_console_begin(const struct sbi_console_device *device)
|
||||
{
|
||||
old_dev = sbi_console_get_device();
|
||||
sbi_console_set_device(device);
|
||||
}
|
||||
|
||||
static inline void test_console_end(void)
|
||||
{
|
||||
sbi_console_set_device(old_dev);
|
||||
}
|
||||
|
||||
static void putc_test(struct sbiunit_test_case *test)
|
||||
{
|
||||
clear_test_console_buf();
|
||||
test_console_begin(&test_console_dev);
|
||||
sbi_putc('a');
|
||||
test_console_end();
|
||||
SBIUNIT_ASSERT_EQ(test, test_console_buf[0], 'a');
|
||||
}
|
||||
|
||||
#define PUTS_TEST(test, expected, str) do { \
|
||||
spin_lock(&test_console_lock); \
|
||||
clear_test_console_buf(); \
|
||||
test_console_begin(&test_console_dev); \
|
||||
sbi_puts(str); \
|
||||
test_console_end(); \
|
||||
SBIUNIT_ASSERT_STREQ(test, test_console_buf, expected, \
|
||||
sbi_strlen(expected)); \
|
||||
spin_unlock(&test_console_lock); \
|
||||
} while (0)
|
||||
|
||||
static void puts_test(struct sbiunit_test_case *test)
|
||||
{
|
||||
PUTS_TEST(test, "Hello, OpenSBI!", "Hello, OpenSBI!");
|
||||
PUTS_TEST(test, "Hello,\r\nOpenSBI!", "Hello,\nOpenSBI!");
|
||||
}
|
||||
|
||||
#define PRINTF_TEST(test, expected, format, ...) do { \
|
||||
spin_lock(&test_console_lock); \
|
||||
clear_test_console_buf(); \
|
||||
test_console_begin(&test_console_dev); \
|
||||
size_t __res = sbi_printf(format, ##__VA_ARGS__); \
|
||||
test_console_end(); \
|
||||
SBIUNIT_ASSERT_EQ(test, __res, sbi_strlen(expected)); \
|
||||
SBIUNIT_ASSERT_STREQ(test, test_console_buf, expected, \
|
||||
sbi_strlen(expected)); \
|
||||
spin_unlock(&test_console_lock); \
|
||||
} while (0)
|
||||
|
||||
static void printf_test(struct sbiunit_test_case *test)
|
||||
{
|
||||
PRINTF_TEST(test, "Hello", "Hello");
|
||||
PRINTF_TEST(test, "3 5 7", "%d %d %d", 3, 5, 7);
|
||||
PRINTF_TEST(test, "Hello", "%s", "Hello");
|
||||
PRINTF_TEST(test, "-1", "%d", -1);
|
||||
PRINTF_TEST(test, "FF", "%X", 255);
|
||||
PRINTF_TEST(test, "ff", "%x", 255);
|
||||
PRINTF_TEST(test, "A", "%c", 'A');
|
||||
PRINTF_TEST(test, "1fe", "%p", (void *)0x1fe);
|
||||
PRINTF_TEST(test, "4294967295", "%u", 4294967295U);
|
||||
PRINTF_TEST(test, "-2147483647", "%ld", -2147483647l);
|
||||
PRINTF_TEST(test, "-9223372036854775807", "%lld", -9223372036854775807LL);
|
||||
PRINTF_TEST(test, "18446744073709551615", "%llu", 18446744073709551615ULL);
|
||||
}
|
||||
|
||||
static struct sbiunit_test_case console_test_cases[] = {
|
||||
SBIUNIT_TEST_CASE(putc_test),
|
||||
SBIUNIT_TEST_CASE(puts_test),
|
||||
SBIUNIT_TEST_CASE(printf_test),
|
||||
SBIUNIT_END_CASE,
|
||||
};
|
||||
|
||||
SBIUNIT_TEST_SUITE(console_test_suite, console_test_cases);
|
43
lib/sbi/tests/sbi_unit_test.c
Normal file
43
lib/sbi/tests/sbi_unit_test.c
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Author: Ivan Orlov <ivan.orlov0322@gmail.com>
|
||||
*/
|
||||
#include <sbi/sbi_unit_test.h>
|
||||
#include <sbi/sbi_types.h>
|
||||
#include <sbi/sbi_console.h>
|
||||
|
||||
extern struct sbiunit_test_suite *sbi_unit_tests[];
|
||||
extern unsigned long sbi_unit_tests_size;
|
||||
|
||||
static void run_test_suite(struct sbiunit_test_suite *suite)
|
||||
{
|
||||
struct sbiunit_test_case *s_case;
|
||||
u32 count_pass = 0, count_fail = 0;
|
||||
|
||||
sbi_printf("## Running test suite: %s\n", suite->name);
|
||||
|
||||
s_case = suite->cases;
|
||||
while (s_case->test_func) {
|
||||
s_case->test_func(s_case);
|
||||
if (s_case->failed)
|
||||
count_fail++;
|
||||
else
|
||||
count_pass++;
|
||||
sbi_printf("[%s] %s\n", s_case->failed ? "FAILED" : "PASSED",
|
||||
s_case->name);
|
||||
s_case++;
|
||||
}
|
||||
sbi_printf("%u PASSED / %u FAILED / %u TOTAL\n", count_pass, count_fail,
|
||||
count_pass + count_fail);
|
||||
}
|
||||
|
||||
void run_all_tests(void)
|
||||
{
|
||||
u32 i;
|
||||
|
||||
sbi_printf("\n# Running SBIUNIT tests #\n");
|
||||
|
||||
for (i = 0; i < sbi_unit_tests_size; i++)
|
||||
run_test_suite(sbi_unit_tests[i]);
|
||||
}
|
3
lib/sbi/tests/sbi_unit_tests.carray
Normal file
3
lib/sbi/tests/sbi_unit_tests.carray
Normal file
@@ -0,0 +1,3 @@
|
||||
HEADER: sbi/sbi_unit_test.h
|
||||
TYPE: struct sbiunit_test_suite
|
||||
NAME: sbi_unit_tests
|
Reference in New Issue
Block a user