summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/test/TestArchive.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/test/TestArchive.cpp')
-rw-r--r--xbmc/utils/test/TestArchive.cpp411
1 files changed, 411 insertions, 0 deletions
diff --git a/xbmc/utils/test/TestArchive.cpp b/xbmc/utils/test/TestArchive.cpp
new file mode 100644
index 0000000..65023fd
--- /dev/null
+++ b/xbmc/utils/test/TestArchive.cpp
@@ -0,0 +1,411 @@
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#if defined(TARGET_WINDOWS)
10# include <windows.h>
11#endif
12
13#include "utils/Archive.h"
14#include "utils/Variant.h"
15#include "filesystem/File.h"
16
17#include "test/TestUtils.h"
18
19#include <gtest/gtest.h>
20
21class TestArchive : public testing::Test
22{
23protected:
24 TestArchive()
25 {
26 file = XBMC_CREATETEMPFILE(".ar");
27 }
28 ~TestArchive() override
29 {
30 EXPECT_TRUE(XBMC_DELETETEMPFILE(file));
31 }
32 XFILE::CFile *file;
33};
34
35TEST_F(TestArchive, IsStoring)
36{
37 ASSERT_NE(nullptr, file);
38 CArchive arstore(file, CArchive::store);
39 EXPECT_TRUE(arstore.IsStoring());
40 EXPECT_FALSE(arstore.IsLoading());
41 arstore.Close();
42}
43
44TEST_F(TestArchive, IsLoading)
45{
46 ASSERT_NE(nullptr, file);
47 CArchive arload(file, CArchive::load);
48 EXPECT_TRUE(arload.IsLoading());
49 EXPECT_FALSE(arload.IsStoring());
50 arload.Close();
51}
52
53TEST_F(TestArchive, FloatArchive)
54{
55 ASSERT_NE(nullptr, file);
56 float float_ref = 1, float_var = 0;
57
58 CArchive arstore(file, CArchive::store);
59 arstore << float_ref;
60 arstore.Close();
61
62 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
63 CArchive arload(file, CArchive::load);
64 arload >> float_var;
65 arload.Close();
66
67 EXPECT_EQ(float_ref, float_var);
68}
69
70TEST_F(TestArchive, DoubleArchive)
71{
72 ASSERT_NE(nullptr, file);
73 double double_ref = 2, double_var = 0;
74
75 CArchive arstore(file, CArchive::store);
76 arstore << double_ref;
77 arstore.Close();
78
79 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
80 CArchive arload(file, CArchive::load);
81 arload >> double_var;
82 arload.Close();
83
84 EXPECT_EQ(double_ref, double_var);
85}
86
87TEST_F(TestArchive, IntegerArchive)
88{
89 ASSERT_NE(nullptr, file);
90 int int_ref = 3, int_var = 0;
91
92 CArchive arstore(file, CArchive::store);
93 arstore << int_ref;
94 arstore.Close();
95
96 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
97 CArchive arload(file, CArchive::load);
98 arload >> int_var;
99 arload.Close();
100
101 EXPECT_EQ(int_ref, int_var);
102}
103
104TEST_F(TestArchive, UnsignedIntegerArchive)
105{
106 ASSERT_NE(nullptr, file);
107 unsigned int unsigned_int_ref = 4, unsigned_int_var = 0;
108
109 CArchive arstore(file, CArchive::store);
110 arstore << unsigned_int_ref;
111 arstore.Close();
112
113 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
114 CArchive arload(file, CArchive::load);
115 arload >> unsigned_int_var;
116 arload.Close();
117
118 EXPECT_EQ(unsigned_int_ref, unsigned_int_var);
119}
120
121TEST_F(TestArchive, Int64tArchive)
122{
123 ASSERT_NE(nullptr, file);
124 int64_t int64_t_ref = 5, int64_t_var = 0;
125
126 CArchive arstore(file, CArchive::store);
127 arstore << int64_t_ref;
128 arstore.Close();
129
130 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
131 CArchive arload(file, CArchive::load);
132 arload >> int64_t_var;
133 arload.Close();
134
135 EXPECT_EQ(int64_t_ref, int64_t_var);
136}
137
138TEST_F(TestArchive, UInt64tArchive)
139{
140 ASSERT_NE(nullptr, file);
141 uint64_t uint64_t_ref = 6, uint64_t_var = 0;
142
143 CArchive arstore(file, CArchive::store);
144 arstore << uint64_t_ref;
145 arstore.Close();
146
147 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
148 CArchive arload(file, CArchive::load);
149 arload >> uint64_t_var;
150 arload.Close();
151
152 EXPECT_EQ(uint64_t_ref, uint64_t_var);
153}
154
155TEST_F(TestArchive, BoolArchive)
156{
157 ASSERT_NE(nullptr, file);
158 bool bool_ref = true, bool_var = false;
159
160 CArchive arstore(file, CArchive::store);
161 arstore << bool_ref;
162 arstore.Close();
163
164 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
165 CArchive arload(file, CArchive::load);
166 arload >> bool_var;
167 arload.Close();
168
169 EXPECT_EQ(bool_ref, bool_var);
170}
171
172TEST_F(TestArchive, CharArchive)
173{
174 ASSERT_NE(nullptr, file);
175 char char_ref = 'A', char_var = '\0';
176
177 CArchive arstore(file, CArchive::store);
178 arstore << char_ref;
179 arstore.Close();
180
181 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
182 CArchive arload(file, CArchive::load);
183 arload >> char_var;
184 arload.Close();
185
186 EXPECT_EQ(char_ref, char_var);
187}
188
189TEST_F(TestArchive, WStringArchive)
190{
191 ASSERT_NE(nullptr, file);
192 std::wstring wstring_ref = L"test wstring", wstring_var;
193
194 CArchive arstore(file, CArchive::store);
195 arstore << wstring_ref;
196 arstore.Close();
197
198 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
199 CArchive arload(file, CArchive::load);
200 arload >> wstring_var;
201 arload.Close();
202
203 EXPECT_STREQ(wstring_ref.c_str(), wstring_var.c_str());
204}
205
206TEST_F(TestArchive, StringArchive)
207{
208 ASSERT_NE(nullptr, file);
209 std::string string_ref = "test string", string_var;
210
211 CArchive arstore(file, CArchive::store);
212 arstore << string_ref;
213 arstore.Close();
214
215 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
216 CArchive arload(file, CArchive::load);
217 arload >> string_var;
218 arload.Close();
219
220 EXPECT_STREQ(string_ref.c_str(), string_var.c_str());
221}
222
223TEST_F(TestArchive, SystemTimeArchive)
224{
225 ASSERT_NE(nullptr, file);
226 KODI::TIME::SystemTime SystemTime_ref = {1, 2, 3, 4, 5, 6, 7, 8};
227 KODI::TIME::SystemTime SystemTime_var = {0, 0, 0, 0, 0, 0, 0, 0};
228
229 CArchive arstore(file, CArchive::store);
230 arstore << SystemTime_ref;
231 arstore.Close();
232
233 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
234 CArchive arload(file, CArchive::load);
235 arload >> SystemTime_var;
236 arload.Close();
237
238 EXPECT_TRUE(!memcmp(&SystemTime_ref, &SystemTime_var, sizeof(KODI::TIME::SystemTime)));
239}
240
241TEST_F(TestArchive, CVariantArchive)
242{
243 ASSERT_NE(nullptr, file);
244 CVariant CVariant_ref((int)1), CVariant_var;
245
246 CArchive arstore(file, CArchive::store);
247 arstore << CVariant_ref;
248 arstore.Close();
249
250 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
251 CArchive arload(file, CArchive::load);
252 arload >> CVariant_var;
253 arload.Close();
254
255 EXPECT_TRUE(CVariant_var.isInteger());
256 EXPECT_EQ(1, CVariant_var.asInteger());
257}
258
259TEST_F(TestArchive, CVariantArchiveString)
260{
261 ASSERT_NE(nullptr, file);
262 CVariant CVariant_ref("teststring"), CVariant_var;
263
264 CArchive arstore(file, CArchive::store);
265 arstore << CVariant_ref;
266 arstore.Close();
267
268 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
269 CArchive arload(file, CArchive::load);
270 arload >> CVariant_var;
271 arload.Close();
272
273 EXPECT_TRUE(CVariant_var.isString());
274 EXPECT_STREQ("teststring", CVariant_var.asString().c_str());
275}
276
277TEST_F(TestArchive, StringVectorArchive)
278{
279 ASSERT_NE(nullptr, file);
280 std::vector<std::string> strArray_ref, strArray_var;
281 strArray_ref.emplace_back("test strArray_ref 0");
282 strArray_ref.emplace_back("test strArray_ref 1");
283 strArray_ref.emplace_back("test strArray_ref 2");
284 strArray_ref.emplace_back("test strArray_ref 3");
285
286 CArchive arstore(file, CArchive::store);
287 arstore << strArray_ref;
288 arstore.Close();
289
290 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
291 CArchive arload(file, CArchive::load);
292 arload >> strArray_var;
293 arload.Close();
294
295 EXPECT_STREQ("test strArray_ref 0", strArray_var.at(0).c_str());
296 EXPECT_STREQ("test strArray_ref 1", strArray_var.at(1).c_str());
297 EXPECT_STREQ("test strArray_ref 2", strArray_var.at(2).c_str());
298 EXPECT_STREQ("test strArray_ref 3", strArray_var.at(3).c_str());
299}
300
301TEST_F(TestArchive, IntegerVectorArchive)
302{
303 ASSERT_NE(nullptr, file);
304 std::vector<int> iArray_ref, iArray_var;
305 iArray_ref.push_back(0);
306 iArray_ref.push_back(1);
307 iArray_ref.push_back(2);
308 iArray_ref.push_back(3);
309
310 CArchive arstore(file, CArchive::store);
311 arstore << iArray_ref;
312 arstore.Close();
313
314 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
315 CArchive arload(file, CArchive::load);
316 arload >> iArray_var;
317 arload.Close();
318
319 EXPECT_EQ(0, iArray_var.at(0));
320 EXPECT_EQ(1, iArray_var.at(1));
321 EXPECT_EQ(2, iArray_var.at(2));
322 EXPECT_EQ(3, iArray_var.at(3));
323}
324
325TEST_F(TestArchive, MultiTypeArchive)
326{
327 ASSERT_NE(nullptr, file);
328 float float_ref = 1, float_var = 0;
329 double double_ref = 2, double_var = 0;
330 int int_ref = 3, int_var = 0;
331 unsigned int unsigned_int_ref = 4, unsigned_int_var = 0;
332 int64_t int64_t_ref = 5, int64_t_var = 0;
333 uint64_t uint64_t_ref = 6, uint64_t_var = 0;
334 bool bool_ref = true, bool_var = false;
335 char char_ref = 'A', char_var = '\0';
336 std::string string_ref = "test string", string_var;
337 std::wstring wstring_ref = L"test wstring", wstring_var;
338 KODI::TIME::SystemTime SystemTime_ref = {1, 2, 3, 4, 5, 6, 7, 8};
339 KODI::TIME::SystemTime SystemTime_var = {0, 0, 0, 0, 0, 0, 0, 0};
340 CVariant CVariant_ref((int)1), CVariant_var;
341 std::vector<std::string> strArray_ref, strArray_var;
342 strArray_ref.emplace_back("test strArray_ref 0");
343 strArray_ref.emplace_back("test strArray_ref 1");
344 strArray_ref.emplace_back("test strArray_ref 2");
345 strArray_ref.emplace_back("test strArray_ref 3");
346 std::vector<int> iArray_ref, iArray_var;
347 iArray_ref.push_back(0);
348 iArray_ref.push_back(1);
349 iArray_ref.push_back(2);
350 iArray_ref.push_back(3);
351
352 CArchive arstore(file, CArchive::store);
353 EXPECT_TRUE(arstore.IsStoring());
354 EXPECT_FALSE(arstore.IsLoading());
355 arstore << float_ref;
356 arstore << double_ref;
357 arstore << int_ref;
358 arstore << unsigned_int_ref;
359 arstore << int64_t_ref;
360 arstore << uint64_t_ref;
361 arstore << bool_ref;
362 arstore << char_ref;
363 arstore << string_ref;
364 arstore << wstring_ref;
365 arstore << SystemTime_ref;
366 arstore << CVariant_ref;
367 arstore << strArray_ref;
368 arstore << iArray_ref;
369 arstore.Close();
370
371 ASSERT_EQ(0, file->Seek(0, SEEK_SET));
372 CArchive arload(file, CArchive::load);
373 EXPECT_TRUE(arload.IsLoading());
374 EXPECT_FALSE(arload.IsStoring());
375 arload >> float_var;
376 arload >> double_var;
377 arload >> int_var;
378 arload >> unsigned_int_var;
379 arload >> int64_t_var;
380 arload >> uint64_t_var;
381 arload >> bool_var;
382 arload >> char_var;
383 arload >> string_var;
384 arload >> wstring_var;
385 arload >> SystemTime_var;
386 arload >> CVariant_var;
387 arload >> strArray_var;
388 arload >> iArray_var;
389 arload.Close();
390
391 EXPECT_EQ(float_ref, float_var);
392 EXPECT_EQ(double_ref, double_var);
393 EXPECT_EQ(int_ref, int_var);
394 EXPECT_EQ(unsigned_int_ref, unsigned_int_var);
395 EXPECT_EQ(int64_t_ref, int64_t_var);
396 EXPECT_EQ(uint64_t_ref, uint64_t_var);
397 EXPECT_EQ(bool_ref, bool_var);
398 EXPECT_EQ(char_ref, char_var);
399 EXPECT_STREQ(string_ref.c_str(), string_var.c_str());
400 EXPECT_STREQ(wstring_ref.c_str(), wstring_var.c_str());
401 EXPECT_TRUE(!memcmp(&SystemTime_ref, &SystemTime_var, sizeof(KODI::TIME::SystemTime)));
402 EXPECT_TRUE(CVariant_var.isInteger());
403 EXPECT_STREQ("test strArray_ref 0", strArray_var.at(0).c_str());
404 EXPECT_STREQ("test strArray_ref 1", strArray_var.at(1).c_str());
405 EXPECT_STREQ("test strArray_ref 2", strArray_var.at(2).c_str());
406 EXPECT_STREQ("test strArray_ref 3", strArray_var.at(3).c_str());
407 EXPECT_EQ(0, iArray_var.at(0));
408 EXPECT_EQ(1, iArray_var.at(1));
409 EXPECT_EQ(2, iArray_var.at(2));
410 EXPECT_EQ(3, iArray_var.at(3));
411}