diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index 4b411e6..0000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 8c83e32..d1c0f60 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ + + diff --git a/src/support/include/Logger.h b/src/support/include/Logger.h index eed5e36..2ad9e12 100644 --- a/src/support/include/Logger.h +++ b/src/support/include/Logger.h @@ -14,14 +14,17 @@ #include #include -class Logger { +class Logger +{ public: Logger(); enum LogLevel { ALWAYS = 0, ERROR = 1, INFO = 2, DEBUG = 3, TRACE = 4 }; + enum LogTag { MemoryContext, VM, Compiler, LogTagMax }; - static void log(LogTag tag, const std::string &what, int level); - static void log(LogTag tag, const std::function &fn, int level); + + static void log(LogTag tag, const std::string& what, int level); + static void log(LogTag tag, const std::function& fn, int level); // 0 - disabled // 1 - error @@ -32,25 +35,32 @@ public: static int get_level(LogTag tag); static bool en_level(LogTag tag, int level); - static void set_out(std::ostream &out); - static void set_out_err(std::ostream &out_err); + static void set_out(std::ostream& out); + static void set_out_err(std::ostream& out_err); static void reset(); - static Logger &get(); + static Logger& get(); - static LogTag str_to_tag(const std::string &str) { return _str_to_tag.at(str); } + static LogTag str_to_tag(const std::string& str) { return _str_to_tag.at(str); } + static const std::string& tag_to_str(LogTag tag) { return _tag_to_str.at(tag); } private: std::array _levels{}; static inline std::unordered_map _level_names{ - {ALWAYS, "ALWAYS"}, {ERROR, "ERROR"}, {INFO, "INFO"}, {DEBUG, "DEBUG"}, {TRACE, "TRACE"}, + {ALWAYS, "ALWAYS"}, {ERROR, "ERROR"}, {INFO, "INFO"}, {DEBUG, "DEBUG"}, {TRACE, "TRACE"}, }; static inline std::unordered_map _str_to_tag{ - {"VM", VM}, - {"MemoryContext", MemoryContext}, - {"Compiler", Compiler}, + {"VM", VM}, + {"MemoryContext", MemoryContext}, + {"Compiler", Compiler}, }; + static inline std::unordered_map _tag_to_str{ + {VM, "VM"}, + {MemoryContext, "MemoryContext"}, + {Compiler, "Compiler"}, + }; + std::chrono::time_point _start_time = std::chrono::high_resolution_clock::now(); std::reference_wrapper _out = std::cout; std::reference_wrapper _out_err = std::cerr; diff --git a/src/support/src/Logger.cpp b/src/support/src/Logger.cpp index 3ed4bfd..ac39b5b 100644 --- a/src/support/src/Logger.cpp +++ b/src/support/src/Logger.cpp @@ -8,6 +8,7 @@ #include #include "Options.h" + Logger &Logger::get() { static Logger logger; return logger; @@ -19,15 +20,14 @@ void Logger::log(LogTag tag, const std::string &what, int level) { auto now = std::chrono::high_resolution_clock::now(); std::stringstream out; out << std::setprecision(3) << std::fixed << "[" - << static_cast( - std::chrono::duration_cast(now - get()._start_time).count()) / - 1000.0 - << "s]" - << "[" << tag << "][" << get()._level_names.at(level) << "] " << what << '\n'; + << static_cast( + std::chrono::duration_cast(now - get()._start_time).count()) / + 1000.0 + << "s]" + << "[" << tag_to_str(tag) << "][" << get()._level_names.at(level) << "] " << what << '\n'; if (level == 1) get()._out_err.get() << out.str(); - else - get()._out.get() << out.str(); + else get()._out.get() << out.str(); } } @@ -52,4 +52,4 @@ bool Logger::en_level(LogTag tag, int level) { return true; } -Logger::Logger() { _levels.fill(static_cast(Options::get("default_log_level"))); } +Logger::Logger() { _levels.fill(static_cast(Options::get("default_log_level"))); } \ No newline at end of file diff --git a/test/vm/CompilerTest.cpp b/test/vm/CompilerTest.cpp index d05ac35..67c33a3 100644 --- a/test/vm/CompilerTest.cpp +++ b/test/vm/CompilerTest.cpp @@ -39,7 +39,7 @@ TEST(CompilerTest, BasicHello) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "3\n"); + ASSERT_EQ(ssout.str(), "3\n"); } TEST(CompilerTest, BasicLet) { @@ -54,7 +54,7 @@ TEST(CompilerTest, BasicLet) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "1\n"); + ASSERT_EQ(ssout.str(), "1\n"); } TEST(CompilerTest, BasicFn) { @@ -69,7 +69,7 @@ TEST(CompilerTest, BasicFn) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "5\n"); + ASSERT_EQ(ssout.str(), "5\n"); } TEST(CompilerTest, BasicFn2) { @@ -84,7 +84,7 @@ TEST(CompilerTest, BasicFn2) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "-1\n"); + ASSERT_EQ(ssout.str(), "-1\n"); } TEST(CompilerTest, MultiLet) { @@ -95,7 +95,7 @@ TEST(CompilerTest, MultiLet) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (let ((plfn (lambda (a b) (- a b))) (plfn2 (lambda (a b) (- b a)))) (plfn 2 3)) EVAL " - "PRINT STOP)"); + "PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } @@ -104,12 +104,12 @@ TEST(CompilerTest, MultiLet) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (let ((plfn (lambda (a b) (- a b))) (plfn2 (lambda (a b) (- b a)))) (plfn2 2 3)) EVAL " - "PRINT STOP)"); + "PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "-1\n1\n"); + ASSERT_EQ(ssout.str(), "-1\n1\n"); } @@ -125,8 +125,9 @@ TEST(CompilerTest, BasicFnIfT) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "1\n"); + ASSERT_EQ(ssout.str(), "1\n"); } + TEST(CompilerTest, BasicFnIfF) { std::stringstream ssin; std::stringstream ssout; @@ -139,8 +140,9 @@ TEST(CompilerTest, BasicFnIfF) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "2\n"); + ASSERT_EQ(ssout.str(), "2\n"); } + TEST(CompilerTest, RecursiveFn) { std::stringstream ssin; std::stringstream ssout; @@ -149,12 +151,12 @@ TEST(CompilerTest, RecursiveFn) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (letrec ((fib (lambda (n) (if n (if (+ n -1) (+ (fib (+ n -1)) (fib(+ n -2))) 1) 0) ))) " - "(fib 10)) EVAL PRINT STOP)"); + "(fib 10)) EVAL PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "55\n"); + ASSERT_EQ(ssout.str(), "55\n"); } TEST(CompilerTest, GlobalDefine) { @@ -169,7 +171,7 @@ TEST(CompilerTest, GlobalDefine) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "1\n"); + ASSERT_EQ(ssout.str(), "1\n"); } @@ -185,7 +187,7 @@ TEST(CompilerTest, GlobalDefineFn) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "5\n"); + ASSERT_EQ(ssout.str(), "5\n"); } TEST(CompilerTest, GlobalDefineFnQuote) { @@ -200,7 +202,7 @@ TEST(CompilerTest, GlobalDefineFnQuote) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "(1 2)\n"); + ASSERT_EQ(ssout.str(), "(1 2)\n"); } TEST(CompilerTest, GlobalDefineFnCar) { @@ -211,12 +213,12 @@ TEST(CompilerTest, GlobalDefineFnCar) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (define (carr l) (car l)) EVAL LDC (carr (quote (1 2))) EVAL PRINT LDC (carr (cdr (quote " - "(1 2)))) EVAL PRINT STOP)"); + "(1 2)))) EVAL PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "1\n2\n"); + ASSERT_EQ(ssout.str(), "1\n2\n"); } TEST(CompilerTest, GlobalDefineFnEq) { @@ -227,7 +229,7 @@ TEST(CompilerTest, GlobalDefineFnEq) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (define (eqtest l) (= l ())) EVAL LDC (eqtest (quote ())) EVAL PRINT LDC (eqtest (nil)) " - "EVAL PRINT STOP)"); + "EVAL PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } @@ -236,12 +238,12 @@ TEST(CompilerTest, GlobalDefineFnEq) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (define (eqtest l) (= l (nil))) EVAL LDC (eqtest (quote ())) EVAL PRINT LDC (eqtest " - "(nil)) EVAL PRINT STOP)"); + "(nil)) EVAL PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "1\n1\n1\n1\n"); + ASSERT_EQ(ssout.str(), "1\n1\n1\n1\n"); } TEST(CompilerTest, GlobalDefineFnMulti) { @@ -252,12 +254,12 @@ TEST(CompilerTest, GlobalDefineFnMulti) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (define (one x y) (+ x y)) EVAL LDC (define (two x y) (one (+ x 1) y)) EVAL LDC (two 2 3) " - "EVAL PRINT STOP)"); + "EVAL PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "6\n"); + ASSERT_EQ(ssout.str(), "6\n"); } TEST(CompilerTest, GlobalDefineFnMultiTwo) { @@ -268,12 +270,12 @@ TEST(CompilerTest, GlobalDefineFnMultiTwo) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (define (one x y) (- x y)) EVAL LDC (define (two x y) (one (+ x 1) y)) EVAL LDC (two 2 3) " - "EVAL PRINT STOP)"); + "EVAL PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "0\n"); + ASSERT_EQ(ssout.str(), "0\n"); } @@ -286,12 +288,12 @@ TEST(CompilerTest, GlobalDefineFnRec) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC (define (fib n) (if n (if (+ n -1) (+ (fib (+ n -1)) (fib(+ n -2))) 1) 0) ) EVAL LDC (fib " - "20) EVAL PRINT STOP)"); + "20) EVAL PRINT STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } Options::set("command_strs", true); Logger::set_level(Logger::VM, Logger::DEBUG); ssout.flush(); - EXPECT_EQ(ssout.str(), "6765\n"); + ASSERT_EQ(ssout.str(), "6765\n"); } \ No newline at end of file diff --git a/test/vm/GCTest.cpp b/test/vm/GCTest.cpp index 5bbb8f8..5acf60a 100644 --- a/test/vm/GCTest.cpp +++ b/test/vm/GCTest.cpp @@ -5,6 +5,7 @@ #include #include "MemoryContext.h" + class Environment : public ::testing::Environment { public: ~Environment() override {} @@ -30,26 +31,26 @@ TEST(GCTest, GCTest) { c.append(Handle::makeNumCell(1)); c.append(Handle::makeNumCell(2)); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(c.car().val(), 1); - EXPECT_EQ(c.cdr().car().val(), 2); + ASSERT_EQ(c.car().val(), 1); + ASSERT_EQ(c.cdr().car().val(), 2); } MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(MemoryContext::get().cell_count(), 0); + ASSERT_EQ(MemoryContext::get().cell_count(), 0); { Handle c = Handle::cons(nullptr, nullptr); MemoryContext::get().request_gc_and_wait(); c.push(Handle::makeNumCell(1)); c.push(Handle::makeNumCell(2)); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(c.car().val(), 2); - EXPECT_EQ(c.cdr().car().val(), 1); + ASSERT_EQ(c.car().val(), 2); + ASSERT_EQ(c.cdr().car().val(), 1); } MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(MemoryContext::get().cell_count(), 0); + ASSERT_EQ(MemoryContext::get().cell_count(), 0); } TEST(GCTest, GCTestAppend) { @@ -59,13 +60,14 @@ TEST(GCTest, GCTestAppend) { MemoryContext::get().request_gc(); c.append(Handle::makeNumCell(1)); MemoryContext::get().request_gc(); - EXPECT_EQ(c.car().val(), 1); + ASSERT_EQ(c.car().val(), 1); } MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(MemoryContext::get().cell_count(), 0); + ASSERT_EQ(MemoryContext::get().cell_count(), 0); } + TEST(GCTest, GCTestPop) { { @@ -77,13 +79,13 @@ TEST(GCTest, GCTestPop) { } for (int i = test_size - 1; i >= 0; i--) { MemoryContext::get().request_gc(); - EXPECT_EQ(i, c.pop().val()); + ASSERT_EQ(i, c.pop().val()); } } MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(MemoryContext::get().cell_count(), 0); + ASSERT_EQ(MemoryContext::get().cell_count(), 0); } TEST(GCTest, GCTestAppend2) { @@ -96,12 +98,12 @@ TEST(GCTest, GCTestAppend2) { } for (int i = 0; i < test_size; i++) { MemoryContext::get().request_gc(); - EXPECT_EQ(i, c.pop().val()); + ASSERT_EQ(i, c.pop().val()); } MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(MemoryContext::get().cell_count(), 0); + ASSERT_EQ(MemoryContext::get().cell_count(), 0); } TEST(GCTest, GCTestAppend3) { @@ -114,11 +116,11 @@ TEST(GCTest, GCTestAppend3) { MemoryContext::get().request_gc(); Handle n = c.cdr(); c.setcdr(nullptr); - EXPECT_EQ(n.car().val(), 2); - EXPECT_EQ(c.car().val(), 1); + ASSERT_EQ(n.car().val(), 2); + ASSERT_EQ(c.car().val(), 1); } MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(MemoryContext::get().cell_count(), 0); + ASSERT_EQ(MemoryContext::get().cell_count(), 0); } diff --git a/test/vm/VMTest.cpp b/test/vm/VMTest.cpp index 7a448c1..9628036 100644 --- a/test/vm/VMTest.cpp +++ b/test/vm/VMTest.cpp @@ -2,6 +2,7 @@ #include "Command.h" #include "VM.h" + class Environment : public ::testing::Environment { public: ~Environment() override {} @@ -36,7 +37,7 @@ TEST(VMTest, BasicHello) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "h"); + ASSERT_EQ(ssout.str(), "h"); } //TODO: maybe rewrite it all... @@ -84,7 +85,7 @@ TEST(VMTest, BasicHello) { // vm.run(); // } // ssout.flush(); -// EXPECT_EQ(ssout.str(), "14"); +// ASSERT_EQ(ssout.str(), "14"); //} // //TEST(VMTest, SimpleFunction) { @@ -112,7 +113,7 @@ TEST(VMTest, BasicHello) { // vm.run(); // } // ssout.flush(); -// EXPECT_EQ(ssout.str(), "3"); +// ASSERT_EQ(ssout.str(), "3"); //} // //TEST(VMTest, RecursiveFunction) { @@ -257,5 +258,5 @@ TEST(VMTest, BasicHello) { //vm.run(); //} //ssout.flush(); -//EXPECT_EQ(ssout.str(), "2358 55"); +//ASSERT_EQ(ssout.str(), "2358 55"); //} \ No newline at end of file diff --git a/test/vm/VMWithParserTest.cpp b/test/vm/VMWithParserTest.cpp index 31b4af2..0cca7f5 100644 --- a/test/vm/VMWithParserTest.cpp +++ b/test/vm/VMWithParserTest.cpp @@ -33,7 +33,7 @@ TEST(VMWithParserTest, BasicHello) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "h"); + ASSERT_EQ(ssout.str(), "h"); } TEST(VMWithParserTest, BasicBranch) { @@ -44,12 +44,12 @@ TEST(VMWithParserTest, BasicBranch) { VM vm(ssin, ssout); Parser parser; parser.loadStr("(LDC 1 SEL (LDC 10 PUTNUM JOIN) (LDC 20 PUTNUM JOIN) LDC 0 SEL (LDC 30 PUTNUM JOIN) (LDC 40 " - "PUTNUM JOIN) STOP)"); + "PUTNUM JOIN) STOP)"); vm.loadControl(parser.parseExpr()); vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "1040"); + ASSERT_EQ(ssout.str(), "1040"); } TEST(VMWithParserTest, BasicFunction) { @@ -64,7 +64,7 @@ TEST(VMWithParserTest, BasicFunction) { vm.run(); } ssout.flush(); - EXPECT_EQ(ssout.str(), "3"); + ASSERT_EQ(ssout.str(), "3"); } TEST(VMWithParserTest, RecFunction) { @@ -75,17 +75,17 @@ TEST(VMWithParserTest, RecFunction) { VM vm(ssin, ssout); Parser parser; parser.loadStr("( DUM NIL LDF ( LD ( 1 . 1 ) SEL ( LD ( 1 . 1 ) LDC -1 ADD SEL ( NIL LD ( 1 . 1 ) LDC -1 ADD " - "CONS LD ( 2 . 1 ) AP " - "NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN " - ") RET ) CONS LDF ( " - "NIL LDC 20 CONS LD ( 1 . 1 ) AP RET ) RAP PUTNUM STOP )"); + "CONS LD ( 2 . 1 ) AP " + "NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN " + ") RET ) CONS LDF ( " + "NIL LDC 20 CONS LD ( 1 . 1 ) AP RET ) RAP PUTNUM STOP )"); vm.loadControl(parser.parseExpr()); vm.run(); } MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); MemoryContext::get().request_gc_and_wait(); - EXPECT_EQ(MemoryContext::get().cell_count(), 0); + ASSERT_EQ(MemoryContext::get().cell_count(), 0); ssout.flush(); - EXPECT_EQ(ssout.str(), "6765"); + ASSERT_EQ(ssout.str(), "6765"); } \ No newline at end of file