diff options
| Commit | e7c2a14f3e424f1ef1109d7634ece820a058e862 (patch) | |
|---|---|---|
| Author | 2018-11-13 23:52:40 +0200 | |
| Committer | 2018-11-13 23:52:40 +0200 | |
| Files | 587a120e78e7c0d50c381d8f38bf0665ed8733a7 /JSImpl | |
| IPL-e7c2a14f3e424f1ef1109d7634ece820a058e862.tar IPL-e7c2a14f3e424f1ef1109d7634ece820a058e862.tar.gz IPL-e7c2a14f3e424f1ef1109d7634ece820a058e862.zip | ||
| Parent | fa077610dfe8b59eb2af069d2f3e92b4790a21d2 (diff) | |
Implemented call and ret opcodes
3 files changed, 182 insertions, 9 deletions
| -rw-r--r-- | JSImpl/solution/JSImpl.sln | 1 | ||||
| -rw-r--r-- | JSImpl/solution/Makefile | 2 | ||||
| -rw-r--r-- | JSImpl/test/sprtTests.cpp | 188 |
diff --git a/JSImpl/solution/JSImpl.sln b/JSImpl/solution/JSImpl.sln index d4acff7..9915cf9 100644 --- a/JSImpl/solution/JSImpl.sln +++ b/JSImpl/solution/JSImpl.sln @@ -3,6 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "..\test\Test.vcxproj", "{45DC8C7C-3113-8E0D-DAFF-7310C6150A0F}"
ProjectSection(ProjectDependencies) = postProject
+ {AE0A9E7C-9A41-9F0D-432E-85102F441B0F} = {AE0A9E7C-9A41-9F0D-432E-85102F441B0F}
{19EA680D-85FE-90BE-4E80-341EBA538DEF} = {19EA680D-85FE-90BE-4E80-341EBA538DEF}
{EC34880F-5849-B0C0-21CB-53208D9EACF1} = {EC34880F-5849-B0C0-21CB-53208D9EACF1}
{B686840F-229B-ACC0-EB1C-502057F0A8F1} = {B686840F-229B-ACC0-EB1C-502057F0A8F1}
diff --git a/JSImpl/solution/Makefile b/JSImpl/solution/Makefile index 37c7a63..b920fc7 100644 --- a/JSImpl/solution/Makefile +++ b/JSImpl/solution/Makefile @@ -20,7 +20,7 @@ JSImpl: JSLib @echo "==== Building JSImpl ($(config)) ====" @${MAKE} --no-print-directory -C . -f JSImpl.make -Test: JSLib gtest gmock gtest_main +Test: sprt JSLib gtest gmock gtest_main @echo "==== Building Test ($(config)) ====" @${MAKE} --no-print-directory -C ../test -f Test.make diff --git a/JSImpl/test/sprtTests.cpp b/JSImpl/test/sprtTests.cpp index 92bd5a6..7621ca3 100644 --- a/JSImpl/test/sprtTests.cpp +++ b/JSImpl/test/sprtTests.cpp @@ -7,20 +7,82 @@ using Spasm::OpCodes; struct SPRTTest : public ::testing::Test { - void Run(Spasm::byte bytecode[], size_t size) - { - VM.Initialize(size, bytecode, Input, Output); - ASSERT_EQ(Spasm::Spasm::RunResult::Success, VM.run()); - } - Spasm::Spasm VM; - std::istringstream Input; - std::ostringstream Output; + typedef std::vector<Spasm::byte> ByteCode; + + void Run(ByteCode& code) + { + Run(code.data(), code.size()); + } + + void Run(Spasm::byte* bytecode, size_t size) + { + VM.Initialize(size, bytecode, Input, Output); + ASSERT_EQ(Spasm::Spasm::RunResult::Success, VM.run()); + } + Spasm::byte Label(Spasm::byte label) const { + return 0x80 + label; + } + Spasm::Spasm VM; + std::istringstream Input; + std::ostringstream Output; }; TEST_F(SPRTTest, Empty) { } +TEST_F(SPRTTest, Add) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::Add, 3, 1, 2, + OpCodes::Print, 3, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "13"); +} + +TEST_F(SPRTTest, Sub) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::Sub, 3, 1, 2, + OpCodes::Print, 3, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "-1"); +} + +TEST_F(SPRTTest, Div) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::Div, 3, 1, 2, + OpCodes::Print, 3, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "0"); +} + +TEST_F(SPRTTest, Mod) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::Mod, 3, 1, 2, + OpCodes::Print, 3, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "6"); +} + TEST_F(SPRTTest, Mutliply) { Spasm::byte bytecode[] = { @@ -40,9 +102,119 @@ TEST_F(SPRTTest, Less) OpCodes::Const, 1, 6, OpCodes::Const, 2, 7, OpCodes::Less, 3, 1, 2, + OpCodes::Less, 4, 2, 1, + OpCodes::Less, 5, 2, 2, + OpCodes::Print, 3, + OpCodes::Print, 4, + OpCodes::Print, 5, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "100"); +} + + +TEST_F(SPRTTest, LessEq) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::LessEq, 3, 1, 2, + OpCodes::LessEq, 4, 2, 1, + OpCodes::LessEq, 5, 2, 2, + OpCodes::Print, 3, + OpCodes::Print, 4, + OpCodes::Print, 5, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "101"); +} + +TEST_F(SPRTTest, Jump) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, + OpCodes::Const, 2, 7, + OpCodes::LessEq, 3, 1, 2, + OpCodes::LessEq, 4, 2, 1, + OpCodes::Jump, 18, + OpCodes::Print, 5, OpCodes::Print, 3, }; Run(bytecode, sizeof(bytecode)); ASSERT_EQ(Output.str(), "1"); } + +TEST_F(SPRTTest, JumpTrue) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, // 3 + OpCodes::Const, 2, 7, // 6 + OpCodes::Less, 3, 1, 2, // 10 + OpCodes::JumpT, 3, 17, // 13 + OpCodes::Print, 2, // 15 + OpCodes::Jump, 19, // 17 + OpCodes::Print, 1, // 19 + OpCodes::Halt, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "6"); +} + +TEST_F(SPRTTest, JumpFalse) +{ + Spasm::byte bytecode[] = { + OpCodes::Const, 1, 6, // 3 + OpCodes::Const, 2, 7, // 6 + OpCodes::Less, 3, 1, 2, // 10 + OpCodes::JumpF, 3, 17, // 13 + OpCodes::Print, 2, // 15 + OpCodes::Jump, 19, // 17 + OpCodes::Print, 1, // 19 + OpCodes::Halt, + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "7"); +} + +TEST_F(SPRTTest, Call) +{ + Spasm::byte bytecode[] = { + OpCodes::Alloc, 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::Call, 25, // 22 + OpCodes::Print, 4, // 24 + 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 + }; + + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "26742"); +} + +TEST_F(SPRTTest, Read) +{ + Spasm::byte bytecode[] = { + OpCodes::Read, 1, + OpCodes::Print, 1, + }; + + Input.str("42"); + Run(bytecode, sizeof(bytecode)); + ASSERT_EQ(Output.str(), "42"); +} + |
