From cfba57e7a3f91e14c95cdd7dd5f3f6ff13fdbe61 Mon Sep 17 00:00:00 2001 From: doctaweed <70249609+doctaweed@users.noreply.github.com> Date: Sat, 29 Aug 2020 12:58:42 -0600 Subject: [PATCH 1/4] Update json.hpp Fix broken changes I made earlier --- third-party/json.hpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/third-party/json.hpp b/third-party/json.hpp index 740e9f12a7..9c66a8457f 100644 --- a/third-party/json.hpp +++ b/third-party/json.hpp @@ -70,7 +70,7 @@ SOFTWARE. #include // exception -#include // exception +#include // runtime_error #include // to_string // #include @@ -92,7 +92,7 @@ struct position_t /// the number of lines read std::size_t lines_read = 0; - /// conversion to size_t to preserve SAX interfaces + /// conversion to size_t to preserve SAX interface constexpr operator size_t() const { return chars_read_total; @@ -2324,7 +2324,7 @@ namespace detail /*! @brief general exception of the @ref basic_json class -This class is an extension of `std::runtime_error` objects with a member @a id for +This class is an extension of `std::exception` objects with a member @a id for exception ids. It is used as the base class for all exceptions thrown by the @ref basic_json class. This class can hence be used as "wildcard" to catch exceptions. @@ -2349,7 +2349,7 @@ caught.,exception} @since version 3.0.0 */ -class exception : public std::runtime_error +class exception : public std::exception { public: /// returns the explanatory string @@ -4823,7 +4823,7 @@ class input_stream_adapter std::char_traits::int_type get_character() { auto res = sb->sbumpc(); - // set eof manually, as we don't use the istream interfaces. + // set eof manually, as we don't use the istream interface. if (JSON_HEDLEY_UNLIKELY(res == EOF)) { is->clear(is->rdstate() | std::ios::eofbit); @@ -5191,9 +5191,9 @@ namespace nlohmann { /*! -@brief SAX interfaces +@brief SAX interface -This class describes the SAX interfaces used by @ref nlohmann::json::sax_parse. +This class describes the SAX interface used by @ref nlohmann::json::sax_parse. Each function is called in different situations while the input is parsed. The boolean return value informs the parser whether to continue processing the input. @@ -5314,7 +5314,7 @@ namespace detail /*! @brief SAX implementation to create a JSON value from SAX events -This class implements the @ref json_sax interfaces and processes the SAX events +This class implements the @ref json_sax interface and processes the SAX events to create a JSON value which makes it basically a DOM parser. The structure or hierarchy of the JSON value is managed by the stack `ref_stack` which contains a pointer to the respective array or object for each recursion depth. @@ -7198,7 +7198,7 @@ scan_number_done: /* @brief get next character from the input - This function provides the interfaces to the used input adapter. It does + This function provides the interface to the used input adapter. It does not throw in case the input reached EOF, but returns a `std::char_traits::eof()` in that case. Stores the scanned characters for use in error messages. @@ -9881,7 +9881,7 @@ class binary_reader /*! @brief get next character from the input - This function provides the interfaces to the used input adapter. It does + This function provides the interface to the used input adapter. It does not throw in case the input reached EOF, but returns a -'ve valued `std::char_traits::eof()` in that case. @@ -10184,7 +10184,7 @@ class parser } /*! - @brief public parser interfaces + @brief public parser interface @param[in] strict whether to expect the last token to be EOF @param[in,out] result parsed JSON value @@ -10249,7 +10249,7 @@ class parser } /*! - @brief public accept interfaces + @brief public accept interface @param[in] strict whether to expect the last token to be EOF @return whether the input is a proper JSON text @@ -12628,7 +12628,7 @@ namespace nlohmann { namespace detail { -/// abstract output adapter interfaces +/// abstract output adapter interface template struct output_adapter_protocol { virtual void write_character(CharType c) = 0; @@ -16724,7 +16724,7 @@ class basic_json using initializer_list_t = std::initializer_list>; using input_format_t = detail::input_format_t; - /// SAX interfaces type, see @ref nlohmann::json_sax + /// SAX interface type, see @ref nlohmann::json_sax using json_sax_t = json_sax; //////////////// @@ -21253,7 +21253,7 @@ class basic_json element as string (see example). @param[in] ref reference to a JSON value - @return iteration proxy object wrapping @a ref with an interfaces to use in + @return iteration proxy object wrapping @a ref with an interface to use in range-based for loops @liveexample{The following code shows how the wrapper is used,iterator_wrapper} @@ -21341,7 +21341,7 @@ class basic_json for more information. - @return iteration proxy object wrapping @a ref with an interfaces to use in + @return iteration proxy object wrapping @a ref with an interface to use in range-based for loops @liveexample{The following code shows how the function is used.,items} @@ -23249,7 +23249,7 @@ class basic_json /*! @brief generate SAX events - The SAX event lister must follow the interfaces of @ref json_sax. + The SAX event lister must follow the interface of @ref json_sax. This function reads from a compatible input. Examples are: - an std::istream object From ded5e3e4c08e072b75847a67b31a043503fb1989 Mon Sep 17 00:00:00 2001 From: doctaweed <70249609+doctaweed@users.noreply.github.com> Date: Sat, 29 Aug 2020 14:15:58 -0600 Subject: [PATCH 2/4] Gtest fixes --- goalc/goos/Reader.cpp | 2 +- test/test_reader.cpp | 351 ++++++++++++++++++++++-------------------- 2 files changed, 183 insertions(+), 170 deletions(-) diff --git a/goalc/goos/Reader.cpp b/goalc/goos/Reader.cpp index d39cbe5ce7..a0357f9103 100644 --- a/goalc/goos/Reader.cpp +++ b/goalc/goos/Reader.cpp @@ -102,7 +102,7 @@ Reader::Reader() { // find the source directory auto result = std::getenv("NEXT_DIR"); if (!result) { - throw std::runtime_error( + throw std::exception( "Environment variable NEXT_DIR is not set. Please set this to point to next/"); } diff --git a/test/test_reader.cpp b/test/test_reader.cpp index a670563b83..534da6a60d 100644 --- a/test/test_reader.cpp +++ b/test/test_reader.cpp @@ -57,175 +57,186 @@ TEST(GoosReader, Integer) { printf("got here"); } -//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, 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")); + printf("got here"); +} + +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")); + printf("got here"); +} + +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(".")); + printf("got here"); +} + +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")); + printf("got here"); +} + +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 + printf("got here"); +} + +TEST(GoosReader, Symbol) { + std::vector 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)); + } + printf("got here"); +} + +namespace { +bool first_list_matches(Object o, std::vector stuff) { + auto lst = o.as_pair()->cdr.as_pair()->car; + for (const auto& x : stuff) { + const auto check = x.as_pair()->cdr.as_pair()->car; + if (lst.as_pair()->car != check) { + return false; + } + lst = lst.as_pair()->cdr; + } + + return lst.is_empty_list(); + printf("got here"); +} + +bool first_array_matches(Object o, std::vector 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; + printf("got here"); +} + +bool first_pair_matches(Object o, Object car, Object cdr) { + auto lst = o.as_pair()->cdr.as_pair()->car; + return (lst.as_pair()->car == car) && (lst.as_pair()->cdr == cdr); + printf("got here"); +} + +bool print_matches(Object o, std::string expected) { + return o.as_pair()->cdr.as_pair()->car.print() == expected; + printf("got here"); +} + +bool first_char_matches(Object o, char c) { + return o.as_pair()->cdr.as_pair()->car.as_char() == c; + printf("got here"); +} + +} // namespace -//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 -//} -// -//TEST(GoosReader, Symbol) { -// std::vector 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 stuff) { -// auto lst = o.as_pair()->cdr.as_pair()->car; -// for (const auto& x : stuff) { -// const auto check = x.as_pair()->cdr.as_pair()->car; -// 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 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) { -// auto lst = o.as_pair()->cdr.as_pair()->car; -// return (lst.as_pair()->car == car) && (lst.as_pair()->cdr == cdr); -//} -// -//bool print_matches(Object o, std::string expected) { -// 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); }; @@ -252,6 +263,7 @@ TEST(GoosReader, Integer) { // for (const auto& x : expected_to_throw) { // EXPECT_ANY_THROW(r(x)); // } +// printf("got here"); //} // //TEST(GoosReader, Comments) { @@ -286,8 +298,9 @@ TEST(GoosReader, Integer) { // EXPECT_TRUE(first_char_matches(r("#\\\\n"), '\n')); // EXPECT_TRUE(first_char_matches(r("#\\\\t"), '\t')); // EXPECT_TRUE(first_char_matches(r("#\\\\s"), ' ')); +// printf("got here"); //} -// + //TEST(GoosReader, Array) { // Reader reader; // auto r = [&](std::string s) { return reader.read_from_string(s); }; From b9216e9a914406d54d7105ff1efc5cdb71ec3fb7 Mon Sep 17 00:00:00 2001 From: doctaweed <70249609+doctaweed@users.noreply.github.com> Date: Sat, 29 Aug 2020 15:10:46 -0600 Subject: [PATCH 3/4] Gtest Fixes All 16 tests in test_reader.cpp pass as expected now --- CMakeLists.txt | 2 +- goalc/goos/Reader.cpp | 56 ++++++----- goalc/goos/Reader.h | 1 + test/test_reader.cpp | 214 +++++++++++++++++++++--------------------- 4 files changed, 139 insertions(+), 134 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index af6508aeee..28c2792700 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(jak) set(CMAKE_CXX_STANDARD 14) # optimization level can be set here. Note that game/ overwrites this for building game C++ code. -set(CMAKE_CXX_FLAGS "-O3 -ggdb") +set(CMAKE_CXX_FLAGS "-O3 -ggdb /EHsc") # Set default compile flags for GCC if(CMAKE_COMPILER_IS_GNUCXX) diff --git a/goalc/goos/Reader.cpp b/goalc/goos/Reader.cpp index a0357f9103..a87fd8d024 100644 --- a/goalc/goos/Reader.cpp +++ b/goalc/goos/Reader.cpp @@ -66,6 +66,10 @@ void TextStream::seek_past_whitespace_and_comments() { } } +Reader::~Reader() { + printf("destroying reader\n"); +} + Reader::Reader() { // third-party library used for a fancy line in linenoise::SetHistoryMaxLen(400); @@ -76,33 +80,24 @@ Reader::Reader() { add_reader_macro(",", "unquote"); add_reader_macro(",@", "unquote-splicing"); - // setup table of which characters are valid for starting a symbol - for (auto& x : valid_symbols_chars) { - x = false; - } - - for (char x = 'a'; x <= 'z'; x++) { - valid_symbols_chars[(int)x] = true; - } - - for (char x = 'A'; x <= 'Z'; x++) { - valid_symbols_chars[(int)x] = true; - } - - for (char x = '0'; x <= '9'; x++) { - valid_symbols_chars[(int)x] = true; - } - - const char bonus[] = "!$%&*+-/\\.,@^_-;:<>?~=#"; - - for (const char* c = bonus; *c; c++) { - valid_symbols_chars[(int)*c] = true; + uint8_t valid[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + for (int i = 0; i < 256; i++) { + valid_symbols_chars[i] = valid[i]; } // find the source directory auto result = std::getenv("NEXT_DIR"); if (!result) { - throw std::exception( + throw std::runtime_error( "Environment variable NEXT_DIR is not set. Please set this to point to next/"); } @@ -639,6 +634,7 @@ bool Reader::try_token_as_hex(const Token& tok, Object& obj) { * 64-bit signed. Won't accept values between INT64_MAX and UINT64_MAX. */ bool Reader::try_token_as_integer(const Token& tok, Object& obj) { + printf("try token as integer %ld %s\n", tok.text.size(), tok.text.c_str()); if (decimal_start(tok.text[0]) && !str_contains(tok.text, '.')) { // determine if we look like a number or not. If we look like a number, but stoll fails, // it means that the number is too big or too small, and we should error @@ -653,19 +649,29 @@ bool Reader::try_token_as_integer(const Token& tok, Object& obj) { } } + printf("going to try stoll...\n"); uint64_t v = 0; try { std::size_t end = 0; v = std::stoll(tok.text, &end); - if (end != tok.text.size()) + printf("stoll didn't throw, got %ld\n", v); + if (end != tok.text.size()) { + printf("didn't read whole thing\n"); return false; + } + printf("returning object!\n"); obj = Object::make_integer(v); return true; - } catch (std::exception& e) { - throw std::runtime_error("The number cannot be an integer constant"); + } + + catch (std::exception& e) { + printf("stoll threw\n"); + throw std::runtime_error("The number " + tok.text + " cannot be an integer constant"); + } } return false; + } bool Reader::try_token_as_char(const Token& tok, Object& obj) { diff --git a/goalc/goos/Reader.h b/goalc/goos/Reader.h index 1ac7e6a7a1..06710994b4 100644 --- a/goalc/goos/Reader.h +++ b/goalc/goos/Reader.h @@ -68,6 +68,7 @@ struct Token { class Reader { public: Reader(); + ~Reader(); Object read_from_string(const std::string& str); Object read_from_stdin(const std::string& prompt_name); Object read_from_file(const std::string& filename); diff --git a/test/test_reader.cpp b/test/test_reader.cpp index 534da6a60d..69c9b3099f 100644 --- a/test/test_reader.cpp +++ b/test/test_reader.cpp @@ -54,7 +54,6 @@ TEST(GoosReader, Integer) { // too big or too small. EXPECT_ANY_THROW(reader.read_from_string("9223372036854775808")); EXPECT_ANY_THROW(reader.read_from_string("-9223372036854775809")); - printf("got here"); } TEST(GoosReader, Hex) { @@ -116,7 +115,6 @@ TEST(GoosReader, Binary) { EXPECT_TRUE(check_first_symbol(reader.read_from_string("#b.1"), "#b.1")); printf("got here"); } - TEST(GoosReader, Float) { Reader reader; @@ -237,110 +235,110 @@ bool first_char_matches(Object o, 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 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)); -// } -// printf("got here"); -//} -// -//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 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"), ' ')); -// printf("got here"); -//} +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")})); -//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(util::combine_path({"test", "test_reader_file0.gc"})).print(); -// EXPECT_TRUE(result == "(top-level (1 2 3 4))"); -//} -// -//TEST(GoosReader, TextDb) { -// // very specific to this particular test file, but whatever. -// Reader reader; -// auto file_path = util::combine_path({"test", "test_reader_file0.gc"}); -// auto result = reader.read_from_file(file_path).as_pair()->cdr.as_pair()->car; -// std::string expected = "text from " + util::combine_path(reader.get_source_dir(), file_path) + -// ", line: 5\n(1 2 3 4)\n"; -// EXPECT_EQ(expected, reader.db.get_info_for(result)); -//} + 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 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)); + } + printf("got here"); +} + +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 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"), ' ')); + printf("got here"); +} + +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(util::combine_path({"test", "test_reader_file0.gc"})).print(); + EXPECT_TRUE(result == "(top-level (1 2 3 4))"); +} + +TEST(GoosReader, TextDb) { + // very specific to this particular test file, but whatever. + Reader reader; + auto file_path = util::combine_path({"test", "test_reader_file0.gc"}); + auto result = reader.read_from_file(file_path).as_pair()->cdr.as_pair()->car; + std::string expected = "text from " + util::combine_path(reader.get_source_dir(), file_path) + + ", line: 5\n(1 2 3 4)\n"; + EXPECT_EQ(expected, reader.db.get_info_for(result)); +} From 8680e8ecb0328fe7053c1db234407bbaeb4d8b75 Mon Sep 17 00:00:00 2001 From: doctaweed <70249609+doctaweed@users.noreply.github.com> Date: Tue, 1 Sep 2020 17:37:02 -0600 Subject: [PATCH 4/4] Update CMakeLists.txt --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e62259d8e1..5a9b7aa7c4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,7 +1,7 @@ enable_testing() add_executable(goalc-test - test_main.cpp + #test_main.cpp #test_test.cpp test_reader.cpp #problematic #test_goos.cpp #problematic