From 88c4f5eba766fc8d3a2bb9ca7891a7ab93c5727a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcial=20Gai=C3=9Fert?= Date: Tue, 5 Nov 2024 16:08:52 +0100 Subject: [PATCH] Move tests from stdlib files to examples/stdlib tests --- examples/stdlib/heap/heap.check | 6 ++++ examples/stdlib/heap/heap.effekt | 27 +++++++++++++++ .../resizable_array/resizable_array.check | 6 ++++ .../resizable_array/resizable_array.effekt | 33 ++++++++++++++++++ examples/stdlib/union_find/union_find.check | 6 ++++ examples/stdlib/union_find/union_find.effekt | 21 ++++++++++++ libraries/common/heap.effekt | 28 --------------- libraries/common/resizable_array.effekt | 34 ------------------- libraries/common/union_find.effekt | 22 ------------ 9 files changed, 99 insertions(+), 84 deletions(-) create mode 100644 examples/stdlib/heap/heap.check create mode 100644 examples/stdlib/heap/heap.effekt create mode 100644 examples/stdlib/resizable_array/resizable_array.check create mode 100644 examples/stdlib/resizable_array/resizable_array.effekt create mode 100644 examples/stdlib/union_find/union_find.check create mode 100644 examples/stdlib/union_find/union_find.effekt diff --git a/examples/stdlib/heap/heap.check b/examples/stdlib/heap/heap.check new file mode 100644 index 000000000..23c6f2f4c --- /dev/null +++ b/examples/stdlib/heap/heap.check @@ -0,0 +1,6 @@ +HeapTests +✓ simple heap sort on integers + + 1 pass + 0 fail + 1 tests total \ No newline at end of file diff --git a/examples/stdlib/heap/heap.effekt b/examples/stdlib/heap/heap.effekt new file mode 100644 index 000000000..e43e1cc31 --- /dev/null +++ b/examples/stdlib/heap/heap.effekt @@ -0,0 +1,27 @@ +import heap +import test + +def main() = { + suite("HeapTests", false) { + test("simple heap sort on integers") { + with on[OutOfBounds].default { assertTrue(false); <> }; + val h = heap[Int](box { (x: Int, y: Int) => + if (x < y) { + Less() + } else Greater() // hacky, should sometimes be Equal() + }) + h.insert(12) + h.insert(10) + h.insert(7) + h.insert(11) + h.insert(14) + assert(h.deleteMin(), 7) + assert(h.deleteMin(), 10) + assert(h.deleteMin(), 11) + assert(h.deleteMin(), 12) + assert(h.deleteMin(), 14) + assert(h.size, 0) + } + }; + () +} \ No newline at end of file diff --git a/examples/stdlib/resizable_array/resizable_array.check b/examples/stdlib/resizable_array/resizable_array.check new file mode 100644 index 000000000..d99b8f8f4 --- /dev/null +++ b/examples/stdlib/resizable_array/resizable_array.check @@ -0,0 +1,6 @@ +ResizableArrayTests +✓ usage as stack + + 1 pass + 0 fail + 1 tests total \ No newline at end of file diff --git a/examples/stdlib/resizable_array/resizable_array.effekt b/examples/stdlib/resizable_array/resizable_array.effekt new file mode 100644 index 000000000..91e5c7257 --- /dev/null +++ b/examples/stdlib/resizable_array/resizable_array.effekt @@ -0,0 +1,33 @@ +import resizable_array +import test + +def main() = { + suite("ResizableArrayTests", false) { + test("usage as stack") { + with on[OutOfBounds].default { assertTrue(false, "out of bounds") } + val a = resizableArray() + a.add(1) + a.add(1) + a.add(2) + a.add(3) + a.add(13) + a.add(21) + a.add(34) + a.add(55) + assert(a.popRight(), 55) + assert(a.popRight(), 34) + assert(a.popRight(), 21) + assert(a.popRight(), 13) + a.add(5) + a.add(8) + assert(a.popRight(), 8) + assert(a.popRight(), 5) + assert(a.popRight(), 3) + assert(a.popRight(), 2) + assert(a.popRight(), 1) + assert(a.popRight(), 1) + } + // TODO + }; + () +} \ No newline at end of file diff --git a/examples/stdlib/union_find/union_find.check b/examples/stdlib/union_find/union_find.check new file mode 100644 index 000000000..ea6f0c408 --- /dev/null +++ b/examples/stdlib/union_find/union_find.check @@ -0,0 +1,6 @@ +UnionFindTests +✓ three elements + + 1 pass + 0 fail + 1 tests total \ No newline at end of file diff --git a/examples/stdlib/union_find/union_find.effekt b/examples/stdlib/union_find/union_find.effekt new file mode 100644 index 000000000..0acd00625 --- /dev/null +++ b/examples/stdlib/union_find/union_find.effekt @@ -0,0 +1,21 @@ +import union_find +import test + +def main() = { + suite("UnionFindTests", false) { + test("three elements") { + with on[MissingValue].default { assertTrue(false, "MissingValue exception") } + val u = unionFind() + val a = u.makeSet() + val b = u.makeSet() + val c = u.makeSet() + assert(u.find(a), a) + assert(u.find(b), b) + assert(u.find(c), c) + u.union(a,b) + assert(u.find(a), u.find(b)) + assert(u.find(c), c) + } + }; + () +} \ No newline at end of file diff --git a/libraries/common/heap.effekt b/libraries/common/heap.effekt index ac6a8577d..df1c6ed5c 100644 --- a/libraries/common/heap.effekt +++ b/libraries/common/heap.effekt @@ -1,6 +1,5 @@ module heap import resizable_array -import test /// Resizable 2-ary min-heap, backed by a resizable array /// `cmp` defines the ordering of elements @@ -101,31 +100,4 @@ def deleteMin[T](heap: Heap[T]): T / Exception[OutOfBounds] = { /// O(1) def size[T](heap: Heap[T]): Int = { heap.rawContents.size -} - -namespace examples { - def main() = { - val r = suite("HeapTests") { - test("simple heap sort on integers") { - with on[OutOfBounds].default { assertTrue(false); <> }; - val h = heap[Int](box { (x: Int, y: Int) => - if (x < y) { - Less() - } else Greater() // hacky, should sometimes be Equal() - }) - h.insert(12) - h.insert(10) - h.insert(7) - h.insert(11) - h.insert(14) - assert(h.deleteMin(), 7) - assert(h.deleteMin(), 10) - assert(h.deleteMin(), 11) - assert(h.deleteMin(), 12) - assert(h.deleteMin(), 14) - assert(h.size, 0) - } - } - () - } } \ No newline at end of file diff --git a/libraries/common/resizable_array.effekt b/libraries/common/resizable_array.effekt index ebea29a83..17b61ef20 100644 --- a/libraries/common/resizable_array.effekt +++ b/libraries/common/resizable_array.effekt @@ -2,7 +2,6 @@ module resizable_array import ref import array -import test record ResizableArray[T](rawSizePtr: Ref[Int], rawContentPtr: Ref[Array[T]]) @@ -232,37 +231,4 @@ def toList[T](arr: ResizableArray[T]): List[T] = { } } go(arr.size - 1, Nil()) -} - -namespace examples { - def main() = { - val r = suite("ResizableArrayTests") { - test("usage as stack") { - with on[OutOfBounds].default { assertTrue(false, "out of bounds") } - val a = resizableArray() - a.add(1) - a.add(1) - a.add(2) - a.add(3) - a.add(13) - a.add(21) - a.add(34) - a.add(55) - assert(a.popRight(), 55) - assert(a.popRight(), 34) - assert(a.popRight(), 21) - assert(a.popRight(), 13) - a.add(5) - a.add(8) - assert(a.popRight(), 8) - assert(a.popRight(), 5) - assert(a.popRight(), 3) - assert(a.popRight(), 2) - assert(a.popRight(), 1) - assert(a.popRight(), 1) - } - // TODO - } - () - } } \ No newline at end of file diff --git a/libraries/common/union_find.effekt b/libraries/common/union_find.effekt index 1e8baf9f8..b11d92552 100644 --- a/libraries/common/union_find.effekt +++ b/libraries/common/union_find.effekt @@ -1,6 +1,5 @@ module union_find import resizable_array -import test /// Classic mutable union-find data structure on integer indices, /// with union-by-rank and path compression. @@ -111,25 +110,4 @@ def union(u: UnionFind, a: Int, b: Int): Int / Exception[MissingValue] = { val x = u.find(a) val y = u.find(b) u.unionRoots(x, y) -} - -namespace examples { - def main() = { - val r = suite("UnionFindTests") { - test("three elements") { - with on[MissingValue].default { assertTrue(false, "MissingValue exception") } - val u = unionFind() - val a = u.makeSet() - val b = u.makeSet() - val c = u.makeSet() - assert(u.find(a), a) - assert(u.find(b), b) - assert(u.find(c), c) - u.union(a,b) - assert(u.find(a), u.find(b)) - assert(u.find(c), c) - } - } - () - } } \ No newline at end of file