Skip to content

Commit

Permalink
Move tests from stdlib files to examples/stdlib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marzipankaiser committed Jan 27, 2025
1 parent 8a50435 commit 88c4f5e
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 84 deletions.
6 changes: 6 additions & 0 deletions examples/stdlib/heap/heap.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HeapTests
✓ simple heap sort on integers

1 pass
0 fail
1 tests total
27 changes: 27 additions & 0 deletions examples/stdlib/heap/heap.effekt
Original file line number Diff line number Diff line change
@@ -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)
}
};
()
}
6 changes: 6 additions & 0 deletions examples/stdlib/resizable_array/resizable_array.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ResizableArrayTests
✓ usage as stack

1 pass
0 fail
1 tests total
33 changes: 33 additions & 0 deletions examples/stdlib/resizable_array/resizable_array.effekt
Original file line number Diff line number Diff line change
@@ -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
};
()
}
6 changes: 6 additions & 0 deletions examples/stdlib/union_find/union_find.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UnionFindTests
✓ three elements

1 pass
0 fail
1 tests total
21 changes: 21 additions & 0 deletions examples/stdlib/union_find/union_find.effekt
Original file line number Diff line number Diff line change
@@ -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)
}
};
()
}
28 changes: 0 additions & 28 deletions libraries/common/heap.effekt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
()
}
}
34 changes: 0 additions & 34 deletions libraries/common/resizable_array.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module resizable_array

import ref
import array
import test

record ResizableArray[T](rawSizePtr: Ref[Int], rawContentPtr: Ref[Array[T]])

Expand Down Expand Up @@ -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
}
()
}
}
22 changes: 0 additions & 22 deletions libraries/common/union_find.effekt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
}
}
()
}
}

0 comments on commit 88c4f5e

Please sign in to comment.