blob: 8b29bc96a4f92ea6d92dc64ee6c0664600aec946 [file] [log] [blame]
William A. Kennington III62e40d62022-09-24 22:36:31 -07001#include <fmt/format.h>
Patrick Williamsd1984dd2023-05-10 16:12:44 -05002
3#include <stdplus/zstring_view.hpp>
4
William A. Kennington III61a81bc2022-09-19 17:46:49 -07005#include <iostream>
6#include <set>
William A. Kennington III61a81bc2022-09-19 17:46:49 -07007#include <string>
8#include <unordered_set>
9
Patrick Williamsd1984dd2023-05-10 16:12:44 -050010#include <gtest/gtest.h>
11
William A. Kennington III61a81bc2022-09-19 17:46:49 -070012namespace stdplus
13{
14
15using std::literals::string_literals::operator""s;
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -070016using std::literals::string_view_literals::operator""sv;
William A. Kennington III61a81bc2022-09-19 17:46:49 -070017using zstring_view_literals::operator""_zsv;
18
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070019TEST(ZstringView, IsString)
20{
21 EXPECT_TRUE((
22 detail::same_string<std::string, char, std::char_traits<char>>::value));
23 EXPECT_FALSE((detail::same_string<std::string_view, char,
24 std::char_traits<char>>::value));
25}
26
William A. Kennington III61a81bc2022-09-19 17:46:49 -070027TEST(ZstringView, Basic)
28{
29 auto s1 = zstring_view("ac");
30 auto s = "b"s;
31 auto s2 = zstring_view(s);
32 std::string_view sv = s1;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070033 char mut[] = "aa";
34 auto s3 = zstring_view(mut);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070035
William A. Kennington III37b2d5e2022-10-11 14:07:14 -070036 EXPECT_EQ(1, s1.find('c'));
37 EXPECT_EQ(0, s3.find('a'));
38 EXPECT_EQ(1, s3.rfind('a'));
39
William A. Kennington III61a81bc2022-09-19 17:46:49 -070040 EXPECT_NE(sv, s2);
41 EXPECT_NE(s1, s2);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070042 EXPECT_EQ(s3, "aa");
William A. Kennington III61a81bc2022-09-19 17:46:49 -070043
44 EXPECT_EQ("ac"_zsv, s1);
45 EXPECT_EQ("ac", s1);
46 EXPECT_EQ(s1, "ac");
47 EXPECT_EQ(s, s2);
48 EXPECT_EQ(s2, s);
49 EXPECT_EQ(s1, sv);
50 EXPECT_EQ(sv, s1);
51 EXPECT_LT("ab", s1);
52 EXPECT_GT("ad", s1);
53 EXPECT_LE(s1, "ac");
54 EXPECT_LE(s, s2);
55 EXPECT_LE(s2, s);
56 EXPECT_LE(s1, sv);
57 EXPECT_LE(sv, s1);
58
59 std::cerr << s1;
William A. Kennington III62e40d62022-09-24 22:36:31 -070060 EXPECT_EQ("bac", fmt::format("b{}", s1));
William A. Kennington III61a81bc2022-09-19 17:46:49 -070061
62 std::unordered_set<zstring_view> uset{s1, s2};
63 EXPECT_EQ(1, uset.count("ac"));
64 EXPECT_EQ(1, uset.count("b"));
65 std::set<zstring_view> set{s2, s2};
66 EXPECT_EQ(0, set.count("ac"));
67 EXPECT_EQ(1, set.count("b"));
William A. Kennington IIIb6032a12022-09-23 18:27:05 -070068
69 auto from_str = [&](zstring_view cs) { EXPECT_EQ(cs, "ac"); };
70 from_str("ac"s);
William A. Kennington III61a81bc2022-09-19 17:46:49 -070071}
72
73TEST(ZstringView, ConstructError)
74{
75 auto s = "hi\0"s;
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070076#ifdef NDEBUG
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070077 EXPECT_EQ("hi\0"sv, zstring_view(s));
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070078#else
William A. Kennington III61a81bc2022-09-19 17:46:49 -070079 EXPECT_THROW((zstring_view(s)), std::invalid_argument);
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070080#endif
81
82 char mut1[] = "aa\0";
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -070083 EXPECT_EQ("aa", zstring_view(mut1));
84#ifndef NDEBUG
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070085 char mut2[] = {'a', 'a'};
William A. Kennington IIIe6e44e72022-09-22 17:54:07 -070086 EXPECT_THROW((zstring_view(mut2)), std::invalid_argument);
87#endif
William A. Kennington III61a81bc2022-09-19 17:46:49 -070088}
89
90TEST(ZstringView, Suffix)
91{
92 auto s1 = zstring_view("ac");
93
94 EXPECT_EQ("ac", s1.suffix(0));
95 EXPECT_EQ("c", s1.suffix(1));
96 EXPECT_EQ("", s1.suffix(2));
97 EXPECT_THROW(s1.suffix(3), std::out_of_range);
98}
99
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700100TEST(ZstringView, NoTypeCoercion)
101{
William A. Kennington IIIaaa8e0d2022-09-24 22:36:31 -0700102 EXPECT_EQ(""_zsv, "\0");
103 EXPECT_EQ("\0", ""_zsv);
William A. Kennington IIIa2d2df72022-09-20 18:49:36 -0700104 EXPECT_NE(""_zsv, "\0"sv);
105 EXPECT_NE("\0"sv, ""_zsv);
106 EXPECT_LT(""_zsv, "\0"sv);
107 EXPECT_GT("\0"sv, ""_zsv);
108 auto str = "\0"s;
109 EXPECT_NE(""_zsv, str);
110 EXPECT_NE(str, ""_zsv);
111 EXPECT_LT(""_zsv, str);
112 EXPECT_GT(str, ""_zsv);
113}
114
William A. Kennington III61a81bc2022-09-19 17:46:49 -0700115} // namespace stdplus