Skip to content

Commit

Permalink
Add tests for the Error object
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed May 12, 2024
1 parent b46b76b commit 2fc133d
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ if(ZASM_BUILD_TESTS OR CMKR_ROOT_PROJECT) # tests
"src/tests/main.cpp"
"src/tests/tests/tests.assembler.cpp"
"src/tests/tests/tests.decoder.cpp"
"src/tests/tests/tests.error.cpp"
"src/tests/tests/tests.externals.cpp"
"src/tests/tests/tests.formatter.cpp"
"src/tests/tests/tests.imports.cpp"
Expand Down
191 changes: 191 additions & 0 deletions src/tests/tests/tests.error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#include <chrono>
#include <gtest/gtest.h>
#include <zasm/core/errors.hpp>

namespace zasm::tests
{
TEST(ErrorTests, TestBasic)
{
Error error(ErrorCode::InvalidMode);
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
}

TEST(ErrorTests, TestExt)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));
}

TEST(ErrorTests, TestExtClear)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

error.clear();
ASSERT_TRUE(error.empty());
ASSERT_EQ(error, ErrorCode::None);
}

TEST(ErrorTests, TestCopy1)
{
Error error(ErrorCode::InvalidMode);
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);

Error error2(error);
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
}

TEST(ErrorTests, TestCopy2)
{
Error error(ErrorCode::InvalidMode);
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);

Error error2;
error2 = error;
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
}

TEST(ErrorTests, TestCopyExt1)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

Error error2(error);
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error2.getErrorMessage(), std::string("test message"));
}

TEST(ErrorTests, TestCopyExt2)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

Error error2;
error2 = error;
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error2.getErrorMessage(), std::string("test message"));
}

TEST(ErrorTests, TestMoveExt1)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

Error error2(std::move(error));
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error2.getErrorMessage(), std::string("test message"));

ASSERT_TRUE(error.empty());
}

TEST(ErrorTests, TestMoveExt2)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

Error error2;
error2 = std::move(error);
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error2.getErrorMessage(), std::string("test message"));

ASSERT_TRUE(error.empty());
}

#if defined(_DEBUG) && 0
// Leak Tests.
// Disabled by default, this is a very crude way to test for memory leaks.
// The process runs for 5 seconds and creates/destroys Error objects in where we can observe
// if the memory usage is stable.

static constexpr auto kLeakTestDurationSecs = std::chrono::milliseconds(5000);

TEST(ErrorTests, TestLeak1)
{
auto start = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start)
< kLeakTestDurationSecs)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));
}
}

TEST(ErrorTests, TestLeak2)
{
auto start = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start)
< kLeakTestDurationSecs)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

Error error2(std::move(error));
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error2.getErrorMessage(), std::string("test message"));
}
}

TEST(ErrorTests, TestLeak3)
{
auto start = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start)
< kLeakTestDurationSecs)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

Error error2;
error2 = std::move(error);
ASSERT_EQ(error2, ErrorCode::InvalidMode);
ASSERT_EQ(error2.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error2.getErrorMessage(), std::string("test message"));
}
}

TEST(ErrorTests, TestLeak4)
{
auto start = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start)
< kLeakTestDurationSecs)
{
Error error(ErrorCode::InvalidMode, "test message");
ASSERT_EQ(error, ErrorCode::InvalidMode);
ASSERT_EQ(error.getCode(), ErrorCode::InvalidMode);
ASSERT_EQ(error.getErrorMessage(), std::string("test message"));

error.clear();
ASSERT_TRUE(error.empty());
ASSERT_EQ(error, ErrorCode::None);
}
}
#endif

} // namespace zasm::tests

0 comments on commit 2fc133d

Please sign in to comment.