blob: 8ed2f63634462121dabca6d145456a1f783aae06 [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;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070021 char mut[] = "aa";
22 auto s3 = zstring_view(mut);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070023
24 EXPECT_NE(sv, s2);
25 EXPECT_NE(s1, s2);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070026 EXPECT_EQ(s3, "aa");
William A. Kennington III61a81bc2022-09-19 17:46:49 -070027
28 EXPECT_EQ("ac"_zsv, s1);
29 EXPECT_EQ("ac", s1);
30 EXPECT_EQ(s1, "ac");
31 EXPECT_EQ(s, s2);
32 EXPECT_EQ(s2, s);
33 EXPECT_EQ(s1, sv);
34 EXPECT_EQ(sv, s1);
35 EXPECT_LT("ab", s1);
36 EXPECT_GT("ad", s1);
37 EXPECT_LE(s1, "ac");
38 EXPECT_LE(s, s2);
39 EXPECT_LE(s2, s);
40 EXPECT_LE(s1, sv);
41 EXPECT_LE(sv, s1);
42
43 std::cerr << s1;
44
45 std::unordered_set<zstring_view> uset{s1, s2};
46 EXPECT_EQ(1, uset.count("ac"));
47 EXPECT_EQ(1, uset.count("b"));
48 std::set<zstring_view> set{s2, s2};
49 EXPECT_EQ(0, set.count("ac"));
50 EXPECT_EQ(1, set.count("b"));
51}
52
53TEST(ZstringView, ConstructError)
54{
55 auto s = "hi\0"s;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070056#ifdef NDEBUG
57 EXPECT_EQ("hi", zstring_view(s));
58#else
William A. Kennington III61a81bc2022-09-19 17:46:49 -070059 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070060#endif
61
62 char mut1[] = "aa\0";
63 char mut2[] = {'a', 'a'};
64#ifdef NDEBUG
65 EXPECT_EQ("aa\0", zstring_view(mut1));
66 EXPECT_EQ("a", zstring_view(mut2));
67#else
68 EXPECT_THROW((zstring_view(mut1)), std::invalid_argument);
69 EXPECT_THROW((zstring_view(mut2)), std::invalid_argument);
70#endif
William A. Kennington III61a81bc2022-09-19 17:46:49 -070071}
72
73TEST(ZstringView, Suffix)
74{
75 auto s1 = zstring_view("ac");
76
77 EXPECT_EQ("ac", s1.suffix(0));
78 EXPECT_EQ("c", s1.suffix(1));
79 EXPECT_EQ("", s1.suffix(2));
80 EXPECT_THROW(s1.suffix(3), std::out_of_range);
81}
82
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070083TEST(ZstringView, NoTypeCoercion)
84{
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070085 EXPECT_NE(""_zsv, "\0");
86 EXPECT_NE("\0", ""_zsv);
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070087 EXPECT_NE(""_zsv, "\0"sv);
88 EXPECT_NE("\0"sv, ""_zsv);
89 EXPECT_LT(""_zsv, "\0"sv);
90 EXPECT_GT("\0"sv, ""_zsv);
91 auto str = "\0"s;
92 EXPECT_NE(""_zsv, str);
93 EXPECT_NE(str, ""_zsv);
94 EXPECT_LT(""_zsv, str);
95 EXPECT_GT(str, ""_zsv);
96}
97
William A. Kennington III61a81bc2022-09-19 17:46:49 -070098} // namespace stdplus