shithub: mc

ref: f762f96f07cf2dfc7b3bb745d6a767dccda0a01a
dir: /lib/std/test/sort.myr/

View raw version
use std

const main = {
	var i

	var a = [ 3, 5, 4, 9, 7, 2, 6, 0, 1, 8, ]
	var a_sorted = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
	var b = [3, 4, 5, 1, 2, 6, 7, 8, 9, 10]
	var b_sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	var c = ["a", "aa", "b", "C", "Cc", "cC", "d", "f", "fuckit", "go",]
	var c_sorted = ["C", "Cc", "a", "aa", "b", "cC", "d", "f", "fuckit", "go"]

	/* with custom intcmp */
	std.sort(a[:], intcmp)
	std.assert(std.eq(a[:], a_sorted[:]), "a was missorted")

	/* with libstd generic numcmp */
	std.sort(b[:], std.numcmp)
	std.assert(std.eq(b[:], b_sorted[:]), "b was missorted")

	/* string sorting */
	std.sort(c[:], std.strcmp)
	for i = 0; i < c.len; i++
		std.assert(std.eq(c[i], c_sorted[i]), "c was missorted")
	;;
}

const intcmp = {a, b
	if a < b
		-> `std.Before
	elif a == b
		-> `std.Equal
	else
		-> `std.After
	;;
}