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