ref: a2ef6bfcd8de0ae9f7f2df2fcdd692bde6ea422c
dir: /lib/iter/test/perm.myr/
use std use testr use iter const main = { testr.run([ [.name = "byperm-01", .fn = byperm01], [.name = "byperm-02", .fn = byperm02], [.name = "byperm-03", .fn = byperm03], [.name = "byperm-04", .fn = byperm04], ][:]) } const byperm01 = {c var v : int[:] = [ 0, 2, 1 ][:] var sb : std.strbuf# = std.mksb() std.sbfmt(sb, " ") for w : iter.byperm(v, std.numcmp) std.sbfmt(sb, "{} ", w) ;; var expected : byte[:] = " [0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0] " var actual : byte[:] = std.sbfin(sb) testr.check(c, std.eq(expected, actual), "expected “{}”, got “{}”", expected, actual) } const byperm02 = {c var v : int[:] = [ 1, 1 ][:] var sb : std.strbuf# = std.mksb() std.sbfmt(sb, " ") for w : iter.byperm(v, std.numcmp) std.sbfmt(sb, "{} ", w) ;; var expected : byte[:] = " [1, 1] " var actual : byte[:] = std.sbfin(sb) testr.check(c, std.eq(expected, actual), "expected “{}”, got “{}”", expected, actual) } const byperm03 = {c var v : int[:] = [ 3, 1, 1 ][:] var sb : std.strbuf# = std.mksb() std.sbfmt(sb, " ") for w : iter.byperm(v, std.numcmp) std.sbfmt(sb, "{} ", w) ;; var expected : byte[:] = " [1, 1, 3] [1, 3, 1] [3, 1, 1] " var actual : byte[:] = std.sbfin(sb) testr.check(c, std.eq(expected, actual), "expected “{}”, got “{}”", expected, actual) } const byperm04 = {c var v : int[:] = [ 0, 3, 1, 1 ][:] var nperm = 0 for w : iter.byperm(v, std.numcmp) nperm++ ;; /* There are 24 permutations for 4 elements, but we get only half because two elements are equal. */ const expected = 12 testr.check(c, std.eq(expected, nperm), "expected “{}”, got “{}”", expected, nperm) }