Files
c3c/test/unit/stdlib/sort/binarysearch.c3
Pierre Curto f8a3e4f6f0 add basic quicksort support (#816)
* lib/std/sort: refactor binarysearch namespace to prepare for sorting

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* std/lib/sort: add basic quicksort support

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/hash: use method first parameter inferred type

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/hash: add fnv64a support

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

---------

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2023-07-04 20:15:03 +02:00

38 lines
1.1 KiB
C

module sort_test @test;
import std::sort;
struct BinarySearchTest
{
int[] data;
int x;
int index;
}
fn void binarysearch()
{
BinarySearchTest[] tcases = {
{ {}, 0, 0 },
{ {1, 2, 3}, 1, 0 },
{ {1, 2, 3}, 2, 1 },
{ {1, 2, 3}, 3, 2 },
{ {1, 2, 3}, 4, 3 },
{ {10, 20, 30}, 14, 1 },
{ {10, 20, 30}, 26, 2 },
};
foreach (tc : tcases)
{
usz idx = sort::binarysearch(tc.data, tc.x);
assert(idx == tc.index, "%s: got %d; want %d", tc.data, idx, tc.index);
usz cmp_idx = sort::binarysearch_with(tc.data, tc.x, &sort::cmp_int);
assert(cmp_idx == tc.index, "%s: got %d; want %d", tc.data, cmp_idx, tc.index);
usz cmp_idx2 = sort::binarysearch_with(tc.data, tc.x, &sort::cmp_int2);
assert(cmp_idx2 == tc.index, "%s: got %d; want %d", tc.data, cmp_idx2, tc.index);
usz cmp_idx3 = sort::binarysearch_with(tc.data, tc.x, fn int(int a, int b) => a - b);
assert(cmp_idx3 == tc.index, "%s: got %d; want %d", tc.data, cmp_idx2, tc.index);
}
}