Dan Albert | 00716d7 | 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) { |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 25 | std::vector<std::string> parts = android::base::Split("", ","); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame^] | 26 | ASSERT_EQ(1U, parts.size()); |
| 27 | ASSERT_EQ("", parts[0]); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | TEST(strings, split_single) { |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 31 | std::vector<std::string> parts = android::base::Split("foo", ","); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 32 | ASSERT_EQ(1U, parts.size()); |
| 33 | ASSERT_EQ("foo", parts[0]); |
| 34 | } |
| 35 | |
| 36 | TEST(strings, split_simple) { |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 37 | std::vector<std::string> parts = android::base::Split("foo,bar,baz", ","); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 38 | ASSERT_EQ(3U, parts.size()); |
| 39 | ASSERT_EQ("foo", parts[0]); |
| 40 | ASSERT_EQ("bar", parts[1]); |
| 41 | ASSERT_EQ("baz", parts[2]); |
| 42 | } |
| 43 | |
| 44 | TEST(strings, split_with_empty_part) { |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 45 | std::vector<std::string> parts = android::base::Split("foo,,bar", ","); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame^] | 46 | ASSERT_EQ(3U, parts.size()); |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 47 | ASSERT_EQ("foo", parts[0]); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame^] | 48 | ASSERT_EQ("", parts[1]); |
| 49 | ASSERT_EQ("bar", parts[2]); |
Dan Albert | 0d716d0 | 2015-03-19 13:24:26 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(strings, split_null_char) { |
| 53 | std::vector<std::string> parts = |
| 54 | android::base::Split(std::string("foo\0bar", 7), std::string("\0", 1)); |
| 55 | ASSERT_EQ(2U, parts.size()); |
| 56 | ASSERT_EQ("foo", parts[0]); |
| 57 | ASSERT_EQ("bar", parts[1]); |
| 58 | } |
| 59 | |
| 60 | TEST(strings, split_any) { |
| 61 | std::vector<std::string> parts = android::base::Split("foo:bar,baz", ",:"); |
| 62 | ASSERT_EQ(3U, parts.size()); |
| 63 | ASSERT_EQ("foo", parts[0]); |
| 64 | ASSERT_EQ("bar", parts[1]); |
| 65 | ASSERT_EQ("baz", parts[2]); |
| 66 | } |
| 67 | |
| 68 | TEST(strings, split_any_with_empty_part) { |
| 69 | std::vector<std::string> parts = android::base::Split("foo:,bar", ",:"); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame^] | 70 | ASSERT_EQ(3U, parts.size()); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 71 | ASSERT_EQ("foo", parts[0]); |
Elliott Hughes | 9964efc | 2015-04-24 23:02:00 -0700 | [diff] [blame^] | 72 | ASSERT_EQ("", parts[1]); |
| 73 | ASSERT_EQ("bar", parts[2]); |
Dan Albert | 00716d7 | 2015-03-13 22:57:40 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(strings, trim_empty) { |
| 77 | ASSERT_EQ("", android::base::Trim("")); |
| 78 | } |
| 79 | |
| 80 | TEST(strings, trim_already_trimmed) { |
| 81 | ASSERT_EQ("foo", android::base::Trim("foo")); |
| 82 | } |
| 83 | |
| 84 | TEST(strings, trim_left) { |
| 85 | ASSERT_EQ("foo", android::base::Trim(" foo")); |
| 86 | } |
| 87 | |
| 88 | TEST(strings, trim_right) { |
| 89 | ASSERT_EQ("foo", android::base::Trim("foo ")); |
| 90 | } |
| 91 | |
| 92 | TEST(strings, trim_both) { |
| 93 | ASSERT_EQ("foo", android::base::Trim(" foo ")); |
| 94 | } |
| 95 | |
| 96 | TEST(strings, trim_no_trim_middle) { |
| 97 | ASSERT_EQ("foo bar", android::base::Trim("foo bar")); |
| 98 | } |
| 99 | |
| 100 | TEST(strings, trim_other_whitespace) { |
| 101 | ASSERT_EQ("foo", android::base::Trim("\v\tfoo\n\f")); |
| 102 | } |
| 103 | |
| 104 | TEST(strings, join_nothing) { |
| 105 | std::vector<std::string> list = {}; |
| 106 | ASSERT_EQ("", android::base::Join(list, ',')); |
| 107 | } |
| 108 | |
| 109 | TEST(strings, join_single) { |
| 110 | std::vector<std::string> list = {"foo"}; |
| 111 | ASSERT_EQ("foo", android::base::Join(list, ',')); |
| 112 | } |
| 113 | |
| 114 | TEST(strings, join_simple) { |
| 115 | std::vector<std::string> list = {"foo", "bar", "baz"}; |
| 116 | ASSERT_EQ("foo,bar,baz", android::base::Join(list, ',')); |
| 117 | } |
| 118 | |
| 119 | TEST(strings, join_separator_in_vector) { |
| 120 | std::vector<std::string> list = {",", ","}; |
| 121 | ASSERT_EQ(",,,", android::base::Join(list, ',')); |
| 122 | } |
| 123 | |
| 124 | TEST(strings, startswith_empty) { |
| 125 | ASSERT_FALSE(android::base::StartsWith("", "foo")); |
| 126 | ASSERT_TRUE(android::base::StartsWith("", "")); |
| 127 | } |
| 128 | |
| 129 | TEST(strings, startswith_simple) { |
| 130 | ASSERT_TRUE(android::base::StartsWith("foo", "")); |
| 131 | ASSERT_TRUE(android::base::StartsWith("foo", "f")); |
| 132 | ASSERT_TRUE(android::base::StartsWith("foo", "fo")); |
| 133 | ASSERT_TRUE(android::base::StartsWith("foo", "foo")); |
| 134 | } |
| 135 | |
| 136 | TEST(strings, startswith_prefix_too_long) { |
| 137 | ASSERT_FALSE(android::base::StartsWith("foo", "foobar")); |
| 138 | } |
| 139 | |
| 140 | TEST(strings, startswith_contains_prefix) { |
| 141 | ASSERT_FALSE(android::base::StartsWith("foobar", "oba")); |
| 142 | ASSERT_FALSE(android::base::StartsWith("foobar", "bar")); |
| 143 | } |
| 144 | |
| 145 | TEST(strings, endswith_empty) { |
| 146 | ASSERT_FALSE(android::base::EndsWith("", "foo")); |
| 147 | ASSERT_TRUE(android::base::EndsWith("", "")); |
| 148 | } |
| 149 | |
| 150 | TEST(strings, endswith_simple) { |
| 151 | ASSERT_TRUE(android::base::EndsWith("foo", "")); |
| 152 | ASSERT_TRUE(android::base::EndsWith("foo", "o")); |
| 153 | ASSERT_TRUE(android::base::EndsWith("foo", "oo")); |
| 154 | ASSERT_TRUE(android::base::EndsWith("foo", "foo")); |
| 155 | } |
| 156 | |
| 157 | TEST(strings, endswith_prefix_too_long) { |
| 158 | ASSERT_FALSE(android::base::EndsWith("foo", "foobar")); |
| 159 | } |
| 160 | |
| 161 | TEST(strings, endswith_contains_prefix) { |
| 162 | ASSERT_FALSE(android::base::EndsWith("foobar", "oba")); |
| 163 | ASSERT_FALSE(android::base::EndsWith("foobar", "foo")); |
| 164 | } |