mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 20:11:17 +00:00
* zip / zip_into * Deprecate `add_array` in favour of `push_all` on lists. * Add support for generic lists for zip. --------- Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
93 lines
1.5 KiB
Plaintext
93 lines
1.5 KiB
Plaintext
module sort_test @test;
|
|
import std::sort;
|
|
import std::collections::list;
|
|
|
|
struct TestCase @local
|
|
{
|
|
int[] input;
|
|
bool want;
|
|
}
|
|
|
|
fn void sorted()
|
|
{
|
|
TestCase[] tcases = {
|
|
{
|
|
.input = {},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {1},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {1,2},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {2,1},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {1,2,3},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {1,2,1},
|
|
.want = false,
|
|
},
|
|
{
|
|
.input = {2,1,2},
|
|
.want = false,
|
|
},
|
|
{
|
|
.input = {3,2,1},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {1,1,1,1,1,2},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {2,2,2,2,2,1},
|
|
.want = true,
|
|
},
|
|
{
|
|
.input = {1,1,1,1,2,1},
|
|
.want = false,
|
|
},
|
|
};
|
|
|
|
bool got;
|
|
foreach (tc : tcases)
|
|
{
|
|
// default
|
|
got = is_sorted(tc.input);
|
|
assert(got == tc.want, "default: %s, got: %s, want: %s",
|
|
tc.input, got, tc.want);
|
|
|
|
// with list
|
|
List{int} list;
|
|
list.tinit();
|
|
list.push_all(tc.input);
|
|
|
|
got = is_sorted(list);
|
|
assert(got == tc.want, "list: %s, got: %s, want: %s",
|
|
list, got, tc.want);
|
|
|
|
// with lambda
|
|
got = is_sorted(tc.input, fn int(int a, int b) => a - b);
|
|
assert(got == tc.want, "lambda: %s, got: %s, want: %s",
|
|
tc.input, got, tc.want);
|
|
|
|
// with value
|
|
got = is_sorted(tc.input, &sort::cmp_int_value);
|
|
assert(got == tc.want, "value: %s, got: %s, want: %s",
|
|
tc.input, got, tc.want);
|
|
|
|
// with ref
|
|
got = is_sorted(tc.input, &sort::cmp_int_ref);
|
|
assert(got == tc.want, "ref: %s, got: %s, want: %s",
|
|
tc.input, got, tc.want);
|
|
}
|
|
}
|