treewide: Test constexpr more thoroughly
Declaring object wide constexpr values unveiled some issues with
constant evaluation that were not previously noticed.
Change-Id: If94e92861f5335ff1292915ed54a9675b56785f2
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/net/addr/ip.cpp b/test/net/addr/ip.cpp
index 5fef26a..1379c19 100644
--- a/test/net/addr/ip.cpp
+++ b/test/net/addr/ip.cpp
@@ -183,6 +183,9 @@
std::hash<InAnyAddr>{}(In6Addr{});
}
+constexpr auto empty4 = "0.0.0.0"_ip;
+constexpr auto empty6 = "::"_ip;
+
TEST(FromStr, InAnyAddr)
{
constexpr FromStr<InAnyAddr> fs;
@@ -196,8 +199,8 @@
EXPECT_TRUE(tv);
constexpr bool tv2 = detail::CompileInAnyAddr("::").valid;
EXPECT_TRUE(tv2);
- EXPECT_EQ((In4Addr{}), "0.0.0.0"_ip);
- EXPECT_EQ((In6Addr{}), "::"_ip);
+ EXPECT_EQ((In4Addr{}), empty4);
+ EXPECT_EQ((In6Addr{}), empty6);
EXPECT_EQ("ff02::"_ip, (In6Addr{0xff, 2}));
EXPECT_EQ("1::"_ip, (In6Addr{0, 1}));
EXPECT_EQ("100::"_ip, (In6Addr{1}));
@@ -229,10 +232,12 @@
EXPECT_EQ("0.0.0.0"_ip, std::optional<InAnyAddr>(In4Addr{}));
}
+constexpr auto loop4 = "127.0.0.0"_ip4;
+constexpr bool loop4IsLoop = loop4.isLoopback();
+
TEST(Loopback, In4Addr)
{
- constexpr bool t = "127.0.0.0"_ip4.isLoopback();
- EXPECT_TRUE(t);
+ EXPECT_TRUE(loop4IsLoop);
EXPECT_TRUE("127.0.0.1"_ip4.isLoopback());
EXPECT_TRUE("127.0.0.83"_ip4.isLoopback());
EXPECT_TRUE("127.253.0.0"_ip4.isLoopback());
@@ -248,10 +253,12 @@
EXPECT_FALSE("255.255.255.255"_ip4.isLoopback());
}
+constexpr auto loop6 = "::1"_ip6;
+constexpr bool loop6IsLoop = loop6.isLoopback();
+
TEST(Loopback, In6Addr)
{
- constexpr bool t = "::1"_ip6.isLoopback();
- EXPECT_TRUE(t);
+ EXPECT_TRUE(loop6IsLoop);
EXPECT_FALSE("::2"_ip6.isLoopback());
EXPECT_FALSE("1::"_ip6.isLoopback());
diff --git a/test/zstring_view.cpp b/test/zstring_view.cpp
index f856757..34046af 100644
--- a/test/zstring_view.cpp
+++ b/test/zstring_view.cpp
@@ -24,6 +24,9 @@
std::char_traits<char>>::value));
}
+constexpr auto ezsv = ""_zsv;
+constexpr auto aczsv = "ac"_zsv;
+
TEST(ZstringView, Basic)
{
auto s1 = zstring_view("ac");
@@ -41,7 +44,7 @@
EXPECT_NE(s1, s2);
EXPECT_EQ(s3, "aa");
- EXPECT_EQ("ac"_zsv, s1);
+ EXPECT_EQ(aczsv, s1);
EXPECT_EQ("ac", s1);
EXPECT_EQ(s1, "ac");
EXPECT_EQ(s, s2);
@@ -100,7 +103,7 @@
TEST(ZstringView, NoTypeCoercion)
{
- EXPECT_EQ(""_zsv, "\0");
+ EXPECT_EQ(ezsv, "\0");
EXPECT_EQ("\0", ""_zsv);
EXPECT_NE(""_zsv, "\0"sv);
EXPECT_NE("\0"sv, ""_zsv);