Dan Albert | 0f1e544 | 2015-03-13 22:57:40 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "base/strings.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | TEST(strings, split_empty) { |
| 25 | std::vector<std::string> parts; |
| 26 | android::base::Split("", '\0', &parts); |
| 27 | ASSERT_EQ(0U, parts.size()); |
| 28 | } |
| 29 | |
| 30 | TEST(strings, split_single) { |
| 31 | std::vector<std::string> parts; |
| 32 | android::base::Split("foo", ',', &parts); |
| 33 | ASSERT_EQ(1U, parts.size()); |
| 34 | ASSERT_EQ("foo", parts[0]); |
| 35 | } |
| 36 | |
| 37 | TEST(strings, split_simple) { |
| 38 | std::vector<std::string> parts; |
| 39 | android::base::Split("foo,bar,baz", ',', &parts); |
| 40 | ASSERT_EQ(3U, parts.size()); |
| 41 | ASSERT_EQ("foo", parts[0]); |
| 42 | ASSERT_EQ("bar", parts[1]); |
| 43 | ASSERT_EQ("baz", parts[2]); |
| 44 | } |
| 45 | |
| 46 | TEST(strings, split_with_empty_part) { |
| 47 | std::vector<std::string> parts; |
| 48 | android::base::Split("foo,,bar", ',', &parts); |
| 49 | ASSERT_EQ(2U, parts.size()); |
| 50 | ASSERT_EQ("foo", parts[0]); |
| 51 | ASSERT_EQ("bar", parts[1]); |
| 52 | } |
| 53 | |
| 54 | TEST(strings, trim_empty) { |
| 55 | ASSERT_EQ("", android::base::Trim("")); |
| 56 | } |
| 57 | |
| 58 | TEST(strings, trim_already_trimmed) { |
| 59 | ASSERT_EQ("foo", android::base::Trim("foo")); |
| 60 | } |
| 61 | |
| 62 | TEST(strings, trim_left) { |
| 63 | ASSERT_EQ("foo", android::base::Trim(" foo")); |
| 64 | } |
| 65 | |
| 66 | TEST(strings, trim_right) { |
| 67 | ASSERT_EQ("foo", android::base::Trim("foo ")); |
| 68 | } |
| 69 | |
| 70 | TEST(strings, trim_both) { |
| 71 | ASSERT_EQ("foo", android::base::Trim(" foo ")); |
| 72 | } |
| 73 | |
| 74 | TEST(strings, trim_no_trim_middle) { |
| 75 | ASSERT_EQ("foo bar", android::base::Trim("foo bar")); |
| 76 | } |
| 77 | |
| 78 | TEST(strings, trim_other_whitespace) { |
| 79 | ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f")); |
| 80 | } |
| 81 | |
| 82 | TEST(strings, join_nothing) { |
| 83 | std::vector<std::string> list = {}; |
| 84 | ASSERT_EQ("", android::base::Join(list, ',')); |
| 85 | } |
| 86 | |
| 87 | TEST(strings, join_single) { |
| 88 | std::vector<std::string> list = {"foo"}; |
| 89 | ASSERT_EQ("foo", android::base::Join(list, ',')); |
| 90 | } |
| 91 | |
| 92 | TEST(strings, join_simple) { |
| 93 | std::vector<std::string> list = {"foo", "bar", "baz"}; |
| 94 | ASSERT_EQ("foo,bar,baz", android::base::Join(list, ',')); |
| 95 | } |
| 96 | |
| 97 | TEST(strings, join_separator_in_vector) { |
| 98 | std::vector<std::string> list = {",", ","}; |
| 99 | ASSERT_EQ(",,,", android::base::Join(list, ',')); |
| 100 | } |
| 101 | |
| 102 | TEST(strings, startswith_empty) { |
| 103 | ASSERT_FALSE(android::base::StartsWith("", "foo")); |
| 104 | ASSERT_TRUE(android::base::StartsWith("", "")); |
| 105 | } |
| 106 | |
| 107 | TEST(strings, startswith_simple) { |
| 108 | ASSERT_TRUE(android::base::StartsWith("foo", "")); |
| 109 | ASSERT_TRUE(android::base::StartsWith("foo", "f")); |
| 110 | ASSERT_TRUE(android::base::StartsWith("foo", "fo")); |
| 111 | ASSERT_TRUE(android::base::StartsWith("foo", "foo")); |
| 112 | } |
| 113 | |
| 114 | TEST(strings, startswith_prefix_too_long) { |
| 115 | ASSERT_FALSE(android::base::StartsWith("foo", "foobar")); |
| 116 | } |
| 117 | |
| 118 | TEST(strings, startswith_contains_prefix) { |
| 119 | ASSERT_FALSE(android::base::StartsWith("foobar", "oba")); |
| 120 | ASSERT_FALSE(android::base::StartsWith("foobar", "bar")); |
| 121 | } |
| 122 | |
| 123 | TEST(strings, endswith_empty) { |
| 124 | ASSERT_FALSE(android::base::EndsWith("", "foo")); |
| 125 | ASSERT_TRUE(android::base::EndsWith("", "")); |
| 126 | } |
| 127 | |
| 128 | TEST(strings, endswith_simple) { |
| 129 | ASSERT_TRUE(android::base::EndsWith("foo", "")); |
| 130 | ASSERT_TRUE(android::base::EndsWith("foo", "o")); |
| 131 | ASSERT_TRUE(android::base::EndsWith("foo", "oo")); |
| 132 | ASSERT_TRUE(android::base::EndsWith("foo", "foo")); |
| 133 | } |
| 134 | |
| 135 | TEST(strings, endswith_prefix_too_long) { |
| 136 | ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); |
| 137 | } |
| 138 | |
| 139 | TEST(strings, endswith_contains_prefix) { |
| 140 | ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); |
| 141 | ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); |
| 142 | } |