#include "common.hh" #include "strutil.hh" #include TEST(strutil, parse_uint16) { std::optional value; value = str::parse_uint16("0"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(0, value.value()); value = str::parse_uint16("10"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(10, value.value()); value = str::parse_uint16("65535"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(65535, value.value()); EXPECT_FALSE(str::parse_uint16("-1")); EXPECT_FALSE(str::parse_uint16("0x10")); EXPECT_FALSE(str::parse_uint16("65536")); EXPECT_FALSE(str::parse_uint16("")); EXPECT_FALSE(str::parse_uint16("NaN")); } TEST(strutil, parse_uint32) { std::optional value; value = str::parse_uint32("0"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(0, value.value()); value = str::parse_uint32("10"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(10, value); value = str::parse_uint32("4294967295"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(4294967295ul, value); EXPECT_FALSE(str::parse_uint32("-1")); EXPECT_FALSE(str::parse_uint32("0x10")); EXPECT_FALSE(str::parse_uint32("4294967296")); EXPECT_FALSE(str::parse_uint32("")); EXPECT_FALSE(str::parse_uint32("NaN")); } TEST(strutil, parse_uint64) { std::optional value; value = str::parse_uint64("0"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(0, value.value()); value = str::parse_uint64("10"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(10, value.value()); value = str::parse_uint64("18446744073709551615"); EXPECT_TRUE(value.has_value()); if (value.has_value()) EXPECT_EQ(18446744073709551615ull, value.value()); EXPECT_FALSE(str::parse_uint64("-1")); EXPECT_FALSE(str::parse_uint64("0x10")); EXPECT_FALSE(str::parse_uint64("18446744073709551616")); EXPECT_FALSE(str::parse_uint64("")); EXPECT_FALSE(str::parse_uint64("NaN")); } TEST(strutil, split) { auto out = str::split(std::string_view("")); ASSERT_EQ(1, out.size()); EXPECT_EQ("", out[0]); out = str::split(std::string_view("foo")); ASSERT_EQ(1, out.size()); EXPECT_EQ("foo", out[0]); out = str::split(std::string_view(" f o o ")); ASSERT_EQ(3, out.size()); EXPECT_EQ("f", out[0]); EXPECT_EQ("o", out[1]); EXPECT_EQ("o", out[2]); out = str::split(std::string_view(" f o o ")); ASSERT_EQ(3, out.size()); EXPECT_EQ("f", out[0]); EXPECT_EQ("o", out[1]); EXPECT_EQ("o", out[2]); out = str::split(std::string_view("abba"), 'b'); ASSERT_EQ(2, out.size()); EXPECT_EQ("a", out[0]); EXPECT_EQ("a", out[1]); } TEST(strutil, split_str) { auto out = str::split(std::string("")); ASSERT_EQ(1, out.size()); EXPECT_EQ("", out[0]); out = str::split(std::string("foo")); ASSERT_EQ(1, out.size()); EXPECT_EQ("foo", out[0]); out = str::split(std::string(" f o o ")); ASSERT_EQ(3, out.size()); EXPECT_EQ("f", out[0]); EXPECT_EQ("o", out[1]); EXPECT_EQ("o", out[2]); out = str::split(std::string(" f o o ")); ASSERT_EQ(3, out.size()); EXPECT_EQ("f", out[0]); EXPECT_EQ("o", out[1]); EXPECT_EQ("o", out[2]); out = str::split(std::string("abba"), 'b'); ASSERT_EQ(2, out.size()); EXPECT_EQ("a", out[0]); EXPECT_EQ("a", out[1]); } TEST(strutil, trim_str) { EXPECT_EQ("", str::trim(std::string(""))); EXPECT_EQ("foo", str::trim(std::string("foo"))); EXPECT_EQ("foo", str::trim(std::string("foo "))); EXPECT_EQ("foo", str::trim(std::string(" foo"))); EXPECT_EQ("foo", str::trim(std::string(" foo "))); EXPECT_EQ("foo", str::trim(std::string(" foo "))); EXPECT_EQ("", str::ltrim(std::string(""))); EXPECT_EQ("foo", str::ltrim(std::string("foo"))); EXPECT_EQ("foo ", str::ltrim(std::string("foo "))); EXPECT_EQ("foo", str::ltrim(std::string(" foo"))); EXPECT_EQ("foo ", str::ltrim(std::string(" foo "))); EXPECT_EQ("foo ", str::ltrim(std::string(" foo "))); EXPECT_EQ("", str::rtrim(std::string(""))); EXPECT_EQ("foo", str::rtrim(std::string("foo"))); EXPECT_EQ("foo", str::rtrim(std::string("foo "))); EXPECT_EQ(" foo", str::rtrim(std::string(" foo"))); EXPECT_EQ(" foo", str::rtrim(std::string(" foo "))); EXPECT_EQ(" foo", str::rtrim(std::string(" foo "))); } TEST(strutil, trim_view) { EXPECT_EQ("", str::trim(std::string_view(""))); EXPECT_EQ("foo", str::trim(std::string_view("foo"))); EXPECT_EQ("foo", str::trim(std::string_view("foo "))); EXPECT_EQ("foo", str::trim(std::string_view(" foo"))); EXPECT_EQ("foo", str::trim(std::string_view(" foo "))); EXPECT_EQ("foo", str::trim(std::string_view(" foo "))); EXPECT_EQ("", str::ltrim(std::string(""))); EXPECT_EQ("foo", str::ltrim(std::string("foo"))); EXPECT_EQ("foo ", str::ltrim(std::string("foo "))); EXPECT_EQ("foo", str::ltrim(std::string(" foo"))); EXPECT_EQ("foo ", str::ltrim(std::string(" foo "))); EXPECT_EQ("foo ", str::ltrim(std::string(" foo "))); EXPECT_EQ("", str::rtrim(std::string(""))); EXPECT_EQ("foo", str::rtrim(std::string("foo"))); EXPECT_EQ("foo", str::rtrim(std::string("foo "))); EXPECT_EQ(" foo", str::rtrim(std::string(" foo"))); EXPECT_EQ(" foo", str::rtrim(std::string(" foo "))); EXPECT_EQ(" foo", str::rtrim(std::string(" foo "))); } TEST(strutil, starts_with) { EXPECT_TRUE(str::starts_with("", "")); EXPECT_TRUE(str::starts_with("foo", "")); EXPECT_TRUE(str::starts_with("foo", "foo")); EXPECT_FALSE(str::starts_with("foo", "foobar")); EXPECT_TRUE(str::starts_with("foo", "f")); EXPECT_FALSE(str::starts_with("foo", "o")); EXPECT_TRUE(str::starts_with("bar", "")); EXPECT_FALSE(str::starts_with("bar", "foo")); EXPECT_FALSE(str::starts_with("bar", "f")); EXPECT_FALSE(str::starts_with("bar", "barfoo")); } TEST(strutil, ends_with) { EXPECT_TRUE(str::ends_with("", "")); EXPECT_TRUE(str::ends_with("foo", "")); EXPECT_TRUE(str::ends_with("foo", "foo")); EXPECT_FALSE(str::ends_with("foo", "barfoo")); EXPECT_TRUE(str::ends_with("foo", "o")); EXPECT_FALSE(str::ends_with("foo", "f")); EXPECT_TRUE(str::ends_with("bar", "")); EXPECT_FALSE(str::ends_with("bar", "foo")); EXPECT_FALSE(str::ends_with("bar", "f")); EXPECT_FALSE(str::ends_with("bar", "barfoo")); } TEST(strutil, join_str) { EXPECT_EQ("", str::join(std::vector(), ',')); EXPECT_EQ("foo", str::join(std::vector({"foo"}), ',')); EXPECT_EQ("foo,bar", str::join(std::vector({"foo", "bar"}), ',')); EXPECT_EQ(",,", str::join(std::vector({"", "", ""}), ',')); EXPECT_EQ(",foo,", str::join(std::vector({",", ","}), "foo")); } TEST(strutil, join_view) { EXPECT_EQ("", str::join(std::vector(), ',')); EXPECT_EQ("foo", str::join(std::vector({"foo"}), ',')); EXPECT_EQ("foo,bar", str::join(std::vector({"foo", "bar"}), ',')); EXPECT_EQ(",,", str::join(std::vector({"", "", ""}), ',')); EXPECT_EQ(",foo,", str::join(std::vector({",", ","}), "foo")); }