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