Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 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 | |
John Stiles | 074a016 | 2022-02-01 15:31:57 -0500 | [diff] [blame] | 8 | #include <string_view> |
Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 9 | |
John Stiles | 074a016 | 2022-02-01 15:31:57 -0500 | [diff] [blame] | 10 | #include "include/private/SkStringView.h" |
Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 11 | #include "tests/Test.h" |
| 12 | |
John Stiles | 074a016 | 2022-02-01 15:31:57 -0500 | [diff] [blame] | 13 | DEF_TEST(SkStringViewStartsAndEnds, r) { |
| 14 | std::string_view empty(""); |
Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 15 | REPORTER_ASSERT(r, empty.empty()); |
John Stiles | 2c764e1 | 2022-01-31 21:45:35 -0500 | [diff] [blame] | 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, "")); |
Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 22 | |
John Stiles | 074a016 | 2022-02-01 15:31:57 -0500 | [diff] [blame] | 23 | std::string_view xyz("xyz"); |
Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 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 | |
John Stiles | 2c764e1 | 2022-01-31 21:45:35 -0500 | [diff] [blame] | 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')); |
Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 33 | |
John Stiles | 2c764e1 | 2022-01-31 21:45:35 -0500 | [diff] [blame] | 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")); |
Ethan Nicholas | 2c9a6ec | 2021-06-07 16:09:59 -0400 | [diff] [blame] | 46 | |
| 47 | xyz.swap(empty); |
| 48 | REPORTER_ASSERT(r, xyz == ""); |
| 49 | REPORTER_ASSERT(r, empty == "xyz"); |
| 50 | } |
| 51 | |
John Stiles | 2c764e1 | 2022-01-31 21:45:35 -0500 | [diff] [blame] | 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")); |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 68 | } |