diff options
| Commit | fb7e4dbb3ec65de00987f2ccfc6d29ebfcec12c7 (patch) | |
|---|---|---|
| Author | 2018-11-15 09:41:48 +0200 | |
| Committer | 2018-11-15 10:16:21 +0200 | |
| Files | 7cf616ffeb0e7d60018ce762b65a96d1d00049d1 /JSImpl/test | |
| IPL-fb7e4dbb3ec65de00987f2ccfc6d29ebfcec12c7.tar IPL-fb7e4dbb3ec65de00987f2ccfc6d29ebfcec12c7.tar.gz IPL-fb7e4dbb3ec65de00987f2ccfc6d29ebfcec12c7.zip | ||
| Parent | 8e5a132e13587cab84441f232e602df23c7fa5b9 (diff) | |
Added tests for spasm
1 files changed, 67 insertions, 2 deletions
| -rw-r--r-- | JSImpl/test/sprtTests.cpp | 69 |
diff --git a/JSImpl/test/sprtTests.cpp b/JSImpl/test/sprtTests.cpp index 7621ca3..fe743f9 100644 --- a/JSImpl/test/sprtTests.cpp +++ b/JSImpl/test/sprtTests.cpp @@ -1,6 +1,7 @@ #include <gtest/gtest.h> #include <spasm.hpp> +#include <assembler.hpp> #include <sstream> using Spasm::OpCodes; @@ -9,12 +10,12 @@ struct SPRTTest : public ::testing::Test { typedef std::vector<Spasm::byte> ByteCode; - void Run(ByteCode& code) + void Run(const ByteCode& code) { Run(code.data(), code.size()); } - void Run(Spasm::byte* bytecode, size_t size) + void Run(const Spasm::byte* bytecode, size_t size) { VM.Initialize(size, bytecode, Input, Output); ASSERT_EQ(Spasm::Spasm::RunResult::Success, VM.run()); @@ -27,6 +28,19 @@ struct SPRTTest : public ::testing::Test std::ostringstream Output; }; +struct SPASMTest : public SPRTTest +{ + void CompileAndRun(const std::string& program) + { + SpasmImpl::ASM::Bytecode_Memory bytecode; + std::istringstream programInput; + programInput.str(program); + ASSERT_TRUE(SpasmImpl::ASM::compile(programInput, bytecode)) + << "Could not compile the program:" << std::endl << program; + Run(bytecode.bytecode()); + } +}; + TEST_F(SPRTTest, Empty) { } @@ -218,3 +232,54 @@ TEST_F(SPRTTest, Read) ASSERT_EQ(Output.str(), "42"); } +TEST_F(SPASMTest, Call) +{ + const char* program = + "alloc 5" "\n" + "const 1 0" "\n" + "const 2 6" "\n" + "const 3 7" "\n" + "const 4 2" "\n" + "push 3" "\n" + "push 2" "\n" + "push 4" "\n" + "call mult" "\n" + "print 4" "\n" + "halt" "\n" + "label mult" "\n" + "print 0" "\n" + "print -1" "\n" + "print -2" "\n" + "mul 1 -2 -1" "\n" + "ret 1" "\n" + "" + ; + CompileAndRun(program); + ASSERT_EQ(Output.str(), "26742"); +} + +TEST_F(SPASMTest, GCD) +{ + const char* program = + "alloc 3" "\n" + "read 1" "\n" + "read 2" "\n" + "label loop" "\n" + "less 3 1 2" "\n" + "jmpt 3 sub_ba" "\n" + "less 3 2 1" "\n" + "jmpt 3 sub_ab" "\n" + "print 1" "\n" + "halt" "\n" + "label sub_ab" "\n" + "sub 1 1 2" "\n" + "jmp loop" "\n" + "label sub_ba" "\n" + "sub 2 2 1" "\n" + "jmp loop" "\n" + "" + ; + Input.str("21 12"); + CompileAndRun(program); + ASSERT_EQ(Output.str(), "3"); +} |
