]> www.infradead.org Git - users/willy/pagecache.git/commitdiff
rosebush: Add test suite
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Fri, 21 Jun 2024 18:59:36 +0000 (14:59 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Wed, 26 Jun 2024 13:08:23 +0000 (09:08 -0400)
This is not a very sophisticated test suite yet, but it helped find
a few bugs and provides a framework for adding more tests as more
bugs are found.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
lib/Kconfig.debug
lib/Makefile
lib/test_rosebush.c [new file with mode: 0644]

index 59b6765d86b8fc6a5f8c55b6daaae545c1c14f7f..f3cfd79d8dbd33b59d3ac1a93b759421811466df 100644 (file)
@@ -2447,6 +2447,9 @@ config TEST_RHASHTABLE
 
          If unsure, say N.
 
+config TEST_ROSEBUSH
+       tristate "Test the Rosebush data structure"
+
 config TEST_IDA
        tristate "Perform selftest on IDA functions"
 
index 723e6c90b58d05f05a79172ca2212900b5b5016a..de4edefc2c1197c8551a68dfadd29fc5b4308dec 100644 (file)
@@ -76,6 +76,7 @@ obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
 obj-$(CONFIG_TEST_MIN_HEAP) += test_min_heap.o
 obj-$(CONFIG_TEST_LKM) += test_module.o
 obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
+obj-$(CONFIG_TEST_ROSEBUSH) += test_rosebush.o
 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
 obj-$(CONFIG_TEST_SORT) += test_sort.o
 obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
diff --git a/lib/test_rosebush.c b/lib/test_rosebush.c
new file mode 100644 (file)
index 0000000..59c342e
--- /dev/null
@@ -0,0 +1,140 @@
+#include <linux/rosebush.h>
+#include <kunit/test.h>
+
+static void iter_rbh(struct kunit *test, struct rbh *rbh, u32 hash, void *p)
+{
+       RBH_ITER(iter, rbh, hash);
+       void *q;
+
+       rcu_read_lock();
+       q = rbh_next(&iter);
+       KUNIT_EXPECT_PTR_EQ_MSG(test, p, q,
+               "rbh_next hash:%u returned %px, expected %px", hash, q, p);
+       q = rbh_next(&iter);
+       KUNIT_EXPECT_PTR_EQ_MSG(test, NULL, q,
+               "rbh_next hash:%u returned %px, expected NULL", hash, q);
+       rcu_read_unlock();
+}
+
+static void check_empty_rbh(struct kunit *test, struct rbh *rbh)
+{
+       iter_rbh(test, rbh, 0, NULL);
+       iter_rbh(test, rbh, 1, NULL);
+       iter_rbh(test, rbh, 17, NULL);
+       iter_rbh(test, rbh, 42, NULL);
+}
+
+static void test_insert(struct kunit *test, struct rbh *rbh, u32 hash)
+{
+       void *p = (void *)((hash << 1) | 1UL);
+       int err;
+
+       err = rbh_insert(rbh, hash, p);
+       KUNIT_EXPECT_EQ(test, err, 0);
+
+       iter_rbh(test, rbh, hash, p);
+}
+
+static void test_reserve(struct kunit *test, struct rbh *rbh, u32 hash)
+{
+       int err;
+
+       err = rbh_reserve(rbh, hash);
+       KUNIT_EXPECT_EQ(test, err, 0);
+
+       iter_rbh(test, rbh, hash, NULL);
+}
+
+static void test_use(struct kunit *test, struct rbh *rbh, u32 hash)
+{
+       void *p = (void *)((hash << 1) | 1UL);
+       int err;
+
+       err = rbh_use(rbh, hash, p);
+       KUNIT_EXPECT_EQ(test, err, 0);
+
+       iter_rbh(test, rbh, hash, p);
+}
+
+static void test_remove(struct kunit *test, struct rbh *rbh, u32 hash)
+{
+       void *p = (void *)((hash << 1) | 1UL);
+       int err;
+
+       err = rbh_remove(rbh, hash, p);
+       KUNIT_EXPECT_EQ(test, err, 0);
+
+       iter_rbh(test, rbh, hash, NULL);
+}
+
+static DEFINE_ROSEBUSH(rosebush);
+
+/*
+ * Conduct a number of tests on a rosebush that has never been used.
+ * They should all return NULL or an errno.  We're looking for crashes
+ * here.
+ */
+static void empty(struct kunit *test)
+{
+       int err;
+
+       check_empty_rbh(test, &rosebush);
+       err = rbh_remove(&rosebush, 0, test);
+       KUNIT_EXPECT_EQ(test, err, -ENOENT);
+       err = rbh_use(&rosebush, 0, test);
+       KUNIT_EXPECT_EQ(test, err, -ENOENT);
+       KUNIT_EXPECT_EQ(test, rosebush.rbh_table, 0);
+}
+
+static void first(struct kunit *test)
+{
+       int err;
+
+       test_insert(test, &rosebush, 5);
+       check_empty_rbh(test, &rosebush);
+       test_remove(test, &rosebush, 5);
+       check_empty_rbh(test, &rosebush);
+
+       err = rbh_remove(&rosebush, 5, NULL);
+       KUNIT_EXPECT_EQ(test, err, -ENOENT);
+       test_reserve(test, &rosebush, 5);
+       err = rbh_remove(&rosebush, 5, test);
+       KUNIT_EXPECT_EQ(test, err, -ENOENT);
+       err = rbh_remove(&rosebush, 5, NULL);
+       KUNIT_EXPECT_EQ(test, err, 0);
+       err = rbh_remove(&rosebush, 5, NULL);
+       KUNIT_EXPECT_EQ(test, err, -ENOENT);
+
+       test_reserve(test, &rosebush, 5);
+       test_use(test, &rosebush, 5);
+       err = rbh_remove(&rosebush, 5, NULL);
+       KUNIT_EXPECT_EQ(test, err, -ENOENT);
+       test_remove(test, &rosebush, 5);
+}
+
+static void grow(struct kunit *test)
+{
+       int i;
+
+       for (i = 3; i < 3333; i += 2)
+               test_insert(test, &rosebush, i);
+
+       rbh_destroy(&rosebush);
+}
+
+static struct kunit_case rosebush_cases[] __refdata = {
+       KUNIT_CASE(empty),
+       KUNIT_CASE(first),
+       KUNIT_CASE(grow),
+       {}
+};
+
+static struct kunit_suite rosebush_suite = {
+       .name = "rosebush",
+       .test_cases = rosebush_cases,
+};
+
+kunit_test_suite(rosebush_suite);
+
+MODULE_AUTHOR("Matthew Wilcox (Oracle) <willy@infradead.org>");
+MODULE_LICENSE("GPL");