Files
c3c/test/unit/stdlib/io/scanner.c3
Pierre Curto 701d6a0746 std/lib/io: add Scanner (#904)
* std/lib/io: add Scanner

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/core: use existing methods in String.convert_ascii_to_{lower, upper}

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

---------

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2023-08-01 10:47:21 +02:00

51 lines
1.2 KiB
C

module scanner_test @test;
import std::collections::list;
import std::io;
def Results = List(<String>);
struct ScanTest
{
String in;
String[] out;
String left_over;
}
fn void! test_scanner()
{
ScanTest[] tcases = {
{"aa,,bb", {"aa"}, "bb"},
{"a,,b,,", {"a", "b"}, ""},
{"ab,,c", {"ab"}, "c"},
{"ab,,cd,,e", {"ab", "cd"}, "e"},
};
foreach (tc : tcases)
{
ByteReader br;
br.init(tc.in);
Scanner sc;
char[4] buffer; // max match (2) + pattern length (2)
sc.init(br.as_stream(), buffer[..]);
Results results;
while LOOP: (true)
{
char[]! res = sc.scan(",,");
if (catch err = res)
{
case SearchResult.MISSING:
break LOOP;
default:
return err?;
}
String str = (String)res;
results.push(str.tconcat(""));
}
String[] got = results.array_view();
assert(got == tc.out, "got %s; want %s", got, tc.out);
char[] fl = sc.flush();
String left_over = (String)fl;
assert(left_over == tc.left_over, "%s -> %s", tc.in, left_over);
}
}