| George Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 5 | constexpr std::string_view helloWorld = "Hello World"; |
| 6 | |
| George Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 7 | TEST(IfindFirstTest, BasicMatch) |
| 8 | { |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 9 | 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 Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | TEST(IfindFirstTest, CaseInsensitiveMatch) |
| 16 | { |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 17 | 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 Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | TEST(IfindFirstTest, NoMatch) |
| 24 | { |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 25 | auto match = iFindFirst(helloWorld, "Planet"); |
| 26 | EXPECT_FALSE(match); |
| George Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | TEST(IfindFirstTest, MatchAtStart) |
| 30 | { |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 31 | 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 Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | TEST(IfindFirstTest, MatchAtEnd) |
| 38 | { |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 39 | 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 Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | TEST(IfindFirstTest, EmptySubstring) |
| 46 | { |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 47 | auto match = iFindFirst(helloWorld, ""); |
| 48 | EXPECT_FALSE(match); |
| George Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(IfindFirstTest, EmptyString) |
| 52 | { |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 53 | auto match = iFindFirst("", "Hello"); |
| 54 | EXPECT_FALSE(match); |
| George Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 55 | } |
| George Liu | ecf1a31 | 2025-08-25 10:43:12 +0800 | [diff] [blame] | 56 | |
| 57 | TEST(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 | |
| 64 | TEST(SplitTest, ConsecutiveDelimiters) |
| 65 | { |
| 66 | auto result = split("a,,b", ','); |
| 67 | std::vector<std::string> expected = {"a", "", "b"}; |
| 68 | EXPECT_EQ(result, expected); |
| 69 | } |
| 70 | |
| 71 | TEST(SplitTest, LeadingDelimiter) |
| 72 | { |
| 73 | auto result = split(",a,b", ','); |
| 74 | std::vector<std::string> expected = {"", "a", "b"}; |
| 75 | EXPECT_EQ(result, expected); |
| 76 | } |
| 77 | |
| 78 | TEST(SplitTest, TrailingDelimiter) |
| 79 | { |
| 80 | auto result = split("a,b,", ','); |
| 81 | std::vector<std::string> expected = {"a", "b", ""}; |
| 82 | EXPECT_EQ(result, expected); |
| 83 | } |
| 84 | |
| 85 | TEST(SplitTest, NoDelimiter) |
| 86 | { |
| 87 | auto result = split("abc", ','); |
| 88 | std::vector<std::string> expected = {"abc"}; |
| 89 | EXPECT_EQ(result, expected); |
| 90 | } |
| 91 | |
| 92 | TEST(SplitTest, EmptyString) |
| 93 | { |
| 94 | auto result = split("", ','); |
| 95 | std::vector<std::string> expected = {""}; |
| 96 | EXPECT_EQ(result, expected); |
| 97 | } |
| George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame] | 98 | |
| 99 | TEST(ReplaceAllTest, BasicReplacement) |
| 100 | { |
| 101 | std::string str = "hello world, world!"; |
| 102 | replaceAll(str, "world", "earth"); |
| 103 | EXPECT_EQ(str, "hello earth, earth!"); |
| 104 | } |
| 105 | |
| 106 | TEST(ReplaceAllTest, NoMatch) |
| 107 | { |
| 108 | std::string str = "hello world"; |
| 109 | replaceAll(str, "xxx", "abc"); |
| 110 | EXPECT_EQ(str, "hello world"); |
| 111 | } |
| 112 | |
| 113 | TEST(ReplaceAllTest, ReplaceWithEmpty) |
| 114 | { |
| 115 | std::string str = "apple apple"; |
| 116 | replaceAll(str, "apple", ""); |
| 117 | EXPECT_EQ(str, " "); |
| 118 | } |
| 119 | |
| 120 | TEST(ReplaceAllTest, ReplaceEmptySearch) |
| 121 | { |
| 122 | std::string str = "abc"; |
| 123 | replaceAll(str, "", "x"); |
| 124 | EXPECT_EQ(str, "abc"); |
| 125 | } |
| 126 | |
| 127 | TEST(IReplaceAllTest, CaseInsensitive) |
| 128 | { |
| 129 | std::string str = "Hello hEllo heLLo"; |
| 130 | iReplaceAll(str, "hello", "hi"); |
| 131 | EXPECT_EQ(str, "hi hi hi"); |
| 132 | } |
| 133 | |
| 134 | TEST(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 | |
| 141 | TEST(IReplaceAllTest, NoMatchCaseInsensitive) |
| 142 | { |
| 143 | std::string str = "Good Morning"; |
| 144 | iReplaceAll(str, "night", "day"); |
| 145 | EXPECT_EQ(str, "Good Morning"); |
| 146 | } |
| 147 | |
| 148 | TEST(IReplaceAllTest, ReplaceWithEmptyCaseInsensitive) |
| 149 | { |
| 150 | std::string str = "ABC abc AbC"; |
| 151 | iReplaceAll(str, "abc", ""); |
| 152 | EXPECT_EQ(str, " "); |
| 153 | } |
| George Liu | fd977ec | 2025-08-25 11:36:44 +0800 | [diff] [blame] | 154 | |
| 155 | TEST(ToLowerCopyTest, BasicTests) |
| 156 | { |
| 157 | EXPECT_EQ(toLowerCopy("HelloWorld"), "helloworld"); |
| 158 | |
| 159 | EXPECT_EQ(toLowerCopy("HELLOWORLD"), "helloworld"); |
| 160 | |
| 161 | EXPECT_EQ(toLowerCopy("helloworld"), "helloworld"); |
| 162 | |
| 163 | EXPECT_EQ(toLowerCopy("123ABC!@#"), "123abc!@#"); |
| 164 | |
| 165 | EXPECT_EQ(toLowerCopy("!@#$%^&*()_+"), "!@#$%^&*()_+"); |
| 166 | |
| 167 | EXPECT_EQ(toLowerCopy(""), ""); |
| 168 | } |