Skip to content

Commit

Permalink
test: migrate from Catch2 to GTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Oct 21, 2024
1 parent fc84cdf commit ec18bb0
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 401 deletions.
3 changes: 1 addition & 2 deletions Bar/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ elseif(UNIX)
INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN")
endif()
target_link_libraries(bar_test PRIVATE
#GTest::gtest_main
Catch2::Catch2WithMain
GTest::gtest_main
${PROJECT_NAMESPACE}::Bar)

add_test(NAME bar_test COMMAND bar_test)
238 changes: 116 additions & 122 deletions Bar/tests/bar_test.cpp
Original file line number Diff line number Diff line change
@@ -1,155 +1,149 @@
#include <catch2/catch_test_macros.hpp>
#include <bar/Bar.hpp>

#include <gtest/gtest.h>
#include <iostream>
#include <numeric>
#include <string>

#include <bar/Bar.hpp>

namespace bar {

TEST_CASE("Bar free function", "[Bar]") {
SECTION("Int Function") { REQUIRE_NOTHROW(freeFunction(42)); }
SECTION("Int64_t Function") { REQUIRE_NOTHROW(freeFunction(int64_t{42})); }
TEST(BarTest, FreeFunction) {
EXPECT_NO_THROW(freeFunction(42));
EXPECT_NO_THROW(freeFunction(int64_t{42}));
}

TEST_CASE("String Vector usage", "[Bar]") {
SECTION("Vector of String Output") {
std::vector<std::string> result;
REQUIRE_NOTHROW(result = stringVectorOutput(8));
REQUIRE(result.size() == 8);
for (const auto& it : result) {
REQUIRE(it == std::to_string(8));
}
}
SECTION("Vector of String Input by value") {
std::vector<std::string> data{"1", "2", "3", "4", "5"};
int size = 0;
REQUIRE_NOTHROW(size = stringVectorInput(data));
REQUIRE(size == 5);
}
SECTION("Vector of String Input by const ref") {
std::vector<std::string> data{"1", "2", "3", "4", "5"};
int size = 0;
REQUIRE_NOTHROW(size = stringVectorRefInput(data));
REQUIRE(size == 5);
TEST(BarTest, StringVectorOutput) {
std::vector<std::string> result;
ASSERT_NO_THROW(result = stringVectorOutput(8));
EXPECT_EQ(result.size(), 8);
for (const auto& it : result) {
EXPECT_EQ(it, std::to_string(8));
}
}

TEST_CASE("String Jagged Array usage", "[Bar]") {
SECTION("Jagged Array of String Output") {
std::vector<std::vector<std::string>> result;
REQUIRE_NOTHROW(result = stringJaggedArrayOutput(8));
REQUIRE(result.size() == 8);
for (int i = 0; i < result.size(); ++i) {
REQUIRE(i + 1 == result[i].size());
}
for (int i = 1; i <= result.size(); ++i) {
const auto& inner = result[i - 1];
for (const auto& it : inner) {
REQUIRE(it == std::to_string(i));
}
}
}
SECTION("Jagged Array of String Input by value") {
std::vector<std::vector<std::string>> data{{"1", "2", "3"}, {"4", "5"}};
int size = 0;
REQUIRE_NOTHROW(size = stringJaggedArrayInput(data));
REQUIRE(size == 2);
}
SECTION("Jagged Array of String Input by const ref") {
std::vector<std::vector<std::string>> data{{"1", "2", "3"}, {"4", "5"}};
int size = 0;
REQUIRE_NOTHROW(size = stringJaggedArrayRefInput(data));
REQUIRE(size == 2);
}
TEST(BarTest, StringVectorValueInput) {
const std::vector<std::string> data{"1", "2", "3", "4", "5"};
int size = 0;
ASSERT_NO_THROW(size = stringVectorInput(data));
EXPECT_EQ(size, 5);
}

TEST_CASE("Pair Vector usage", "[Bar]") {
SECTION("Vector of Pair Output") {
std::vector<std::string> result;
REQUIRE_NOTHROW(result = stringVectorOutput(8));
REQUIRE(result.size() == 8);
for (const auto& it : result) {
REQUIRE(it == std::to_string(8));
}
}
SECTION("Vector of Pair Input by value") {
std::vector<std::string> data{"1", "2", "3", "4", "5"};
int size = 0;
REQUIRE_NOTHROW(size = stringVectorInput(data));
REQUIRE(size == 5);
}
SECTION("Vector of Pair Input by const ref") {
std::vector<std::string> data{"1", "2", "3", "4", "5"};
int size = 0;
REQUIRE_NOTHROW(size = stringVectorRefInput(data));
REQUIRE(size == 5);
}
TEST(BarTest, StringVectorRefInput) {
const std::vector<std::string> data{"1", "2", "3", "4", "5"};
int size = 0;
ASSERT_NO_THROW(size = stringVectorRefInput(data));
EXPECT_EQ(size, 5);
}

TEST_CASE("Pair Jagged Array usage", "[Bar]") {
SECTION("Jagged Array of Pair Output") {
std::vector<std::vector<std::pair<int, int>>> result;
REQUIRE_NOTHROW(result = pairJaggedArrayOutput(8));
REQUIRE(result.size() == 8);
for (int i = 0; i < result.size(); ++i) {
REQUIRE(i + 1 == result[i].size());
}
for (int i = 1; i <= result.size(); ++i) {
const auto& inner = result[i - 1];
for (const auto& it : inner) {
REQUIRE(it == std::make_pair(i, i));
}
}
TEST(BarTest, StringJaggedArrayOutput) {
std::vector<std::vector<std::string>> result;
ASSERT_NO_THROW(result = stringJaggedArrayOutput(8));
EXPECT_EQ(result.size(), 8);
for (std::size_t i = 0; i < result.size(); ++i) {
EXPECT_EQ(i + 1, result[i].size());
}
SECTION("Jagged Array of Pair Input by value") {
std::vector<std::vector<std::pair<int, int>>> data{{{1, 1}, {2, 2}, {3, 3}}, {{4, 4}, {5, 5}}};
int size = 0;
REQUIRE_NOTHROW(size = pairJaggedArrayInput(data));
REQUIRE(size == 2);
}
SECTION("Jagged Array of Pair Input by const ref") {
std::vector<std::vector<std::pair<int, int>>> data{{{1, 1}, {2, 2}, {3, 3}}, {{4, 4}, {5, 5}}};
int size = 0;
REQUIRE_NOTHROW(size = pairJaggedArrayRefInput(data));
REQUIRE(size == 2);
for (std::size_t i = 1; i <= result.size(); ++i) {
const auto& inner = result[i - 1];
for (const auto& it : inner) {
EXPECT_EQ(it, std::to_string(i));
}
}
}

TEST_CASE("Bar static method", "[Bar]") {
SECTION("Int Method") { REQUIRE_NOTHROW(Bar::staticFunction(42)); }
SECTION("Int64_t Method") { REQUIRE_NOTHROW(Bar::staticFunction(int64_t{42})); }
TEST(BarTest, StringJaggedArrayValueInput) {
const std::vector<std::vector<std::string>> data{{"1", "2", "3"}, {"4", "5"}};
int size = 0;
ASSERT_NO_THROW(size = stringJaggedArrayInput(data));
EXPECT_EQ(size, 2);
}

TEST_CASE("Bar::Ctor", "[Bar]") {
SECTION("Default constructor") {
Bar* b = new Bar();
REQUIRE(b != nullptr);
}
TEST(BarTest, StringJaggedArrayRefInput) {
const std::vector<std::vector<std::string>> data{{"1", "2", "3"}, {"4", "5"}};
int size = 0;
ASSERT_NO_THROW(size = stringJaggedArrayRefInput(data));
EXPECT_EQ(size, 2);
}

SCENARIO("Bar Int", "[Bar]") {
GIVEN("A Bar instance") {
Bar bar;
WHEN("Setting a value") {
REQUIRE_NOTHROW(bar.setInt(42));
THEN("The value is updated") { REQUIRE(bar.getInt() == 42); }
}
TEST(BarTest, PairVectorOutput) {
std::vector<std::pair<int, int>> result;
ASSERT_NO_THROW(result = pairVectorOutput(8));
EXPECT_EQ(result.size(), 8);
for (const auto& it : result) {
EXPECT_EQ(it.first, 8);
EXPECT_EQ(it.second, 8);
}
}

SCENARIO("Bar Int64", "[Bar]") {
GIVEN("A Bar instance") {
Bar bar;
WHEN("Setting a value") {
REQUIRE_NOTHROW(bar.setInt64(31));
THEN("The value is updated") { REQUIRE(bar.getInt64() == 31); }
TEST(BarTest, PairVectorValueInput) {
const std::vector<std::pair<int, int>> data{{1, 2}, {3, 4}, {5, 6}};
int size = 0;
ASSERT_NO_THROW(size = pairVectorInput(data));
EXPECT_EQ(size, 3);
}

TEST(BarTest, PairVectorRefInput) {
const std::vector<std::pair<int, int>> data{{1, 2}, {3, 4}, {5, 6}};
int size = 0;
ASSERT_NO_THROW(size = pairVectorRefInput(data));
EXPECT_EQ(size, 3);
}

TEST(BarTest, PairJaggedArrayOutput) {
std::vector<std::vector<std::pair<int, int>>> result;
ASSERT_NO_THROW(result = pairJaggedArrayOutput(8));
EXPECT_EQ(result.size(), 8);
for (std::size_t i = 0; i < result.size(); ++i) {
EXPECT_EQ(i + 1, result[i].size());
}
for (int i = 1; i <= static_cast<int>(result.size()); ++i) {
const auto& inner = result[i - 1];
for (const auto& it : inner) {
EXPECT_EQ(it, std::make_pair(i, i));
}
}
}

TEST_CASE("Bar::operator()", "[Bar]") {
SECTION("Debug print") { INFO("Bar: " << Bar()()); }
TEST(BarTest, PairJaggedArrayValueInput) {
std::vector<std::vector<std::pair<int, int>>> data{{{1, 1}, {2, 2}, {3, 3}}, {{4, 4}, {5, 5}}};
int size = 0;
ASSERT_NO_THROW(size = pairJaggedArrayInput(data));
EXPECT_EQ(size, 2);
}

TEST(BarTest, PairJaggedArrayRefInput) {
std::vector<std::vector<std::pair<int, int>>> data{{{1, 1}, {2, 2}, {3, 3}}, {{4, 4}, {5, 5}}};
int size = 0;
ASSERT_NO_THROW(size = pairJaggedArrayRefInput(data));
EXPECT_EQ(size, 2);
}

TEST(BarTest, StaticMethods) {
EXPECT_NO_THROW(Bar::staticFunction(42));
EXPECT_NO_THROW(Bar::staticFunction(int64_t{42}));
}

TEST(BarTest, Constructor) {
Bar* b = new Bar();
EXPECT_NE(b, nullptr);
}

TEST(BarTest, IntMethods) {
Bar bar;
ASSERT_NO_THROW(bar.setInt(42));
EXPECT_EQ(42, bar.getInt());
}

TEST(BarTest, Int64Methods) {
Bar bar;
ASSERT_NO_THROW(bar.setInt64(31));
EXPECT_EQ(31, bar.getInt64());
}

TEST(BarTest, PrintMethod) {
Bar bar;
std::string str("");
ASSERT_NO_THROW(str = bar());
EXPECT_EQ("\"Bar\":{\"int\":0,\"int64\":0}", str);
}

} // namespace bar
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ option(BUILD_Protobuf "Build the Protobuf dependency Library" ON)
message(STATUS "Build protobuf: ${BUILD_Protobuf}")

if(BUILD_TESTING)
option(BUILD_Catch2 "Build Catch2" ON)
message(STATUS "Build Catch2: ${BUILD_Catch2}")

option(BUILD_re2 "Build the re2 dependency Library" ON)
message(STATUS "Build re2: ${BUILD_re2}")

Expand All @@ -98,7 +95,6 @@ if(BUILD_TESTING)
option(BUILD_benchmark "Build benchmark" ON)
message(STATUS "Build benchmark: ${BUILD_benchmark}")
else()
set(BUILD_Catch2 OFF)
set(BUILD_re2 OFF)
set(BUILD_googletest OFF)
set(BUILD_benchmark OFF)
Expand Down
3 changes: 1 addition & 2 deletions Foo/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ elseif(UNIX)
INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN")
endif()
target_link_libraries(foo_test PRIVATE
#GTest::gtest_main
Catch2::Catch2WithMain
GTest::gtest_main
${PROJECT_NAMESPACE}::Foo)

add_test(NAME foo_test COMMAND foo_test)
Loading

0 comments on commit ec18bb0

Please sign in to comment.