diff options
Diffstat (limited to 'tests/std')
| -rw-r--r-- | tests/std/test_queue.zc | 52 | ||||
| -rw-r--r-- | tests/std/test_string_utils.zc | 46 |
2 files changed, 92 insertions, 6 deletions
diff --git a/tests/std/test_queue.zc b/tests/std/test_queue.zc index 131eb05..d8895b8 100644 --- a/tests/std/test_queue.zc +++ b/tests/std/test_queue.zc @@ -1,15 +1,15 @@ import "std/queue.zc" test "Queue Push/Pop" { - print "Testing Queue Push/Pop"; + println "Testing Queue Push/Pop"; var queue = Queue<i32>::new(); defer queue.free(); - print "Popping on an empty queue without pushing anything prior" + println "Popping on an empty queue without pushing anything prior" var v = queue.pop(); assert(v.is_none(), "v should not have a valid value"); - print "Pushing in three values..." + println "Pushing in three values..." queue.push(123); queue.push(456); queue.push(789); @@ -23,13 +23,13 @@ test "Queue Push/Pop" { v = queue.pop(); assert(v.is_some() && v.unwrap() == 789, "v's value should be 789"); - print "Popping on an empty queue after pushing and popping three values" + println "Popping on an empty queue after pushing and popping three values" v = queue.pop(); assert(v.is_none(), "v should not have a valid value"); } test "Queue Length and Clear" { - print "Testing Queue clear"; + println "Testing Queue clear"; var queue = Queue<i32>::new(); defer queue.free(); @@ -43,7 +43,7 @@ test "Queue Length and Clear" { } test "Queue Clone" { - print "Testing Queue Cloning"; + println "Testing Queue Cloning"; var queue = Queue<i32>::new(); defer queue.free(); queue.push(123); @@ -60,3 +60,43 @@ test "Queue Clone" { v = queue.pop(); assert(v.is_none(), "v should not have a valid value"); } + +test "Queue Ring Buffer (Advanced)" { + var q = Queue<int>::new(); + + println "Fill slightly" + q.push(1); + q.push(2); + q.push(3); + assert(*q.pop().unwrap_ref() == 1); + + println "Pushing until capacity (assume 8)" + q.push(4); q.push(5); q.push(6); q.push(7); q.push(8); q.push(9); + + println "Popping some to advance head" + assert(*q.pop().unwrap_ref() == 2); + assert(*q.pop().unwrap_ref() == 3); + + println "Pushing to wrap tail" + q.push(10); + q.push(11); + + while (!q.is_empty()) { + q.pop(); + } + + println "Testing clear" + q.push(100); + q.clear(); + assert(q.is_empty()); + + println "Large scale test" + for (var i = 0; i < 100; i=i+1) { + q.push(i); + } + for (var i = 0; i < 100; i=i+1) { + assert(*q.pop().unwrap_ref() == i); + } + + q.free(); +} diff --git a/tests/std/test_string_utils.zc b/tests/std/test_string_utils.zc new file mode 100644 index 0000000..212bac1 --- /dev/null +++ b/tests/std/test_string_utils.zc @@ -0,0 +1,46 @@ +import "std/string.zc" + +test "string trim" { + var s1 = String::from(" hello "); + var t1 = s1.trim(); + var e1 = String::from("hello"); + assert(t1.eq(&e1)); + t1.free(); s1.free(); e1.free(); + + var s2 = String::from("\n\t world \r "); + var t2 = s2.trim(); + var e2 = String::from("world"); + assert(t2.eq(&e2)); + t2.free(); s2.free(); e2.free(); + + var s3 = String::from("no_trim"); + var t3 = s3.trim(); + var e3 = String::from("no_trim"); + assert(t3.eq(&e3)); + t3.free(); s3.free(); e3.free(); + + var s4 = String::from(" "); + var t4 = s4.trim(); + assert(t4.is_empty()); + t4.free(); s4.free(); +} + +test "string replace" { + var s1 = String::from("foo bar foo"); + var r1 = s1.replace("foo", "baz"); + var e1 = String::from("baz bar baz"); + assert(r1.eq(&e1)); + r1.free(); s1.free(); e1.free(); + + var s2 = String::from("hello world"); + var r2 = s2.replace("world", "ZenC"); + var e2 = String::from("hello ZenC"); + assert(r2.eq(&e2)); + r2.free(); s2.free(); e2.free(); + + var s3 = String::from("aaaa"); + var r3 = s3.replace("aa", "b"); + var e3 = String::from("bb"); + assert(r3.eq(&e3)); + r3.free(); s3.free(); e3.free(); +} |
