-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtest_trim_str.cpp
More file actions
112 lines (94 loc) · 3 KB
/
gtest_trim_str.cpp
File metadata and controls
112 lines (94 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// clang-format off
/*****************************************************************//**
* \file gtest_trim_str.cpp
* \brief Unit tests for trimstr.hpp
* $ g++ gtest_trim_str.cpp -o runTests -std=c++23 -lgtest -lgtest_main -pthread
*
* \author Xuhua Huang
* \date November 2022
*********************************************************************/
// clang-format on
#include <gtest/gtest.h>
#include <iostream>
#include "trimstr.hpp"
#define STDTEST std::cout << "\033[32m[---TEST---]\033[m "
class TrimStrTest : public ::testing::Test {
public:
void SetUp(void) override {}
void TearDown(void) override {}
TrimStrTest() {}
virtual ~TrimStrTest() {}
};
TEST_F(TrimStrTest, TrimFront_Test) {
EXPECT_EQ(std::string((std::ranges::to<std::string>(
std::string_view{" test"} | trim_front))
.c_str()),
"test");
}
TEST_F(TrimStrTest, TrimBack_Test) {
EXPECT_EQ(std::string((std::ranges::to<std::string>(
std::string_view{"test "} | trim_back))
.c_str()),
"test");
}
TEST_F(TrimStrTest, TrimFrontAndBack_Test) {
EXPECT_EQ(std::string((std::ranges::to<std::string>(
std::string_view{" test "} | trim_spaces))
.c_str()),
"test");
}
TEST_F(TrimStrTest, TrimStrFn_Test) {
EXPECT_EQ(trim_str(" test "), "test");
}
// Test 1: Standard trimming (front and back)
TEST(TrimStrTest, StandardTrimming) {
std::string input = " Hello World ";
std::string expected = "Hello World";
EXPECT_EQ(util::trim_str(input), expected);
}
// Test 2: Trim front only
TEST(TrimStrTest, TrimFrontOnly) {
std::string input = " C++23";
std::string expected = "C++23";
EXPECT_EQ(util::trim_str(input), expected);
}
// Test 3: Trim back only
TEST(TrimStrTest, TrimBackOnly) {
std::string input = "Ranges ";
std::string expected = "Ranges";
EXPECT_EQ(util::trim_str(input), expected);
}
// Test 4: No trimming needed
TEST(TrimStrTest, NoTrimmingNeeded) {
std::string input = "Clean";
std::string expected = "Clean";
EXPECT_EQ(util::trim_str(input), expected);
}
// Test 5: String containing only spaces
TEST(TrimStrTest, AllSpaces) {
std::string input = " ";
std::string expected = "";
EXPECT_EQ(util::trim_str(input), expected);
}
// Test 6: Empty string
TEST(TrimStrTest, EmptyString) {
std::string input = "";
std::string expected = "";
EXPECT_EQ(util::trim_str(input), expected);
}
// Test 7: Embedded spaces should be preserved
TEST(TrimStrTest, PreserveEmbeddedSpaces) {
std::string input = " A B C ";
std::string expected = "A B C";
EXPECT_EQ(util::trim_str(input), expected);
}
// Test 8: Newlines and Tabs
TEST(TrimStrTest, NewlinesAndTabs) {
std::string input = "\t Multi Line \n ";
std::string expected = "Multi Line";
EXPECT_EQ(trim_str(input), expected);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}