1 // Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <brillo/strings/string_utils.h>
6
7 #include <list>
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include <gtest/gtest.h>
13
14 namespace brillo {
15
TEST(StringUtils,Split)16 TEST(StringUtils, Split) {
17 std::vector<std::string> parts;
18
19 parts = string_utils::Split("", ",", false, false);
20 EXPECT_EQ(0, parts.size());
21
22 parts = string_utils::Split("abc", ",", false, false);
23 EXPECT_EQ(1, parts.size());
24 EXPECT_EQ("abc", parts[0]);
25
26 parts = string_utils::Split(",a,bc , d, ,e, ", ",", true, true);
27 EXPECT_EQ(4, parts.size());
28 EXPECT_EQ("a", parts[0]);
29 EXPECT_EQ("bc", parts[1]);
30 EXPECT_EQ("d", parts[2]);
31 EXPECT_EQ("e", parts[3]);
32
33 parts = string_utils::Split(",a,bc , d, ,e, ", ",", false, true);
34 EXPECT_EQ(6, parts.size());
35 EXPECT_EQ("a", parts[0]);
36 EXPECT_EQ("bc ", parts[1]);
37 EXPECT_EQ(" d", parts[2]);
38 EXPECT_EQ(" ", parts[3]);
39 EXPECT_EQ("e", parts[4]);
40 EXPECT_EQ(" ", parts[5]);
41
42 parts = string_utils::Split(",a,bc , d, ,e, ", ",", true, false);
43 EXPECT_EQ(7, parts.size());
44 EXPECT_EQ("", parts[0]);
45 EXPECT_EQ("a", parts[1]);
46 EXPECT_EQ("bc", parts[2]);
47 EXPECT_EQ("d", parts[3]);
48 EXPECT_EQ("", parts[4]);
49 EXPECT_EQ("e", parts[5]);
50 EXPECT_EQ("", parts[6]);
51
52 parts = string_utils::Split(",a,bc , d, ,e, ", ",", false, false);
53 EXPECT_EQ(7, parts.size());
54 EXPECT_EQ("", parts[0]);
55 EXPECT_EQ("a", parts[1]);
56 EXPECT_EQ("bc ", parts[2]);
57 EXPECT_EQ(" d", parts[3]);
58 EXPECT_EQ(" ", parts[4]);
59 EXPECT_EQ("e", parts[5]);
60 EXPECT_EQ(" ", parts[6]);
61
62 parts = string_utils::Split("abc:=xyz", ":=", false, false);
63 EXPECT_EQ(2, parts.size());
64 EXPECT_EQ("abc", parts[0]);
65 EXPECT_EQ("xyz", parts[1]);
66
67 parts = string_utils::Split("abc", "", false, false);
68 EXPECT_EQ(3, parts.size());
69 EXPECT_EQ("a", parts[0]);
70 EXPECT_EQ("b", parts[1]);
71 EXPECT_EQ("c", parts[2]);
72 }
73
TEST(StringUtils,SplitAtFirst)74 TEST(StringUtils, SplitAtFirst) {
75 std::pair<std::string, std::string> pair;
76
77 pair = string_utils::SplitAtFirst(" 123 : 4 : 56 : 789 ", ":", true);
78 EXPECT_EQ("123", pair.first);
79 EXPECT_EQ("4 : 56 : 789", pair.second);
80
81 pair = string_utils::SplitAtFirst(" 123 : 4 : 56 : 789 ", ":", false);
82 EXPECT_EQ(" 123 ", pair.first);
83 EXPECT_EQ(" 4 : 56 : 789 ", pair.second);
84
85 pair = string_utils::SplitAtFirst("", "=");
86 EXPECT_EQ("", pair.first);
87 EXPECT_EQ("", pair.second);
88
89 pair = string_utils::SplitAtFirst("=", "=");
90 EXPECT_EQ("", pair.first);
91 EXPECT_EQ("", pair.second);
92
93 pair = string_utils::SplitAtFirst("a=", "=");
94 EXPECT_EQ("a", pair.first);
95 EXPECT_EQ("", pair.second);
96
97 pair = string_utils::SplitAtFirst("abc=", "=");
98 EXPECT_EQ("abc", pair.first);
99 EXPECT_EQ("", pair.second);
100
101 pair = string_utils::SplitAtFirst("=a", "=");
102 EXPECT_EQ("", pair.first);
103 EXPECT_EQ("a", pair.second);
104
105 pair = string_utils::SplitAtFirst("=abc=", "=");
106 EXPECT_EQ("", pair.first);
107 EXPECT_EQ("abc=", pair.second);
108
109 pair = string_utils::SplitAtFirst("abc", "=");
110 EXPECT_EQ("abc", pair.first);
111 EXPECT_EQ("", pair.second);
112
113 pair = string_utils::SplitAtFirst("abc:=xyz", ":=");
114 EXPECT_EQ("abc", pair.first);
115 EXPECT_EQ("xyz", pair.second);
116
117 pair = string_utils::SplitAtFirst("abc", "");
118 EXPECT_EQ("", pair.first);
119 EXPECT_EQ("abc", pair.second);
120 }
121
TEST(StringUtils,Join_String)122 TEST(StringUtils, Join_String) {
123 EXPECT_EQ("", string_utils::Join(",", {}));
124 EXPECT_EQ("abc", string_utils::Join(",", {"abc"}));
125 EXPECT_EQ("abc,,xyz", string_utils::Join(",", {"abc", "", "xyz"}));
126 EXPECT_EQ("abc,defg", string_utils::Join(",", {"abc", "defg"}));
127 EXPECT_EQ("1 : 2 : 3", string_utils::Join(" : ", {"1", "2", "3"}));
128 EXPECT_EQ("1:2", string_utils::Join(":", std::set<std::string>{"1", "2"}));
129 EXPECT_EQ("1:2", string_utils::Join(":", std::vector<std::string>{"1", "2"}));
130 EXPECT_EQ("1:2", string_utils::Join(":", std::list<std::string>{"1", "2"}));
131 EXPECT_EQ("123", string_utils::Join("", {"1", "2", "3"}));
132 }
133
TEST(StringUtils,Join_Pair)134 TEST(StringUtils, Join_Pair) {
135 EXPECT_EQ("ab,cd", string_utils::Join(",", "ab", "cd"));
136 EXPECT_EQ("key = value", string_utils::Join(" = ", "key", "value"));
137 }
138
TEST(StringUtils,GetBytesAsString)139 TEST(StringUtils, GetBytesAsString) {
140 EXPECT_EQ("abc", string_utils::GetBytesAsString({'a', 'b', 'c'}));
141 EXPECT_TRUE(string_utils::GetBytesAsString({}).empty());
142 auto str = string_utils::GetBytesAsString({0xFF, 0x00, 0x01, 0x7F, 0x80});
143 ASSERT_EQ(5, str.size());
144 EXPECT_EQ('\xFF', str[0]);
145 EXPECT_EQ('\x00', str[1]);
146 EXPECT_EQ('\x01', str[2]);
147 EXPECT_EQ('\x7F', str[3]);
148 EXPECT_EQ('\x80', str[4]);
149 }
150
TEST(StringUtils,GetStringAsBytes)151 TEST(StringUtils, GetStringAsBytes) {
152 EXPECT_EQ((std::vector<uint8_t>{'a', 'b', 'c'}),
153 string_utils::GetStringAsBytes("abc"));
154 EXPECT_TRUE(string_utils::GetStringAsBytes("").empty());
155 auto buf = string_utils::GetStringAsBytes(std::string{"\x80\0\1\xFF", 4});
156 ASSERT_EQ(4, buf.size());
157 EXPECT_EQ(128, buf[0]);
158 EXPECT_EQ(0, buf[1]);
159 EXPECT_EQ(1, buf[2]);
160 EXPECT_EQ(255, buf[3]);
161 }
162
163 } // namespace brillo
164