diff options
| Commit | c32eda4e697803824e79878c74d541b81fbd04d8 (patch) | |
|---|---|---|
| Author | 2018-11-15 11:35:44 +0200 | |
| Committer | 2018-11-15 11:35:44 +0200 | |
| Files | ace7bdc0df3791b6db8878d52ba98cb368e180b8 /JSImpl/test/sprtTests.cpp | |
| IPL-c32eda4e697803824e79878c74d541b81fbd04d8.tar IPL-c32eda4e697803824e79878c74d541b81fbd04d8.tar.gz IPL-c32eda4e697803824e79878c74d541b81fbd04d8.zip | ||
| Parent | a7cd9d4f8d83024402bacb39786e5e1a018a0a04 (diff) | |
| Parent | d7b2e339ff06c64bcb1322eec396d4995da6b17a (diff) | |
Merge pull request #48 from SofiaCPP/feature/spasm_asm
Spasm tests and latest instruction set
1 files changed, 100 insertions, 8 deletions
| -rw-r--r-- | JSImpl/test/sprtTests.cpp | 108 |
diff --git a/JSImpl/test/sprtTests.cpp b/JSImpl/test/sprtTests.cpp index 7621ca3..eef7147 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) { } @@ -184,22 +198,22 @@ TEST_F(SPRTTest, JumpFalse) TEST_F(SPRTTest, Call) { Spasm::byte bytecode[] = { - OpCodes::Alloc, 5, // 2 + OpCodes::Push, 5, // 2 OpCodes::Const, 1, 0, // 5 OpCodes::Const, 2, 6, // 8 OpCodes::Const, 3, 7, // 11 OpCodes::Const, 4, 2, // 14 - OpCodes::Push, 3, // 16 - OpCodes::Push, 2, // 18 - OpCodes::Push, 4, // 20 + OpCodes::PushFrom, 3, // 16 + OpCodes::PushFrom, 2, // 18 + OpCodes::PushFrom, 4, // 20 OpCodes::Call, 25, // 22 OpCodes::Print, 4, // 24 - OpCodes::Halt, // 25 + OpCodes::Halt, // 25 OpCodes::Print, 0, // 27 OpCodes::Print, -1, // 29 OpCodes::Print, -2, // 31 OpCodes::Mul, 1, -2, -1,// 33 - OpCodes::Ret, 1, // 35 + OpCodes::Ret, 1, // 35 }; Run(bytecode, sizeof(bytecode)); @@ -218,3 +232,81 @@ TEST_F(SPRTTest, Read) ASSERT_EQ(Output.str(), "42"); } +TEST_F(SPASMTest, RSyntax) +{ + const char* program = + "push 5" "\n" + "const r1 0" "\n" + "const r2 6" "\n" + "const r3 7" "\n" + "const r4 2" "\n" + "pushr r3" "\n" + "pushr r2" "\n" + "pushr r4" "\n" + "call mult" "\n" + "popr r1" "\n" + "print r1" "\n" + "halt" "\n" + "label mult" "\n" + "print a0" "\n" + "print a1" "\n" + "print a2" "\n" + "mul r1 a2 a1" "\n" + "ret r1" "\n" + "" + ; + CompileAndRun(program); + ASSERT_EQ(Output.str(), "26742"); +} + +TEST_F(SPASMTest, Call) +{ + const char* program = + "push 5" "\n" + "const 1 0" "\n" + "const 2 6" "\n" + "const 3 7" "\n" + "const 4 2" "\n" + "pushr 3" "\n" + "pushr 2" "\n" + "pushr 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 = + "push 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"); +} |
