Debian Codegraph @

src/test/unittests/base/StringTests.cpp

raw
1 /*
2 * barrier -- mouse and keyboard sharing utility
3 * Copyright (C) 2014-2016 Symless Ltd.
4 *
5 * This package is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * found in the file LICENSE that should have accompanied this file.
8 *
9 * This package is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "base/String.h"
19
20 #include "test/global/gtest.h"
21
22 using namespace barrier;
23
24 TEST(StringTests, format_formatWithArguments_formatedString)
25 {
26 const char* format = "%%%{1}=%{2}";
27 const char* arg1 = "answer";
28 const char* arg2 = "42";
29
30 String result = string::format(format, arg1, arg2);
31
32 EXPECT_EQ("%answer=42", result);
33 }
34
35 TEST(StringTests, findReplaceAll_inputString_replacedString)
36 {
37 String subject = "foobar";
38 String find = "bar";
39 String replace = "baz";
40
41 string::findReplaceAll(subject, find, replace);
42
43 EXPECT_EQ("foobaz", subject);
44 }
45
46 TEST(StringTests, sprintf_formatWithArgument_formatedString)
47 {
48 const char* format = "%s=%d";
49 const char* arg1 = "answer";
50 int arg2 = 42;
51
52 String result = string::sprintf(format, arg1, arg2);
53
54 EXPECT_EQ("answer=42", result);
55 }
56
57 TEST(StringTests, toHex_plaintext_hexString)
58 {
59 std::vector<std::uint8_t> subject{'f', 'o', 'o', 'b', 'a', 'r'};
60 int width = 2;
61
62 EXPECT_EQ("666f6f626172", string::to_hex(subject, width));
63 }
64
65 TEST(StringTests, fromhex_plaintext_string)
66 {
67 auto result = string::from_hex("666f6f626172");
68 std::string expected = "foobar";
69 EXPECT_EQ(result, std::vector<std::uint8_t>(expected.begin(), expected.end()));
70 }
71
72 TEST(StringTests, fromhex_plaintext_string_colons)
73 {
74 auto result = string::from_hex("66:6f:6f:62:61:72");
75 std::string expected = "foobar";
76 EXPECT_EQ(result, std::vector<std::uint8_t>(expected.begin(), expected.end()));
77 }
78
79 TEST(StringTests, fromhex_binary_string)
80 {
81 auto result = string::from_hex("01020304050600fff9");
82 auto expected = std::vector<std::uint8_t>{1, 2, 3, 4, 5, 6, 0, 0xff, 0xf9};
83 EXPECT_EQ(result, expected);
84 }
85
86 TEST(StringTests, fromhex_invalid_string)
87 {
88 EXPECT_TRUE(string::from_hex("66:6").empty());
89 EXPECT_TRUE(string::from_hex("66:612").empty());
90 EXPECT_TRUE(string::from_hex("66:WW").empty());
91 }
92
93 TEST(StringTests, uppercase_lowercaseInput_uppercaseOutput)
94 {
95 String subject = "12foo3BaR";
96
97 string::uppercase(subject);
98
99 EXPECT_EQ("12FOO3BAR", subject);
100 }
101
102 TEST(StringTests, removeChar_inputString_removeAllSpecifiedCharactors)
103 {
104 String subject = "foobar";
105 const char c = 'o';
106
107 string::removeChar(subject, c);
108
109 EXPECT_EQ("fbar", subject);
110 }
111
112 TEST(StringTests, intToString_inputInt_outputString)
113 {
114 size_t value = 123;
115
116 String number = string::sizeTypeToString(value);
117
118 EXPECT_EQ("123", number);
119 }
120
121 TEST(StringTests, stringToUint_inputString_outputInt)
122 {
123 String number = "123";
124
125 size_t value = string::stringToSizeType(number);
126
127 EXPECT_EQ(123, value);
128 }
129
130 TEST(StringTests, splitString_twoSeparator_returnThreeParts)
131 {
132 String string = "stub1:stub2:stub3";
133
134 std::vector<String> results = string::splitString(string, ':');
135
136 EXPECT_EQ(3, results.size());
137 EXPECT_EQ("stub1", results[0]);
138 EXPECT_EQ("stub2", results[1]);
139 EXPECT_EQ("stub3", results[2]);
140 }
141
142 TEST(StringTests, splitString_oneSeparator_returnTwoParts)
143 {
144 String string = "stub1:stub2";
145
146 std::vector<String> results = string::splitString(string, ':');
147
148 EXPECT_EQ(2, results.size());
149 EXPECT_EQ("stub1", results[0]);
150 EXPECT_EQ("stub2", results[1]);
151 }
152
153 TEST(StringTests, splitString_noSeparator_returnOriginalString)
154 {
155 String string = "stub1";
156
157 std::vector<String> results = string::splitString(string, ':');
158
159 EXPECT_EQ(1, results.size());
160 EXPECT_EQ("stub1", results[0]);
161 }
162
163 TEST(StringTests, splitString_emptyString_returnEmptyVector)
164 {
165 String string;
166
167 std::vector<String> results = string::splitString(string, ':');
168
169 EXPECT_EQ(0, results.size());
170 }
171
172 TEST(StringTests, splitString_tailSeparator_returnTwoParts)
173 {
174 String string = "stub1:stub2:";
175
176 std::vector<String> results = string::splitString(string, ':');
177
178 EXPECT_EQ(2, results.size());
179 EXPECT_EQ("stub1", results[0]);
180 EXPECT_EQ("stub2", results[1]);
181 }
182
183 TEST(StringTests, splitString_headSeparator_returnTwoParts)
184 {
185 String string = ":stub1:stub2";
186
187 std::vector<String> results = string::splitString(string, ':');
188
189 EXPECT_EQ(2, results.size());
190 EXPECT_EQ("stub1", results[0]);
191 EXPECT_EQ("stub2", results[1]);
192 }
193
194 TEST(StringTests, splitString_headAndTailSeparators_returnTwoParts)
195 {
196 String string = ":stub1:stub2:";
197
198 std::vector<String> results = string::splitString(string, ':');
199
200 EXPECT_EQ(2, results.size());
201 EXPECT_EQ("stub1", results[0]);
202 EXPECT_EQ("stub2", results[1]);
203 }