jak-project/test/test_reader.cpp

356 lines
14 KiB
C++
Raw Normal View History

2020-08-22 22:30:12 -04:00
/*!
* @file test_reader.cpp
* Test the reader.
* For some reason this runs at ~5 fps in CLion IDE.
*/
#include "common/goos/Reader.h"
#include "common/util/FileUtil.h"
2020-08-22 22:30:12 -04:00
#include "gtest/gtest.h"
2020-08-22 22:30:12 -04:00
using namespace goos;
TEST(GoosReader, Construction) {
// test that reader is able to find the source directory
Reader reader;
}
namespace {
bool check_first_integer(Object o, int64_t x) {
return o.as_pair()->cdr.as_pair()->car.as_int() == x;
}
bool check_first_float(Object o, double x) {
return o.as_pair()->cdr.as_pair()->car.as_float() == x;
}
bool check_first_symbol(Object o, const std::string& sym) {
return o.as_pair()->cdr.as_pair()->car.as_symbol() == sym;
2020-08-22 22:30:12 -04:00
}
bool check_first_string(Object o, const std::string& str) {
return o.as_pair()->cdr.as_pair()->car.as_string()->data == str;
}
} // namespace
TEST(GoosReader, Integer) {
Reader reader;
EXPECT_TRUE(check_first_integer(reader.read_from_string("123"), 123));
EXPECT_TRUE(check_first_integer(reader.read_from_string("1"), 1));
EXPECT_TRUE(check_first_integer(reader.read_from_string("-1"), -1));
EXPECT_TRUE(check_first_integer(reader.read_from_string("0"), 0));
EXPECT_TRUE(check_first_integer(reader.read_from_string("9223372036854775807"), INT64_MAX));
EXPECT_TRUE(check_first_integer(reader.read_from_string("-9223372036854775808"), INT64_MIN));
EXPECT_TRUE(check_first_integer(reader.read_from_string("-0"), 0));
EXPECT_TRUE(check_first_integer(reader.read_from_string("-000000"), 0));
EXPECT_TRUE(check_first_integer(
reader.read_from_string(
"-0000000000000000000000000000000000000000000000000000000000000000000000000000000001"),
-1));
EXPECT_TRUE(reader.read_from_string("--0").as_pair()->cdr.as_pair()->car.is_symbol());
// too big or too small.
EXPECT_ANY_THROW(reader.read_from_string("9223372036854775808"));
EXPECT_ANY_THROW(reader.read_from_string("-9223372036854775809"));
}
TEST(GoosReader, Hex) {
Reader reader;
EXPECT_TRUE(check_first_integer(reader.read_from_string("#x0"), 0));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#x1"), 1));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#xf"), 15));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#xF"), 15));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#x0F"), 15));
EXPECT_TRUE(check_first_integer(
reader.read_from_string("#x0000000000000000000000000000000000000000000000000000f"), 15));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#xffffffff"), UINT32_MAX));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#x100000000"), (1LL << 32LL)));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#x7FFFFFFFFFFFFFFF"), INT64_MAX));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#x8000000000000000"), INT64_MIN));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#xffffffffffffffff"), -1));
EXPECT_ANY_THROW(reader.read_from_string("#x10000000000000000"));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x"), "#x"));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x-1"), "#x-1"));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#x.1"), "#x.1"));
}
TEST(GoosReader, Binary) {
Reader reader;
EXPECT_TRUE(check_first_integer(reader.read_from_string("#b0"), 0));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#b0000000000"), 0));
EXPECT_TRUE(check_first_integer(
reader.read_from_string("#b000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000"),
0));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#b1"), 1));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#b10"), 2));
EXPECT_TRUE(check_first_integer(reader.read_from_string("#b01011"), 11));
EXPECT_TRUE(check_first_integer(
reader.read_from_string("#b1111111111111111111111111111111111111111111111111111111111111111"),
-1));
EXPECT_TRUE(check_first_integer(
reader.read_from_string(
"#b000001111111111111111111111111111111111111111111111111111111111111111"),
-1));
EXPECT_TRUE(check_first_integer(
reader.read_from_string("#b0111111111111111111111111111111111111111111111111111111111111111"),
INT64_MAX));
EXPECT_TRUE(check_first_integer(
reader.read_from_string("#b1000000000000000000000000000000000000000000000000000000000000000"),
INT64_MIN));
EXPECT_ANY_THROW(reader.read_from_string(
"#b11111111111111111111111111111111111111111111111111111111111111111"));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b"), "#b"));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b-1"), "#b-1"));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b.1"), "#b.1"));
}
TEST(GoosReader, Float) {
Reader reader;
EXPECT_TRUE(check_first_float(reader.read_from_string("1.6"), 1.6));
EXPECT_TRUE(check_first_float(reader.read_from_string("0000001.6"), 1.6));
EXPECT_TRUE(check_first_float(reader.read_from_string("0.6"), 0.6));
EXPECT_TRUE(check_first_float(reader.read_from_string("00000.6"), 0.6));
EXPECT_TRUE(check_first_float(reader.read_from_string("-0.6"), -0.6));
EXPECT_TRUE(check_first_float(reader.read_from_string("-000000.6"), -0.6));
EXPECT_TRUE(check_first_float(reader.read_from_string("-.6"), -.6));
EXPECT_TRUE(check_first_float(reader.read_from_string("1."), 1));
EXPECT_TRUE(check_first_float(reader.read_from_string("1.0"), 1));
EXPECT_TRUE(check_first_float(reader.read_from_string("01."), 1));
EXPECT_TRUE(check_first_float(reader.read_from_string("01.0"), 1));
EXPECT_TRUE(check_first_float(reader.read_from_string("0."), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string(".0"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("0.0"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("000."), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string(".000"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("0.000"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("000.0"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("000.0000"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-0."), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-.0"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-0.0"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-000."), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-.000"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-0.000"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-000.0"), 0));
EXPECT_TRUE(check_first_float(reader.read_from_string("-000.0000"), 0));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("1e0"), "1e0"));
EXPECT_ANY_THROW(reader.read_from_string("."));
}
TEST(GoosReader, Boolean) {
Reader reader;
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#f"), "#f"));
EXPECT_TRUE(check_first_symbol(reader.read_from_string("#t"), "#t"));
}
TEST(GoosReader, String) {
Reader reader;
EXPECT_TRUE(
check_first_string(reader.read_from_string("\"testing string ()\""), "testing string ()"));
EXPECT_TRUE(check_first_string(reader.read_from_string("\"\""), ""));
EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\t \""), " \t "));
EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\n \""), " \n "));
EXPECT_TRUE(check_first_string(reader.read_from_string("\"test \\n\""), "test \n"));
EXPECT_TRUE(check_first_string(reader.read_from_string("\" \\\\ \""), " \\ "));
EXPECT_ANY_THROW(reader.read_from_string("\"\\\"")); // "\" invalid escape
EXPECT_ANY_THROW(reader.read_from_string("\"\\w\"")); // "\w" invalid escape
}
2020-11-17 15:25:48 -05:00
TEST(GoosReader, StringWithNumberEscapes) {
Reader reader;
// build a weird test string
std::string str;
for (int i = 1; i < 256; i++) {
str.push_back(i);
}
// create a readable string:
std::string readable = "\"";
readable += goos::get_readable_string(str.data());
readable.push_back('"');
EXPECT_TRUE(check_first_string(reader.read_from_string(readable), str));
EXPECT_ANY_THROW(reader.read_from_string("\"\\c\""));
EXPECT_ANY_THROW(reader.read_from_string("\"\\c1\""));
EXPECT_ANY_THROW(reader.read_from_string("\"\\cag\""));
EXPECT_ANY_THROW(reader.read_from_string("\"\\c-1\""));
EXPECT_ANY_THROW(reader.read_from_string("\"\\c-2\""));
}
2020-08-22 22:30:12 -04:00
TEST(GoosReader, Symbol) {
std::vector<std::string> test_symbols = {
"test", "test-two", "__werid-sym__", "-a", "-", "/", "*", "+", "a", "#f"};
Reader reader;
for (const auto& sym : test_symbols) {
EXPECT_TRUE(check_first_symbol(reader.read_from_string(sym), sym));
}
}
namespace {
bool first_list_matches(Object o, std::vector<Object> stuff) {
[game] pc port progress menu (#1281) * fix typo * more typo * shorten discord rpc text * allow expanding enums after the fact (untested) * make `game_text` work similar to subtitles * update progress decomp * update some types + `do-not-decompile` in bitfield * fixes and fall back to original progress code * update `progress` decomp with new enums * update config files * fix enums and debug menu * always allocate (but not use) a lot of particles * small rework to display mode options * revert resolution/aspect-ratio symbol mess * begin the override stuff * make `progress-draw` more readable * more fixes * codacy good boy points * first step overriding code * finish progress overrides, game options menu fully functional! * minor fixes * Update game.gp * Update sparticle-launcher.gc * clang * change camera controls text * oops * some cleanup * derp * nice job * implement menu scrolling lol * make scrollable menus less cramped, fix arrows * make some carousell things i guess * add msaa carousell to test * oops * Update progress-pc.gc * make `pc-get-screen-size` (untested) * resolution menu * input fixes * return when selecting resolution * scroll fixes * Update progress-pc.gc * add "fit to screen" button * bug * complete resolutions menu * aspect ratio menu * subtitles language * subtitle speaker * final adjustments * ref test * fix tests * fix ref! * reduce redundancy a bit * fix mem leaks? * save settings on progress exit * fix init reorder * remove unused code * rename goal project-like files to the project extension * sha display toggle * aspect ratio settings fixes * dont store text db's in compiler * properly save+load native aspect stuff
2022-04-11 18:38:54 -04:00
auto& lst = o.as_pair()->cdr.as_pair()->car;
2020-08-22 22:30:12 -04:00
for (const auto& x : stuff) {
[game] pc port progress menu (#1281) * fix typo * more typo * shorten discord rpc text * allow expanding enums after the fact (untested) * make `game_text` work similar to subtitles * update progress decomp * update some types + `do-not-decompile` in bitfield * fixes and fall back to original progress code * update `progress` decomp with new enums * update config files * fix enums and debug menu * always allocate (but not use) a lot of particles * small rework to display mode options * revert resolution/aspect-ratio symbol mess * begin the override stuff * make `progress-draw` more readable * more fixes * codacy good boy points * first step overriding code * finish progress overrides, game options menu fully functional! * minor fixes * Update game.gp * Update sparticle-launcher.gc * clang * change camera controls text * oops * some cleanup * derp * nice job * implement menu scrolling lol * make scrollable menus less cramped, fix arrows * make some carousell things i guess * add msaa carousell to test * oops * Update progress-pc.gc * make `pc-get-screen-size` (untested) * resolution menu * input fixes * return when selecting resolution * scroll fixes * Update progress-pc.gc * add "fit to screen" button * bug * complete resolutions menu * aspect ratio menu * subtitles language * subtitle speaker * final adjustments * ref test * fix tests * fix ref! * reduce redundancy a bit * fix mem leaks? * save settings on progress exit * fix init reorder * remove unused code * rename goal project-like files to the project extension * sha display toggle * aspect ratio settings fixes * dont store text db's in compiler * properly save+load native aspect stuff
2022-04-11 18:38:54 -04:00
const auto& check = x.as_pair()->cdr.as_pair()->car;
2020-08-22 22:30:12 -04:00
if (lst.as_pair()->car != check) {
return false;
}
lst = lst.as_pair()->cdr;
}
return lst.is_empty_list();
}
bool first_array_matches(Object o, std::vector<Object> stuff) {
auto array = o.as_pair()->cdr.as_pair()->car.as_array();
if (stuff.size() != array->size()) {
return false;
}
for (size_t i = 0; i < array->size(); i++) {
if ((*array)[i] != stuff.at(i)) {
return false;
}
}
return true;
}
bool first_pair_matches(Object o, Object car, Object cdr) {
[game] pc port progress menu (#1281) * fix typo * more typo * shorten discord rpc text * allow expanding enums after the fact (untested) * make `game_text` work similar to subtitles * update progress decomp * update some types + `do-not-decompile` in bitfield * fixes and fall back to original progress code * update `progress` decomp with new enums * update config files * fix enums and debug menu * always allocate (but not use) a lot of particles * small rework to display mode options * revert resolution/aspect-ratio symbol mess * begin the override stuff * make `progress-draw` more readable * more fixes * codacy good boy points * first step overriding code * finish progress overrides, game options menu fully functional! * minor fixes * Update game.gp * Update sparticle-launcher.gc * clang * change camera controls text * oops * some cleanup * derp * nice job * implement menu scrolling lol * make scrollable menus less cramped, fix arrows * make some carousell things i guess * add msaa carousell to test * oops * Update progress-pc.gc * make `pc-get-screen-size` (untested) * resolution menu * input fixes * return when selecting resolution * scroll fixes * Update progress-pc.gc * add "fit to screen" button * bug * complete resolutions menu * aspect ratio menu * subtitles language * subtitle speaker * final adjustments * ref test * fix tests * fix ref! * reduce redundancy a bit * fix mem leaks? * save settings on progress exit * fix init reorder * remove unused code * rename goal project-like files to the project extension * sha display toggle * aspect ratio settings fixes * dont store text db's in compiler * properly save+load native aspect stuff
2022-04-11 18:38:54 -04:00
auto& lst = o.as_pair()->cdr.as_pair()->car;
2020-08-22 22:30:12 -04:00
return (lst.as_pair()->car == car) && (lst.as_pair()->cdr == cdr);
}
[game] pc port progress menu (#1281) * fix typo * more typo * shorten discord rpc text * allow expanding enums after the fact (untested) * make `game_text` work similar to subtitles * update progress decomp * update some types + `do-not-decompile` in bitfield * fixes and fall back to original progress code * update `progress` decomp with new enums * update config files * fix enums and debug menu * always allocate (but not use) a lot of particles * small rework to display mode options * revert resolution/aspect-ratio symbol mess * begin the override stuff * make `progress-draw` more readable * more fixes * codacy good boy points * first step overriding code * finish progress overrides, game options menu fully functional! * minor fixes * Update game.gp * Update sparticle-launcher.gc * clang * change camera controls text * oops * some cleanup * derp * nice job * implement menu scrolling lol * make scrollable menus less cramped, fix arrows * make some carousell things i guess * add msaa carousell to test * oops * Update progress-pc.gc * make `pc-get-screen-size` (untested) * resolution menu * input fixes * return when selecting resolution * scroll fixes * Update progress-pc.gc * add "fit to screen" button * bug * complete resolutions menu * aspect ratio menu * subtitles language * subtitle speaker * final adjustments * ref test * fix tests * fix ref! * reduce redundancy a bit * fix mem leaks? * save settings on progress exit * fix init reorder * remove unused code * rename goal project-like files to the project extension * sha display toggle * aspect ratio settings fixes * dont store text db's in compiler * properly save+load native aspect stuff
2022-04-11 18:38:54 -04:00
bool print_matches(Object o, const std::string& expected) {
2020-08-22 22:30:12 -04:00
return o.as_pair()->cdr.as_pair()->car.print() == expected;
}
bool first_char_matches(Object o, char c) {
return o.as_pair()->cdr.as_pair()->car.as_char() == c;
}
} // namespace
TEST(GoosReader, List) {
Reader reader;
auto r = [&](std::string s) { return reader.read_from_string(s); };
EXPECT_TRUE(first_list_matches(r("()"), {}));
EXPECT_TRUE(first_list_matches(r("(1)"), {r("1")}));
EXPECT_TRUE(first_list_matches(r(" ( 1 ) "), {r("1")}));
EXPECT_TRUE(first_list_matches(r("(1 2 3)"), {r("1"), r("2"), r("3")}));
EXPECT_TRUE(first_list_matches(r(" ( 1 bbbb 3 ) "), {r("1"), r("bbbb"), r("3")}));
EXPECT_TRUE(first_pair_matches(r("(1 . 2)"), Object::make_integer(1), Object::make_integer(2)));
EXPECT_TRUE(print_matches(r(" ( 1 . 2 ) "), "(1 . 2)"));
EXPECT_TRUE(print_matches(r(" ( 1 1 . 2 ) "), "(1 1 . 2)"));
EXPECT_TRUE(print_matches(r(" ( 1 . ( 1 . 2 ) ) "), "(1 1 . 2)"));
EXPECT_TRUE(
print_matches(r(" ( 1 ( 1 2 ) ( 1 ( 12 3 ) ) . 3 ) "), "(1 (1 2) (1 (12 3)) . 3)"));
EXPECT_TRUE(
print_matches(r(" ( 1 ( 1 2 ) ( 1 ( 12 3 ) ) . ( ) ) "), "(1 (1 2) (1 (12 3)))"));
std::vector<std::string> expected_to_throw = {"(", ")", " (", " )()() ",
")(", "(1 2 ))", "(( 1 2)", "(1 . . 2)",
"(1 . )", "(1 . 2 3)", "( . 2)"};
for (const auto& x : expected_to_throw) {
EXPECT_ANY_THROW(r(x));
}
}
TEST(GoosReader, Comments) {
Reader reader;
auto r = [&](std::string s) { return reader.read_from_string(s); };
EXPECT_TRUE(first_list_matches(r(";;\n(1)\n;;"), {r("1")}));
EXPECT_TRUE(first_list_matches(r(";;\n(;1\n1;)\n);;\n;"), {r("1")}));
r(";");
r(" ;");
r("\n;");
r(";\n");
EXPECT_TRUE(first_list_matches(
r("#|multi line\n com(((((ment |# (1) #| multi line\n comm)))))ent |#"), {r("1")}));
EXPECT_TRUE(first_list_matches(
r("#| #| multi l#|ine\n com#|ment |# (1) #| multi line\n commen))))))t |#"), {r("1")}));
std::vector<std::string> expected_to_throw = {"|#", "#| |# |#"};
for (const auto& x : expected_to_throw) {
EXPECT_ANY_THROW(r(x));
}
}
TEST(GoosReader, Char) {
Reader reader;
auto r = [&](std::string s) { return reader.read_from_string(s); };
EXPECT_TRUE(first_char_matches(r("#\\c"), 'c'));
EXPECT_TRUE(first_char_matches(r("#\\n"), 'n'));
EXPECT_TRUE(first_char_matches(r("#\\\\n"), '\n'));
EXPECT_TRUE(first_char_matches(r("#\\\\t"), '\t'));
EXPECT_TRUE(first_char_matches(r("#\\\\s"), ' '));
}
TEST(GoosReader, Array) {
Reader reader;
auto r = [&](std::string s) { return reader.read_from_string(s); };
EXPECT_TRUE(print_matches(r(" #( ) "), "#()"));
EXPECT_TRUE(first_array_matches(r("#()"), {}));
EXPECT_TRUE(first_array_matches(r("#(1 2)"), {Object::make_integer(1), Object::make_integer(2)}));
EXPECT_TRUE(first_array_matches(r("#( 1 #| 2 |# 3 )"),
{Object::make_integer(1), Object::make_integer(3)}));
EXPECT_TRUE(
first_array_matches(r("#( 1 #|2|# 3 )"), {Object::make_integer(1), Object::make_integer(3)}));
}
TEST(GoosReader, Macros) {
Reader reader;
auto r = [&](std::string s) { return reader.read_from_string(s); };
EXPECT_TRUE(print_matches(r("'x"), "(quote x)"));
EXPECT_TRUE(print_matches(r("`x"), "(quasiquote x)"));
EXPECT_TRUE(print_matches(r(",x"), "(unquote x)"));
EXPECT_TRUE(print_matches(r(",@x"), "(unquote-splicing x)"));
}
TEST(GoosReader, TopLevel) {
Reader reader;
auto r = [&](std::string s) { return reader.read_from_string(s); };
EXPECT_EQ(r("x").print(), "(top-level x)");
}
TEST(GoosReader, FromFile) {
Reader reader;
auto result = reader.read_from_file({"test", "test_data", "test_reader_file0.gc"}).print();
2020-08-22 22:30:12 -04:00
EXPECT_TRUE(result == "(top-level (1 2 3 4))");
}
TEST(GoosReader, TextDb) {
// very specific to this particular test file, but whatever.
Reader reader;
auto result = reader.read_from_file({"test", "test_data", "test_reader_file0.gc"})
.as_pair()
->cdr.as_pair()
->car;
decomp: finish the remainder of untouched gameplay code (#893) * decomp: finish `sidekick` * decomp: got a lot of `target` done * decompiler: Add support for non power of 2 offsets for inline arr access * decomp: finish `target` mostly * decomp: finish `water` * decomp: finished `robotboss-weapon` * decomp: finish `robotboss-misc` * decomp: finish the majority of `robotboss` * blocked: `racer` has an issue around entering a state * blocked: `target-racer` done mostly, but NYI case in one function * blocked: `racer-states` mostly finished, but bitfield issue * blocked: `billy` on state decomping * blocked: `bully` on state decomping * waiting: `rolling-lightning-mole` waiting on navigate for 2 funcs * blocked: `rolling-robber` finished but `s6-1` issue * blocked: `ogreboss` uint64's for types cant label load em! * blocked: `mother-spider` state decompilation * half-done `target-flut` * blocked: `target-flut` some sort of new bitfield state * some improvements in `racer-states` with my new-found knowledge * progress: started on `target-death` * blocked: `target-death` handle casts * decomp: finish `collide-reaction-racer` * blocked: `target-handler` handler forced to return `none` * decomp: 99% of `target2` finished * decomp: finish `target2` * gsrc: update * update post merge * address feedback * scripts: add script to detect decomp issues * fix wide-spread `collide-shape` method missing arg * some small things i changed from master * address feedback * fix typeconsistency issue
2021-11-24 00:33:10 -05:00
std::string expected = "test/test_data/test_reader_file0.gc:5\n(1 2 3 4)\n ^\n";
2020-08-22 22:30:12 -04:00
EXPECT_EQ(expected, reader.db.get_info_for(result));
}