Skip to content

Commit

Permalink
Merge pull request #10 from hassansalehe/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
hassansalehe authored Jun 26, 2021
2 parents 4b1f8ba + 4016f8d commit e0aa7ff
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
13 changes: 9 additions & 4 deletions src/common/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Instruction {
Instruction(std::string stmt) {

raw = trim( stmt );
std::vector<std::string> contents = splitInstruction( raw );
auto contents = splitInstruction( raw );

if (contents[0] == "store") {
oper = STORE;
Expand Down Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
File renamed without changes.
55 changes: 55 additions & 0 deletions test/Common_Instruction_gtest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <gtest/gtest.h>

#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);
}
File renamed without changes.

0 comments on commit e0aa7ff

Please sign in to comment.