From cfabc0db8e215697ed242eaf3efe7b3708630b5e Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sat, 26 Jun 2021 23:11:02 +0200 Subject: [PATCH 1/6] Fix out-of-bound exection on empty instruction --- src/common/instruction.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/common/instruction.h b/src/common/instruction.h index 7903c6e..9d8fccc 100644 --- a/src/common/instruction.h +++ b/src/common/instruction.h @@ -38,7 +38,7 @@ class Instruction { Instruction(std::string stmt) { raw = trim( stmt ); - std::vector contents = splitInstruction( raw ); + auto contents = splitInstruction( raw ); if (contents[0] == "store") { oper = STORE; @@ -139,9 +139,14 @@ class Instruction { // Trims the left and right spaces from a std::string static std::string trim(std::string sentence) { - size_t start = sentence.find_first_not_of(' '); - size_t end = sentence.find_last_not_of(' '); - return sentence.substr(start, (end -start)+1); + auto start = sentence.find_first_not_of(' '); + auto end = sentence.find_last_not_of(' '); + + if (start != std::string::npos) { + return sentence.substr(start, (end -start)+1); + } else { + return ""; + } } // Splits std::string into tokens substrings From d2566493428ea058d19245ca5c1626c293535641 Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sat, 26 Jun 2021 23:13:09 +0200 Subject: [PATCH 2/6] Rename test names add more tests for Instruction --- test/CMakeLists.txt | 14 ++++--- ....cc => Common_CriticalSignatures_gtest.cc} | 0 test/Common_Instruction_gtest.cc | 41 +++++++++++++++++++ ...gtest.cc => Common_MemoryActions_gtest.cc} | 0 4 files changed, 49 insertions(+), 6 deletions(-) rename test/{CriticalSignatures_gtest.cc => Common_CriticalSignatures_gtest.cc} (100%) create mode 100644 test/Common_Instruction_gtest.cc rename test/{MemoryActions_gtest.cc => Common_MemoryActions_gtest.cc} (100%) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b846bfa..1fb3ab6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -28,11 +28,13 @@ link_libraries(${GTEST_LIBRARIES} pthread gtest_main gcov --coverage) # Link commonTests with what we want to test and # the GTest and pthread library -add_executable(commonTests Common_Defs_gtest.cc) -add_executable(critalSigTests CriticalSignatures_gtest.cc) -add_executable(memoryActionsTests MemoryActions_gtest.cc) +add_executable(commonDefsTests Common_Defs_gtest.cc) +add_executable(commonCritalSigTests Common_CriticalSignatures_gtest.cc) +add_executable(commonMemoryActionsTests Common_MemoryActions_gtest.cc) +add_executable(commonInstructionTests Common_Instruction_gtest.cc) # Add tests for Ctest -add_test(common_tests, commonTests) -add_test(critical_signatures_tests, critalSigTests) -add_test(memory_actions_tests, memoryActionsTests) +add_test(common_defs_tests, commonDefsTests) +add_test(common_critical_signatures_tests, commonCritalSigTests) +add_test(common_memory_actions_tests, commonMemoryActionsTests) +add_test(common_instruction_tests, commonInstructionTests) diff --git a/test/CriticalSignatures_gtest.cc b/test/Common_CriticalSignatures_gtest.cc similarity index 100% rename from test/CriticalSignatures_gtest.cc rename to test/Common_CriticalSignatures_gtest.cc diff --git a/test/Common_Instruction_gtest.cc b/test/Common_Instruction_gtest.cc new file mode 100644 index 0000000..0461a1b --- /dev/null +++ b/test/Common_Instruction_gtest.cc @@ -0,0 +1,41 @@ +#include + +#include "common/instruction.h" + +// TEST(TestInstruction, TestConversion) { +// } + +TEST(TestInstruction, TestTrimHelperFunction) { + std::string raw_instruction(" store balance r6 "); + EXPECT_EQ(std::string("store balance r6"), Instruction::trim(raw_instruction)); + + EXPECT_TRUE(Instruction::trim(" ").empty()); +} + +TEST(TestInstruction, TestSplitInstructionFunction) { + std::string raw_instruction(" store balance r6 "); + Instruction instr; + auto chunks = instr.splitInstruction(raw_instruction); + + EXPECT_EQ(3UL, chunks.size()); + EXPECT_EQ("store", chunks.at(0)); + EXPECT_EQ("balance", chunks.at(1)); + EXPECT_EQ("r6", chunks.at(2)); +} + +TEST(TestInstruction, TestSplitInstructionFunctionEmptyString) { + std::string raw_instruction(" "); + Instruction instr; + auto chunks = instr.splitInstruction(raw_instruction); + EXPECT_EQ(0UL, chunks.size()); +} + +TEST(TestInstruction, checkInstructionForStore) { + std::string raw_instruction(" store i32 %2, i32* %balance "); + Instruction instr(raw_instruction); + EXPECT_EQ(STORE, instr.oper); + EXPECT_EQ("%balance", instr.destination); + EXPECT_EQ("i32", instr.type); + EXPECT_EQ("%2", instr.operand1); + EXPECT_EQ("%2", instr.operand2); +} \ No newline at end of file diff --git a/test/MemoryActions_gtest.cc b/test/Common_MemoryActions_gtest.cc similarity index 100% rename from test/MemoryActions_gtest.cc rename to test/Common_MemoryActions_gtest.cc From c0c9f912228457e2ff3cff300de2bb55fdccf013 Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sat, 26 Jun 2021 23:21:17 +0200 Subject: [PATCH 3/6] Remove commented code and rename tests --- test/Common_Instruction_gtest.cc | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/Common_Instruction_gtest.cc b/test/Common_Instruction_gtest.cc index 0461a1b..b165cae 100644 --- a/test/Common_Instruction_gtest.cc +++ b/test/Common_Instruction_gtest.cc @@ -2,17 +2,14 @@ #include "common/instruction.h" -// TEST(TestInstruction, TestConversion) { -// } - -TEST(TestInstruction, TestTrimHelperFunction) { +TEST(InstructionTests, CheckTrimHelperFunction) { std::string raw_instruction(" store balance r6 "); EXPECT_EQ(std::string("store balance r6"), Instruction::trim(raw_instruction)); EXPECT_TRUE(Instruction::trim(" ").empty()); } -TEST(TestInstruction, TestSplitInstructionFunction) { +TEST(InstructionTests, CheckSplitInstructionFunction) { std::string raw_instruction(" store balance r6 "); Instruction instr; auto chunks = instr.splitInstruction(raw_instruction); @@ -23,14 +20,14 @@ TEST(TestInstruction, TestSplitInstructionFunction) { EXPECT_EQ("r6", chunks.at(2)); } -TEST(TestInstruction, TestSplitInstructionFunctionEmptyString) { +TEST(InstructionTests, CheckSplitInstructionFunctionEmptyString) { std::string raw_instruction(" "); Instruction instr; auto chunks = instr.splitInstruction(raw_instruction); EXPECT_EQ(0UL, chunks.size()); } -TEST(TestInstruction, checkInstructionForStore) { +TEST(InstructionTests, CheckInstructionForStore) { std::string raw_instruction(" store i32 %2, i32* %balance "); Instruction instr(raw_instruction); EXPECT_EQ(STORE, instr.oper); From 04b83b993675a9a7b05863666e18195393e37771 Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sat, 26 Jun 2021 23:22:05 +0200 Subject: [PATCH 4/6] Add empty line to end of a file --- test/Common_Instruction_gtest.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Common_Instruction_gtest.cc b/test/Common_Instruction_gtest.cc index b165cae..9bbea95 100644 --- a/test/Common_Instruction_gtest.cc +++ b/test/Common_Instruction_gtest.cc @@ -35,4 +35,4 @@ TEST(InstructionTests, CheckInstructionForStore) { EXPECT_EQ("i32", instr.type); EXPECT_EQ("%2", instr.operand1); EXPECT_EQ("%2", instr.operand2); -} \ No newline at end of file +} From fa1a90f8577c3ec8f62144da1cb5d371b01f25e3 Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sat, 26 Jun 2021 23:37:22 +0200 Subject: [PATCH 5/6] Add test case for load instructions --- test/Common_Instruction_gtest.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/Common_Instruction_gtest.cc b/test/Common_Instruction_gtest.cc index 9bbea95..e0158ed 100644 --- a/test/Common_Instruction_gtest.cc +++ b/test/Common_Instruction_gtest.cc @@ -36,3 +36,12 @@ TEST(InstructionTests, CheckInstructionForStore) { EXPECT_EQ("%2", instr.operand1); EXPECT_EQ("%2", instr.operand2); } + +TEST(InstructionTests, CheckInstructionForLoad) { + std::string raw_instruction(" %1 = load i32 * %balance"); + Instruction instr(raw_instruction); + EXPECT_EQ(LOAD, instr.oper); + EXPECT_EQ("%1", instr.destination); + EXPECT_EQ("i32", instr.type); + EXPECT_EQ("%balance", instr.operand1); +} From 4016f8d8cedcce475dd7e3bcc702869ef1967e7f Mon Sep 17 00:00:00 2001 From: "Hassan @Ubuntu" Date: Sat, 26 Jun 2021 23:43:53 +0200 Subject: [PATCH 6/6] Add test coverage for alloca operations --- test/Common_Instruction_gtest.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/Common_Instruction_gtest.cc b/test/Common_Instruction_gtest.cc index e0158ed..8760329 100644 --- a/test/Common_Instruction_gtest.cc +++ b/test/Common_Instruction_gtest.cc @@ -45,3 +45,11 @@ TEST(InstructionTests, CheckInstructionForLoad) { EXPECT_EQ("i32", instr.type); EXPECT_EQ("%balance", instr.operand1); } + +TEST(InstructionTests, CheckInstructionForAlloca) { + std::string raw_instruction("%balance = alloca i32 , align 4 "); + Instruction instr(raw_instruction); + EXPECT_EQ(ALLOCA, instr.oper); + EXPECT_EQ("%balance", instr.destination); + EXPECT_EQ("i32", instr.type); +}