blob: 54e295779cbb063e9e855842192fee521cefdc78 [file] [log] [blame]
George Liuce8d1d02025-08-25 08:58:25 +08001#include "utils.hpp"
2
3#include <gtest/gtest.h>
4
Ed Tanousc5a2af92025-08-25 08:58:25 +08005constexpr std::string_view helloWorld = "Hello World";
6
George Liuce8d1d02025-08-25 08:58:25 +08007TEST(IfindFirstTest, BasicMatch)
8{
Ed Tanousc5a2af92025-08-25 08:58:25 +08009 auto match = iFindFirst(helloWorld, "World");
10 EXPECT_TRUE(match);
11 EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 6);
12 EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 11);
George Liuce8d1d02025-08-25 08:58:25 +080013}
14
15TEST(IfindFirstTest, CaseInsensitiveMatch)
16{
Ed Tanousc5a2af92025-08-25 08:58:25 +080017 auto match = iFindFirst(helloWorld, "world");
18 EXPECT_TRUE(match);
19 EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 6);
20 EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 11);
George Liuce8d1d02025-08-25 08:58:25 +080021}
22
23TEST(IfindFirstTest, NoMatch)
24{
Ed Tanousc5a2af92025-08-25 08:58:25 +080025 auto match = iFindFirst(helloWorld, "Planet");
26 EXPECT_FALSE(match);
George Liuce8d1d02025-08-25 08:58:25 +080027}
28
29TEST(IfindFirstTest, MatchAtStart)
30{
Ed Tanousc5a2af92025-08-25 08:58:25 +080031 auto match = iFindFirst(helloWorld, "HeLLo");
32 EXPECT_TRUE(match);
33 EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 0);
34 EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 5);
George Liuce8d1d02025-08-25 08:58:25 +080035}
36
37TEST(IfindFirstTest, MatchAtEnd)
38{
Ed Tanousc5a2af92025-08-25 08:58:25 +080039 auto match = iFindFirst(helloWorld, "LD");
40 EXPECT_TRUE(match);
41 EXPECT_EQ(std::distance(helloWorld.begin(), match.begin()), 9);
42 EXPECT_EQ(std::distance(helloWorld.begin(), match.end()), 11);
George Liuce8d1d02025-08-25 08:58:25 +080043}
44
45TEST(IfindFirstTest, EmptySubstring)
46{
Ed Tanousc5a2af92025-08-25 08:58:25 +080047 auto match = iFindFirst(helloWorld, "");
48 EXPECT_FALSE(match);
George Liuce8d1d02025-08-25 08:58:25 +080049}
50
51TEST(IfindFirstTest, EmptyString)
52{
Ed Tanousc5a2af92025-08-25 08:58:25 +080053 auto match = iFindFirst("", "Hello");
54 EXPECT_FALSE(match);
George Liuce8d1d02025-08-25 08:58:25 +080055}
George Liuecf1a312025-08-25 10:43:12 +080056
57TEST(SplitTest, NormalSplit)
58{
59 auto result = split("a,b,c", ',');
60 std::vector<std::string> expected = {"a", "b", "c"};
61 EXPECT_EQ(result, expected);
62}
63
64TEST(SplitTest, ConsecutiveDelimiters)
65{
66 auto result = split("a,,b", ',');
67 std::vector<std::string> expected = {"a", "", "b"};
68 EXPECT_EQ(result, expected);
69}
70
71TEST(SplitTest, LeadingDelimiter)
72{
73 auto result = split(",a,b", ',');
74 std::vector<std::string> expected = {"", "a", "b"};
75 EXPECT_EQ(result, expected);
76}
77
78TEST(SplitTest, TrailingDelimiter)
79{
80 auto result = split("a,b,", ',');
81 std::vector<std::string> expected = {"a", "b", ""};
82 EXPECT_EQ(result, expected);
83}
84
85TEST(SplitTest, NoDelimiter)
86{
87 auto result = split("abc", ',');
88 std::vector<std::string> expected = {"abc"};
89 EXPECT_EQ(result, expected);
90}
91
92TEST(SplitTest, EmptyString)
93{
94 auto result = split("", ',');
95 std::vector<std::string> expected = {""};
96 EXPECT_EQ(result, expected);
97}
George Liu5a61ec82025-08-25 11:16:44 +080098
99TEST(ReplaceAllTest, BasicReplacement)
100{
101 std::string str = "hello world, world!";
102 replaceAll(str, "world", "earth");
103 EXPECT_EQ(str, "hello earth, earth!");
104}
105
106TEST(ReplaceAllTest, NoMatch)
107{
108 std::string str = "hello world";
109 replaceAll(str, "xxx", "abc");
110 EXPECT_EQ(str, "hello world");
111}
112
113TEST(ReplaceAllTest, ReplaceWithEmpty)
114{
115 std::string str = "apple apple";
116 replaceAll(str, "apple", "");
117 EXPECT_EQ(str, " ");
118}
119
120TEST(ReplaceAllTest, ReplaceEmptySearch)
121{
122 std::string str = "abc";
123 replaceAll(str, "", "x");
124 EXPECT_EQ(str, "abc");
125}
126
127TEST(IReplaceAllTest, CaseInsensitive)
128{
129 std::string str = "Hello hEllo heLLo";
130 iReplaceAll(str, "hello", "hi");
131 EXPECT_EQ(str, "hi hi hi");
132}
133
134TEST(IReplaceAllTest, MixedContent)
135{
136 std::string str = "Hello World! WORLD world";
137 iReplaceAll(str, "world", "Earth");
138 EXPECT_EQ(str, "Hello Earth! Earth Earth");
139}
140
141TEST(IReplaceAllTest, NoMatchCaseInsensitive)
142{
143 std::string str = "Good Morning";
144 iReplaceAll(str, "night", "day");
145 EXPECT_EQ(str, "Good Morning");
146}
147
148TEST(IReplaceAllTest, ReplaceWithEmptyCaseInsensitive)
149{
150 std::string str = "ABC abc AbC";
151 iReplaceAll(str, "abc", "");
152 EXPECT_EQ(str, " ");
153}