Skip to content

Commit

Permalink
update assertion package and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin committed Feb 29, 2024
1 parent 44e8d96 commit 9846bda
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 167 deletions.
24 changes: 15 additions & 9 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ pub fn iter[T](self : Array[T], f : (T) -> Unit) {
}
}

test "iteri" {
test "iter" {
let mut i = 0
let mut failed = false
[1, 2, 3, 4, 5].iter(
fn(elem) { @assertion.assert_eq(elem, i + 1); i = i + 1 },
fn(elem) {
if elem != i + 1 { failed = true }
i = i + 1
}
)
if failed { return Err("iter test failed") }
}

/// Iterates over the array with index.
Expand All @@ -49,13 +54,14 @@ pub fn iteri[T](self : Array[T], f : (Int, T) -> Unit) {

test "iteri" {
let mut i = 0
let mut failed = false
[1, 2, 3, 4, 5].iteri(
fn(index, elem) {
@assertion.assert_eq(index, i)
@assertion.assert_eq(elem, i + 1)
if index != i || elem != i + 1 { failed = true }
i = i + 1
},
)
if failed { return Err("iteri test failed") }
}

/// Applies a function to each element of the array and returns a new array with the results.
Expand All @@ -80,7 +86,7 @@ pub fn map[T, U](self : Array[T], f : (T) -> U) -> Array[U] {
test "map" {
let arr = [1, 2, 3, 4, 5]
let doubled = arr.map(fn(x) { x * 2 })
@assertion.assert_eq(doubled, [2, 4, 6, 8, 10])
@assertion.assert_eq(doubled, [2, 4, 6, 8, 10])?
}

pub fn map_with_index[T, U](self : Array[T], f : (T, Int) -> U) -> Array[U] {
Expand All @@ -97,7 +103,7 @@ pub fn map_with_index[T, U](self : Array[T], f : (T, Int) -> U) -> Array[U] {
test "map with index" {
let arr = [1, 2, 3, 4, 5]
let doubled = arr.map_with_index(fn(x, i) { x * 2 + i })
@assertion.assert_eq(doubled, [2, 5, 8, 11, 14])
@assertion.assert_eq(doubled, [2, 5, 8, 11, 14])?
}

pub fn op_equal[T : Eq](self : Array[T], that : Array[T]) -> Bool {
Expand Down Expand Up @@ -132,7 +138,7 @@ pub fn new[T](length : Int, value : () -> T) -> Array[T] {

test "new" {
let arr = new(2, fn() { { val: 1 } })
@assertion.assert_false(arr[0] === arr[1])
@assertion.assert_false(arr[0] === arr[1])?
}

/// Create a new array. Values are built from indexes.
Expand All @@ -153,6 +159,6 @@ pub fn new_with_index[T](length : Int, value : (Int) -> T) -> Array[T] {

test "new index" {
let arr = new_with_index(2, fn { i => i })
@assertion.assert_eq(arr[0], 0)
@assertion.assert_eq(arr[1], 1)
@assertion.assert_eq(arr[0], 0)?
@assertion.assert_eq(arr[1], 1)?
}
62 changes: 36 additions & 26 deletions assertion/assertion.mbt
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@

pub fn assert_eq[T : Debug + Eq](a : T, b : T) {
fn debug_string[T : Debug](t : T) -> String {
let buf = Buffer::make(50)
t.debug_write(buf)
buf.to_string()
}

pub fn assert_eq[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
if a == b {
Ok(())
} else {
let a = debug_string(a)
let b = debug_string(b)
Err("assertion failed for `\(a) == \(b)`")
}
}

pub fn assert_ne[T : Debug + Eq](a : T, b : T) -> Result[Unit,String] {
if a != b {
let buf = Buffer::make(100)
"assertion failed: ".debug_write(buf)
a.debug_write(buf)
" == ".debug_write(buf)
b.debug_write(buf)
"\n".debug_write(buf)
abort(buf.to_string())
Ok(())
} else {
let a = debug_string(a)
let b = debug_string(b)
Err("assertion failed for `\(a) == \(b)`")
}
}

pub fn assert_false(b : Bool) {
if b {
let buf = Buffer::make(100)
"assertion failed: ".debug_write(buf)
b.debug_write(buf)
" == false".debug_write(buf)
"\n".debug_write(buf)
abort(buf.to_string())
pub fn assert_false(x : Bool) -> Result[Unit,String] {
if x == false {
Ok(())
} else {
Err("assert_false failed")
}
}

pub fn assert_true(b : Bool) {
if not (b) {
let buf = Buffer::make(100)
buf.write_string("assertion failed: ")
b.debug_write(buf)
buf.write_string(" == true\n")
abort(buf.to_string())
pub fn assert_true(x : Bool) -> Result[Unit,String] {
if x {
Ok(())
} else {
Err("assert_true failed")
}
}

test "assert_true.true" {
assert_true(true)
assert_true(true)?
}

test "assert_false.false" {
assert_false(false)
assert_false(false)?
}

test "assert_eq.eq" {
assert_eq(1, 1)
assert_eq(1, 1)?
assert_eq("123","123")?
}
Loading

0 comments on commit 9846bda

Please sign in to comment.