summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-02-21 22:42:42 -0800
committerOri Bernstein <ori@eigenstate.org>2015-02-21 22:42:42 -0800
commit6263d1ed56b04df831ad16ac4837965aa052a894 (patch)
treeb1a58bdcd2af9f2cd0a41525f3b7ef1b2ee1cb01 /libstd
parentb93cc4922482a7d96724c022442a45e02085f0aa (diff)
downloadmc-6263d1ed56b04df831ad16ac4837965aa052a894.tar.gz
Start adding in some unit tests to run with mbld.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/test/bigint.myr146
-rw-r--r--libstd/test/bytebuf.myr72
-rw-r--r--libstd/test/chartype.myr25
-rw-r--r--libstd/test/search.myr34
-rw-r--r--libstd/test/slcp.myr17
-rw-r--r--libstd/test/sort.myr43
-rw-r--r--libstd/test/try.myr10
7 files changed, 347 insertions, 0 deletions
diff --git a/libstd/test/bigint.myr b/libstd/test/bigint.myr
new file mode 100644
index 0000000..db74732
--- /dev/null
+++ b/libstd/test/bigint.myr
@@ -0,0 +1,146 @@
+use "bigint.use"
+
+/* suport libs */
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "fmt.use"
+use "mk.use"
+use "option.use"
+use "slpush.use"
+use "sleq.use"
+
+type cmd = union
+ `Add (cmd#, cmd#)
+ `Sub (cmd#, cmd#)
+ `Mul (cmd#, cmd#)
+ `Div (cmd#, cmd#)
+ `Mod (cmd#, cmd#)
+ `Shl (cmd#, cmd#)
+ `Shr (cmd#, cmd#)
+ `Modpow (cmd#, cmd#, cmd#)
+ `Val byte[:]
+;;
+
+const main = {
+ var a, b, c, d, e
+ var buf : byte[64], n
+
+ /* a few combined ops */
+ a = std.mkbigint(1234)
+ b = std.mkbigint(0x7fffffff)
+ c = std.mkbigint(7919)
+ d = std.mkbigint(113051)
+ e = std.mkbigint(11)
+
+ std.bigmul(a, b)
+ std.bigmul(a, b)
+ std.bigadd(a, c)
+ std.bigsub(a, d)
+ std.bigdiv(a, e)
+
+ std.bigfree(b)
+ std.bigfree(c)
+ std.bigfree(d)
+ std.bigfree(e)
+
+ n = std.bigbfmt(buf[:], a, 0)
+ std.assert(std.sleq(buf[:n], "517347321949036993306"), "simple smoke test failed")
+
+ /* make sure we format '0' correctly */
+ run(std.mk(`Val "0"), "0")
+ /* smoke test for division */
+ run(std.mk(`Div (\
+ std.mk(`Val "1234_5678_1234_6789_6666_7777_8888"), \
+ std.mk(`Val "1234_5678_1234_6789_6666_7777"))), \
+ "10000")
+ run(std.mk(`Div (\
+ std.mk(`Val "0xffff_1234_1234_1234_1234"), \
+ std.mk(`Val "0xf010_1234_2314"))), \
+ "4580035496")
+ run(std.mk(`Div (\
+ std.mk(`Val "5192296858534810493479828944327220"), \
+ std.mk(`Val "75557863709417659441940"))), \
+ "68719476751")
+ run(std.mk(`Div (\
+ std.mk(`Val "75557863709417659441940"), \
+ std.mk(`Val "5192296858534810493479828944327220"))), \
+ "0")
+
+ /* smoke test for mod */
+ run(std.mk(`Mod (\
+ std.mk(`Val "5192296858534810493479828944327220"), \
+ std.mk(`Val "75557863709417659441940"))),\
+ "257025710597479990280")
+
+ run(std.mk(`Modpow (\
+ std.mk(`Val "1"), \
+ std.mk(`Val "3"), \
+ std.mk(`Val "2"))), \
+ "1")
+
+ run(std.mk(`Modpow (\
+ std.mk(`Val "5192296858534810493479828944327220"), \
+ std.mk(`Val "75557863709417659441940"), \
+ std.mk(`Val "755578"))), \
+ "49054")
+ run(std.mk(`Modpow (\
+ std.mk(`Val "7220"), \
+ std.mk(`Val "755578"), \
+ std.mk(`Val "75557863709417659441940"))), \
+ "27076504425474791131220")
+
+}
+
+const run = {e : cmd#, res : byte[:]
+ var buf : byte[4096]
+ var v, n
+
+ v = eval(e)
+ n = std.bigbfmt(buf[:], v, 0)
+ if !std.sleq(buf[:n], res)
+ std.fatal(1, "%s != %s\n", buf[:n], res)
+ ;;
+}
+
+const eval = {e : cmd#
+ var buf : byte[2048]
+ var a, b, c /* scratch vars */
+ var n /* buf len */
+
+ match e#
+ | `Add (x, y): -> binop("+", std.bigadd, x, y)
+ | `Sub (x, y): -> binop("-", std.bigsub, x, y)
+ | `Mul (x, y): -> binop("*", std.bigmul, x, y)
+ | `Div (x, y): -> binop("/", std.bigdiv, x, y)
+ | `Mod (x, y): -> binop("%", std.bigmod, x, y)
+ | `Shl (x, y): -> binop("<<", std.bigshl, x, y)
+ | `Shr (x, y): -> binop(">>", std.bigshr, x, y)
+ | `Val x:
+ a = try(std.bigparse(x))
+ n = std.bigbfmt(buf[:], a, 0)
+ -> a
+ | `Modpow (x, y, z):
+ a = eval(x)
+ b = eval(y)
+ c = eval(z)
+ -> std.bigmodpow(a, b, c)
+ ;;
+}
+
+const binop = {name, op, x, y
+ var a, b
+
+ a = eval(x)
+ b = eval(y)
+ op(a, b)
+ std.bigfree(b)
+ -> a
+}
+
+generic try = {x : std.option(@a)
+ match x
+ | `std.Some v: -> v
+ | `std.None: std.die("failed to get val")
+ ;;
+}
diff --git a/libstd/test/bytebuf.myr b/libstd/test/bytebuf.myr
new file mode 100644
index 0000000..48f6715
--- /dev/null
+++ b/libstd/test/bytebuf.myr
@@ -0,0 +1,72 @@
+use "bytebuf.use"
+
+/* support */
+use "die.use"
+use "fmt.use"
+use "sleq.use"
+
+const main = {
+ var bb, v
+
+ bb = std.mkbytebuf()
+
+ std.bytebufputc(bb, 'a')
+ std.assert(bb.len == 1, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "a"), \
+ "byte buf contents not \"a\"\n")
+
+ std.bytebufputs(bb, "bc")
+ std.assert(bb.len == 3, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abc"), \
+ "byte buf contents not \"abc\"\n")
+
+ std.bytebufputb(bb, 'd' castto(byte))
+ std.assert(bb.len == 4, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcd"), \
+ "byte buf contents not \"abcd\"\n")
+
+ std.bytebufputle8(bb, 'e' castto(int64))
+ std.assert(bb.len == 5, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcde"), \
+ "byte buf contents not \"abcde\"\n")
+
+ std.bytebufputbe8(bb, 'e' castto(int64))
+ std.assert(bb.len == 6, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdee"), \
+ "byte buf contents not \"abcdee\"\n")
+
+ std.bytebufputle16(bb, ('f' | ('g' << 8)) castto(int64))
+ std.assert(bb.len == 8, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefg"), \
+ "byte buf contents not \"abcdeefg\"\n")
+
+ std.bytebufputbe16(bb, ('f' | ('g' << 8)) castto(int64))
+ std.assert(bb.len == 10, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggf"), \
+ "byte buf contents not \"abcdeefggf\"\n")
+
+ std.bytebufputle32(bb, ('h' | ('i' << 8) | ('j' << 16) | ('k' << 24)) castto(int64))
+ std.assert(bb.len == 14, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijk"), \
+ "byte buf contents not \"abcdeefggfhijk\"\n")
+
+ std.bytebufputbe32(bb, ('h' | ('i' << 8) | ('j' << 16) | ('k' << 24)) castto(int64))
+ std.assert(bb.len == 18, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjih"), \
+ "byte buf contents not \"abcdeefggfhijkkji\"\n")
+
+ v = ('l' | ('m' << 8) | ('n' << 16) | ('o' << 24)) castto(int64)
+ v |= v << 32
+ std.bytebufputle64(bb, v)
+ std.assert(bb.len == 26, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjihlmnolmno"), \
+ "byte buf contents not \"abcdeefggfhijkkjihlmnolmno\"\n")
+
+ v = ('l' | ('m' << 8) | ('n' << 16) | ('o' << 24)) castto(int64)
+ v |= v << 32
+ std.bytebufputbe64(bb, v)
+ std.assert(bb.len == 34, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjihlmnolmnoonmlonml"), \
+ "byte buf contents not \"abcdeefggfhijkkjilmnolmnoonmlonml\"\n")
+}
+
diff --git a/libstd/test/chartype.myr b/libstd/test/chartype.myr
new file mode 100644
index 0000000..f77b29e
--- /dev/null
+++ b/libstd/test/chartype.myr
@@ -0,0 +1,25 @@
+use "chartype.use"
+
+use "die.use"
+
+const main = {
+ std.assert(std.isalpha('a'), "a should be alpha\n")
+ std.assert(std.isupper('A'), "A should be upper\n")
+ std.assert(std.islower('a'), "a should be lower\n")
+ std.assert(std.isdigit('0'), "0 should be isdigit\n")
+ std.assert(std.isnum('\u{0c66}'), "\u{0c66} should be isnum\n")
+ std.assert(std.isalnum('a'), "a should be isalnum\n")
+ std.assert(std.isalnum('0'), "0 should be isalnum\n")
+ std.assert(std.isspace(' '), "' ' should be isspace\n")
+ std.assert(std.isblank(' '), "' ' should be isblank\n")
+
+ std.assert(!std.isalpha('0'), "0 should not be alpha\n")
+ std.assert(!std.isupper('a'), "a should not be upper\n")
+ std.assert(!std.islower('A'), "A should not be lower\n")
+ std.assert(!std.isdigit('a'), "a should not be isdigit\n")
+ std.assert(!std.isnum('a'), " should not be isnum\n")
+ std.assert(!std.isalnum('}'), "a should not be isalnum\n")
+ std.assert(!std.isalnum('!'), "! should not be isalnum\n")
+ std.assert(!std.isspace('@'), "@ should not be isspace\n")
+ std.assert(!std.isblank('@'), "@ should not be isblank\n")
+}
diff --git a/libstd/test/search.myr b/libstd/test/search.myr
new file mode 100644
index 0000000..3e3ac53
--- /dev/null
+++ b/libstd/test/search.myr
@@ -0,0 +1,34 @@
+use std
+
+const sl = [1, 3, 5, 8, 9, 33]
+
+const main = {
+
+ expect(std.lsearch(sl[:], 1, std.numcmp), `std.Some 0)
+ expect(std.lsearch(sl[:], 33, std.numcmp), `std.Some sl.len - 1)
+ expect(std.lsearch(sl[:], 5, std.numcmp), `std.Some 2)
+ expect(std.lsearch(sl[:], 6, std.numcmp), `std.None)
+
+ expect(std.bsearch(sl[:], 1, std.numcmp), `std.Some 0)
+ expect(std.bsearch(sl[:], 33, std.numcmp), `std.Some sl.len - 1)
+ expect(std.bsearch(sl[:], 5, std.numcmp), `std.Some 2)
+ expect(std.bsearch(sl[:], 6, std.numcmp), `std.None)
+}
+
+const expect = {a, b
+ match a
+ | `std.None:
+ match b
+ | `std.Some x: std.fatal(1, "Expected `std.None, `std.None, got `std.None, `std.Some %i\n", x)
+ | `std.None: /* nothing */
+ ;;
+ | `std.Some x:
+ match b
+ | `std.None: std.fatal(1, "Expected `std.Some %i, `std.Some %i, got `std.Some %i, `std.None\n", x, x, x)
+ | `std.Some y:
+ if x != y
+ std.fatal(1, "Expected `std.Some %i, `std.Some %i, got `std.Some %i, `std.Some %i\n", x, x, x, y)
+ ;;
+ ;;
+ ;;
+}
diff --git a/libstd/test/slcp.myr b/libstd/test/slcp.myr
new file mode 100644
index 0000000..385b6ef
--- /dev/null
+++ b/libstd/test/slcp.myr
@@ -0,0 +1,17 @@
+use "slcp.use"
+
+use "die.use"
+use "sleq.use"
+
+const main = {
+ var a = [1,2,3,4,5]
+ var b = [6,7,8,9,10]
+ var a_cped = [3, 4, 5, 4, 5]
+ var b_cped = [6, 7, 6, 7, 8]
+
+
+ std.slcp(a[:a.len-2], a[2:])
+ std.slcp(b[2:], b[:b.len-2])
+ std.assert(std.sleq(a[:], a_cped[:]), "slcp of a failed")
+ std.assert(std.sleq(b[:], b_cped[:]), "slcp of a failed")
+}
diff --git a/libstd/test/sort.myr b/libstd/test/sort.myr
new file mode 100644
index 0000000..4c6bd38
--- /dev/null
+++ b/libstd/test/sort.myr
@@ -0,0 +1,43 @@
+use "sort.use"
+
+use "fmt.use"
+use "die.use"
+use "sleq.use"
+use "cmp.use"
+
+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.sleq(a[:], a_sorted[:]), "a was missorted")
+
+ /* with libstd generic numcmp */
+ std.sort(b[:], std.numcmp)
+ std.assert(std.sleq(b[:], b_sorted[:]), "b was missorted")
+
+ /* string sorting */
+ std.sort(c[:], std.strcmp)
+ for i = 0; i < c.len; i++
+ std.assert(std.sleq(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
+ ;;
+}
+
+
diff --git a/libstd/test/try.myr b/libstd/test/try.myr
new file mode 100644
index 0000000..7458fc4
--- /dev/null
+++ b/libstd/test/try.myr
@@ -0,0 +1,10 @@
+use "try.use"
+
+use "die.use"
+use "option.use"
+use "fmt.use"
+
+const main = {
+ var x = `std.Some 123
+ std.assert(std.try(x) == 123, "expected 123 from try")
+}