blob: 6e6b2044903fff827bbc1a5ccaa9a084c5a1e31a [file] [log] [blame]
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -04001/*
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
John Stiles074a0162022-02-01 15:31:57 -05008#include <string_view>
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -04009
John Stiles074a0162022-02-01 15:31:57 -050010#include "include/private/SkStringView.h"
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -040011#include "tests/Test.h"
12
John Stiles074a0162022-02-01 15:31:57 -050013DEF_TEST(SkStringViewStartsAndEnds, r) {
14 std::string_view empty("");
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -040015 REPORTER_ASSERT(r, empty.empty());
John Stiles2c764e12022-01-31 21:45:35 -050016 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, ""));
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -040022
John Stiles074a0162022-02-01 15:31:57 -050023 std::string_view xyz("xyz");
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -040024 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
John Stiles2c764e12022-01-31 21:45:35 -050029 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'));
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -040033
John Stiles2c764e12022-01-31 21:45:35 -050034 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"));
Ethan Nicholas2c9a6ec2021-06-07 16:09:59 -040046
47 xyz.swap(empty);
48 REPORTER_ASSERT(r, xyz == "");
49 REPORTER_ASSERT(r, empty == "xyz");
50}
51
John Stiles2c764e12022-01-31 21:45:35 -050052DEF_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"));
Ethan Nicholas32724122021-09-07 13:49:07 -040068}