xref: /aosp_15_r20/external/skia/tests/SkStringViewTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "src/base/SkStringView.h"
9 #include "tests/Test.h"
10 
11 #include <string>
12 #include <string_view>
13 
DEF_TEST(SkStringViewStartsAndEnds,r)14 DEF_TEST(SkStringViewStartsAndEnds, r) {
15     std::string_view empty("");
16     REPORTER_ASSERT(r, empty.empty());
17     REPORTER_ASSERT(r, !skstd::starts_with(empty, 'x'));
18     REPORTER_ASSERT(r, !skstd::ends_with(empty, 'x'));
19     REPORTER_ASSERT(r, !skstd::starts_with(empty, "x"));
20     REPORTER_ASSERT(r, !skstd::ends_with(empty, "x"));
21     REPORTER_ASSERT(r, skstd::starts_with(empty, ""));
22     REPORTER_ASSERT(r, skstd::ends_with(empty, ""));
23 
24     std::string_view xyz("xyz");
25     REPORTER_ASSERT(r, !xyz.empty());
26     REPORTER_ASSERT(r, xyz.front() == 'x');
27     REPORTER_ASSERT(r, xyz.back() == 'z');
28     REPORTER_ASSERT(r, xyz.length() == 3);
29 
30     REPORTER_ASSERT(r, skstd::starts_with(xyz, 'x'));
31     REPORTER_ASSERT(r, !skstd::starts_with(xyz, 'y'));
32     REPORTER_ASSERT(r, skstd::ends_with(xyz, 'z'));
33     REPORTER_ASSERT(r, !skstd::ends_with(xyz, 'y'));
34 
35     REPORTER_ASSERT(r, skstd::starts_with(xyz, ""));
36     REPORTER_ASSERT(r, skstd::ends_with(xyz, ""));
37     REPORTER_ASSERT(r, skstd::starts_with(xyz, "x"));
38     REPORTER_ASSERT(r, skstd::ends_with(xyz, "z"));
39     REPORTER_ASSERT(r, !skstd::starts_with(xyz, "xa"));
40     REPORTER_ASSERT(r, !skstd::ends_with(xyz, "az"));
41     REPORTER_ASSERT(r, skstd::starts_with(xyz, "xy"));
42     REPORTER_ASSERT(r, skstd::ends_with(xyz, "yz"));
43     REPORTER_ASSERT(r, skstd::starts_with(xyz, "xyz"));
44     REPORTER_ASSERT(r, skstd::ends_with(xyz, "xyz"));
45     REPORTER_ASSERT(r, !skstd::starts_with(xyz, "wxyz"));
46     REPORTER_ASSERT(r, !skstd::ends_with(xyz, "wxyz"));
47 
48     xyz.swap(empty);
49     REPORTER_ASSERT(r, xyz == "");
50     REPORTER_ASSERT(r, empty == "xyz");
51 }
52 
DEF_TEST(SkStringViewContains,r)53 DEF_TEST(SkStringViewContains, r) {
54     REPORTER_ASSERT(r, skstd::contains("ttttest1tttest2tttest3", "test"));
55     REPORTER_ASSERT(r, skstd::contains("ttttest1tttest2tttest3", "test3"));
56     REPORTER_ASSERT(r, !skstd::contains("ttttest1tttest2tttest3", "test4"));
57     REPORTER_ASSERT(r, skstd::contains("", ""));
58     REPORTER_ASSERT(r, !skstd::contains("", "a"));
59     REPORTER_ASSERT(r, skstd::contains("abcabcd", "abcd"));
60     REPORTER_ASSERT(r, skstd::contains("abc", ""));
61     REPORTER_ASSERT(r, skstd::contains("abc", "a"));
62     REPORTER_ASSERT(r, skstd::contains("abc", "b"));
63     REPORTER_ASSERT(r, skstd::contains("abc", "c"));
64     REPORTER_ASSERT(r, skstd::contains("abc", "ab"));
65     REPORTER_ASSERT(r, skstd::contains("abc", "bc"));
66     REPORTER_ASSERT(r, !skstd::contains("abc", "ac"));
67     REPORTER_ASSERT(r, !skstd::contains("abc", "cb"));
68     REPORTER_ASSERT(r, !skstd::contains("abc", "abcd"));
69 }
70