Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #10

Merged
merged 6 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
41 changes: 41 additions & 0 deletions test/Common_Instruction_gtest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <gtest/gtest.h>

#include "common/instruction.h"

// TEST(TestInstruction, TestConversion) {
// }
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented out code. It does not add any value.


TEST(TestInstruction, TestTrimHelperFunction) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about improving test names. Why does each test start with Test?

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