summaryrefslogtreecommitdiff
path: root/src/components/utils/test/custom_string_test.cc
blob: 3610bfb36ba60065bc9b19aa4d234d53cca6564f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2015, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <iostream>
#include <string>
#include "gtest/gtest.h"
#include "utils/custom_string.h"

namespace custom_str = utils::custom_string;

namespace test {
namespace components {
namespace utils {

std::string CreateMultibyteString(uint8_t* array, size_t array_size) {
  return std::string(array, array + array_size);
}

class CustomStringTest : public ::testing::TestWithParam<std::string> {
 protected:
  static void SetUpTestCase() {
    const size_t kSizeStr = 8;
    uint8_t array[] = {0xD0,
                       0xA2,
                       0xD0,
                       0xB5,
                       0xD1,
                       0x81,
                       0xD1,
                       0x82};  // Array contains russian word "Тест"
    mbstring1_ = CreateMultibyteString(array, kSizeStr);
    mbstring2_ = mbstring1_ + "abc";
    amount_symbols_mbstring1_ = 4;  // amount of symbols from string mbstring1_
    amount_symbols_mbstring2_ = 7;  // amount of symbols from string mbstring2_
    amount_bytes_mbstring1_ = mbstring1_.size();
    amount_bytes_mbstring2_ = mbstring2_.size();
  }

 public:
  static std::string mbstring1_;  // String contains russian word "Тест"
  static std::string
      mbstring2_;  // String contains russian word with ASCII symbols "Тестabc"
  static size_t amount_symbols_mbstring1_;
  static size_t amount_symbols_mbstring2_;
  static size_t amount_bytes_mbstring1_;
  static size_t amount_bytes_mbstring2_;
};

std::string CustomStringTest::mbstring1_ = "";
std::string CustomStringTest::mbstring2_ = "";
size_t CustomStringTest::amount_symbols_mbstring1_ = 0;
size_t CustomStringTest::amount_symbols_mbstring2_ = 0;
size_t CustomStringTest::amount_bytes_mbstring1_ = 0;
size_t CustomStringTest::amount_bytes_mbstring2_ = 0;

TEST_F(CustomStringTest,
       AddASCIIStringToCustomString_ExpectCorrectSizeAndIsASCIIStringEQTrue) {
  std::string str("Test string");
  custom_str::CustomString obj(str);

  EXPECT_EQ(str.size(), obj.size());
  EXPECT_EQ(str.length(), obj.length());
  EXPECT_EQ(str.size(), obj.length_bytes());
  EXPECT_TRUE(obj.is_ascii_string());
  EXPECT_FALSE(obj.empty());
}

TEST_F(CustomStringTest,
       AddEmptyToCustomString_ExpectCorrectSizeAndIsASCIIStringEQTrue) {
  std::string str;
  custom_str::CustomString obj(str);

  EXPECT_EQ(str.size(), obj.size());
  EXPECT_EQ(str.length(), obj.length());
  EXPECT_EQ(str.size(), obj.length_bytes());
  EXPECT_TRUE(obj.is_ascii_string());
  EXPECT_TRUE(obj.empty());
}

TEST_F(CustomStringTest,
       AddEmptyToCustomString_ExpectCorrectWorkOfMethodAsMBString) {
  std::string str;
  custom_str::CustomString obj(str);
  EXPECT_TRUE(str == obj.AsMBString());
}

TEST_F(CustomStringTest,
       AddASCIIStringToCustomString_ExpectCorrectWorkOfMethodAsMBString) {
  std::string str("Test string");
  custom_str::CustomString obj(str);
  EXPECT_TRUE(str == obj.AsMBString());
}

TEST_F(CustomStringTest,
       AddASCIIStringToCustomString_ExpectCorrectWorkOfAppendOperator) {
  custom_str::CustomString obj("Test string");
  custom_str::CustomString obj1("abc");
  custom_str::CustomString obj_empty;
  std::string str("abc");
  std::string str_empty;
  std::string str_result("Test stringabc");
  std::string str_result1("Test string");
  custom_str::CustomString obj_result;
  obj_result = obj + obj1;
  EXPECT_TRUE(str_result == obj_result.AsMBString());
  obj_result = obj + obj_empty;
  EXPECT_TRUE(str_result1 == obj_result.AsMBString());
  obj_result = obj + str;
  EXPECT_TRUE(str_result == obj_result.AsMBString());
  obj_result = obj + str_empty;
  EXPECT_TRUE(str_result1 == obj_result.AsMBString());
}

TEST_F(CustomStringTest,
       AddEmptyStringToCustomString_ExpectCorrectWorkOfAppendOperator) {
  custom_str::CustomString obj_empty;
  custom_str::CustomString obj("abc");
  custom_str::CustomString obj_empty1;
  std::string str("abc");
  std::string str_empty;
  custom_str::CustomString obj_result;
  obj_result = obj_empty + obj;
  EXPECT_TRUE(str == obj_result.AsMBString());
  obj_result = obj_empty + obj_empty1;
  EXPECT_TRUE(str_empty == obj_result.AsMBString());
  obj_result = obj_empty + str;
  EXPECT_TRUE(str == obj_result.AsMBString());
  obj_result = obj_empty + str_empty;
  EXPECT_TRUE(str_empty == obj_result.AsMBString());
}

TEST_F(
    CustomStringTest,
    AddASCIIAndEmptyStringToCustomString_ExpectCorrectWorkOfCompareOperator) {
  custom_str::CustomString obj_empty;
  custom_str::CustomString obj_empty1;
  custom_str::CustomString obj("abc");
  custom_str::CustomString obj1("abc");
  std::string str_empty;
  std::string str("abc");

  EXPECT_TRUE(obj_empty == obj_empty1);
  EXPECT_FALSE(obj_empty == obj1);
  EXPECT_TRUE(obj_empty == str_empty);
  EXPECT_FALSE(obj_empty == str);
  EXPECT_TRUE(obj == obj1);
  EXPECT_TRUE(obj == str);
  EXPECT_FALSE(obj == str_empty);
}

TEST_F(CustomStringTest,
       AddASCIIAndEmptyStringToCustomString_ExpectCorrectWorkOfCompareMethod) {
  custom_str::CustomString obj_empty;
  custom_str::CustomString obj("abc");
  std::string str_empty;
  std::string str("abc");

  EXPECT_TRUE(obj_empty.compare(str_empty) == 0);
  EXPECT_TRUE(obj_empty.compare(str) != 0);
  EXPECT_TRUE(obj_empty.compare("") == 0);
  EXPECT_TRUE(obj_empty.compare("abc") != 0);
  EXPECT_TRUE(obj.compare(str_empty) != 0);
  EXPECT_TRUE(obj.compare(str) == 0);
  EXPECT_TRUE(obj.compare("") != 0);
  EXPECT_TRUE(obj.compare("abc") == 0);
}

TEST_F(CustomStringTest,
       AddASCIIStringToCustomString_ExpectCorrectWorkOfAtMethod) {
  std::string str("abc");
  custom_str::CustomString obj(str);
  for (size_t i = 0; i < obj.size(); ++i) {
    EXPECT_TRUE(obj.at(i) == str[i]);
  }
}

TEST_F(
    CustomStringTest,
    AddASCIIAndEmptyStringToCustomString_ExpectCorrectWorkOfCompareIgnoreCaseMethod) {
  custom_str::CustomString obj_empty;
  custom_str::CustomString obj_empty1;
  custom_str::CustomString obj("abc");
  custom_str::CustomString obj1("AbC");
  custom_str::CustomString obj2("AbCd");

  EXPECT_TRUE(obj_empty.CompareIgnoreCase(obj_empty1));
  EXPECT_FALSE(obj_empty.CompareIgnoreCase(obj));
  EXPECT_TRUE(obj.CompareIgnoreCase(obj1));
  EXPECT_TRUE(obj.CompareIgnoreCase(obj));
  EXPECT_FALSE(obj.CompareIgnoreCase(obj2));
  EXPECT_FALSE(obj.CompareIgnoreCase(obj_empty));
}

TEST_F(CustomStringTest,
       AddASCIIStringToCustomString_ExpectCorrectWorkOfToWStringMethod) {
  custom_str::CustomString obj("abc");
  std::wstring wstr(L"abc");
  EXPECT_TRUE(wstr == obj.ToWString());
}

TEST_F(CustomStringTest,
       AddUTF8StringToCustomString_ExpectCorrectSizeAndIsASCIIStringEQFalse) {
  custom_str::CustomString obj(CustomStringTest::mbstring1_);
  EXPECT_EQ(obj.size(), CustomStringTest::amount_symbols_mbstring1_);
  EXPECT_EQ(obj.length(), CustomStringTest::amount_symbols_mbstring1_);
  EXPECT_EQ(obj.length_bytes(), CustomStringTest::amount_bytes_mbstring1_);
  EXPECT_FALSE(obj.is_ascii_string());
}

TEST_F(
    CustomStringTest,
    AddMixingUTF8WithASCIIStringToCustomString_ExpectCorrectSizeAndIsASCIIStringEQFalse) {
  custom_str::CustomString obj(CustomStringTest::mbstring2_);
  EXPECT_EQ(obj.size(), CustomStringTest::amount_symbols_mbstring2_);
  EXPECT_EQ(obj.length(), CustomStringTest::amount_symbols_mbstring2_);
  EXPECT_EQ(obj.length_bytes(), CustomStringTest::amount_bytes_mbstring2_);
  EXPECT_FALSE(obj.is_ascii_string());
}

TEST_F(
    CustomStringTest,
    AddUTF8StringAndMixingUTF8WithASCIIToCustomString_ExpectCorrectWorkOfMethodAsMBString) {
  custom_str::CustomString obj(CustomStringTest::mbstring1_);
  custom_str::CustomString obj1(CustomStringTest::mbstring2_);
  EXPECT_TRUE(CustomStringTest::mbstring1_ == obj.AsMBString());
  EXPECT_TRUE(CustomStringTest::mbstring2_ == obj1.AsMBString());
}

TEST_F(CustomStringTest,
       AddUTF8StringToCustomString_ExpectCorrectConvertingToWString) {
  custom_str::CustomString obj(CustomStringTest::mbstring1_);
  custom_str::CustomString obj1(CustomStringTest::mbstring2_);
  std::wstring wstr1(L"Тест");
  std::wstring wstr2(L"Тестabc");
  EXPECT_TRUE(wstr1 == obj.ToWString());
  EXPECT_TRUE(wstr2 == obj1.ToWString());
}

TEST_F(
    CustomStringTest,
    AddSameMultiByteStringsToCustomString_ExpectCorrectCaseSensetiveComparing) {
  custom_str::CustomString obj(CustomStringTest::mbstring1_);
  custom_str::CustomString obj_1(CustomStringTest::mbstring1_);
  EXPECT_TRUE(obj == obj_1);
  EXPECT_TRUE(obj == CustomStringTest::mbstring1_);
  EXPECT_EQ(0, obj.compare(CustomStringTest::mbstring1_.c_str()));
  EXPECT_EQ(0, obj.compare(CustomStringTest::mbstring1_));
}

TEST_F(
    CustomStringTest,
    AddDiferenceMultiByteStringsToCustomString_ExpectCorrectCaseSensetiveComparing) {
  custom_str::CustomString obj(CustomStringTest::mbstring1_);
  custom_str::CustomString obj1(CustomStringTest::mbstring2_);
  EXPECT_FALSE(obj == obj1);
  EXPECT_FALSE(obj == CustomStringTest::mbstring2_);
  EXPECT_TRUE(obj.compare(CustomStringTest::mbstring2_) != 0);
}

TEST_F(
    CustomStringTest,
    AddDiferenceMultiByteStringsToCustomString_ExpectCorrectCaseInsensitiveComparing) {
  custom_str::CustomString obj(CustomStringTest::mbstring1_);
  custom_str::CustomString obj1(CustomStringTest::mbstring2_);
  EXPECT_FALSE(obj.CompareIgnoreCase(obj1));
  EXPECT_FALSE(obj.CompareIgnoreCase(CustomStringTest::mbstring2_.c_str()));
}

TEST_F(
    CustomStringTest,
    AddSameMultiByteStringsToCustomString_ExpectCorrectCaseInsensitiveComparing) {
  const size_t kSizeStr = 8;
  uint8_t array[] = {0xD1,
                     0x82,
                     0xD0,
                     0xB5,
                     0xD1,
                     0x81,
                     0xD0,
                     0xA2};  // String contains russian word "тесТ"
  std::string mbstring = CreateMultibyteString(array, kSizeStr);
  custom_str::CustomString obj(CustomStringTest::mbstring1_);
  custom_str::CustomString obj1(mbstring);
  EXPECT_TRUE(obj.CompareIgnoreCase(obj1));
  EXPECT_TRUE(obj.CompareIgnoreCase(mbstring.c_str()));
}

}  // namespace utils
}  // namespace components
}  // namespace test