bcachefs: Add eytzinger0_find self test
authorAndreas Gruenbacher <agruenba@redhat.com>
Sat, 1 Feb 2025 12:55:46 +0000 (13:55 +0100)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 15 Mar 2025 01:02:14 +0000 (21:02 -0400)
Function eytzinger0_find() isn't currently covered, so add a self test.

We can rely on eytzinger0_find_le() here because it is being
tested independently.

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/util.c

index 14686ff32003eec811984601566bf8c639511bb7..525734528f3534d77ecbe9f72434cceed8b9a163 100644 (file)
@@ -899,11 +899,40 @@ static void eytzinger0_find_test_ge(u16 *test_array, unsigned nr, u16 search)
        }
 }
 
+static void eytzinger0_find_test_eq(u16 *test_array, unsigned nr, u16 search)
+{
+       unsigned r;
+       int s;
+       bool bad;
+
+       r = eytzinger0_find(test_array, nr,
+                           sizeof(test_array[0]),
+                           cmp_u16, &search);
+
+       if (r < nr) {
+               bad = test_array[r] != search;
+       } else {
+               s = eytzinger0_find_le(test_array, nr,
+                                      sizeof(test_array[0]),
+                                      cmp_u16, &search);
+               bad = s >= 0 && test_array[s] == search;
+       }
+
+       if (bad) {
+               eytzinger0_for_each(j, nr)
+                       pr_info("[%3u] = %12u\n", j, test_array[j]);
+               pr_info("find(%12u) = %3i is incorrect\n",
+                       search, r);
+               BUG();
+       }
+}
+
 static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
 {
        eytzinger0_find_test_le(test_array, nr, search);
        eytzinger0_find_test_gt(test_array, nr, search);
        eytzinger0_find_test_ge(test_array, nr, search);
+       eytzinger0_find_test_eq(test_array, nr, search);
 }
 
 void eytzinger0_find_test(void)