• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkStringView.h"
9 
10 #include "tests/Test.h"
11 
DEF_TEST(SkStringViewConstructors,r)12 DEF_TEST(SkStringViewConstructors, r) {
13     skstd::string_view empty;
14     REPORTER_ASSERT(r, empty.data() == nullptr);
15     REPORTER_ASSERT(r, empty.length() == 0);
16 
17     const char* str = "Hello, World!";
18     skstd::string_view helloWorld(str);
19     REPORTER_ASSERT(r, helloWorld.data() == str);
20     REPORTER_ASSERT(r, helloWorld.length() == strlen(str));
21 
22     skstd::string_view hello(str, 5);
23     REPORTER_ASSERT(r, hello.data() == str);
24     REPORTER_ASSERT(r, hello.length() == 5);
25 
26     skstd::string_view copy(hello);
27     REPORTER_ASSERT(r, copy.data() == str);
28     REPORTER_ASSERT(r, copy.length() == 5);
29 
30     copy = helloWorld;
31     REPORTER_ASSERT(r, copy.data() == str);
32     REPORTER_ASSERT(r, copy.length() == strlen(str));
33 }
34 
DEF_TEST(SkStringViewBasics,r)35 DEF_TEST(SkStringViewBasics, r) {
36     skstd::string_view empty("");
37     REPORTER_ASSERT(r, empty.empty());
38     REPORTER_ASSERT(r, !empty.starts_with('x'));
39     REPORTER_ASSERT(r, !empty.ends_with('x'));
40     REPORTER_ASSERT(r, !empty.starts_with("x"));
41     REPORTER_ASSERT(r, !empty.ends_with("x"));
42     REPORTER_ASSERT(r, empty.starts_with(""));
43     REPORTER_ASSERT(r, empty.ends_with(""));
44 
45     skstd::string_view xyz("xyz");
46     REPORTER_ASSERT(r, !xyz.empty());
47     REPORTER_ASSERT(r, xyz.front() == 'x');
48     REPORTER_ASSERT(r, xyz.back() == 'z');
49     REPORTER_ASSERT(r, xyz.length() == 3);
50 
51     REPORTER_ASSERT(r, xyz.starts_with('x'));
52     REPORTER_ASSERT(r, !xyz.starts_with('y'));
53     REPORTER_ASSERT(r, xyz.ends_with('z'));
54     REPORTER_ASSERT(r, !xyz.ends_with('y'));
55 
56     REPORTER_ASSERT(r, xyz.starts_with(""));
57     REPORTER_ASSERT(r, xyz.ends_with(""));
58     REPORTER_ASSERT(r, xyz.starts_with("x"));
59     REPORTER_ASSERT(r, xyz.ends_with("z"));
60     REPORTER_ASSERT(r, !xyz.starts_with("xa"));
61     REPORTER_ASSERT(r, !xyz.ends_with("az"));
62     REPORTER_ASSERT(r, xyz.starts_with("xy"));
63     REPORTER_ASSERT(r, xyz.ends_with("yz"));
64     REPORTER_ASSERT(r, xyz.starts_with("xyz"));
65     REPORTER_ASSERT(r, xyz.ends_with("xyz"));
66     REPORTER_ASSERT(r, !xyz.starts_with("wxyz"));
67     REPORTER_ASSERT(r, !xyz.ends_with("wxyz"));
68 
69     xyz.swap(empty);
70     REPORTER_ASSERT(r, xyz == "");
71     REPORTER_ASSERT(r, empty == "xyz");
72 }
73 
DEF_TEST(SkStringViewIterator,r)74 DEF_TEST(SkStringViewIterator, r) {
75     skstd::string_view str("abc");
76     skstd::string_view::iterator iter = str.begin();
77     REPORTER_ASSERT(r, *(iter++) == 'a');
78     REPORTER_ASSERT(r, *(iter++) == 'b');
79     REPORTER_ASSERT(r, *(iter++) == 'c');
80     REPORTER_ASSERT(r, iter == str.end());
81     REPORTER_ASSERT(r, *(--iter) == 'c');
82     REPORTER_ASSERT(r, *(--iter) == 'b');
83     REPORTER_ASSERT(r, *(--iter) == 'a');
84     REPORTER_ASSERT(r, iter == str.begin());
85 
86     skstd::string_view empty;
87     REPORTER_ASSERT(r, empty.begin() == empty.end());
88 }
89 
DEF_TEST(SkStringViewOperators,r)90 DEF_TEST(SkStringViewOperators, r) {
91     skstd::string_view empty;
92     REPORTER_ASSERT(r, empty == empty);
93     REPORTER_ASSERT(r, empty == "");
94     REPORTER_ASSERT(r, "" == empty);
95     REPORTER_ASSERT(r, !(empty != ""));
96 
97     skstd::string_view str("abc");
98     REPORTER_ASSERT(r, str == str);
99     REPORTER_ASSERT(r, str == "abc");
100     REPORTER_ASSERT(r, "abc" == str);
101     REPORTER_ASSERT(r, str != "");
102     REPORTER_ASSERT(r, "" != str);
103     REPORTER_ASSERT(r, str != "abcd");
104     REPORTER_ASSERT(r, "abcd" != str);
105 
106     skstd::string_view str2("abcd");
107     REPORTER_ASSERT(r, str < str2);
108     REPORTER_ASSERT(r, str <= str2);
109     REPORTER_ASSERT(r, str <= str);
110     REPORTER_ASSERT(r, str2 > str);
111     REPORTER_ASSERT(r, str2 >= str);
112     REPORTER_ASSERT(r, str >= str);
113     REPORTER_ASSERT(r, !(str2 < str));
114     REPORTER_ASSERT(r, !(str < str));
115     REPORTER_ASSERT(r, !(str2 <= str));
116     REPORTER_ASSERT(r, !(str > str2));
117     REPORTER_ASSERT(r, !(str > str));
118     REPORTER_ASSERT(r, !(str >= str2));
119 
120     REPORTER_ASSERT(r, str < "b");
121     REPORTER_ASSERT(r, str <= "b");
122     REPORTER_ASSERT(r, str <= str);
123     REPORTER_ASSERT(r, "b" > str);
124     REPORTER_ASSERT(r, "b" >= str);
125     REPORTER_ASSERT(r, str >= str);
126     REPORTER_ASSERT(r, !("b" < str));
127     REPORTER_ASSERT(r, !(str < str));
128     REPORTER_ASSERT(r, !("b" <= str));
129     REPORTER_ASSERT(r, !(str > "b"));
130     REPORTER_ASSERT(r, !(str > str));
131     REPORTER_ASSERT(r, !(str >= "b"));
132 }
133 
DEF_TEST(SkStringViewSubstr,r)134 DEF_TEST(SkStringViewSubstr, r) {
135     skstd::string_view xyz("xyz");
136     REPORTER_ASSERT(r, xyz.substr() == xyz);
137     REPORTER_ASSERT(r, xyz.substr(0, 1) == "x");
138     REPORTER_ASSERT(r, xyz.substr(0, 2) == "xy");
139     REPORTER_ASSERT(r, xyz.substr(0, 3) == "xyz");
140     REPORTER_ASSERT(r, xyz.substr(0, 4) == "xyz");
141 
142     REPORTER_ASSERT(r, xyz.substr(1) == "yz");
143     REPORTER_ASSERT(r, xyz.substr(1, 1) == "y");
144     REPORTER_ASSERT(r, xyz.substr(1, 2) == "yz");
145     REPORTER_ASSERT(r, xyz.substr(1, 3) == "yz");
146 
147     REPORTER_ASSERT(r, xyz.substr(2) == "z");
148     REPORTER_ASSERT(r, xyz.substr(2, 1) == "z");
149     REPORTER_ASSERT(r, xyz.substr(2, 2) == "z");
150 
151     REPORTER_ASSERT(r, xyz.substr(0, 0).empty());
152     REPORTER_ASSERT(r, xyz.substr(1, 0).empty());
153     REPORTER_ASSERT(r, xyz.substr(2, 0).empty());
154     REPORTER_ASSERT(r, xyz.substr(3, 0).empty());
155 
156     REPORTER_ASSERT(r, xyz.substr(3).empty());
157     REPORTER_ASSERT(r, xyz.substr(4).empty());
158 }
159 
DEF_TEST(SkStringViewFind,r)160 DEF_TEST(SkStringViewFind, r) {
161     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("abcdef") == 0);
162     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("abcdefg") == skstd::string_view::npos);
163     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("") == 0);
164     REPORTER_ASSERT(r, skstd::string_view("").find("") == 0);
165     REPORTER_ASSERT(r, skstd::string_view("").find("a") == skstd::string_view::npos);
166     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("b") == 1);
167     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("f") == 5);
168     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("q") == skstd::string_view::npos);
169     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("bcd") == 1);
170     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("bcd", 2) == skstd::string_view::npos);
171     REPORTER_ASSERT(r, skstd::string_view("abcdef").find("bce") == skstd::string_view::npos);
172     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").find("test") == 3);
173     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").find("test", 1) == 3);
174     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").find("test", 3) == 3);
175     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").find("test", 4) == 10);
176     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").find("test2") == 10);
177     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").find("test3") == 17);
178     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").contains("test"));
179     REPORTER_ASSERT(r, skstd::string_view("ttttest1tttest2tttest3").contains("test3"));
180     REPORTER_ASSERT(r, !skstd::string_view("ttttest1tttest2tttest3").contains("test4"));
181     REPORTER_ASSERT(r, skstd::string_view("").contains(""));
182     REPORTER_ASSERT(r, !skstd::string_view("").contains("a"));
183     REPORTER_ASSERT(r, skstd::string_view("abcabcd").contains("abcd"));
184     REPORTER_ASSERT(r, skstd::string_view("abc").contains(""));
185     REPORTER_ASSERT(r, skstd::string_view("abc").contains("a"));
186     REPORTER_ASSERT(r, skstd::string_view("abc").contains("b"));
187     REPORTER_ASSERT(r, skstd::string_view("abc").contains("c"));
188     REPORTER_ASSERT(r, skstd::string_view("abc").contains("ab"));
189     REPORTER_ASSERT(r, skstd::string_view("abc").contains("bc"));
190     REPORTER_ASSERT(r, !skstd::string_view("abc").contains("ac"));
191     REPORTER_ASSERT(r, !skstd::string_view("abc").contains("cb"));
192     REPORTER_ASSERT(r, !skstd::string_view("abc").contains("abcd"));
193 }
194