]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bcachefs: add eytzinger0_find_ge self test
authorAndreas Gruenbacher <agruenba@redhat.com>
Mon, 27 Jan 2025 16:15:36 +0000 (17:15 +0100)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 15 Mar 2025 01:02:14 +0000 (21:02 -0400)
Add an eytzinger0_find_ge() self test similar to eytzinger0_find_gt().

Note that this test requires eytzinger0_find_ge() to return the first
matching element in the array in case of duplicates.  To prevent
bisection errors, we only add this test after strenghening the original
implementation (see the previous commit).

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

index 9c6e5d7122b4a85ed1d74f4ff164804fba589315..14686ff32003eec811984601566bf8c639511bb7 100644 (file)
@@ -862,10 +862,48 @@ static void eytzinger0_find_test_gt(u16 *test_array, unsigned nr, u16 search)
        }
 }
 
+static void eytzinger0_find_test_ge(u16 *test_array, unsigned nr, u16 search)
+{
+       int r, s;
+       bool bad;
+
+       r = eytzinger0_find_ge(test_array, nr,
+                              sizeof(test_array[0]),
+                              cmp_u16, &search);
+       if (r >= 0) {
+               if (test_array[r] < search) {
+                       bad = true;
+               } else {
+                       s = eytzinger0_prev(r, nr);
+                       bad = s >= 0 && test_array[s] >= search;
+               }
+       } else {
+               s = eytzinger0_first(nr);
+               bad = s >= 0 && test_array[s] >= search;
+       }
+
+       if (bad) {
+               s = -1;
+               eytzinger0_for_each(j, nr) {
+                       if (test_array[j] >= search) {
+                               s = j;
+                               break;
+                       }
+               }
+
+               eytzinger0_for_each(j, nr)
+                       pr_info("[%3u] = %12u\n", j, test_array[j]);
+               pr_info("find_ge(%12u) = %3i should be %3i\n",
+                       search, r, s);
+               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);
 }
 
 void eytzinger0_find_test(void)