233 lines
6.6 KiB
Hare
233 lines
6.6 KiB
Hare
use fmt;
|
|
use math;
|
|
use strings;
|
|
|
|
@test fn parse_string() void = {
|
|
assert(parse_string("hello")! == "hello");
|
|
assert(parse_string("hel\\s\\n\\t\\r\\\\\\;lo")! == "hel \n\t\r\\;lo");
|
|
assert(parse_string("hello☠") is invalid_ascii);
|
|
assert(parse_string("hello;world") is expected_single);
|
|
assert(parse_string("hello\\d") is invalid_escape);
|
|
};
|
|
|
|
@test fn parse_localestring() void = {
|
|
assert(parse_localestring("hello")! == "hello");
|
|
assert(parse_localestring("hel\\s\\n\\t\\r\\\\\\;lo")! == "hel \n\t\r\\;lo");
|
|
assert(parse_localestring("hello☠")! == "hello☠");
|
|
assert(parse_localestring("hello;world") is expected_single);
|
|
assert(parse_localestring("hello\\d") is invalid_escape);
|
|
};
|
|
|
|
@test fn parse_iconstring() void = {
|
|
assert(parse_iconstring("hello")! == "hello");
|
|
assert(parse_iconstring("hel\\s\\n\\t\\r\\\\\\;lo")! == "hel \n\t\r\\;lo");
|
|
assert(parse_iconstring("hello☠")! == "hello☠");
|
|
assert(parse_iconstring("hello;world") is expected_single);
|
|
assert(parse_iconstring("hello\\d") is invalid_escape);
|
|
};
|
|
|
|
@test fn parse_boolean() void = {
|
|
assert(parse_boolean("true")! == true);
|
|
assert(parse_boolean("false")! == false);
|
|
assert(parse_boolean("hello") is invalid_boolean);
|
|
assert(parse_boolean("ttrue") is invalid_boolean);
|
|
assert(parse_boolean("falsee") is invalid_boolean);
|
|
assert(parse_boolean("") is invalid_boolean);
|
|
assert(parse_boolean("1") is invalid_boolean);
|
|
assert(parse_boolean("0") is invalid_boolean);
|
|
assert(parse_boolean("true;false") is expected_single);
|
|
};
|
|
|
|
@test fn parse_numeric() void = {
|
|
assert(parse_numeric("9")! == 9.0f32);
|
|
assert(parse_numeric("9.0")! == 9.0f32);
|
|
assert(parse_numeric("34.93")! == 34.93f32);
|
|
assert(parse_numeric("-100.895")! == -100.895f32);
|
|
assert(math::isnan(parse_numeric("NaN")!));
|
|
assert(parse_numeric("Infinity")! == math::INF);
|
|
assert(parse_numeric("hello") is invalid_numeric);
|
|
assert(parse_numeric("--") is invalid_numeric);
|
|
assert(parse_numeric("....") is invalid_numeric);
|
|
assert(parse_numeric("234;7.4") is expected_single);
|
|
};
|
|
|
|
@test fn parse_integer() void = {
|
|
assert(parse_integer("9")! == 9);
|
|
assert(parse_integer("2348")! == 2348);
|
|
assert(parse_integer("-324")! == -324);
|
|
assert(parse_integer("324.9") is invalid_integer);
|
|
assert(parse_integer("324.0") is invalid_integer);
|
|
};
|
|
|
|
@test fn parse_strings() void = {
|
|
let correct: []str = [
|
|
"b\r\tird",
|
|
"wa;ter",
|
|
"",
|
|
"riv\ner",
|
|
"nuh uh",
|
|
];
|
|
let got = parse_strings("b\\r\\tird;wa\\;ter;;riv\\ner;nuh uh")!;
|
|
defer strings::freeall(got);
|
|
for (let index = 0z; index < len(correct); index += 1) {
|
|
assert(index < len(got), "ran out");
|
|
assert(compare_strings(correct[index], got[index]));
|
|
};
|
|
assert(len(got) == len(correct), "not done");
|
|
assert(parse_strings("hello☠;world") is invalid_ascii);
|
|
assert(parse_strings("hello\\d;world") is invalid_escape);
|
|
};
|
|
|
|
@test fn parse_localestrings() void = {
|
|
let correct: []str = [
|
|
"b\r\tir☠d",
|
|
"wa;ter",
|
|
"",
|
|
"ri☠v\ner",
|
|
"nuh uh",
|
|
];
|
|
let got = parse_localestrings("b\\r\\tir☠d;wa\\;ter;;ri☠v\\ner;nuh uh")!;
|
|
defer strings::freeall(got);
|
|
for (let index = 0z; index < len(correct); index += 1) {
|
|
assert(index < len(got), "ran out");
|
|
assert(compare_strings(correct[index], got[index]));
|
|
};
|
|
assert(len(got) == len(correct), "not done");
|
|
assert(parse_strings("hello\\d;world") is invalid_escape);
|
|
};
|
|
|
|
@test fn parse_booleans() void = {
|
|
let correct: []bool = [
|
|
true,
|
|
true,
|
|
false,
|
|
true,
|
|
false,
|
|
true,
|
|
];
|
|
let got = parse_booleans("true;true;false;true;false;true")!;
|
|
defer free(got);
|
|
for (let index = 0z; index < len(correct); index += 1) {
|
|
assert(index < len(got), "ran out");
|
|
let correct = correct[index];
|
|
let got = got[index];
|
|
fmt::printf("[{}]\t[{}]\n", correct, got)!;
|
|
assert(correct == got);
|
|
};
|
|
assert(len(got) == len(correct), "not done");
|
|
assert(parse_booleans("hello;world") is invalid_boolean);
|
|
assert(parse_booleans("true;;") is invalid_boolean);
|
|
assert(parse_booleans(";false;") is invalid_boolean);
|
|
};
|
|
|
|
@test fn parse_numerics() void = {
|
|
let correct: []f64 = [
|
|
5.0f32,
|
|
34.9f32,
|
|
29.0f32,
|
|
32498.23784f32,
|
|
];
|
|
let got = parse_numerics("5;34.9;29;32498.23784;")!;
|
|
defer free(got);
|
|
for (let index = 0z; index < len(correct); index += 1) {
|
|
assert(index < len(got), "ran out");
|
|
let correct = correct[index];
|
|
let got = got[index];
|
|
fmt::printf("[{}]\t[{}]\n", correct, got)!;
|
|
assert(correct == got);
|
|
};
|
|
assert(len(got) == len(correct), "not done");
|
|
assert(parse_numerics("hello;world") is invalid_numeric);
|
|
assert(parse_numerics("5;;") is invalid_numeric);
|
|
assert(parse_numerics(";5;") is invalid_numeric);
|
|
};
|
|
|
|
@test fn parse_integers() void = {
|
|
let correct: []int = [
|
|
5,
|
|
34,
|
|
-29,
|
|
32498,
|
|
];
|
|
let got = parse_integers("5;34;-29;32498")!;
|
|
defer free(got);
|
|
for (let index = 0z; index < len(correct); index += 1) {
|
|
assert(index < len(got), "ran out");
|
|
let correct = correct[index];
|
|
let got = got[index];
|
|
fmt::printf("[{}]\t[{}]\n", correct, got)!;
|
|
assert(correct == got);
|
|
};
|
|
assert(len(got) == len(correct), "not done");
|
|
assert(parse_integers("hello;world") is invalid_integer);
|
|
assert(parse_integers("5;;") is invalid_integer);
|
|
assert(parse_integers(";5;") is invalid_integer);
|
|
};
|
|
|
|
@test fn string_escaper_next_a() void = {
|
|
let escaper = escape_string("bird;water;;river;");
|
|
let correct: []str = [
|
|
"bird",
|
|
"water",
|
|
"",
|
|
"river",
|
|
];
|
|
for (let correct .. correct) {
|
|
let got = string_escaper_next(&escaper) as str;
|
|
assert(compare_strings(correct, got));
|
|
};
|
|
assert(string_escaper_next(&escaper) is done, "not done");
|
|
};
|
|
|
|
@test fn string_escaper_next_b() void = {
|
|
let escaper = escape_string("b\\r\\tird;wa\\;ter;;riv\\ner;nuh uh");
|
|
let correct: []str = [
|
|
"b\r\tird",
|
|
"wa;ter",
|
|
"",
|
|
"riv\ner",
|
|
"nuh uh",
|
|
];
|
|
for (let correct .. correct) {
|
|
let got = string_escaper_next(&escaper) as str;
|
|
assert(compare_strings(correct, got));
|
|
};
|
|
assert(string_escaper_next(&escaper) is done, "not done");
|
|
};
|
|
|
|
@test fn splitter_next_a() void = {
|
|
let splitte = split("bird;water;;river;");
|
|
let correct: []str = [
|
|
"bird",
|
|
"water",
|
|
"",
|
|
"river",
|
|
];
|
|
for (let correct .. correct) {
|
|
let got = splitter_next(&splitte) as str;
|
|
assert(compare_strings(correct, got));
|
|
};
|
|
assert(splitter_next(&splitte) is done, "not done");
|
|
};
|
|
|
|
@test fn splitter_next_b() void = {
|
|
let splitte = split("bird;water;;river");
|
|
let correct: []str = [
|
|
"bird",
|
|
"water",
|
|
"",
|
|
"river",
|
|
];
|
|
for (let correct .. correct) {
|
|
let got = splitter_next(&splitte) as str;
|
|
fmt::printf("[{}]\t[{}]\n", correct, got)!;
|
|
assert(compare_strings(correct, got));
|
|
};
|
|
assert(splitter_next(&splitte) is done, "not done");
|
|
};
|
|
|
|
fn compare_strings(a: str, b: str) bool = {
|
|
fmt::printf("[{}]\t[{}]\n", a, b)!;
|
|
return a == b;
|
|
};
|