Skip to content

Commit 7ec7270

Browse files
committed
Added Google Test for trimstr function with CMake.
1 parent 6d9edae commit 7ec7270

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

Util/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(Utility)
4+
5+
set(CMAKE_CXX_STANDARD 23)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
8+
# Add sub-directories with CMakeLists.txt in them
9+
add_subdirectory("stdcxx23/CountLeaves")
10+
add_subdirectory("tests")

Util/tests/CMakelists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,25 @@ include_directories(${UTIL_ROOT})
1313
add_executable(is_instance_of "${UTIL_ROOT}/is_instance_of.hpp" "test_is_instance_of.cpp")
1414
add_executable(parse_str "${UTIL_ROOT}/parse.hpp" "${UTIL_ROOT}/print_vec.hpp" "test_parse_str.cpp")
1515
add_executable(safe_free "${UTIL_ROOT}/safe_free.hpp" "test_safe_free.cpp")
16+
17+
# Adding Google Test component
18+
# Fetch content from GitHub archive
19+
include(FetchContent)
20+
FetchContent_Declare(
21+
googletest
22+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
23+
)
24+
25+
# For Windows: Prevent overriding the parent project's compiler/linker settings
26+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
27+
FetchContent_MakeAvailable(googletest)
28+
29+
enable_testing()
30+
31+
# GoogleTest for trim_str implementation
32+
add_executable(gtest_trim_str "${UTIL_ROOT}/trimstr.hpp" "gtest_trim_str.cpp")
33+
target_link_libraries(gtest_trim_str GTest::gtest_main)
34+
include(GoogleTest)
35+
36+
# Make added executables discoved by GTest
37+
gtest_discover_tests(gtest_trim_str)

Util/tests/gtest_trim_str.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*****************************************************************//**
2+
* \file gtest_trim_str.cpp
3+
* \brief
4+
*
5+
* \author Xuhua Huang
6+
* \date November 2022
7+
*********************************************************************/
8+
9+
#include <iostream>
10+
#include <gtest/gtest.h>
11+
#include "trimstr.hpp"
12+
13+
#define STDTEST std::cout << "\033[32m[---TEST---]\033[m "
14+
15+
class TrimStrTest : public ::testing::Test
16+
{
17+
public:
18+
void SetUp(void) override {}
19+
void TearDown(void) override {}
20+
TrimStrTest() {}
21+
virtual ~TrimStrTest() {}
22+
};
23+
24+
TEST_F(TrimStrTest, TrimFront_Test)
25+
{
26+
EXPECT_EQ(std::string((std::ranges::to<std::string>(" test" | trim_front)).c_str()), "test");
27+
}
28+
29+
TEST_F(TrimStrTest, TrimBack_Test)
30+
{
31+
EXPECT_EQ(std::string((std::ranges::to<std::string>("test " | trim_back)).c_str()), "test");
32+
}
33+
34+
TEST_F(TrimStrTest, TrimFrontAndBack_Test)
35+
{
36+
EXPECT_EQ(std::string((std::ranges::to<std::string>(" test " | trim_spaces)).c_str()), "test");
37+
}
38+
39+
TEST_F(TrimStrTest, TrimStrFn_Test)
40+
{
41+
EXPECT_EQ(trim_str(" test "), "test");
42+
}

Util/trimstr.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* \brief Demonstration of handy constant expressions that trim
44
* `std::string` at compile time with `std::ranges`
55
*
6-
* $ g++ trimstr.hpp -o trimstr.o -std=c++23 -Wall -Wextra -Wpedantic
6+
* $ g++ trimstr.hpp -std=c++23 -Wall -Wextra -Wpedantic
77
*
88
* \author Xuhua Huang
99
* \date March 2022
@@ -28,5 +28,6 @@ inline constexpr auto trim_spaces = trim_front | trim_back;
2828
std::string trim_str(const std::string& str) {
2929
// std::rangesnext::to in C++23 proposal
3030
// that converts ranges to a container
31-
return str | trim_spaces | std::rangesnext::to<std::string>;
31+
// return str | trim_spaces | std::rangesnext::to<std::string>;
32+
return std::ranges::to<std::string>(str | trim_spaces);
3233
}

0 commit comments

Comments
 (0)