gostrings/gostrings_test.cpp

246 lines
7.2 KiB
C++
Raw Permalink Normal View History

2025-11-30 17:56:18 -05:00
#include "gostrings.hpp"
#include <gtest/gtest.h>
TEST(GoStringsTest, Compare) {
EXPECT_EQ(gostrings::Compare("apple", "apple"), 0);
EXPECT_EQ(gostrings::Compare("apple", "banana"), -1);
EXPECT_EQ(gostrings::Compare("banana", "apple"), 1);
}
TEST(GoStringsTest, Contains) {
EXPECT_TRUE(gostrings::Contains("hello world", "world"));
EXPECT_FALSE(gostrings::Contains("hello world", "mars"));
}
TEST(GoStringsTest, ContainsFunc) {
auto isVowel = [](char c) {
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
};
EXPECT_TRUE(gostrings::ContainsFunc("hello", isVowel));
EXPECT_FALSE(gostrings::ContainsFunc("rhythm", isVowel));
}
TEST(GoStringsTest, Count) {
EXPECT_EQ(gostrings::Count("ababab", "ab"), 3);
EXPECT_EQ(gostrings::Count("aaaa", "aa"), 2);
EXPECT_EQ(gostrings::Count("hello", "world"), 0);
EXPECT_EQ(gostrings::Count("hello", ""), 6);
}
TEST(GoStringsTest, Cut) {
auto result = gostrings::Cut("hello world", " ");
EXPECT_TRUE(result.found());
EXPECT_EQ(result.prefix.after, "world");
EXPECT_EQ(result.suffix.before, "hello");
result = gostrings::Cut("helloworld", " ");
EXPECT_FALSE(result.found());
}
TEST(GoStringsTest, CutPrefix) {
auto result = gostrings::CutPrefix("hello world", "hello ");
EXPECT_TRUE(result.found);
EXPECT_EQ(result.after, "world");
result = gostrings::CutPrefix("hello world", "world ");
EXPECT_FALSE(result.found);
}
TEST(GoStringsTest, CutSuffix) {
auto result = gostrings::CutSuffix("hello world", " world");
EXPECT_TRUE(result.found);
EXPECT_EQ(result.before, "hello");
result = gostrings::CutSuffix("hello world", " hello");
EXPECT_FALSE(result.found);
}
TEST(GoStringsTest, Fields) {
auto result = gostrings::Fields(" hello world ");
ASSERT_EQ(result.size(), 2);
EXPECT_EQ(result[0], "hello");
EXPECT_EQ(result[1], "world");
}
TEST(GoStringsTest, FieldsFunc) {
auto isSpace = [](char c) {
return c == ' ' || c == '\t' || c == '\n';
};
auto result = gostrings::FieldsFunc(" hello\tworld\n", isSpace);
ASSERT_EQ(result.size(), 2);
EXPECT_EQ(result[0], "hello");
EXPECT_EQ(result[1], "world");
}
TEST(GoStringsTest, HasPrefix) {
EXPECT_TRUE(gostrings::HasPrefix("hello world", "hello"));
EXPECT_FALSE(gostrings::HasPrefix("hello world", "world"));
}
TEST(GoStringsTest, Index) {
EXPECT_EQ(gostrings::Index("hello world", "world"), 6);
EXPECT_EQ(gostrings::Index("hello world", "mars"), -1);
}
TEST(GoStringsTest, IndexAny) {
EXPECT_EQ(gostrings::IndexAny("hello world", "aeiou"), 1);
EXPECT_EQ(gostrings::IndexAny("rhythm", "aeiou"), -1);
}
TEST(GoStringsTest, IndexByte) {
EXPECT_EQ(gostrings::IndexByte("hello world", 'o'), 4);
EXPECT_EQ(gostrings::IndexByte("hello world", 'z'), -1);
}
TEST(GoStringsTest, IndexFunc) {
auto isVowel = [](char c) {
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
};
EXPECT_EQ(gostrings::IndexFunc("hello", isVowel), 1);
EXPECT_EQ(gostrings::IndexFunc("rhythm", isVowel), -1);
}
TEST(GoStringsTest, Join) {
std::vector<std::string> elems = {"hello", "world"};
EXPECT_EQ(gostrings::Join(elems, " "), "hello world");
}
TEST(GoStringsTest, LastIndex) {
EXPECT_EQ(gostrings::LastIndex("ababcabc", "abc"), 5);
EXPECT_EQ(gostrings::LastIndex("hello world", "mars"), -1);
}
TEST(GoStringsTest, LastIndexAny) {
EXPECT_EQ(gostrings::LastIndexAny("hello world", "aeiou"), 7);
EXPECT_EQ(gostrings::LastIndexAny("rhythm", "aeiou"), -1);
}
TEST(GoStringsTest, LastIndexByte) {
EXPECT_EQ(gostrings::LastIndexByte("hello world", 'o'), 7);
EXPECT_EQ(gostrings::LastIndexByte("hello world", 'z'), -1);
}
TEST(GoStringsTest, LastIndexFunc) {
auto isVowel = [](char c) {
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
};
EXPECT_EQ(gostrings::LastIndexFunc("hello", isVowel), 4);
EXPECT_EQ(gostrings::LastIndexFunc("rhythm", isVowel), -1);
}
TEST(GoStringsTest, Repeat) {
EXPECT_EQ(gostrings::Repeat("ab", 3), "ababab");
EXPECT_EQ(gostrings::Repeat("xyz", 0), "");
}
TEST(GoStringsTest, Replace) {
EXPECT_EQ(gostrings::Replace("ababab", "ab", "xy", 2), "xyxyab");
EXPECT_EQ(gostrings::Replace("hello world", "world", "mars", -1), "hello mars");
}
TEST(GoStringsTest, ReplaceAll) {
EXPECT_EQ(gostrings::ReplaceAll("ababab", "ab", "xy"), "xyxyxy");
EXPECT_EQ(gostrings::ReplaceAll("hello world", "world", "mars"), "hello mars");
}
TEST(GoStringsTest, Split) {
auto result = gostrings::Split("a,b,c", ",");
ASSERT_EQ(result.size(), 3);
EXPECT_EQ(result[0], "a");
EXPECT_EQ(result[1], "b");
EXPECT_EQ(result[2], "c");
}
TEST(GoStringsTest, SplitAfter) {
auto result = gostrings::SplitAfter("a,b,c", ",");
ASSERT_EQ(result.size(), 3);
EXPECT_EQ(result[0], "a,");
EXPECT_EQ(result[1], "b,");
EXPECT_EQ(result[2], "c");
}
TEST(GoStringsTest, SplitAfterN) {
auto result = gostrings::SplitAfterN("a,b,c,d", ",", 3);
ASSERT_EQ(result.size(), 3);
EXPECT_EQ(result[0], "a,");
EXPECT_EQ(result[1], "b,");
EXPECT_EQ(result[2], "c,d");
}
TEST(GoStringsTest, SplitN) {
auto result = gostrings::SplitN("a,b,c,d", ",", 3);
ASSERT_EQ(result.size(), 3);
EXPECT_EQ(result[0], "a");
EXPECT_EQ(result[1], "b");
EXPECT_EQ(result[2], "c,d");
}
TEST(GoStringsTest, ToLower) {
EXPECT_EQ(gostrings::ToLower("HeLLo WoRLD"), "hello world");
}
TEST(GoStringsTest, ToUpper) {
EXPECT_EQ(gostrings::ToUpper("HeLLo WoRLD"), "HELLO WORLD");
}
TEST(GoStringsTest, Trim) {
EXPECT_EQ(gostrings::Trim(" hello world ", " "), "hello world");
}
TEST(GoStringsTest, TrimFunc) {
auto isSpace = [](char c) {
return c == ' ' || c == '\t' || c == '\n';
};
EXPECT_EQ(gostrings::TrimFunc(" \thello world\n ", isSpace), "hello world");
}
TEST(GoStringsTest, TrimLeft) {
EXPECT_EQ(gostrings::TrimLeft(" hello world ", " "), "hello world ");
}
TEST(GoStringsTest, TrimLeftFunc) {
auto isSpace = [](char c) {
return c == ' ' || c == '\t' || c == '\n';
};
EXPECT_EQ(gostrings::TrimLeftFunc(" \thello world\n ", isSpace), "hello world\n ");
}
TEST(GoStringsTest, TrimPrefix) {
auto result = gostrings::TrimPrefix("hello world", "hello ");
EXPECT_EQ(result, "world");
result = gostrings::TrimPrefix("hello world", "world ");
EXPECT_EQ(result, "hello world");
}
TEST(GoStringsTest, TrimRight) {
EXPECT_EQ(gostrings::TrimRight(" hello world ", " "), " hello world");
}
TEST(GoStringsTest, TrimRightFunc) {
auto isSpace = [](char c) {
return c == ' ' || c == '\t' || c == '\n';
};
EXPECT_EQ(gostrings::TrimRightFunc(" \thello world\n ", isSpace), " \thello world");
}
TEST(GoStringsTest, TrimSpace) {
EXPECT_EQ(gostrings::TrimSpace(" \thello world\n "), "hello world");
}
TEST(GoStringsTest, TrimSuffix) {
auto result = gostrings::TrimSuffix("hello world", " world");
EXPECT_EQ(result, "hello");
result = gostrings::TrimSuffix("hello world", " hello");
EXPECT_EQ(result, "hello world");
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}