hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 3bdc71dc322ecb8938d7e47d48d5fc72743fca7b
parent 6d0c8c52cea98c087c6b944e18d1652508b5bfc8
Author: Bor Grošelj Simić <bgs@turminal.net>
Date:   Sat, 29 Apr 2023 16:10:36 +0200

strio: expand tests

Signed-off-by: Bor Grošelj Simić <bgs@turminal.net>

Diffstat:
Mstrio/ops.ha | 66+++++++++++++++++++++++++++++++++++++++++++++++-------------------
Mstrio/stream.ha | 4++++
2 files changed, 51 insertions(+), 19 deletions(-)

diff --git a/strio/ops.ha b/strio/ops.ha @@ -20,9 +20,21 @@ export fn concat(out: io::handle, strs: str...) (size | io::error) = { @test fn concat() void = { let st = dynamic(); defer io::close(&st)!; - concat(&st, "hello") as size; - concat(&st, " ", "world") as size; - assert(string(&st) == "hello world"); + let tests: [_]([]str, str) = [ + ([], ""), + ([""], ""), + (["", ""], ""), + (["hello"], "hello"), + (["hello", " ", "world"], "hello world"), + (["", "hello", " ", "world"], "hello world"), + (["hello", " ", "world", ""], "hello world"), + (["hello", "", " ", "world"], "hello world") + ]; + for (let i = 0z; i < len(tests); i += 1) { + let ln = concat(&st, tests[i].0...) as size; + assert(ln == len(tests[i].1) && string(&st) == tests[i].1); + truncate(&st); + }; }; // Joins several strings together by a delimiter and writes them to a handle. @@ -43,14 +55,22 @@ export fn join(out: io::handle, delim: str, strs: str...) (size | io::error) = { @test fn join() void = { let st = dynamic(); defer io::close(&st)!; - join(&st, "::", "hello", "world") as size; - assert(string(&st) == "hello::world"); - truncate(&st); - join(&st, "::") as size; - assert(string(&st) == ""); - truncate(&st); - join(&st, "::", "foo") as size; - assert(string(&st) == "foo"); + let tests: [_](str, []str, str) = [ + ("::", [], ""), + ("::", [""], ""), + ("::", ["", ""], "::"), + ("::", ["", "", ""], "::::"), + ("::", ["hello"], "hello"), + ("::", ["hello", "world"], "hello::world"), + ("::", ["", "hello", "world"], "::hello::world"), + ("::", ["hello", "world", ""], "hello::world::"), + ("::", ["hello", "", "world"], "hello::::world"), + ]; + for (let i = 0z; i < len(tests); i += 1) { + let ln = join(&st, tests[i].0, tests[i].1...) as size; + assert(ln == len(tests[i].2) && string(&st) == tests[i].2); + truncate(&st); + }; }; // Joins several strings together by a delimiter and writes them to a handle, in @@ -71,14 +91,22 @@ export fn rjoin(out: io::handle, delim: str, strs: str...) (size | io::error) = @test fn rjoin() void = { let st = dynamic(); defer io::close(&st)!; - rjoin(&st, "::", "hello", "world") as size; - assert(string(&st) == "world::hello"); - truncate(&st); - rjoin(&st, "::") as size; - assert(string(&st) == ""); - truncate(&st); - rjoin(&st, "::", "foo") as size; - assert(string(&st) == "foo"); + let tests: [_](str, []str, str) = [ + ("::", [], ""), + ("::", [""], ""), + ("::", ["", ""], "::"), + ("::", ["", "", ""], "::::"), + ("::", ["hello"], "hello"), + ("::", ["hello", "world"], "world::hello"), + ("::", ["", "hello", "world"], "world::hello::"), + ("::", ["hello", "world", ""], "::world::hello"), + ("::", ["hello", "", "world"], "world::::hello"), + ]; + for (let i = 0z; i < len(tests); i += 1) { + let ln = rjoin(&st, tests[i].0, tests[i].1...) as size; + assert(ln == len(tests[i].2) && string(&st) == tests[i].2); + truncate(&st); + }; }; // Appends a rune to a stream. diff --git a/strio/stream.ha b/strio/stream.ha @@ -52,7 +52,9 @@ fn fixed_write(s: *io::stream, buf: const []u8) (size | io::error) = { @test fn fixed() void = { static let buf: [1024]u8 = [0...]; let stream = fixed(buf); + assert(string(&stream) == ""); io::writeall(&stream, strings::toutf8("hello ")) as size; + assert(string(&stream) == "hello "); io::writeall(&stream, strings::toutf8("world")) as size; assert(string(&stream) == "hello world"); }; @@ -94,7 +96,9 @@ fn dynamic_close(s: *io::stream) (void | io::error) = { @test fn dynamic() void = { let stream = dynamic(); defer io::close(&stream)!; + assert(string(&stream) == ""); io::writeall(&stream, strings::toutf8("hello ")) as size; + assert(string(&stream) == "hello "); io::writeall(&stream, strings::toutf8("world")) as size; assert(string(&stream) == "hello world"); };