import qsort import sortkit import std/list import std/test @test fn sorts_with_a_comparator :: { test.assert_equal (qsort.sort [5, 3, 9, 1, 7] fn a b { a - b }) Ok([1, 3, 5, 7, 9]) test.assert_equal (qsort.sort [5, 3, 9, 1, 7] fn a b { b - a }) Ok([9, 7, 5, 3, 1]) test.assert_equal (qsort.sort [] fn a b { a - b }) Ok([]) } @test fn the_comparator_is_a_closure :: { // By last digit; the sort is stable, so 21 stays before 11. let base = 10 test.assert_equal (qsort.sort [22, 21, 12, 11] fn a b { a % base - b % base }) Ok([21, 11, 22, 12]) } @test fn sorts_long_lists :: { let sorted = qsort.sort (list.reverse (list.range 1 5000)) fn a b { a - b } test.assert_equal sorted Ok(list.range 1 5000) } @test fn strings_cross_with_their_length :: { test.assert_equal (qsort.version ()) "sortkit 1.0" test.assert_equal (qsort.count_byte "banana" 97) 3 test.assert_equal (qsort.count_byte "" 97) 0 } @test @should_panic "comparator gave up" fn a_panicking_comparator_stops_the_caller :: { let _ = qsort.sort [1, 2, 3] fn a b { panic "comparator gave up" } } @test @should_panic "a callback result is out of range for int" fn a_comparator_result_out_of_range_panics :: { let _ = qsort.sort [1, 2] fn a b { 10000000000 } } @test @should_panic "sk_vec_get: `i` is out of range for size_t" fn a_negative_index_panics_in_the_binding :: { let v = sortkit.sk_vec_new () let _ = sortkit.sk_vec_get v (-1) }