blob: 1b7120d6e141e62e502dde43adc20cbd351bc043 [file] [log] [blame]
William A. Kennington III61a81bc2022-09-19 17:46:49 -07001#include <gtest/gtest.h>
2#include <iostream>
3#include <set>
4#include <stdplus/zstring_view.hpp>
5#include <string>
6#include <unordered_set>
7
8namespace stdplus
9{
10
11using std::literals::string_literals::operator""s;
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070012using std::literals::string_view_literals::operator""sv;
William A. Kennington III61a81bc2022-09-19 17:46:49 -070013using zstring_view_literals::operator""_zsv;
14
15TEST(ZstringView, Basic)
16{
17 auto s1 = zstring_view("ac");
18 auto s = "b"s;
19 auto s2 = zstring_view(s);
20 std::string_view sv = s1;
21
22 EXPECT_NE(sv, s2);
23 EXPECT_NE(s1, s2);
24
25 EXPECT_EQ("ac"_zsv, s1);
26 EXPECT_EQ("ac", s1);
27 EXPECT_EQ(s1, "ac");
28 EXPECT_EQ(s, s2);
29 EXPECT_EQ(s2, s);
30 EXPECT_EQ(s1, sv);
31 EXPECT_EQ(sv, s1);
32 EXPECT_LT("ab", s1);
33 EXPECT_GT("ad", s1);
34 EXPECT_LE(s1, "ac");
35 EXPECT_LE(s, s2);
36 EXPECT_LE(s2, s);
37 EXPECT_LE(s1, sv);
38 EXPECT_LE(sv, s1);
39
40 std::cerr << s1;
41
42 std::unordered_set<zstring_view> uset{s1, s2};
43 EXPECT_EQ(1, uset.count("ac"));
44 EXPECT_EQ(1, uset.count("b"));
45 std::set<zstring_view> set{s2, s2};
46 EXPECT_EQ(0, set.count("ac"));
47 EXPECT_EQ(1, set.count("b"));
48}
49
50TEST(ZstringView, ConstructError)
51{
52 auto s = "hi\0"s;
53 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
54}
55
56TEST(ZstringView, Suffix)
57{
58 auto s1 = zstring_view("ac");
59
60 EXPECT_EQ("ac", s1.suffix(0));
61 EXPECT_EQ("c", s1.suffix(1));
62 EXPECT_EQ("", s1.suffix(2));
63 EXPECT_THROW(s1.suffix(3), std::out_of_range);
64}
65
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070066TEST(ZstringView, NoTypeCoercion)
67{
68 EXPECT_NE(""_zsv, "\0"sv);
69 EXPECT_NE("\0"sv, ""_zsv);
70 EXPECT_LT(""_zsv, "\0"sv);
71 EXPECT_GT("\0"sv, ""_zsv);
72 auto str = "\0"s;
73 EXPECT_NE(""_zsv, str);
74 EXPECT_NE(str, ""_zsv);
75 EXPECT_LT(""_zsv, str);
76 EXPECT_GT(str, ""_zsv);
77}
78
William A. Kennington III61a81bc2022-09-19 17:46:49 -070079} // namespace stdplus