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 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..8760329 --- /dev/null +++ b/test/Common_Instruction_gtest.cc @@ -0,0 +1,55 @@ +#include + +#include "common/instruction.h" + +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(InstructionTests, CheckSplitInstructionFunction) { + 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(InstructionTests, CheckSplitInstructionFunctionEmptyString) { + std::string raw_instruction(" "); + Instruction instr; + auto chunks = instr.splitInstruction(raw_instruction); + EXPECT_EQ(0UL, chunks.size()); +} + +TEST(InstructionTests, 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); +} + +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); +} + +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); +} 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