Files
c3c/test/unit/stdlib/sort/quicksort.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

92 lines
1.6 KiB
C

module sort_test @test;
import std::sort;
import std::sort::quicksort;
def QSInt = quicksort::Quicksort<int, void*>;
fn void quicksort()
{
int[][] tcases = {
{},
{10, 3},
{3, 2, 1},
{1, 2, 3},
{2, 1, 3},
};
foreach (tc : tcases)
{
sort::quicksort(tc, QSInt);
assert(sort::check_int_sort(tc));
}
}
def Cmp = fn int (void*, void*);
def QSIntCmp = quicksort::Quicksort<int, Cmp>;
fn void quicksort_with()
{
int[][] tcases = {
{},
{10, 3},
{3, 2, 1},
{1, 2, 3},
{2, 1, 3},
};
foreach (tc : tcases)
{
sort::quicksort_with(tc, QSIntCmp, &sort::cmp_int);
assert(sort::check_int_sort(tc));
}
}
def Cmp2 = fn int (int, int);
def QSIntCmp2 = quicksort::Quicksort<int, Cmp2>;
fn void quicksort_with2()
{
int[][] tcases = {
{},
{10, 3},
{3, 2, 1},
{1, 2, 3},
{2, 1, 3},
};
foreach (tc : tcases)
{
sort::quicksort_with(tc, QSIntCmp2, &sort::cmp_int2);
assert(sort::check_int_sort(tc));
}
}
fn void quicksort_with_lambda()
{
int[][] tcases = {
{},
{10, 3},
{3, 2, 1},
{1, 2, 3},
{2, 1, 3},
};
foreach (tc : tcases)
{
sort::quicksort_with(tc, QSIntCmp2, fn int(int a, int b) => a - b);
assert(sort::check_int_sort(tc));
}
}
module std::sort;
fn bool check_int_sort(int[] list)
{
int prev = int.min;
foreach (x : list)
{
if (prev > x) return false;
prev = x;
}
return true;
}