lib/test_string.c: add strspn and strcspn tests
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Fri, 29 Apr 2022 21:38:00 +0000 (14:38 -0700)
committerakpm <akpm@linux-foundation.org>
Fri, 29 Apr 2022 21:38:00 +0000 (14:38 -0700)
Before refactoring strspn() and strcspn(), add some simple test cases.

Link: https://lkml.kernel.org/r/20220328224119.3003834-1-linux@rasmusvillemoes.dk
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Andy Shevchenko <andy@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
lib/test_string.c

index 9dfd6f52de9211fa243289e3baa36b3870f75d4b..c5cb92fb710e729a2de3e1a6f2c1352f8fd5127b 100644 (file)
@@ -179,6 +179,34 @@ static __init int strnchr_selftest(void)
        return 0;
 }
 
+static __init int strspn_selftest(void)
+{
+       static const struct strspn_test {
+               const char str[16];
+               const char accept[16];
+               const char reject[16];
+               unsigned a;
+               unsigned r;
+       } tests[] __initconst = {
+               { "foobar", "", "", 0, 6 },
+               { "abba", "abc", "ABBA", 4, 4 },
+               { "abba", "a", "b", 1, 1 },
+               { "", "abc", "abc", 0, 0},
+       };
+       const struct strspn_test *s = tests;
+       size_t i, res;
+
+       for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) {
+               res = strspn(s->str, s->accept);
+               if (res != s->a)
+                       return 0x100 + 2*i;
+               res = strcspn(s->str, s->reject);
+               if (res != s->r)
+                       return 0x100 + 2*i + 1;
+       }
+       return 0;
+}
+
 static __exit void string_selftest_remove(void)
 {
 }
@@ -212,6 +240,11 @@ static __init int string_selftest_init(void)
        if (subtest)
                goto fail;
 
+       test = 6;
+       subtest = strspn_selftest();
+       if (subtest)
+               goto fail;
+
        pr_info("String selftests succeeded\n");
        return 0;
 fail: