VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTCRest-1.cpp@ 74263

Last change on this file since 74263 was 74263, checked in by vboxsync, 6 years ago

IPRT/rest: Reimplement RTCRestArrayBase::fromString and adjusted response header handling of arrays to deal with multiple headers for the same field (just in case). bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 99.6 KB
Line 
1/* $Id: tstRTCRest-1.cpp 74263 2018-09-14 12:53:18Z vboxsync $ */
2/** @file
3 * IPRT Testcase - REST C++ classes.
4 */
5
6/*
7 * Copyright (C) 2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/cpp/restbase.h>
32#include <iprt/cpp/restarray.h>
33#include <iprt/cpp/reststringmap.h>
34#include <iprt/cpp/restclient.h>
35
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/message.h>
39#include <iprt/string.h>
40#include <iprt/test.h>
41
42#include <float.h> /* DBL_MIN, DBL_MAX */
43
44
45/*********************************************************************************************************************************
46* Global Variables *
47*********************************************************************************************************************************/
48static RTTEST g_hTest;
49
50
51static char *toJson(RTCRestObjectBase const *pObj)
52{
53 RTCString str;
54 RTCRestOutputToString Dst(&str, false);
55 pObj->serializeAsJson(Dst);
56
57 static char s_szReturnBuffer[4096];
58 RTStrCopy(s_szReturnBuffer, sizeof(s_szReturnBuffer), str.c_str());
59 return s_szReturnBuffer;
60}
61
62
63static int deserializeFromJson(RTCRestObjectBase *pObj, const char *pszJson, PRTERRINFOSTATIC pErrInfo, const char *pszName)
64{
65 RTJSONVAL hValue;
66 RTTESTI_CHECK_RC_OK_RET(RTJsonParseFromString(&hValue, pszJson, pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL), rcCheck);
67 RTCRestJsonPrimaryCursor Cursor(hValue, pszName, pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL);
68 return pObj->deserializeFromJson(Cursor.m_Cursor);
69}
70
71
72static int fromString(RTCRestObjectBase *pObj, const char *pszString, PRTERRINFOSTATIC pErrInfo, const char *pszName)
73{
74 RTCString strValue(pszString);
75 return pObj->fromString(strValue, pszName, pErrInfo ? RTErrInfoInitStatic(pErrInfo) : NULL);
76}
77
78
79static void testBool(void)
80{
81 RTTestSub(g_hTest, "RTCRestBool");
82
83 {
84 RTCRestBool obj1;
85 RTTESTI_CHECK(obj1.m_fValue == false);
86 RTTESTI_CHECK(obj1.isNull() == false);
87 RTTESTI_CHECK(strcmp(obj1.typeName(), "bool") == 0);
88 RTTESTI_CHECK(obj1.typeClass() == RTCRestObjectBase::kTypeClass_Bool);
89 }
90
91 {
92 RTCRestBool obj2(true);
93 RTTESTI_CHECK(obj2.m_fValue == true);
94 RTTESTI_CHECK(obj2.isNull() == false);
95 }
96
97 {
98 RTCRestBool obj2(false);
99 RTTESTI_CHECK(obj2.m_fValue == false);
100 RTTESTI_CHECK(obj2.isNull() == false);
101 }
102
103 {
104 /* Value assignments: */
105 RTCRestBool obj3;
106 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
107 RTTESTI_CHECK(obj3.isNull() == true);
108 obj3.assignValue(true);
109 RTTESTI_CHECK(obj3.m_fValue == true);
110 RTTESTI_CHECK(obj3.isNull() == false);
111
112 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
113 RTTESTI_CHECK(obj3.isNull() == true);
114 obj3.assignValue(false);
115 RTTESTI_CHECK(obj3.m_fValue == false);
116 RTTESTI_CHECK(obj3.isNull() == false);
117
118 obj3.assignValue(true);
119 RTTESTI_CHECK(obj3.m_fValue == true);
120 RTTESTI_CHECK(obj3.isNull() == false);
121
122 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
123 RTTESTI_CHECK(obj3.m_fValue == false);
124 RTTESTI_CHECK(obj3.isNull() == false);
125
126 obj3.assignValue(true);
127 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
128 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
129 RTTESTI_CHECK(obj3.m_fValue == false);
130 RTTESTI_CHECK(obj3.isNull() == false);
131
132 /* Copy assignments: */
133 RTCRestBool obj3True(true);
134 RTTESTI_CHECK(obj3True.m_fValue == true);
135 RTTESTI_CHECK(obj3True.isNull() == false);
136 RTCRestBool obj3False(false);
137 RTTESTI_CHECK(obj3False.m_fValue == false);
138 RTTESTI_CHECK(obj3False.isNull() == false);
139 RTCRestBool obj3Null;
140 obj3Null.setNull();
141 RTTESTI_CHECK(obj3Null.m_fValue == false);
142 RTTESTI_CHECK(obj3Null.isNull() == true);
143
144 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
145 RTTESTI_CHECK_RC(obj3.assignCopy(obj3True), VINF_SUCCESS);
146 RTTESTI_CHECK(obj3.m_fValue == true);
147 RTTESTI_CHECK(obj3.isNull() == false);
148
149 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Null), VINF_SUCCESS);
150 RTTESTI_CHECK(obj3.m_fValue == false);
151 RTTESTI_CHECK(obj3.isNull() == true);
152
153 RTTESTI_CHECK_RC(obj3.assignCopy(obj3False), VINF_SUCCESS);
154 RTTESTI_CHECK(obj3.m_fValue == false);
155 RTTESTI_CHECK(obj3.isNull() == false);
156
157 obj3 = obj3Null;
158 RTTESTI_CHECK(obj3.m_fValue == false);
159 RTTESTI_CHECK(obj3.isNull() == true);
160
161 obj3 = obj3True;
162 RTTESTI_CHECK(obj3.m_fValue == true);
163 RTTESTI_CHECK(obj3.isNull() == false);
164
165 obj3 = obj3Null;
166 RTTESTI_CHECK(obj3.m_fValue == false);
167 RTTESTI_CHECK(obj3.isNull() == true);
168
169 obj3 = obj3False;
170 RTTESTI_CHECK(obj3.m_fValue == false);
171 RTTESTI_CHECK(obj3.isNull() == false);
172
173 /* setNull implies resetToDefault: */
174 obj3 = obj3True;
175 RTTESTI_CHECK(obj3.m_fValue == true);
176 RTTESTI_CHECK(obj3.isNull() == false);
177 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
178 RTTESTI_CHECK(obj3.isNull() == true);
179 RTTESTI_CHECK(obj3.m_fValue == false);
180
181 /* Copy constructors: */
182 {
183 RTCRestBool obj3a(obj3True);
184 RTTESTI_CHECK(obj3a.m_fValue == true);
185 RTTESTI_CHECK(obj3a.isNull() == false);
186 }
187 {
188 RTCRestBool obj3b(obj3False);
189 RTTESTI_CHECK(obj3b.m_fValue == false);
190 RTTESTI_CHECK(obj3b.isNull() == false);
191 }
192 {
193 RTCRestBool obj3c(obj3Null);
194 RTTESTI_CHECK(obj3c.m_fValue == false);
195 RTTESTI_CHECK(obj3c.isNull() == true);
196 }
197
198 /* Serialization to json: */
199 const char *pszJson = toJson(&obj3True);
200 RTTESTI_CHECK_MSG(strcmp(pszJson, "true") == 0, ("pszJson=%s\n", pszJson));
201 pszJson = toJson(&obj3False);
202 RTTESTI_CHECK_MSG(strcmp(pszJson, "false") == 0, ("pszJson=%s\n", pszJson));
203 pszJson = toJson(&obj3Null);
204 RTTESTI_CHECK_MSG(strcmp(pszJson, "null") == 0, ("pszJson=%s\n", pszJson));
205
206 /* Serialization to string. */
207 RTCString str;
208 str = "lead-in:";
209 RTTESTI_CHECK_RC(obj3True.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
210 RTTESTI_CHECK_MSG(str.equals("lead-in:true"), ("str=%s\n", str.c_str()));
211 RTTESTI_CHECK_RC(obj3True.toString(&str), VINF_SUCCESS);
212 RTTESTI_CHECK_MSG(str.equals("true"), ("str=%s\n", str.c_str()));
213
214 str = "lead-in:";
215 RTTESTI_CHECK_RC(obj3False.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
216 RTTESTI_CHECK_MSG(str.equals("lead-in:false"), ("str=%s\n", str.c_str()));
217 RTTESTI_CHECK_RC(obj3False.toString(&str), VINF_SUCCESS);
218 RTTESTI_CHECK_MSG(str.equals("false"), ("str=%s\n", str.c_str()));
219
220 str = "lead-in:";
221 RTTESTI_CHECK_RC(obj3Null.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
222 RTTESTI_CHECK_MSG(str.equals("lead-in:null"), ("str=%s\n", str.c_str()));
223 RTTESTI_CHECK_RC(obj3Null.toString(&str), VINF_SUCCESS);
224 RTTESTI_CHECK_MSG(str.equals("null"), ("str=%s\n", str.c_str()));
225 }
226
227 /* deserialize: */
228 RTERRINFOSTATIC ErrInfo;
229 {
230 RTCRestBool obj4;
231 obj4.setNull();
232 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "false", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
233 RTTESTI_CHECK(obj4.m_fValue == false);
234 RTTESTI_CHECK(obj4.isNull() == false);
235
236 obj4.setNull();
237 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "true", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
238 RTTESTI_CHECK(obj4.m_fValue == true);
239 RTTESTI_CHECK(obj4.isNull() == false);
240
241 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
242 RTTESTI_CHECK(obj4.m_fValue == false);
243 RTTESTI_CHECK(obj4.isNull() == true);
244
245 /* object goes to default state on failure: */
246 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "0", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_BOOL);
247 RTTESTI_CHECK(obj4.m_fValue == false);
248 RTTESTI_CHECK(obj4.isNull() == false);
249 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
250
251 obj4.assignValue(true);
252 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"false\"", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_BOOL);
253 RTTESTI_CHECK(obj4.m_fValue == false);
254 RTTESTI_CHECK(obj4.isNull() == false);
255 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
256
257 obj4.setNull();
258 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "[ null ]", NULL, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_BOOL);
259 RTTESTI_CHECK(obj4.m_fValue == false);
260 RTTESTI_CHECK(obj4.isNull() == false);
261
262 obj4.setNull();
263 RTTESTI_CHECK_RC(fromString(&obj4, "true", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
264 RTTESTI_CHECK(obj4.m_fValue == true);
265 RTTESTI_CHECK(obj4.isNull() == false);
266
267 RTTESTI_CHECK_RC(fromString(&obj4, "false", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
268 RTTESTI_CHECK(obj4.m_fValue == false);
269 RTTESTI_CHECK(obj4.isNull() == false);
270
271 obj4.m_fValue = true;
272 RTTESTI_CHECK_RC(fromString(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
273 RTTESTI_CHECK(obj4.m_fValue == false);
274 RTTESTI_CHECK(obj4.isNull() == true);
275
276 obj4.setNull();
277 RTTESTI_CHECK_RC(fromString(&obj4, " TrUe ", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
278 RTTESTI_CHECK(obj4.m_fValue == true);
279 RTTESTI_CHECK(obj4.isNull() == false);
280
281 RTTESTI_CHECK_RC(fromString(&obj4, "\tfAlSe;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
282 RTTESTI_CHECK(obj4.m_fValue == false);
283 RTTESTI_CHECK(obj4.isNull() == false);
284
285 RTTESTI_CHECK_RC(fromString(&obj4, "\r\nfAlSe\n;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
286 RTTESTI_CHECK(obj4.m_fValue == false);
287 RTTESTI_CHECK(obj4.isNull() == false);
288
289 RTTESTI_CHECK_RC(fromString(&obj4, "\r\tNuLl\n;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
290 RTTESTI_CHECK(obj4.m_fValue == false);
291 RTTESTI_CHECK(obj4.isNull() == true);
292
293 RTTESTI_CHECK_RC(fromString(&obj4, "1", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_UNABLE_TO_PARSE_STRING_AS_BOOL);
294 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
295
296 RTTESTI_CHECK_RC(fromString(&obj4, "0", NULL, RT_XSTR(__LINE__)), VERR_REST_UNABLE_TO_PARSE_STRING_AS_BOOL);
297 }
298}
299
300class Int64Constants
301{
302public:
303 Int64Constants() {}
304 const char *getSubName() const { return "RTCRestInt64"; }
305 int64_t getMin() const { return INT64_MIN; }
306 const char *getMinStr() const { return "-9223372036854775808"; }
307 int64_t getMax() const { return INT64_MAX; }
308 const char *getMaxStr() const { return "9223372036854775807"; }
309 const char *getTypeName() const { return "int64_t"; }
310 RTCRestObjectBase::kTypeClass getTypeClass() const { return RTCRestObjectBase::kTypeClass_Int64; }
311};
312
313class Int32Constants
314{
315public:
316 Int32Constants() { }
317 const char *getSubName() const { return "RTCRestInt32"; }
318 int32_t getMin() const { return INT32_MIN; }
319 const char *getMinStr() const { return "-2147483648"; }
320 int32_t getMax() const { return INT32_MAX; }
321 const char *getMaxStr() const { return "2147483647"; }
322 const char *getTypeName() const { return "int32_t"; }
323 RTCRestObjectBase::kTypeClass getTypeClass() const { return RTCRestObjectBase::kTypeClass_Int32; }
324};
325
326class Int16Constants
327{
328public:
329 Int16Constants() { }
330 const char *getSubName() const { return "RTCRestInt16"; }
331 int16_t getMin() const { return INT16_MIN; }
332 const char *getMinStr() const { return "-32768"; }
333 int16_t getMax() const { return INT16_MAX; }
334 const char *getMaxStr() const { return "32767"; }
335 const char *getTypeName() const { return "int16_t"; }
336 RTCRestObjectBase::kTypeClass getTypeClass() const { return RTCRestObjectBase::kTypeClass_Int16; }
337};
338
339template<typename RestType, typename IntType, typename ConstantClass>
340void testInteger(void)
341{
342 ConstantClass const Consts;
343 RTTestSub(g_hTest, Consts.getSubName());
344
345 {
346 RestType obj1;
347 RTTESTI_CHECK(obj1.m_iValue == 0);
348 RTTESTI_CHECK(obj1.isNull() == false);
349 RTTESTI_CHECK(strcmp(obj1.typeName(), Consts.getTypeName()) == 0);
350 RTTESTI_CHECK(obj1.typeClass() == Consts.getTypeClass());
351 }
352
353 {
354 RestType obj2(2398);
355 RTTESTI_CHECK(obj2.m_iValue == 2398);
356 RTTESTI_CHECK(obj2.isNull() == false);
357 }
358
359 {
360 RestType obj2(-7345);
361 RTTESTI_CHECK(obj2.m_iValue == -7345);
362 RTTESTI_CHECK(obj2.isNull() == false);
363 }
364
365 {
366 /* Value assignments: */
367 RestType obj3;
368 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
369 RTTESTI_CHECK(obj3.isNull() == true);
370 RTTESTI_CHECK(obj3.m_iValue == 0);
371 obj3.assignValue(-1);
372 RTTESTI_CHECK(obj3.m_iValue == -1);
373 RTTESTI_CHECK(obj3.isNull() == false);
374
375 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
376 RTTESTI_CHECK(obj3.isNull() == true);
377 obj3.assignValue(42);
378 RTTESTI_CHECK(obj3.m_iValue == 42);
379 RTTESTI_CHECK(obj3.isNull() == false);
380
381 obj3.assignValue(Consts.getMax());
382 RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
383 RTTESTI_CHECK(obj3.isNull() == false);
384
385 obj3.assignValue(Consts.getMin());
386 RTTESTI_CHECK(obj3.m_iValue == Consts.getMin());
387 RTTESTI_CHECK(obj3.isNull() == false);
388
389 /* Reset to default: */
390 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
391 RTTESTI_CHECK(obj3.m_iValue == 0);
392 RTTESTI_CHECK(obj3.isNull() == false);
393
394 obj3.assignValue(42);
395 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
396 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
397 RTTESTI_CHECK(obj3.m_iValue == 0);
398 RTTESTI_CHECK(obj3.isNull() == false);
399
400 /* Copy assignments: */
401 RestType obj3Max(Consts.getMax());
402 RTTESTI_CHECK(obj3Max.m_iValue == Consts.getMax());
403 RTTESTI_CHECK(obj3Max.isNull() == false);
404 RestType obj3Min(Consts.getMin());
405 RTTESTI_CHECK(obj3Min.m_iValue == Consts.getMin());
406 RTTESTI_CHECK(obj3Min.isNull() == false);
407 RestType obj3Null;
408 obj3Null.setNull();
409 RTTESTI_CHECK(obj3Null.m_iValue == 0);
410 RTTESTI_CHECK(obj3Null.isNull() == true);
411
412 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
413 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Max), VINF_SUCCESS);
414 RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
415 RTTESTI_CHECK(obj3.isNull() == false);
416
417 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Null), VINF_SUCCESS);
418 RTTESTI_CHECK(obj3.m_iValue == 0);
419 RTTESTI_CHECK(obj3.isNull() == true);
420
421 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Min), VINF_SUCCESS);
422 RTTESTI_CHECK(obj3.m_iValue == Consts.getMin());
423 RTTESTI_CHECK(obj3.isNull() == false);
424
425 obj3 = obj3Null;
426 RTTESTI_CHECK(obj3.m_iValue == 0);
427 RTTESTI_CHECK(obj3.isNull() == true);
428
429 obj3 = obj3Max;
430 RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
431 RTTESTI_CHECK(obj3.isNull() == false);
432
433 obj3 = obj3Null;
434 RTTESTI_CHECK(obj3.m_iValue == 0);
435 RTTESTI_CHECK(obj3.isNull() == true);
436
437 obj3 = obj3Min;
438 RTTESTI_CHECK(obj3.m_iValue == Consts.getMin());
439 RTTESTI_CHECK(obj3.isNull() == false);
440
441 /* setNull implies resetToDefault: */
442 obj3 = obj3Max;
443 RTTESTI_CHECK(obj3.m_iValue == Consts.getMax());
444 RTTESTI_CHECK(obj3.isNull() == false);
445 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
446 RTTESTI_CHECK(obj3.isNull() == true);
447 RTTESTI_CHECK(obj3.m_iValue == 0);
448
449 /* Copy constructors: */
450 {
451 RestType obj3a(obj3Max);
452 RTTESTI_CHECK(obj3a.m_iValue == Consts.getMax());
453 RTTESTI_CHECK(obj3a.isNull() == false);
454 }
455 {
456 RestType obj3b(obj3Min);
457 RTTESTI_CHECK(obj3b.m_iValue == Consts.getMin());
458 RTTESTI_CHECK(obj3b.isNull() == false);
459 }
460 {
461 RestType obj3c(obj3Null);
462 RTTESTI_CHECK(obj3c.m_iValue == 0);
463 RTTESTI_CHECK(obj3c.isNull() == true);
464 }
465
466 /* Serialization to json: */
467 const char *pszJson = toJson(&obj3Max);
468 RTTESTI_CHECK_MSG(strcmp(pszJson, Consts.getMaxStr()) == 0, ("pszJson=%s\n", pszJson));
469 pszJson = toJson(&obj3Min);
470 RTTESTI_CHECK_MSG(strcmp(pszJson, Consts.getMinStr()) == 0, ("pszJson=%s\n", pszJson));
471 pszJson = toJson(&obj3Null);
472 RTTESTI_CHECK_MSG(strcmp(pszJson, "null") == 0, ("pszJson=%s\n", pszJson));
473
474 /* Serialization to string. */
475 RTCString str;
476 RTCString strExpect;
477 str = "lead-in:";
478 RTTESTI_CHECK_RC(obj3Max.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
479 strExpect.printf("lead-in:%s", Consts.getMaxStr());
480 RTTESTI_CHECK_MSG(str.equals(strExpect), ("str=%s strExpect=%s\n", str.c_str(), strExpect.c_str()));
481 RTTESTI_CHECK_RC(obj3Max.toString(&str), VINF_SUCCESS);
482 RTTESTI_CHECK_MSG(str.equals(Consts.getMaxStr()), ("str=%s\n", str.c_str()));
483
484 str = "lead-in:";
485 RTTESTI_CHECK_RC(obj3Min.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
486 strExpect.printf("lead-in:%s", Consts.getMinStr());
487 RTTESTI_CHECK_MSG(str.equals(strExpect), ("str=%s strExpect=%s\n", str.c_str(), strExpect.c_str()));
488 RTTESTI_CHECK_RC(obj3Min.toString(&str), VINF_SUCCESS);
489 RTTESTI_CHECK_MSG(str.equals(Consts.getMinStr()), ("str=%s\n", str.c_str()));
490
491 str = "lead-in:";
492 RTTESTI_CHECK_RC(obj3Null.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
493 RTTESTI_CHECK_MSG(str.equals("lead-in:null"), ("str=%s\n", str.c_str()));
494 RTTESTI_CHECK_RC(obj3Null.toString(&str), VINF_SUCCESS);
495 RTTESTI_CHECK_MSG(str.equals("null"), ("str=%s\n", str.c_str()));
496 }
497
498 /* deserialize: */
499 RTERRINFOSTATIC ErrInfo;
500 {
501 /* from json: */
502 RestType obj4;
503 obj4.setNull();
504 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "42", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
505 RTTESTI_CHECK(obj4.m_iValue == 42);
506 RTTESTI_CHECK(obj4.isNull() == false);
507
508 obj4.setNull();
509 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "-22", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
510 RTTESTI_CHECK(obj4.m_iValue == -22);
511 RTTESTI_CHECK(obj4.isNull() == false);
512
513 obj4.setNull();
514 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, Consts.getMaxStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
515 RTTESTI_CHECK(obj4.m_iValue == Consts.getMax());
516 RTTESTI_CHECK(obj4.isNull() == false);
517
518 obj4.setNull();
519 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, Consts.getMinStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
520 RTTESTI_CHECK(obj4.m_iValue == Consts.getMin());
521 RTTESTI_CHECK(obj4.isNull() == false);
522
523 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
524 RTTESTI_CHECK(obj4.m_iValue == 0);
525 RTTESTI_CHECK(obj4.isNull() == true);
526
527 /* object goes to default state on failure: */
528 obj4.assignValue(Consts.getMin());
529 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "0.0", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_INTEGER);
530 RTTESTI_CHECK(obj4.m_iValue == 0);
531 RTTESTI_CHECK(obj4.isNull() == false);
532 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
533
534 obj4.assignValue(Consts.getMax());
535 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"false\"", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_INTEGER);
536 RTTESTI_CHECK(obj4.m_iValue == 0);
537 RTTESTI_CHECK(obj4.isNull() == false);
538 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
539
540 obj4.setNull();
541 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "[ null ]", NULL, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_INTEGER);
542 RTTESTI_CHECK(obj4.m_iValue == 0);
543 RTTESTI_CHECK(obj4.isNull() == false);
544
545 /* from string: */
546 obj4.setNull();
547 RTTESTI_CHECK_RC(fromString(&obj4, "22", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
548 RTTESTI_CHECK(obj4.m_iValue == 22);
549 RTTESTI_CHECK(obj4.isNull() == false);
550
551 RTTESTI_CHECK_RC(fromString(&obj4, "-42", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
552 RTTESTI_CHECK(obj4.m_iValue == -42);
553 RTTESTI_CHECK(obj4.isNull() == false);
554
555 RTTESTI_CHECK_RC(fromString(&obj4, Consts.getMaxStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
556 RTTESTI_CHECK(obj4.m_iValue == Consts.getMax());
557 RTTESTI_CHECK(obj4.isNull() == false);
558
559 RTTESTI_CHECK_RC(fromString(&obj4, Consts.getMinStr(), &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
560 RTTESTI_CHECK(obj4.m_iValue == Consts.getMin());
561 RTTESTI_CHECK(obj4.isNull() == false);
562
563 obj4.m_iValue = 33;
564 RTTESTI_CHECK_RC(fromString(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
565 RTTESTI_CHECK(obj4.m_iValue == 0);
566 RTTESTI_CHECK(obj4.isNull() == true);
567
568 obj4.m_iValue = 33;
569 RTTESTI_CHECK_RC(fromString(&obj4, " nULl;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
570 RTTESTI_CHECK(obj4.m_iValue == 0);
571 RTTESTI_CHECK(obj4.isNull() == true);
572
573 obj4.setNull();
574 RTTESTI_CHECK_RC(fromString(&obj4, " 0x42 ", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
575 RTTESTI_CHECK(obj4.m_iValue == 0x42);
576 RTTESTI_CHECK(obj4.isNull() == false);
577
578 RTTESTI_CHECK_RC(fromString(&obj4, "\t010\t", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
579 RTTESTI_CHECK(obj4.m_iValue == 8);
580 RTTESTI_CHECK(obj4.isNull() == false);
581
582 RTTESTI_CHECK_RC(fromString(&obj4, "\r\t0X4FDB\t", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
583 RTTESTI_CHECK(obj4.m_iValue == 0x4fdb);
584 RTTESTI_CHECK(obj4.isNull() == false);
585
586 RTTESTI_CHECK_RC(fromString(&obj4, "1.1", &ErrInfo, RT_XSTR(__LINE__)), VERR_TRAILING_CHARS);
587 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
588
589 RTTESTI_CHECK_RC(fromString(&obj4, "false", NULL, RT_XSTR(__LINE__)), VERR_NO_DIGITS);
590 }
591}
592
593
594void testDouble(void)
595{
596 RTTestSub(g_hTest, "RTCRestDouble");
597#define DBL_MAX_STRING "1.7976931348623157e+308"
598#define DBL_MIN_STRING "2.2250738585072014e-308"
599
600 {
601 RTCRestDouble obj1;
602 RTTESTI_CHECK(obj1.m_rdValue == 0.0);
603 RTTESTI_CHECK(obj1.isNull() == false);
604 RTTESTI_CHECK(strcmp(obj1.typeName(), "double") == 0);
605 RTTESTI_CHECK(obj1.typeClass() == RTCRestObjectBase::kTypeClass_Double);
606 }
607
608 {
609 RTCRestDouble obj2(2398.1);
610 RTTESTI_CHECK(obj2.m_rdValue == 2398.1);
611 RTTESTI_CHECK(obj2.isNull() == false);
612 }
613
614 {
615 RTCRestDouble obj2(-7345.2);
616 RTTESTI_CHECK(obj2.m_rdValue == -7345.2);
617 RTTESTI_CHECK(obj2.isNull() == false);
618 }
619
620 {
621 /* Value assignments: */
622 RTCRestDouble obj3;
623 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
624 RTTESTI_CHECK(obj3.isNull() == true);
625 RTTESTI_CHECK(obj3.m_rdValue == 0.0);
626 obj3.assignValue(-1.0);
627 RTTESTI_CHECK(obj3.m_rdValue == -1.0);
628 RTTESTI_CHECK(obj3.isNull() == false);
629
630 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
631 RTTESTI_CHECK(obj3.isNull() == true);
632 obj3.assignValue(42.42);
633 RTTESTI_CHECK(obj3.m_rdValue == 42.42);
634 RTTESTI_CHECK(obj3.isNull() == false);
635
636 obj3.assignValue(DBL_MAX);
637 RTTESTI_CHECK(obj3.m_rdValue == DBL_MAX);
638 RTTESTI_CHECK(obj3.isNull() == false);
639
640 obj3.assignValue(DBL_MIN);
641 RTTESTI_CHECK(obj3.m_rdValue == DBL_MIN);
642 RTTESTI_CHECK(obj3.isNull() == false);
643
644 /* Reset to default: */
645 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
646 RTTESTI_CHECK(obj3.m_rdValue == 0.0);
647 RTTESTI_CHECK(obj3.isNull() == false);
648
649 obj3.assignValue(42);
650 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
651 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
652 RTTESTI_CHECK(obj3.m_rdValue == 0.0);
653 RTTESTI_CHECK(obj3.isNull() == false);
654
655 /* Copy assignments: */
656 RTCRestDouble obj3Max(DBL_MAX);
657 RTTESTI_CHECK(obj3Max.m_rdValue == DBL_MAX);
658 RTTESTI_CHECK(obj3Max.isNull() == false);
659 RTCRestDouble obj3Min(DBL_MIN);
660 RTTESTI_CHECK(obj3Min.m_rdValue == DBL_MIN);
661 RTTESTI_CHECK(obj3Min.isNull() == false);
662 RTCRestDouble obj3Null;
663 obj3Null.setNull();
664 RTTESTI_CHECK(obj3Null.m_rdValue == 0.0);
665 RTTESTI_CHECK(obj3Null.isNull() == true);
666
667 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
668 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Max), VINF_SUCCESS);
669 RTTESTI_CHECK(obj3.m_rdValue == DBL_MAX);
670 RTTESTI_CHECK(obj3.isNull() == false);
671
672 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Null), VINF_SUCCESS);
673 RTTESTI_CHECK(obj3.m_rdValue == 0.0);
674 RTTESTI_CHECK(obj3.isNull() == true);
675
676 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Min), VINF_SUCCESS);
677 RTTESTI_CHECK(obj3.m_rdValue == DBL_MIN);
678 RTTESTI_CHECK(obj3.isNull() == false);
679
680 obj3 = obj3Null;
681 RTTESTI_CHECK(obj3.m_rdValue == 0.0);
682 RTTESTI_CHECK(obj3.isNull() == true);
683
684 obj3 = obj3Max;
685 RTTESTI_CHECK(obj3.m_rdValue == DBL_MAX);
686 RTTESTI_CHECK(obj3.isNull() == false);
687
688 obj3 = obj3Null;
689 RTTESTI_CHECK(obj3.m_rdValue == 0.0);
690 RTTESTI_CHECK(obj3.isNull() == true);
691
692 obj3 = obj3Min;
693 RTTESTI_CHECK(obj3.m_rdValue == DBL_MIN);
694 RTTESTI_CHECK(obj3.isNull() == false);
695
696 /* setNull implies resetToDefault: */
697 obj3 = obj3Max;
698 RTTESTI_CHECK(obj3.m_rdValue == DBL_MAX);
699 RTTESTI_CHECK(obj3.isNull() == false);
700 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
701 RTTESTI_CHECK(obj3.isNull() == true);
702 RTTESTI_CHECK(obj3.m_rdValue == 0.0);
703
704 /* Copy constructors: */
705 {
706 RTCRestDouble obj3a(obj3Max);
707 RTTESTI_CHECK(obj3a.m_rdValue == DBL_MAX);
708 RTTESTI_CHECK(obj3a.isNull() == false);
709 }
710 {
711 RTCRestDouble obj3b(obj3Min);
712 RTTESTI_CHECK(obj3b.m_rdValue == DBL_MIN);
713 RTTESTI_CHECK(obj3b.isNull() == false);
714 }
715 {
716 RTCRestDouble obj3c(obj3Null);
717 RTTESTI_CHECK(obj3c.m_rdValue == 0.0);
718 RTTESTI_CHECK(obj3c.isNull() == true);
719 }
720
721 /* Serialization to json: */
722 const char *pszJson = toJson(&obj3Max);
723 RTTESTI_CHECK_MSG(strcmp(pszJson, DBL_MAX_STRING) == 0, ("pszJson=%s\n", pszJson));
724 pszJson = toJson(&obj3Min);
725 RTTESTI_CHECK_MSG(strcmp(pszJson, DBL_MIN_STRING) == 0, ("pszJson=%s\n", pszJson));
726 pszJson = toJson(&obj3Null);
727 RTTESTI_CHECK_MSG(strcmp(pszJson, "null") == 0, ("pszJson=%s\n", pszJson));
728
729 /* Serialization to string. */
730 RTCString str;
731 RTCString strExpect;
732 str = "lead-in:";
733 RTTESTI_CHECK_RC(obj3Max.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
734 strExpect.printf("lead-in:%s", DBL_MAX_STRING);
735 RTTESTI_CHECK_MSG(str.equals(strExpect), ("str=%s strExpect=%s\n", str.c_str(), strExpect.c_str()));
736 RTTESTI_CHECK_RC(obj3Max.toString(&str), VINF_SUCCESS);
737 RTTESTI_CHECK_MSG(str.equals(DBL_MAX_STRING), ("str=%s\n", str.c_str()));
738
739 str = "lead-in:";
740 RTTESTI_CHECK_RC(obj3Min.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
741 strExpect.printf("lead-in:%s", DBL_MIN_STRING);
742 RTTESTI_CHECK_MSG(str.equals(strExpect), ("str=%s strExpect=%s\n", str.c_str(), strExpect.c_str()));
743 RTTESTI_CHECK_RC(obj3Min.toString(&str), VINF_SUCCESS);
744 RTTESTI_CHECK_MSG(str.equals(DBL_MIN_STRING), ("str=%s\n", str.c_str()));
745
746 str = "lead-in:";
747 RTTESTI_CHECK_RC(obj3Null.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
748 RTTESTI_CHECK_MSG(str.equals("lead-in:null"), ("str=%s\n", str.c_str()));
749 RTTESTI_CHECK_RC(obj3Null.toString(&str), VINF_SUCCESS);
750 RTTESTI_CHECK_MSG(str.equals("null"), ("str=%s\n", str.c_str()));
751 }
752
753 /* deserialize: */
754 RTERRINFOSTATIC ErrInfo;
755 {
756 /* from json: */
757 RTCRestDouble obj4;
758 obj4.setNull();
759 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "42.42", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
760 RTTESTI_CHECK(obj4.m_rdValue == 42.42);
761 RTTESTI_CHECK(obj4.isNull() == false);
762
763 obj4.setNull();
764 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "-22.22", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
765 RTTESTI_CHECK(obj4.m_rdValue == -22.22);
766 RTTESTI_CHECK(obj4.isNull() == false);
767
768 obj4.setNull();
769 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, DBL_MAX_STRING, &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
770 RTTESTI_CHECK(obj4.m_rdValue == DBL_MAX);
771 RTTESTI_CHECK(obj4.isNull() == false);
772
773 obj4.setNull();
774 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, DBL_MIN_STRING, &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
775 RTTESTI_CHECK(obj4.m_rdValue == DBL_MIN);
776 RTTESTI_CHECK(obj4.isNull() == false);
777
778 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
779 RTTESTI_CHECK(obj4.m_rdValue == 0.0);
780 RTTESTI_CHECK(obj4.isNull() == true);
781
782 obj4.setNull();
783 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "14323", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
784 RTTESTI_CHECK(obj4.m_rdValue == 14323.0);
785 RTTESTI_CHECK(obj4.isNull() == false);
786
787 obj4.setNull();
788 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "-234875", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
789 RTTESTI_CHECK(obj4.m_rdValue == -234875.0);
790 RTTESTI_CHECK(obj4.isNull() == false);
791
792 /* object goes to default state on failure: */
793 obj4.assignValue(DBL_MIN);
794 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "false", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_DOUBLE);
795 RTTESTI_CHECK(obj4.m_rdValue == 0.0);
796 RTTESTI_CHECK(obj4.isNull() == false);
797 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
798
799 obj4.assignValue(DBL_MAX);
800 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"false\"", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_DOUBLE);
801 RTTESTI_CHECK(obj4.m_rdValue == 0.0);
802 RTTESTI_CHECK(obj4.isNull() == false);
803 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
804
805 obj4.setNull();
806 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "[ null ]", NULL, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_DOUBLE);
807 RTTESTI_CHECK(obj4.m_rdValue == 0.0);
808 RTTESTI_CHECK(obj4.isNull() == false);
809
810 /* from string: */
811 obj4.setNull();
812 RTTESTI_CHECK_RC(fromString(&obj4, "22.42", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
813 RTTESTI_CHECK(obj4.m_rdValue == 22.42);
814 RTTESTI_CHECK(obj4.isNull() == false);
815
816 RTTESTI_CHECK_RC(fromString(&obj4, "-42.22", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
817 RTTESTI_CHECK(obj4.m_rdValue == -42.22);
818 RTTESTI_CHECK(obj4.isNull() == false);
819
820 RTTESTI_CHECK_RC(fromString(&obj4, DBL_MAX_STRING, &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
821 RTTESTI_CHECK(obj4.m_rdValue == DBL_MAX);
822 RTTESTI_CHECK(obj4.isNull() == false);
823
824 RTTESTI_CHECK_RC(fromString(&obj4, DBL_MIN_STRING, &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
825 RTTESTI_CHECK(obj4.m_rdValue == DBL_MIN);
826 RTTESTI_CHECK(obj4.isNull() == false);
827
828 obj4.m_rdValue = 33.33;
829 RTTESTI_CHECK_RC(fromString(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
830 RTTESTI_CHECK(obj4.m_rdValue == 0.0);
831 RTTESTI_CHECK(obj4.isNull() == true);
832
833 obj4.m_rdValue = 33.33;
834 RTTESTI_CHECK_RC(fromString(&obj4, " nULl;", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
835 RTTESTI_CHECK(obj4.m_rdValue == 0.0);
836 RTTESTI_CHECK(obj4.isNull() == true);
837
838 obj4.setNull();
839 RTTESTI_CHECK_RC(fromString(&obj4, " 42.22 ", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
840 RTTESTI_CHECK(obj4.m_rdValue == 42.22);
841 RTTESTI_CHECK(obj4.isNull() == false);
842
843 RTTESTI_CHECK_RC(fromString(&obj4, "\t010\t", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
844 RTTESTI_CHECK(obj4.m_rdValue ==10.0);
845 RTTESTI_CHECK(obj4.isNull() == false);
846
847 RTTESTI_CHECK_RC(fromString(&obj4, "\r\t03495.344\t\r\n", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
848 RTTESTI_CHECK(obj4.m_rdValue == 3495.344);
849 RTTESTI_CHECK(obj4.isNull() == false);
850
851 RTTESTI_CHECK_RC(fromString(&obj4, "1.1;", &ErrInfo, RT_XSTR(__LINE__)), VERR_TRAILING_CHARS);
852 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
853
854 RTTESTI_CHECK_RC(fromString(&obj4, "false", NULL, RT_XSTR(__LINE__)), VERR_NO_DIGITS);
855
856 RTTESTI_CHECK_RC(fromString(&obj4, " 0x42 ", &ErrInfo, RT_XSTR(__LINE__)), VERR_TRAILING_CHARS);
857 RTTESTI_CHECK(obj4.m_rdValue == 0.0);
858 RTTESTI_CHECK(obj4.isNull() == false);
859 }
860}
861
862
863void testString(const char *pszDummy, ...)
864{
865 RTTestSub(g_hTest, "RTCRestString");
866
867 {
868 RTCRestString obj1;
869 RTTESTI_CHECK(obj1.isEmpty());
870 RTTESTI_CHECK(obj1.isNull() == false);
871 RTTESTI_CHECK(strcmp(obj1.typeName(), "RTCString") == 0);
872 RTTESTI_CHECK(obj1.typeClass() == RTCRestObjectBase::kTypeClass_String);
873 }
874
875 {
876 RTCRestString obj2(RTCString("2398.1"));
877 RTTESTI_CHECK(obj2 == "2398.1");
878 RTTESTI_CHECK(obj2.isNull() == false);
879 }
880
881 {
882 RTCRestString obj2("-7345.2");
883 RTTESTI_CHECK(obj2 == "-7345.2");
884 RTTESTI_CHECK(obj2.isNull() == false);
885 }
886
887 {
888 /* Value assignments: */
889 RTCRestString obj3;
890 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
891 RTTESTI_CHECK(obj3.isNull() == true);
892 RTTESTI_CHECK(obj3.isEmpty());
893 obj3 = "-1.0";
894 RTTESTI_CHECK(obj3 == "-1.0");
895 RTTESTI_CHECK(obj3.isNull() == false);
896
897 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
898 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
899 obj3 = RTCString("-2.0");
900 RTTESTI_CHECK(obj3 == "-2.0");
901 RTTESTI_CHECK(obj3.isNull() == false);
902
903 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
904 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
905 obj3 = RTCRestString("-3.0");
906 RTTESTI_CHECK(obj3 == "-3.0");
907 RTTESTI_CHECK(obj3.isNull() == false);
908
909 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
910 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
911 RTTESTI_CHECK_RC(obj3.assignNoThrow(RTCRestString("4.0")), VINF_SUCCESS);
912 RTTESTI_CHECK(obj3 == "4.0");
913 RTTESTI_CHECK(obj3.isNull() == false);
914
915 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
916 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
917 RTTESTI_CHECK_RC(obj3.assignNoThrow("-4.0"), VINF_SUCCESS);
918 RTTESTI_CHECK(obj3 == "-4.0");
919 RTTESTI_CHECK(obj3.isNull() == false);
920
921 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
922 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
923 RTTESTI_CHECK_RC(obj3.assignNoThrow(RTCRestString("0123456789"), 3, 5), VINF_SUCCESS);
924 RTTESTI_CHECK(obj3 == "34567");
925 RTTESTI_CHECK(obj3.isNull() == false);
926
927 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
928 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
929 RTTESTI_CHECK_RC(obj3.assignNoThrow("0123456789", 4), VINF_SUCCESS);
930 RTTESTI_CHECK(obj3 == "0123");
931 RTTESTI_CHECK(obj3.isNull() == false);
932
933 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
934 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
935 RTTESTI_CHECK_RC(obj3.assignNoThrow(8, 'x'), VINF_SUCCESS);
936 RTTESTI_CHECK(obj3 == "xxxxxxxx");
937 RTTESTI_CHECK(obj3.isNull() == false);
938
939 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
940 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
941 RTTESTI_CHECK_RC(obj3.printfNoThrow("%d%s%d", 42, "asdf", 22), VINF_SUCCESS);
942 RTTESTI_CHECK(obj3 == "42asdf22");
943 RTTESTI_CHECK(obj3.isNull() == false);
944
945 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
946 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
947 va_list va;
948 va_start(va, pszDummy);
949 RTTESTI_CHECK_RC(obj3.printfVNoThrow("asdf", va), VINF_SUCCESS);
950 va_end(va);
951 RTTESTI_CHECK(obj3 == "asdf");
952 RTTESTI_CHECK(obj3.isNull() == false);
953
954 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
955 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
956 obj3.assign(RTCRestString("4.0"));
957 RTTESTI_CHECK(obj3 == "4.0");
958 RTTESTI_CHECK(obj3.isNull() == false);
959
960 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
961 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
962 obj3.assign("-4.0");
963 RTTESTI_CHECK(obj3 == "-4.0");
964 RTTESTI_CHECK(obj3.isNull() == false);
965
966 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
967 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
968 obj3.assign(RTCRestString("0123456789"), 3, 5);
969 RTTESTI_CHECK(obj3 == "34567");
970 RTTESTI_CHECK(obj3.isNull() == false);
971
972 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
973 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
974 obj3.assign("0123456789", 4);
975 RTTESTI_CHECK(obj3 == "0123");
976 RTTESTI_CHECK(obj3.isNull() == false);
977
978 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
979 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
980 obj3.assign(8, 'x');
981 RTTESTI_CHECK(obj3 == "xxxxxxxx");
982 RTTESTI_CHECK(obj3.isNull() == false);
983
984 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
985 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
986 obj3.printf("%d%s%d", 42, "asdf", 22);
987 RTTESTI_CHECK(obj3 == "42asdf22");
988 RTTESTI_CHECK(obj3.isNull() == false);
989
990 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
991 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
992 va_start(va, pszDummy);
993 obj3.printfV("asdf", va);
994 va_end(va);
995 RTTESTI_CHECK(obj3 == "asdf");
996 RTTESTI_CHECK(obj3.isNull() == false);
997
998 /* Reset to default: */
999 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
1000 RTTESTI_CHECK(obj3.isEmpty());
1001 RTTESTI_CHECK(obj3.isNull() == false);
1002
1003 obj3 = "1";
1004 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1005 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
1006 RTTESTI_CHECK(obj3.isEmpty());
1007 RTTESTI_CHECK(obj3.isNull() == false);
1008
1009 /* Copy assignments: */
1010 RTCRestString const obj3Max("max");
1011 RTTESTI_CHECK(obj3Max == "max");
1012 RTTESTI_CHECK(obj3Max.isNull() == false);
1013 RTCRestString obj3Null;
1014 obj3Null.setNull();
1015 RTTESTI_CHECK(obj3Null.isEmpty());
1016 RTTESTI_CHECK(obj3Null.isNull() == true);
1017 RTCRestString obj3Empty;
1018 RTTESTI_CHECK(obj3Empty.isEmpty());
1019 RTTESTI_CHECK(obj3Empty.isNull() == false);
1020
1021 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1022 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
1023 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Max), VINF_SUCCESS);
1024 RTTESTI_CHECK(obj3 == "max");
1025 RTTESTI_CHECK(obj3.isNull() == false);
1026
1027 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1028 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
1029 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Null), VINF_SUCCESS);
1030 RTTESTI_CHECK(obj3.isEmpty());
1031 RTTESTI_CHECK(obj3.isNull() == true);
1032
1033 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1034 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
1035 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Empty), VINF_SUCCESS);
1036 RTTESTI_CHECK(obj3 == "");
1037 RTTESTI_CHECK(obj3.isEmpty());
1038 RTTESTI_CHECK(obj3.isNull() == false);
1039
1040 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1041 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
1042 RTTESTI_CHECK_RC(obj3.assignCopy(RTCString("11.0")), VINF_SUCCESS);
1043 RTTESTI_CHECK(obj3 == "11.0");
1044 RTTESTI_CHECK(obj3.isNull() == false);
1045
1046 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1047 RTTESTI_CHECK(obj3.isNull() == true && obj3.isEmpty());
1048 RTTESTI_CHECK_RC(obj3.assignCopy("12.0"), VINF_SUCCESS);
1049 RTTESTI_CHECK(obj3 == "12.0");
1050 RTTESTI_CHECK(obj3.isNull() == false);
1051
1052 /* setNull implies resetToDefault: */
1053 obj3 = obj3Max;
1054 RTTESTI_CHECK(obj3 == "max");
1055 RTTESTI_CHECK(obj3.isNull() == false);
1056 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1057 RTTESTI_CHECK(obj3.isNull() == true);
1058 RTTESTI_CHECK(obj3.isEmpty());
1059
1060 /* Copy constructors: */
1061 {
1062 RTCRestString obj3a(obj3Max);
1063 RTTESTI_CHECK(obj3a == "max");
1064 RTTESTI_CHECK(obj3a.isNull() == false);
1065 }
1066 {
1067 RTCRestString const obj3c(obj3Null);
1068 RTTESTI_CHECK(obj3c.isEmpty());
1069 RTTESTI_CHECK(obj3c.isNull() == true);
1070 }
1071
1072 /* Serialization to json: */
1073 const char *pszJson = toJson(&obj3Max);
1074 RTTESTI_CHECK_MSG(strcmp(pszJson, "\"max\"") == 0, ("pszJson=%s\n", pszJson));
1075 pszJson = toJson(&obj3Null);
1076 RTTESTI_CHECK_MSG(strcmp(pszJson, "null") == 0, ("pszJson=%s\n", pszJson));
1077 pszJson = toJson(&obj3Empty);
1078 RTTESTI_CHECK_MSG(strcmp(pszJson, "\"\"") == 0, ("pszJson=%s\n", pszJson));
1079
1080 /* Serialization to string. */
1081 RTCString str;
1082 RTCString strExpect;
1083 str = "lead-in:";
1084 RTTESTI_CHECK_RC(obj3Max.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
1085 RTTESTI_CHECK_MSG(str.equals("lead-in:max"), ("str=%s\n", str.c_str()));
1086 RTTESTI_CHECK_RC(obj3Max.toString(&str), VINF_SUCCESS);
1087 RTTESTI_CHECK_MSG(str.equals("max"), ("str=%s\n", str.c_str()));
1088
1089 str = "lead-in:";
1090 RTTESTI_CHECK_RC(obj3Empty.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
1091 RTTESTI_CHECK_MSG(str.equals("lead-in:"), ("str=%s\n", str.c_str()));
1092 RTTESTI_CHECK_RC(obj3Empty.toString(&str), VINF_SUCCESS);
1093 RTTESTI_CHECK_MSG(str.equals(""), ("str=%s\n", str.c_str()));
1094
1095 str = "lead-in:";
1096 RTTESTI_CHECK_RC(obj3Null.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
1097 RTTESTI_CHECK_MSG(str.equals("lead-in:"), ("str=%s\n", str.c_str()));
1098 RTTESTI_CHECK_RC(obj3Null.toString(&str), VINF_SUCCESS);
1099 RTTESTI_CHECK_MSG(str.equals(""), ("str=%s\n", str.c_str()));
1100 }
1101
1102 /* deserialize: */
1103 RTERRINFOSTATIC ErrInfo;
1104 {
1105 /* from json: */
1106 RTCRestString obj4;
1107 obj4.setNull();
1108 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"42.42\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1109 RTTESTI_CHECK(obj4 == "42.42");
1110 RTTESTI_CHECK(obj4.isNull() == false);
1111
1112 obj4.setNull();
1113 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"-22.22\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1114 RTTESTI_CHECK(obj4 == "-22.22");
1115 RTTESTI_CHECK(obj4.isNull() == false);
1116
1117 obj4.setNull();
1118 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"maximum\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1119 RTTESTI_CHECK(obj4 == "maximum");
1120 RTTESTI_CHECK(obj4.isNull() == false);
1121
1122 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1123 RTTESTI_CHECK(obj4.isEmpty());
1124 RTTESTI_CHECK(obj4.isNull() == true);
1125
1126 obj4.setNull();
1127 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"\\u0020\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1128 RTTESTI_CHECK(obj4 == " ");
1129 RTTESTI_CHECK(obj4.isNull() == false);
1130
1131 obj4.setNull();
1132 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"\\u004f\\u004D\\u0047\\u0021 :-)\"",
1133 &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1134 RTTESTI_CHECK(obj4 == "OMG! :-)");
1135 RTTESTI_CHECK(obj4.isNull() == false);
1136
1137 obj4.setNull();
1138 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"42:\\uD801\\udC37\\ud852\\uDf62:42\"", /* U+10437 U+24B62 */
1139 &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1140 RTTESTI_CHECK(obj4 == "42:" "\xf0\x90\x90\xb7" "\xf0\xa4\xad\xa2" ":42");
1141 RTTESTI_CHECK(obj4.isNull() == false);
1142
1143 /* object goes to default state on failure: */
1144 obj4 = "asdf";
1145 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "false", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_STRING);
1146 RTTESTI_CHECK(obj4.isEmpty());
1147 RTTESTI_CHECK(obj4.isNull() == false);
1148 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
1149
1150 obj4 = "asdf";
1151 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "1", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_STRING);
1152 RTTESTI_CHECK(obj4.isEmpty());
1153 RTTESTI_CHECK(obj4.isNull() == false);
1154 RTTESTI_CHECK(RTErrInfoIsSet(&ErrInfo.Core));
1155
1156 obj4.setNull();
1157 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "[ null ]", NULL, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_STRING);
1158 RTTESTI_CHECK(obj4.isEmpty());
1159 RTTESTI_CHECK(obj4.isNull() == false);
1160
1161 /* from string: */
1162 obj4.setNull();
1163 RTTESTI_CHECK_RC(fromString(&obj4, "22.42", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1164 RTTESTI_CHECK(obj4 == "22.42");
1165 RTTESTI_CHECK(obj4.isNull() == false);
1166
1167 RTTESTI_CHECK_RC(fromString(&obj4, "-42.22", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1168 RTTESTI_CHECK(obj4 == "-42.22");
1169 RTTESTI_CHECK(obj4.isNull() == false);
1170
1171 obj4.setNull();
1172 RTTESTI_CHECK_RC(fromString(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1173 RTTESTI_CHECK(obj4 == "null");
1174 RTTESTI_CHECK(obj4.isNull() == false);
1175 }
1176}
1177
1178
1179void testDate()
1180{
1181 RTTestSub(g_hTest, "RTCRestDate");
1182 int64_t const iRecent = INT64_C(1536580687739632500);
1183 int64_t const iRecentSec = INT64_C(1536580687000000000);
1184 RTTIMESPEC TimeSpec;
1185
1186#define CHECK_DATE(a_obj, a_fNull, a_fOkay, a_i64Nano, a_sz, a_fUtc) \
1187 do { \
1188 RTTESTI_CHECK((a_obj).isOkay() == (a_fOkay)); \
1189 if ((a_obj).getEpochNano() != (a_i64Nano)) \
1190 RTTestIFailed("line " RT_XSTR(__LINE__) ": getEpochNano=%RI64, expected %RI64", (a_obj).getEpochNano(), (int64_t)(a_i64Nano)); \
1191 if (!(a_obj).getString().equals(a_sz)) \
1192 RTTestIFailed("line " RT_XSTR(__LINE__) ": getString=%s, expected %s", (a_obj).getString().c_str(), a_sz); \
1193 RTTESTI_CHECK((a_obj).isUtc() == (a_fUtc)); \
1194 RTTESTI_CHECK((a_obj).isNull() == (a_fNull)); \
1195 } while (0)
1196#define CHECK_DATE_FMT(a_obj, a_fNull, a_fOkay, a_i64Nano, a_sz, a_fUtc, a_enmFormat) \
1197 do { \
1198 CHECK_DATE(a_obj, a_fNull, a_fOkay, a_i64Nano, a_sz, a_fUtc); \
1199 if ((a_obj).getFormat() != (a_enmFormat)) \
1200 RTTestIFailed("line " RT_XSTR(__LINE__) ": getFormat=%d, expected %d (%s)", (a_obj).getFormat(), (a_enmFormat), #a_enmFormat); \
1201 } while (0)
1202
1203 {
1204 RTCRestDate obj1;
1205 CHECK_DATE(obj1, true, false, 0, "", true);
1206 RTTESTI_CHECK(strcmp(obj1.typeName(), "RTCRestDate") == 0);
1207 RTTESTI_CHECK(obj1.typeClass() == RTCRestObjectBase::kTypeClass_Date);
1208 }
1209
1210 {
1211 /* Value assignments: */
1212 RTCRestDate obj3;
1213 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc3339), VINF_SUCCESS);
1214 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00Z", true);
1215
1216 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1217 CHECK_DATE(obj3, true, false, 0, "", true);
1218 RTTESTI_CHECK_RC(obj3.assignValueRfc3339(RTTimeSpecSetNano(&TimeSpec, 0)), VINF_SUCCESS);
1219 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00Z", true);
1220
1221 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1222 CHECK_DATE(obj3, true, false, 0, "", true);
1223 RTTESTI_CHECK_RC(obj3.assignValueRfc2822(RTTimeSpecSetNano(&TimeSpec, 0)), VINF_SUCCESS);
1224 CHECK_DATE(obj3, false, true, 0, "Thu, 1 Jan 1970 00:00:00 -0000", true);
1225
1226 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1227 RTTESTI_CHECK_RC(obj3.assignValueRfc7131(RTTimeSpecSetNano(&TimeSpec, 0)), VINF_SUCCESS);
1228 CHECK_DATE(obj3, false, true, 0, "Thu, 1 Jan 1970 00:00:00 GMT", true);
1229
1230 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1231 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1232 CHECK_DATE(obj3, false, true, 0, "Thu, 1 Jan 1970 00:00:00 -0000", true);
1233
1234 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1235 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc7131), VINF_SUCCESS);
1236 CHECK_DATE(obj3, false, true, 0, "Thu, 1 Jan 1970 00:00:00 GMT", true);
1237
1238 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1239 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc3339_Fraction_9), VINF_SUCCESS);
1240 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00.000000000Z", true);
1241
1242 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1243 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc3339_Fraction_6), VINF_SUCCESS);
1244 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00.000000Z", true);
1245
1246 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1247 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc3339_Fraction_3), VINF_SUCCESS);
1248 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00.000Z", true);
1249
1250 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1251 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc3339_Fraction_2), VINF_SUCCESS);
1252 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00.00Z", true);
1253
1254 /* Format changes: */
1255 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 59123456789), RTCRestDate::kFormat_Rfc3339_Fraction_9), VINF_SUCCESS);
1256 CHECK_DATE(obj3, false, true, 59123456789, "1970-01-01T00:00:59.123456789Z", true);
1257 RTTESTI_CHECK_RC(obj3.setFormat(RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1258 CHECK_DATE(obj3, false, true, 59123456789, "Thu, 1 Jan 1970 00:00:59 -0000", true);
1259 RTTESTI_CHECK_RC(obj3.setFormat(RTCRestDate::kFormat_Rfc7131), VINF_SUCCESS);
1260 CHECK_DATE(obj3, false, true, 59123456789, "Thu, 1 Jan 1970 00:00:59 GMT", true);
1261 RTTESTI_CHECK_RC(obj3.setFormat(RTCRestDate::kFormat_Rfc3339), VINF_SUCCESS);
1262 CHECK_DATE(obj3, false, true, 59123456789, "1970-01-01T00:00:59Z", true);
1263 RTTESTI_CHECK_RC(obj3.setFormat(RTCRestDate::kFormat_Rfc3339_Fraction_2), VINF_SUCCESS);
1264 CHECK_DATE(obj3, false, true, 59123456789, "1970-01-01T00:00:59.12Z", true);
1265 RTTESTI_CHECK_RC(obj3.setFormat(RTCRestDate::kFormat_Rfc3339_Fraction_3), VINF_SUCCESS);
1266 CHECK_DATE(obj3, false, true, 59123456789, "1970-01-01T00:00:59.123Z", true);
1267 RTTESTI_CHECK_RC(obj3.setFormat(RTCRestDate::kFormat_Rfc3339_Fraction_6), VINF_SUCCESS);
1268 CHECK_DATE(obj3, false, true, 59123456789, "1970-01-01T00:00:59.123456Z", true);
1269 RTTESTI_CHECK_RC(obj3.setFormat(RTCRestDate::kFormat_Rfc3339_Fraction_9), VINF_SUCCESS);
1270 CHECK_DATE(obj3, false, true, 59123456789, "1970-01-01T00:00:59.123456789Z", true);
1271
1272 /* Reset to default and setNull works identically: */
1273 RTTESTI_CHECK_RC(obj3.resetToDefault(), VINF_SUCCESS);
1274 CHECK_DATE(obj3, true, false, 0, "", true);
1275
1276 RTTESTI_CHECK_RC(obj3.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc3339_Fraction_2), VINF_SUCCESS);
1277 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00.00Z", true);
1278 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1279 CHECK_DATE(obj3, true, false, 0, "", true);
1280
1281 /* Copy assignments: */
1282 RTCRestDate obj3Epoch_3339_9;
1283 RTTESTI_CHECK_RC(obj3Epoch_3339_9.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc3339_Fraction_9), VINF_SUCCESS);
1284 CHECK_DATE(obj3Epoch_3339_9, false, true, 0, "1970-01-01T00:00:00.000000000Z", true);
1285
1286 RTCRestDate obj3Epoch_7131;
1287 RTTESTI_CHECK_RC(obj3Epoch_7131.assignValue(RTTimeSpecSetNano(&TimeSpec, 0), RTCRestDate::kFormat_Rfc7131), VINF_SUCCESS);
1288 CHECK_DATE(obj3Epoch_7131, false, true, 0, "Thu, 1 Jan 1970 00:00:00 GMT", true);
1289
1290 RTCRestDate obj3Recent_3339;
1291 RTTESTI_CHECK_RC(obj3Recent_3339.assignValue(RTTimeSpecSetNano(&TimeSpec, iRecent), RTCRestDate::kFormat_Rfc3339), VINF_SUCCESS);
1292 CHECK_DATE(obj3Recent_3339, false, true, iRecent, "2018-09-10T11:58:07Z", true);
1293
1294 RTCRestDate obj3Recent_2822;
1295 RTTESTI_CHECK_RC(obj3Recent_2822.assignValue(RTTimeSpecSetNano(&TimeSpec, iRecent), RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1296 CHECK_DATE(obj3Recent_2822, false, true, iRecent, "Mon, 10 Sep 2018 11:58:07 -0000", true);
1297
1298 RTCRestDate const obj3Null;
1299 CHECK_DATE(obj3Null, true, false, 0, "", true);
1300
1301 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1302 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Epoch_3339_9), VINF_SUCCESS);
1303 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00.000000000Z", true);
1304
1305 RTTESTI_CHECK_RC(obj3.setNull(), VINF_SUCCESS);
1306 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Epoch_7131), VINF_SUCCESS);
1307 CHECK_DATE(obj3, false, true, 0, "Thu, 1 Jan 1970 00:00:00 GMT", true);
1308
1309 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Recent_3339), VINF_SUCCESS);
1310 CHECK_DATE(obj3Recent_2822, false, true, iRecent, "Mon, 10 Sep 2018 11:58:07 -0000", true);
1311
1312 RTTESTI_CHECK_RC(obj3.assignCopy(obj3Null), VINF_SUCCESS);
1313 CHECK_DATE(obj3, true, false, 0, "", true);
1314
1315 obj3 = obj3Recent_2822;
1316 CHECK_DATE(obj3Recent_2822, false, true, iRecent, "Mon, 10 Sep 2018 11:58:07 -0000", true);
1317
1318 obj3 = obj3Epoch_3339_9;
1319 CHECK_DATE(obj3, false, true, 0, "1970-01-01T00:00:00.000000000Z", true);
1320
1321 obj3 = obj3Null;
1322 CHECK_DATE(obj3, true, false, 0, "", true);
1323
1324 /* Copy constructors: */
1325 {
1326 RTCRestDate obj3a(obj3Epoch_3339_9);
1327 CHECK_DATE(obj3a, false, true, 0, "1970-01-01T00:00:00.000000000Z", true);
1328 }
1329 {
1330 RTCRestDate obj3b(obj3Epoch_7131);
1331 CHECK_DATE(obj3b, false, true, 0, "Thu, 1 Jan 1970 00:00:00 GMT", true);
1332 }
1333 {
1334 RTCRestDate obj3c(obj3Recent_3339);
1335 CHECK_DATE(obj3Recent_3339, false, true, iRecent, "2018-09-10T11:58:07Z", true);
1336 }
1337 {
1338 RTCRestDate obj3d(obj3Recent_2822);
1339 CHECK_DATE(obj3d, false, true, iRecent, "Mon, 10 Sep 2018 11:58:07 -0000", true);
1340 }
1341 {
1342 RTCRestDate obj3e(obj3Null);
1343 CHECK_DATE(obj3e, true, false, 0, "", true);
1344 }
1345
1346 /* Serialization to json: */
1347 const char *pszJson = toJson(&obj3Epoch_3339_9);
1348 RTTESTI_CHECK_MSG(strcmp(pszJson, "\"1970-01-01T00:00:00.000000000Z\"") == 0, ("pszJson=%s\n", pszJson));
1349 pszJson = toJson(&obj3Epoch_7131);
1350 RTTESTI_CHECK_MSG(strcmp(pszJson, "\"Thu, 1 Jan 1970 00:00:00 GMT\"") == 0, ("pszJson=%s\n", pszJson));
1351 pszJson = toJson(&obj3Recent_3339);
1352 RTTESTI_CHECK_MSG(strcmp(pszJson, "\"2018-09-10T11:58:07Z\"") == 0, ("pszJson=%s\n", pszJson));
1353 pszJson = toJson(&obj3Recent_2822);
1354 RTTESTI_CHECK_MSG(strcmp(pszJson, "\"Mon, 10 Sep 2018 11:58:07 -0000\"") == 0, ("pszJson=%s\n", pszJson));
1355 pszJson = toJson(&obj3Null);
1356 RTTESTI_CHECK_MSG(strcmp(pszJson, "null") == 0, ("pszJson=%s\n", pszJson));
1357
1358 /* Serialization to string. */
1359 RTCString str;
1360 str = "lead-in:";
1361 RTTESTI_CHECK_RC(obj3Epoch_7131.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
1362 RTTESTI_CHECK_MSG(str.equals("lead-in:Thu, 1 Jan 1970 00:00:00 GMT"), ("str=%s\n", str.c_str()));
1363 RTTESTI_CHECK_RC(obj3Epoch_7131.toString(&str), VINF_SUCCESS);
1364 RTTESTI_CHECK_MSG(str.equals("Thu, 1 Jan 1970 00:00:00 GMT"), ("str=%s\n", str.c_str()));
1365
1366 str = "lead-in:";
1367 RTTESTI_CHECK_RC(obj3Recent_3339.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
1368 RTTESTI_CHECK_MSG(str.equals("lead-in:2018-09-10T11:58:07Z"), ("str=%s\n", str.c_str()));
1369 RTTESTI_CHECK_RC(obj3Recent_3339.toString(&str), VINF_SUCCESS);
1370 RTTESTI_CHECK_MSG(str.equals("2018-09-10T11:58:07Z"), ("str=%s\n", str.c_str()));
1371
1372 str = "lead-in:";
1373 RTTESTI_CHECK_RC(obj3Null.toString(&str, RTCRestObjectBase::kToString_Append), VINF_SUCCESS);
1374 RTTESTI_CHECK_MSG(str.equals("lead-in:null"), ("str=%s\n", str.c_str()));
1375 RTTESTI_CHECK_RC(obj3Null.toString(&str), VINF_SUCCESS);
1376 RTTESTI_CHECK_MSG(str.equals("null"), ("str=%s\n", str.c_str()));
1377 }
1378
1379 /* deserialize: */
1380 RTERRINFOSTATIC ErrInfo;
1381 {
1382 RTCRestDate obj4;
1383 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"Thu, 1 Jan 1970 00:00:00 GMT\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1384 CHECK_DATE_FMT(obj4, false, true, 0, "Thu, 1 Jan 1970 00:00:00 GMT", true, RTCRestDate::kFormat_Rfc7131);
1385
1386 obj4.setNull();
1387 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"Thu, 1 Jan 1970 00:00:00.0000 GMT\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1388 CHECK_DATE_FMT(obj4, false, true, 0, "Thu, 1 Jan 1970 00:00:00.0000 GMT", true, RTCRestDate::kFormat_Rfc7131);
1389
1390 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"1 Jan 1970 00:00:00 GMT\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1391 CHECK_DATE_FMT(obj4, false, true, 0, "1 Jan 1970 00:00:00 GMT", true, RTCRestDate::kFormat_Rfc7131);
1392
1393 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"1 Jan 1970 00:00:00\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1394 CHECK_DATE_FMT(obj4, false, true, 0, "1 Jan 1970 00:00:00", false, RTCRestDate::kFormat_Rfc2822);
1395
1396 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"1 Jan 070 00:00:00\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1397 CHECK_DATE_FMT(obj4, false, true, 0, "1 Jan 070 00:00:00", false, RTCRestDate::kFormat_Rfc2822);
1398
1399 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"2018-09-10T11:58:07Z\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1400 CHECK_DATE_FMT(obj4, false, true, iRecentSec , "2018-09-10T11:58:07Z", true, RTCRestDate::kFormat_Rfc3339);
1401
1402 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"1 Jan 70 00:00:00\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1403 CHECK_DATE_FMT(obj4, false, true, 0, "1 Jan 70 00:00:00", false, RTCRestDate::kFormat_Rfc2822);
1404
1405 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"2018-09-10T11:58:07.739632500Z\"", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1406 CHECK_DATE_FMT(obj4, false, true, iRecent, "2018-09-10T11:58:07.739632500Z", true, RTCRestDate::kFormat_Rfc3339_Fraction_9);
1407
1408 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1409 CHECK_DATE_FMT(obj4, true, false, 0, "", true, RTCRestDate::kFormat_Rfc3339_Fraction_9);
1410
1411 /* object goes to default state if not string and to non-okay if string: */
1412 RTTESTI_CHECK_RC(obj4.assignValue(RTTimeSpecSetNano(&TimeSpec, iRecent), RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1413 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "true", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_DATE);
1414 CHECK_DATE_FMT(obj4, true, false, 0, "", true, RTCRestDate::kFormat_Rfc2822);
1415
1416 RTTESTI_CHECK_RC(obj4.assignValue(RTTimeSpecSetNano(&TimeSpec, iRecent), RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1417 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"string\"", &ErrInfo, RT_XSTR(__LINE__)), VWRN_REST_UNABLE_TO_DECODE_DATE);
1418 CHECK_DATE_FMT(obj4, false, false, 0, "string", false, RTCRestDate::kFormat_Rfc2822);
1419
1420 RTTESTI_CHECK_RC(obj4.assignValue(RTTimeSpecSetNano(&TimeSpec, iRecent), RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1421 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "\"0x199 string\"", &ErrInfo, RT_XSTR(__LINE__)), VWRN_REST_UNABLE_TO_DECODE_DATE);
1422 CHECK_DATE_FMT(obj4, false, false, 0, "0x199 string", false, RTCRestDate::kFormat_Rfc2822);
1423
1424 RTTESTI_CHECK_RC(obj4.assignValue(RTTimeSpecSetNano(&TimeSpec, iRecent), RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1425 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "[ null ]", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_DATE);
1426 CHECK_DATE_FMT(obj4, true, false, 0, "", true, RTCRestDate::kFormat_Rfc2822);
1427
1428 RTTESTI_CHECK_RC(obj4.assignValue(RTTimeSpecSetNano(&TimeSpec, iRecent), RTCRestDate::kFormat_Rfc2822), VINF_SUCCESS);
1429 RTTESTI_CHECK_RC(deserializeFromJson(&obj4, "{ \"foo\": 1 }", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_WRONG_JSON_TYPE_FOR_DATE);
1430 CHECK_DATE_FMT(obj4, true, false, 0, "", true, RTCRestDate::kFormat_Rfc2822);
1431
1432 /* From string: */
1433 obj4.setNull();
1434 RTTESTI_CHECK_RC(fromString(&obj4, "Thu, 1 Jan 1970 00:00:00 GMT", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1435 CHECK_DATE_FMT(obj4, false, true, 0, "Thu, 1 Jan 1970 00:00:00 GMT", true, RTCRestDate::kFormat_Rfc7131);
1436
1437 RTTESTI_CHECK_RC(fromString(&obj4, "Mon, 10 Sep 2018 11:58:07 -0000", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1438 CHECK_DATE_FMT(obj4, false, true, iRecentSec, "Mon, 10 Sep 2018 11:58:07 -0000", true, RTCRestDate::kFormat_Rfc2822);
1439
1440 RTTESTI_CHECK_RC(fromString(&obj4, "\t\n\rnull;\r\n\t", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1441 CHECK_DATE_FMT(obj4, true, false, 0, "", true, RTCRestDate::kFormat_Rfc2822);
1442
1443 RTTESTI_CHECK_RC(fromString(&obj4, "Mon, 10 Sep 2018 11:58:07 +0000", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1444 CHECK_DATE_FMT(obj4, false, true, iRecentSec, "Mon, 10 Sep 2018 11:58:07 +0000", false, RTCRestDate::kFormat_Rfc2822);
1445
1446 RTTESTI_CHECK_RC(fromString(&obj4, "1970-01-01T00:00:00.000000000Z", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1447 CHECK_DATE_FMT(obj4, false, true, 0, "1970-01-01T00:00:00.000000000Z", true, RTCRestDate::kFormat_Rfc3339_Fraction_9);
1448
1449 RTTESTI_CHECK_RC(fromString(&obj4, "10 Sep 2018 11:58:07 -0000", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1450 CHECK_DATE_FMT(obj4, false, true, iRecentSec, "10 Sep 2018 11:58:07 -0000", true, RTCRestDate::kFormat_Rfc2822);
1451
1452 RTTESTI_CHECK_RC(fromString(&obj4, "null", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1453 CHECK_DATE_FMT(obj4, true, false, 0, "", true, RTCRestDate::kFormat_Rfc2822);
1454
1455 RTTESTI_CHECK_RC(fromString(&obj4, "Mon, 10 Sep 18 11:58:07 -0000", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1456 CHECK_DATE_FMT(obj4, false, true, iRecentSec, "Mon, 10 Sep 18 11:58:07 -0000", true, RTCRestDate::kFormat_Rfc2822);
1457
1458 RTTESTI_CHECK_RC(fromString(&obj4, "fa;se", &ErrInfo, RT_XSTR(__LINE__)), VERR_REST_UNABLE_TO_DECODE_DATE);
1459 CHECK_DATE_FMT(obj4, false, false, 0, "fa;se", false, RTCRestDate::kFormat_Rfc2822);
1460
1461 RTTESTI_CHECK_RC(fromString(&obj4, "10 Sep 18 11:58:07", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1462 CHECK_DATE_FMT(obj4, false, true, iRecentSec, "10 Sep 18 11:58:07", false, RTCRestDate::kFormat_Rfc2822);
1463
1464 obj4.setNull();
1465 RTTESTI_CHECK_RC(fromString(&obj4, "10 Sep 118 11:58:07", &ErrInfo, RT_XSTR(__LINE__)), VINF_SUCCESS);
1466 CHECK_DATE_FMT(obj4, false, true, iRecentSec, "10 Sep 118 11:58:07", false, RTCRestDate::kFormat_Rfc2822);
1467 }
1468}
1469
1470
1471/** Wraps RTCRestInt16 to check for leaks. */
1472class MyRestInt16 : public RTCRestInt16
1473{
1474public:
1475 static size_t s_cInstances;
1476 MyRestInt16() : RTCRestInt16() { s_cInstances++; }
1477 MyRestInt16(MyRestInt16 const &a_rThat) : RTCRestInt16(a_rThat) { s_cInstances++; }
1478 MyRestInt16(int16_t a_iValue) : RTCRestInt16(a_iValue) { s_cInstances++; }
1479 ~MyRestInt16() { s_cInstances--; }
1480};
1481
1482size_t MyRestInt16::s_cInstances = 0;
1483
1484
1485static void verifyArray(RTCRestArray<MyRestInt16> const &rArray, int iLine, unsigned cElements, ...)
1486{
1487 if (rArray.size() != cElements)
1488 RTTestIFailed("line %u: size() -> %zu, expected %u", iLine, rArray.size(), cElements);
1489 va_list va;
1490 va_start(va, cElements);
1491 for (unsigned i = 0; i < cElements; i++)
1492 {
1493 int iExpected = va_arg(va, int);
1494 if (rArray.at(i)->m_iValue != iExpected)
1495 RTTestIFailed("line %u: element #%u: %d, expected %d", iLine, i, rArray.at(i)->m_iValue, iExpected);
1496 }
1497 va_end(va);
1498}
1499
1500
1501static void testArray()
1502{
1503 RTTestSub(g_hTest, "RTCRestArray");
1504
1505 {
1506 RTCRestArray<RTCRestBool> obj1;
1507 RTTESTI_CHECK(obj1.size() == 0);
1508 RTTESTI_CHECK(obj1.isEmpty() == true);
1509 RTTESTI_CHECK(obj1.isNull() == false);
1510 RTTESTI_CHECK(strcmp(obj1.typeName(), "RTCRestArray<ElementType>") == 0);
1511 RTTESTI_CHECK(obj1.typeClass() == RTCRestObjectBase::kTypeClass_Array);
1512 }
1513
1514 /* Some random order insertion and manipulations: */
1515 {
1516 RTCRestArray<MyRestInt16> Arr2;
1517 RTCRestArray<MyRestInt16> const *pConstArr2 = &Arr2;
1518
1519 RTTESTI_CHECK_RC(Arr2.insert(0, new MyRestInt16(3)), VINF_SUCCESS);
1520 verifyArray(Arr2, __LINE__, 1, 3);
1521 RTTESTI_CHECK_RC(Arr2.append( new MyRestInt16(7)), VINF_SUCCESS);
1522 verifyArray(Arr2, __LINE__, 2, 3, 7);
1523 RTTESTI_CHECK_RC(Arr2.insert(1, new MyRestInt16(5)), VINF_SUCCESS);
1524 verifyArray(Arr2, __LINE__, 3, 3, 5, 7);
1525 RTTESTI_CHECK_RC(Arr2.insert(2, new MyRestInt16(6)), VINF_SUCCESS);
1526 verifyArray(Arr2, __LINE__, 4, 3, 5, 6, 7);
1527 RTTESTI_CHECK_RC(Arr2.prepend( new MyRestInt16(0)), VINF_SUCCESS);
1528 verifyArray(Arr2, __LINE__, 5, 0, 3, 5, 6, 7);
1529 RTTESTI_CHECK_RC(Arr2.append( new MyRestInt16(9)), VINF_SUCCESS);
1530 verifyArray(Arr2, __LINE__, 6, 0, 3, 5, 6, 7, 9);
1531 RTTESTI_CHECK_RC(Arr2.insert(5, new MyRestInt16(8)), VINF_SUCCESS);
1532 verifyArray(Arr2, __LINE__, 7, 0, 3, 5, 6, 7, 8, 9);
1533 RTTESTI_CHECK_RC(Arr2.insert(1, new MyRestInt16(1)), VINF_SUCCESS);
1534 verifyArray(Arr2, __LINE__, 8, 0, 1, 3, 5, 6, 7, 8, 9);
1535 RTTESTI_CHECK_RC(Arr2.insert(3, new MyRestInt16(4)), VINF_SUCCESS);
1536 verifyArray(Arr2, __LINE__, 9, 0, 1, 3, 4, 5, 6, 7, 8, 9);
1537 RTTESTI_CHECK_RC(Arr2.insert(2, new MyRestInt16(2)), VINF_SUCCESS);
1538 verifyArray(Arr2, __LINE__, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1539 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1540
1541 RTTESTI_CHECK(Arr2.size() == 10);
1542
1543 for (size_t i = 0; i < Arr2.size(); i++)
1544 {
1545 MyRestInt16 *pCur = Arr2.at(i);
1546 RTTESTI_CHECK(pCur->m_iValue == (int16_t)i);
1547
1548 MyRestInt16 const *pCur2 = pConstArr2->at(i);
1549 RTTESTI_CHECK(pCur2->m_iValue == (int16_t)i);
1550 }
1551
1552 RTTESTI_CHECK_RC(Arr2.replace(2, new MyRestInt16(22)), VWRN_ALREADY_EXISTS);
1553 verifyArray(Arr2, __LINE__, 10, 0, 1, 22, 3, 4, 5, 6, 7, 8, 9);
1554
1555 RTTESTI_CHECK_RC(Arr2.replace(7, new MyRestInt16(77)), VWRN_ALREADY_EXISTS);
1556 verifyArray(Arr2, __LINE__, 10, 0, 1, 22, 3, 4, 5, 6, 77, 8, 9);
1557
1558 RTTESTI_CHECK_RC(Arr2.replace(10, new MyRestInt16(10)), VINF_SUCCESS);
1559 verifyArray(Arr2, __LINE__, 11, 0, 1, 22, 3, 4, 5, 6, 77, 8, 9, 10);
1560
1561 RTTESTI_CHECK_RC(Arr2.replaceCopy(2, MyRestInt16(2)), VWRN_ALREADY_EXISTS);
1562 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1563 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1564
1565 /* copy constructor: */
1566 {
1567 RTCRestArray<MyRestInt16> const Arr2Copy(Arr2);
1568 verifyArray(Arr2Copy, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1569 }
1570 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1571 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1572
1573 {
1574 RTCRestArray<MyRestInt16> Arr2Copy2(Arr2);
1575 verifyArray(Arr2Copy2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1576 RTTESTI_CHECK_RC(Arr2Copy2.removeAt(7), VINF_SUCCESS);
1577 verifyArray(Arr2Copy2, __LINE__, 10, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10);
1578 }
1579 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1580 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1581
1582 /* copy method + clear: */
1583 {
1584 RTCRestArray<MyRestInt16> Arr2Copy3;
1585 RTTESTI_CHECK_RC(Arr2Copy3.assignCopy(Arr2), VINF_SUCCESS);
1586 verifyArray(Arr2Copy3, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1587 Arr2Copy3.at(3)->m_iValue = 33;
1588 verifyArray(Arr2Copy3, __LINE__, 11, 0, 1, 2, 33, 4, 5, 6, 77, 8, 9, 10);
1589 Arr2Copy3.clear();
1590 verifyArray(Arr2Copy3, __LINE__, 0);
1591 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1592 }
1593 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1594 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1595
1596 /* Check setNull and resetToDefaults with copies: */
1597 {
1598 RTCRestArray<MyRestInt16> Arr2Copy4(Arr2);
1599 verifyArray(Arr2Copy4, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1600
1601 RTTESTI_CHECK_RC(Arr2Copy4.setNull(), VINF_SUCCESS);
1602 verifyArray(Arr2Copy4, __LINE__, 0);
1603 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1604 RTTESTI_CHECK(Arr2Copy4.isNull() == true);
1605
1606 RTTESTI_CHECK_RC(Arr2Copy4.resetToDefault(), VINF_SUCCESS);
1607 RTTESTI_CHECK(Arr2Copy4.isNull() == false);
1608 verifyArray(Arr2Copy4, __LINE__, 0);
1609 }
1610 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1611 {
1612 RTCRestArray<MyRestInt16> Arr2Copy5(Arr2);
1613 verifyArray(Arr2Copy5, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1614
1615 RTTESTI_CHECK_RC(Arr2Copy5.resetToDefault(), VINF_SUCCESS);
1616 verifyArray(Arr2Copy5, __LINE__, 0);
1617 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1618 RTTESTI_CHECK(Arr2Copy5.isNull() == false);
1619
1620 RTTESTI_CHECK_RC(Arr2Copy5.setNull(), VINF_SUCCESS);
1621 RTTESTI_CHECK(Arr2Copy5.isNull() == true);
1622
1623 RTTESTI_CHECK_RC(Arr2Copy5.append(new MyRestInt16(100)), VINF_SUCCESS);
1624 RTTESTI_CHECK(Arr2Copy5.isNull() == false);
1625 verifyArray(Arr2Copy5, __LINE__, 1, 100);
1626 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size() + 1, ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size() + 1));
1627 }
1628 verifyArray(Arr2, __LINE__, 11, 0, 1, 2, 3, 4, 5, 6, 77, 8, 9, 10);
1629
1630 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == Arr2.size(), ("%zu vs %zu\n", MyRestInt16::s_cInstances, Arr2.size()));
1631 }
1632 RTTESTI_CHECK_MSG(MyRestInt16::s_cInstances == 0, ("%zu\n", MyRestInt16::s_cInstances));
1633
1634 {
1635 RTCRestArray<RTCRestInt64> Arr3;
1636 RTCRestArray<RTCRestInt64> const *pConstArr3 = &Arr3;
1637
1638 /* Insert a range of numbers into a int64 array. */
1639 for (int64_t i = 0; i < _64K; i++)
1640 {
1641 if (i & 1)
1642 {
1643 RTCRestInt64 toCopy(i);
1644 if (i & 2)
1645 RTTESTI_CHECK_RC(Arr3.insertCopy(i, toCopy), VINF_SUCCESS);
1646 else
1647 RTTESTI_CHECK_RC(Arr3.appendCopy(toCopy), VINF_SUCCESS);
1648 }
1649 else
1650 {
1651 RTCRestInt64 *pDirect = new RTCRestInt64(i);
1652 if (i & 2)
1653 RTTESTI_CHECK_RC(Arr3.insert(i, pDirect), VINF_SUCCESS);
1654 else
1655 RTTESTI_CHECK_RC(Arr3.append(pDirect), VINF_SUCCESS);
1656 }
1657 RTTESTI_CHECK(Arr3.size() == (size_t)i + 1);
1658 RTTESTI_CHECK(Arr3.isEmpty() == false);
1659 }
1660
1661 /* Verify insertions: */
1662 size_t cElements = _64K;
1663 RTTESTI_CHECK(Arr3.size() == cElements);
1664
1665 for (int64_t i = 0; i < _64K; i++)
1666 {
1667 RTCRestInt64 *pCur = Arr3.at(i);
1668 RTTESTI_CHECK(pCur->m_iValue == i);
1669
1670 RTCRestInt64 const *pCur2 = pConstArr3->at(i);
1671 RTTESTI_CHECK(pCur2->m_iValue == i);
1672 }
1673 RTTESTI_CHECK(Arr3.first()->m_iValue == 0);
1674 RTTESTI_CHECK(Arr3.last()->m_iValue == _64K - 1);
1675 RTTESTI_CHECK(pConstArr3->first()->m_iValue == 0);
1676 RTTESTI_CHECK(pConstArr3->last()->m_iValue == _64K - 1);
1677
1678 /* Remove every 3rd element: */
1679 RTTESTI_CHECK(Arr3.size() == cElements);
1680 for (int64_t i = _64K - 1; i >= 0; i -= 3)
1681 {
1682 RTTESTI_CHECK_RC(Arr3.removeAt(i), VINF_SUCCESS);
1683 cElements--;
1684 RTTESTI_CHECK(Arr3.size() == cElements);
1685 }
1686
1687 /* Verify after removal: */
1688 for (int64_t i = 0, iValue = 0; i < (ssize_t)Arr3.size(); i++, iValue++)
1689 {
1690 if ((iValue % 3) == 0)
1691 iValue++;
1692 RTTESTI_CHECK_MSG(Arr3.at(i)->m_iValue == iValue, ("%RI64: %RI64 vs %RI64\n", i, Arr3.at(i)->m_iValue, iValue));
1693 }
1694
1695 /* Clear it and we're done: */
1696 Arr3.clear();
1697 RTTESTI_CHECK(Arr3.size() == 0);
1698 RTTESTI_CHECK(Arr3.isEmpty() == true);
1699 }
1700
1701 {
1702 RTCRestArray<RTCRestInt32> Arr4;
1703
1704 /* Insert a range of numbers into a int32 array, in reverse order. */
1705 for (int32_t i = 0; i < 2048; i++)
1706 {
1707 if (i & 1)
1708 {
1709 RTCRestInt32 toCopy(i);
1710 if (i & 2)
1711 RTTESTI_CHECK_RC(Arr4.insertCopy(0, toCopy), VINF_SUCCESS);
1712 else
1713 RTTESTI_CHECK_RC(Arr4.prependCopy(toCopy), VINF_SUCCESS);
1714 }
1715 else
1716 {
1717 RTCRestInt32 *pDirect = new RTCRestInt32(i);
1718 if (i & 2)
1719 RTTESTI_CHECK_RC(Arr4.insert(0, pDirect), VINF_SUCCESS);
1720 else
1721 RTTESTI_CHECK_RC(Arr4.prepend(pDirect), VINF_SUCCESS);
1722 }
1723 RTTESTI_CHECK((ssize_t)Arr4.size() == i + 1);
1724 RTTESTI_CHECK(Arr4.isEmpty() == false);
1725 }
1726
1727 for (int32_t i = 0, iValue = (int32_t)Arr4.size() - 1; i < (ssize_t)Arr4.size(); i++, iValue--)
1728 RTTESTI_CHECK_MSG(Arr4.at(i)->m_iValue == iValue, ("%RI32: %RI32 vs %RI32\n", i, Arr4.at(i)->m_iValue, iValue));
1729
1730 for (int32_t i = 0; i < 512; i++)
1731 RTTESTI_CHECK_RC(Arr4.removeAt(0), VINF_SUCCESS);
1732 RTTESTI_CHECK(Arr4.size() == 1536);
1733
1734 for (int32_t i = 0; i < 512; i++)
1735 RTTESTI_CHECK_RC(Arr4.removeAt(~(size_t)0), VINF_SUCCESS);
1736 RTTESTI_CHECK(Arr4.size() == 1024);
1737
1738 for (int32_t i = 0, iValue = 1535; i < (ssize_t)Arr4.size(); i++, iValue--)
1739 RTTESTI_CHECK_MSG(Arr4.at(i)->m_iValue == iValue, ("%RI32: %RI32 vs %RI32\n", i, Arr4.at(i)->m_iValue, iValue));
1740 }
1741}
1742
1743
1744static void verifyMap(RTCRestStringMap<MyRestInt16> const &rMap, int iLine, unsigned cEntries, ...)
1745{
1746 if (rMap.size() != cEntries)
1747 RTTestIFailed("line %u: size() -> %zu, expected %u", iLine, rMap.size(), cEntries);
1748 if (rMap.isEmpty() != (cEntries ? false : true))
1749 RTTestIFailed("line %u: isEmpty() -> %RTbool, with %u entries", iLine, rMap.isEmpty(), cEntries);
1750
1751 va_list va;
1752 va_start(va, cEntries);
1753 for (unsigned i = 0; i < cEntries; i++)
1754 {
1755 const char *pszKey = va_arg(va, const char *);
1756 int iValue = va_arg(va, int);
1757 if ( rMap.containsKey(pszKey) != true
1758 || rMap.containsKey(RTCString(pszKey)) != true
1759 || rMap.get(pszKey) == NULL
1760 || rMap.get(RTCString(pszKey)) == NULL)
1761 RTTestIFailed("line %u: entry '%s' not found!", iLine, pszKey);
1762 else if (rMap.get(pszKey)->m_iValue != iValue)
1763 RTTestIFailed("line %u: entry '%s' value mismatch: %d, expected %d",
1764 iLine, pszKey, rMap.get(pszKey)->m_iValue, iValue);
1765 RTTESTI_CHECK(rMap.get(pszKey) == rMap.get(RTCString(pszKey)));
1766 }
1767 va_end(va);
1768 RTTESTI_CHECK(rMap.isNull() == false);
1769
1770 uint64_t fFound = 0;
1771 for (RTCRestStringMapBase::ConstIterator it = rMap.begin(); it != rMap.end(); ++it)
1772 {
1773 MyRestInt16 const *pObj = (MyRestInt16 const *)it.getValue();
1774 RTTESTI_CHECK(RT_VALID_PTR(pObj));
1775
1776 bool fFoundIt = false;
1777 va_start(va, cEntries);
1778 for (unsigned i = 0; i < cEntries; i++)
1779 {
1780 const char *pszKey = va_arg(va, const char *);
1781 int iValue = va_arg(va, int);
1782 if (it.getKey().equals(pszKey))
1783 {
1784 if (fFound & RT_BIT_64(i))
1785 RTTestIFailed("line %u: base enum: entry '%s' returned more than once!", iLine, pszKey);
1786 if (pObj->m_iValue != iValue)
1787 RTTestIFailed("line %u: base enum: entry '%s' value mismatch: %d, expected %d",
1788 iLine, pszKey, pObj->m_iValue, iValue);
1789 fFound |= RT_BIT_64(i);
1790 fFoundIt = true;
1791 va_end(va);
1792 return;
1793 }
1794 }
1795 va_end(va);
1796 if (!fFoundIt)
1797 RTTestIFailed("line %u: base enum: entry '%s' not expected!", iLine, it.getKey().c_str());
1798 }
1799}
1800
1801
1802void testStringMap(void)
1803{
1804 RTTestSub(g_hTest, "RTCRestMap");
1805
1806 {
1807 RTCRestStringMap<RTCRestString> obj1;
1808 RTTESTI_CHECK(obj1.size() == 0);
1809 RTTESTI_CHECK(obj1.isEmpty() == true);
1810 RTTESTI_CHECK(obj1.isNull() == false);
1811 RTTESTI_CHECK(strcmp(obj1.typeName(), "RTCRestStringMap<ValueType>") == 0);
1812 RTTESTI_CHECK(obj1.typeClass() == RTCRestObjectBase::kTypeClass_StringMap);
1813 }
1814
1815 /* Basic operations: */
1816 {
1817 MyRestInt16::s_cInstances = 0;
1818 RTCRestStringMap<MyRestInt16> Map2;
1819 verifyMap(Map2, __LINE__, 0);
1820
1821 RTTESTI_CHECK_RC(Map2.putCopy("0x0004", MyRestInt16(4)), VINF_SUCCESS);
1822 verifyMap(Map2, __LINE__, 1, "0x0004", 4);
1823 RTTESTI_CHECK(MyRestInt16::s_cInstances == 1);
1824 RTTESTI_CHECK_RC(Map2.put("0x0001", new MyRestInt16(1)), VINF_SUCCESS);
1825 verifyMap(Map2, __LINE__, 2, "0x0004",4, "0x0001",1);
1826 RTTESTI_CHECK_RC(Map2.put("0x0003", new MyRestInt16(3)), VINF_SUCCESS);
1827 verifyMap(Map2, __LINE__, 3, "0x0004",4, "0x0001",1, "0x0003",3);
1828 RTTESTI_CHECK_RC(Map2.put("0x0002", new MyRestInt16(2)), VINF_SUCCESS);
1829 verifyMap(Map2, __LINE__, 4, "0x0004",4, "0x0001",1, "0x0003",3, "0x0002",2);
1830 RTTESTI_CHECK(MyRestInt16::s_cInstances == 4);
1831 RTTESTI_CHECK_RC(Map2.put("0x0000", new MyRestInt16(0)), VINF_SUCCESS);
1832 verifyMap(Map2, __LINE__, 5, "0x0004",4, "0x0001",1, "0x0003",3, "0x0002",2, "0x0000",0);
1833 RTTESTI_CHECK(MyRestInt16::s_cInstances == 5);
1834 RTTESTI_CHECK_RC(Map2.putCopy("towel", MyRestInt16(42)), VINF_SUCCESS);
1835 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0003",3, "0x0002",2, "0x0000",0, "towel",42);
1836 RTTESTI_CHECK(MyRestInt16::s_cInstances == 6);
1837
1838 RTTESTI_CHECK(Map2.containsKey("0x0005") == false);
1839 RTTESTI_CHECK(Map2.get("0x0005") == NULL);
1840
1841 RTTESTI_CHECK(Map2.remove("0x0003") == true);
1842 verifyMap(Map2, __LINE__, 5, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42);
1843 RTTESTI_CHECK(MyRestInt16::s_cInstances == 5);
1844
1845 RTTESTI_CHECK(Map2.remove("0x0003") == false);
1846 verifyMap(Map2, __LINE__, 5, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42);
1847 RTTESTI_CHECK(MyRestInt16::s_cInstances == 5);
1848
1849 RTCRestObjectBase *pNewBase = NULL;
1850 RTTESTI_CHECK_RC(Map2.putNewValue(&pNewBase, "putNewValue"), VINF_SUCCESS);
1851 ((MyRestInt16 *)pNewBase)->m_iValue = 88;
1852 RTTESTI_CHECK(MyRestInt16::s_cInstances == 6);
1853 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",88);
1854
1855 pNewBase = NULL;
1856 RTTESTI_CHECK_RC(Map2.putNewValue(&pNewBase, RTCString("putNewValue")), VERR_ALREADY_EXISTS);
1857 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",88);
1858 pNewBase = NULL;
1859 RTTESTI_CHECK_RC(Map2.putNewValue(&pNewBase, RTCString("putNewValue"), true /*a_fReplace*/), VWRN_ALREADY_EXISTS);
1860 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1861 RTTESTI_CHECK(MyRestInt16::s_cInstances == 6);
1862
1863 /* Make copy and remove all: */
1864 {
1865 RTCRestStringMap<MyRestInt16> Map2Copy1;
1866
1867 RTTESTI_CHECK_RC(Map2Copy1.assignCopy(Map2), VINF_SUCCESS);
1868 verifyMap(Map2Copy1, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1869 RTTESTI_CHECK(MyRestInt16::s_cInstances == 12);
1870
1871 RTTESTI_CHECK(Map2Copy1.remove("0x0004") == true);
1872 verifyMap(Map2Copy1, __LINE__, 5, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1873 RTTESTI_CHECK(MyRestInt16::s_cInstances == 11);
1874
1875 RTTESTI_CHECK(Map2Copy1.remove("putNewValue") == true);
1876 verifyMap(Map2Copy1, __LINE__, 4, "0x0001",1, "0x0002",2, "0x0000",0);
1877 RTTESTI_CHECK(MyRestInt16::s_cInstances == 10);
1878
1879 RTTESTI_CHECK(Map2Copy1.remove("towel") == true);
1880 verifyMap(Map2Copy1, __LINE__, 3, "0x0001",1, "0x0002",2, "0x0000",0);
1881 RTTESTI_CHECK(MyRestInt16::s_cInstances == 9);
1882
1883 RTTESTI_CHECK(Map2Copy1.remove("0x0002") == true);
1884 verifyMap(Map2Copy1, __LINE__, 2, "0x0001",1, "0x0000",0);
1885 RTTESTI_CHECK(MyRestInt16::s_cInstances == 8);
1886
1887 RTTESTI_CHECK(Map2Copy1.remove("0x0000") == true);
1888 verifyMap(Map2Copy1, __LINE__, 1, "0x0001",1);
1889 RTTESTI_CHECK(MyRestInt16::s_cInstances == 7);
1890
1891 RTTESTI_CHECK(Map2Copy1.remove("0x0001") == true);
1892 verifyMap(Map2Copy1, __LINE__, 0);
1893 RTTESTI_CHECK(Map2Copy1.isEmpty() == true);
1894 RTTESTI_CHECK(MyRestInt16::s_cInstances == 6);
1895 }
1896 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1897
1898 /* Make copy and use clear: */
1899 {
1900 RTCRestStringMap<MyRestInt16> Map2Copy2(Map2);
1901 verifyMap(Map2Copy2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1902 RTTESTI_CHECK(MyRestInt16::s_cInstances == 12);
1903 Map2Copy2.clear();
1904 verifyMap(Map2Copy2, __LINE__, 0);
1905 RTTESTI_CHECK(Map2Copy2.isEmpty() == true);
1906 RTTESTI_CHECK(MyRestInt16::s_cInstances == 6);
1907 }
1908 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1909
1910 /* Make copy and reset to default: */
1911 {
1912 RTCRestStringMap<MyRestInt16> Map2Copy3(Map2);
1913 verifyMap(Map2Copy3, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1914 RTTESTI_CHECK(MyRestInt16::s_cInstances == 12);
1915 RTTESTI_CHECK_RC(Map2Copy3.resetToDefault(), VINF_SUCCESS);
1916 verifyMap(Map2Copy3, __LINE__, 0);
1917 RTTESTI_CHECK(Map2Copy3.isEmpty() == true);
1918 RTTESTI_CHECK(MyRestInt16::s_cInstances == 6);
1919 }
1920 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1921
1922 /* Make copy and set to null: */
1923 {
1924 RTCRestStringMap<MyRestInt16> Map2Copy4;
1925 Map2Copy4 = Map2;
1926 verifyMap(Map2Copy4, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1927 RTTESTI_CHECK(MyRestInt16::s_cInstances == 12);
1928 RTTESTI_CHECK_RC(Map2Copy4.setNull(), VINF_SUCCESS);
1929 RTTESTI_CHECK(Map2Copy4.size() == 0);
1930 RTTESTI_CHECK(Map2Copy4.isEmpty() == true);
1931 RTTESTI_CHECK(Map2Copy4.isNull() == true);
1932 RTTESTI_CHECK(MyRestInt16::s_cInstances == 6);
1933 }
1934 verifyMap(Map2, __LINE__, 6, "0x0004",4, "0x0001",1, "0x0002",2, "0x0000",0, "towel",42, "putNewValue",0);
1935 }
1936 RTTESTI_CHECK(MyRestInt16::s_cInstances == 0);
1937
1938 /* Check that null indicator is reset when it should: */
1939 {
1940 RTCRestStringMap<MyRestInt16> Map3;
1941 Map3.setNull();
1942 RTTESTI_CHECK_RC(Map3.setNull(), VINF_SUCCESS);
1943 RTTESTI_CHECK(Map3.size() == 0);
1944 RTTESTI_CHECK(Map3.isEmpty() == true);
1945 RTTESTI_CHECK(Map3.isNull() == true);
1946 RTTESTI_CHECK_RC(Map3.putCopy("not-null-anymore", MyRestInt16(1)), VINF_SUCCESS);
1947 verifyMap(Map3, __LINE__, 1, "not-null-anymore",1);
1948 }
1949 RTTESTI_CHECK(MyRestInt16::s_cInstances == 0);
1950
1951 {
1952 RTCRestStringMap<MyRestInt16> Map4;
1953 Map4.setNull();
1954 RTTESTI_CHECK_RC(Map4.setNull(), VINF_SUCCESS);
1955 RTTESTI_CHECK(Map4.size() == 0);
1956 RTTESTI_CHECK(Map4.isEmpty() == true);
1957 RTTESTI_CHECK(Map4.isNull() == true);
1958 RTCRestObjectBase *pNewBase = NULL;
1959 RTTESTI_CHECK_RC(Map4.putNewValue(&pNewBase, "putNewValue"), VINF_SUCCESS);
1960 verifyMap(Map4, __LINE__, 1, "putNewValue",0);
1961 }
1962 RTTESTI_CHECK(MyRestInt16::s_cInstances == 0);
1963}
1964
1965
1966class TestRequest : public RTCRestClientRequestBase
1967{
1968public:
1969 RTCRestString m_strValue;
1970 RTCRestInt64 m_iValue;
1971 RTCRestArray<RTCRestString> m_Array;
1972 RTCRestStringMap<RTCRestString> m_Map;
1973 /** @todo add more attributes. */
1974
1975 TestRequest(const char *a_pszValue, int64_t a_iValue, unsigned a_cElements, ...)
1976 : RTCRestClientRequestBase()
1977 , m_strValue(a_pszValue)
1978 , m_iValue(a_iValue)
1979 {
1980 m_fIsSet = UINT64_MAX;
1981 va_list va;
1982 va_start(va, a_cElements);
1983 for (unsigned i = 0; i < a_cElements; i++)
1984 m_Array.append(new RTCRestString(va_arg(va, const char *)));
1985 va_end(va);
1986 }
1987
1988 int resetToDefault() RT_OVERRIDE
1989 {
1990 m_strValue = "";
1991 m_iValue = 0;
1992 return m_Array.resetToDefault();
1993 }
1994
1995 int xmitPrepare(RTCString *a_pStrPath, RTCString *a_pStrQuery, RTHTTP a_hHttp, RTCString *a_pStrBody) const RT_OVERRIDE
1996 {
1997 RT_NOREF(a_pStrPath, a_pStrQuery, a_hHttp, a_pStrBody);
1998 return VINF_SUCCESS;
1999 }
2000
2001 void xmitComplete(int a_rcStatus, RTHTTP a_hHttp) const RT_OVERRIDE
2002 {
2003 RT_NOREF(a_rcStatus, a_hHttp);
2004 }
2005
2006 void testPath(const char *a_pszExpected)
2007 {
2008 static PATHPARAMDESC const s_aParams[] =
2009 {
2010 { RT_STR_TUPLE("{string}"), 0, 0 },
2011 { RT_STR_TUPLE("{integer}"), 0, 0 },
2012 { RT_STR_TUPLE("{array}"), 0, 0 },
2013 };
2014 PATHPARAMSTATE aState[] = { { &m_strValue, 0 }, { &m_iValue, 0 }, { &m_Array, 0 } };
2015 RTCString strPath;
2016 RTTESTI_CHECK_RC(doPathParameters(&strPath, RT_STR_TUPLE("my/{integer}/{string}/array:{array}/path"),
2017 s_aParams, aState, RT_ELEMENTS(aState)), VINF_SUCCESS);
2018 RTTESTI_CHECK_MSG(strPath.equals(a_pszExpected), ("actual: '%s'\nexpect: %s\n", strPath.c_str(), a_pszExpected));
2019 }
2020
2021 void testQuery(const char *a_pszCsv,
2022 const char *a_pszSsv,
2023 const char *a_pszTsv,
2024 const char *a_pszPipes,
2025 const char *a_pszMulti)
2026 {
2027 QUERYPARAMDESC aParams[] =
2028 {
2029 { "string", 0, true, 0 },
2030 { "integer", 0, true, 0 },
2031 { "array", 0, true, 0 },
2032 };
2033
2034 RTCRestObjectBase const *apObjects[] = { &m_strValue, &m_iValue, &m_Array };
2035 RTCString strQuery;
2036 RTTESTI_CHECK_RC(doQueryParameters(&strQuery, aParams, apObjects, RT_ELEMENTS(apObjects)), VINF_SUCCESS);
2037 RTTESTI_CHECK_MSG(strQuery.equals(a_pszCsv), ("actual: '%s'\nexpect: %s\n", strQuery.c_str(), a_pszCsv));
2038
2039 strQuery.setNull();
2040 aParams[2].fFlags = RTCRestObjectBase::kCollectionFormat_csv;
2041 RTTESTI_CHECK_RC(doQueryParameters(&strQuery, aParams, apObjects, RT_ELEMENTS(apObjects)), VINF_SUCCESS);
2042 RTTESTI_CHECK_MSG(strQuery.equals(a_pszCsv), ("actual: '%s'\nexpect: %s\n", strQuery.c_str(), a_pszCsv));
2043
2044 strQuery.setNull();
2045 aParams[2].fFlags = RTCRestObjectBase::kCollectionFormat_ssv;
2046 RTTESTI_CHECK_RC(doQueryParameters(&strQuery, aParams, apObjects, RT_ELEMENTS(apObjects)), VINF_SUCCESS);
2047 RTTESTI_CHECK_MSG(strQuery.equals(a_pszSsv), ("actual: '%s'\nexpect: %s\n", strQuery.c_str(), a_pszSsv));
2048
2049 strQuery.setNull();
2050 aParams[2].fFlags = RTCRestObjectBase::kCollectionFormat_tsv;
2051 RTTESTI_CHECK_RC(doQueryParameters(&strQuery, aParams, apObjects, RT_ELEMENTS(apObjects)), VINF_SUCCESS);
2052 RTTESTI_CHECK_MSG(strQuery.equals(a_pszTsv), ("actual: '%s'\nexpect: %s\n", strQuery.c_str(), a_pszTsv));
2053
2054 strQuery.setNull();
2055 aParams[2].fFlags = RTCRestObjectBase::kCollectionFormat_pipes;
2056 RTTESTI_CHECK_RC(doQueryParameters(&strQuery, aParams, apObjects, RT_ELEMENTS(apObjects)), VINF_SUCCESS);
2057 RTTESTI_CHECK_MSG(strQuery.equals(a_pszPipes), ("actual: '%s'\nexpect: %s\n", strQuery.c_str(), a_pszPipes));
2058
2059 strQuery.setNull();
2060 aParams[2].fFlags = RTCRestObjectBase::kCollectionFormat_multi;
2061 RTTESTI_CHECK_RC(doQueryParameters(&strQuery, aParams, apObjects, RT_ELEMENTS(apObjects)), VINF_SUCCESS);
2062 RTTESTI_CHECK_MSG(strQuery.equals(a_pszMulti), ("actual: '%s'\nexpect: %s\n", strQuery.c_str(), a_pszMulti));
2063 }
2064
2065 void testHeader(unsigned a_cHeaders, ...)
2066 {
2067 HEADERPARAMDESC aParams[] =
2068 {
2069 { "x-string", 0, true, 0, false },
2070 { "x-integer", 0, true, 0, false },
2071 { "x-array", 0, true, 0, false },
2072 { "x-map-", 0, true, 0, true },
2073 };
2074 RTCRestObjectBase const *apObjects[] = { &m_strValue, &m_iValue, &m_Array, &m_Map };
2075 RTHTTP hHttp = NIL_RTHTTP;
2076 RTTESTI_CHECK_RC(RTHttpCreate(&hHttp), VINF_SUCCESS);
2077 RTTESTI_CHECK_RC(doHeaderParameters(hHttp, aParams, apObjects, RT_ELEMENTS(apObjects)), VINF_SUCCESS);
2078 RTTESTI_CHECK(RTHttpGetHeaderCount(hHttp) == a_cHeaders);
2079 va_list va;
2080 va_start(va, a_cHeaders);
2081 for (size_t i = 0; i < a_cHeaders; i++)
2082 {
2083 const char *pszField = va_arg(va, const char *);
2084 const char *pszValue = va_arg(va, const char *);
2085 const char *pszActual = RTHttpGetHeader(hHttp, pszField, RTSTR_MAX);
2086 RTTESTI_CHECK_MSG(RTStrCmp(pszActual, pszValue) == 0,
2087 ("Header '%s' value is '%s' rather than '%s'", pszField, pszActual, pszValue));
2088 }
2089 va_end(va);
2090 RTTESTI_CHECK_RC(RTHttpDestroy(hHttp), VINF_SUCCESS);
2091 }
2092};
2093
2094
2095void testClientRequestBase()
2096{
2097 RTTestSub(g_hTest, "RTCRestClientRequestBase");
2098 {
2099 TestRequest Req1("this-is-a-string", 123456789, 5, "1", "22", "333", "444", "555");
2100 Req1.testPath("my/123456789/this-is-a-string/array:1,22,333,444,555/path");
2101 Req1.testQuery("?string=this-is-a-string&integer=123456789&array=1%2C22%2C333%2C444%2C555",
2102 "?string=this-is-a-string&integer=123456789&array=1%2022%20333%20444%20555",
2103 "?string=this-is-a-string&integer=123456789&array=1%0922%09333%09444%09555",
2104 "?string=this-is-a-string&integer=123456789&array=1%7C22%7C333%7C444%7C555",
2105 "?string=this-is-a-string&integer=123456789&array=1&array=22&array=333&array=444&array=555");
2106 Req1.testHeader(3, "x-string","this-is-a-string", "x-integer","123456789", "x-array","1,22,333,444,555");
2107 }
2108 {
2109 TestRequest Req2(";'[]", 42, 3, "null", "foo", "bar");
2110 RTTESTI_CHECK_RC(Req2.m_Map.put("stuff-1", new RTCRestString("stuffy-value-1")), VINF_SUCCESS);
2111 RTTESTI_CHECK_RC(Req2.m_Map.put("stuff-2", new RTCRestString("stuffy-value-2")), VINF_SUCCESS);
2112 RTTESTI_CHECK_RC(Req2.m_Map.put("2222", new RTCRestString("33")), VINF_SUCCESS);
2113 Req2.testPath("my/42/;'[]/array:null,foo,bar/path"); /// @todo shouldn't the string chars (;'[]) be escaped?
2114 Req2.testQuery("?string=%3B%27%5B%5D&integer=42&array=null%2Cfoo%2Cbar",
2115 "?string=%3B%27%5B%5D&integer=42&array=null%20foo%20bar",
2116 "?string=%3B%27%5B%5D&integer=42&array=null%09foo%09bar",
2117 "?string=%3B%27%5B%5D&integer=42&array=null%7Cfoo%7Cbar",
2118 "?string=%3B%27%5B%5D&integer=42&array=null&array=foo&array=bar");
2119 Req2.testHeader(6, "x-string",";'[]", "x-integer","42", "x-array","null,foo,bar",
2120 "x-map-stuff-1","stuffy-value-1",
2121 "x-map-stuff-2","stuffy-value-2",
2122 "x-map-2222","33");
2123 }
2124}
2125
2126
2127class TestResponse : public RTCRestClientResponseBase
2128{
2129public:
2130 RTCRestArray<RTCRestString> *m_pArray;
2131 RTCRestStringMap<RTCRestString> *m_pMap;
2132 RTCRestInt64 *m_pInteger;
2133 RTCRestString *m_pStrContentType;
2134
2135 TestResponse() : m_pArray(NULL), m_pMap(NULL), m_pInteger(NULL), m_pStrContentType(NULL)
2136 { }
2137
2138protected:
2139 virtual int consumeHeader(uint32_t a_uMatchWord, const char *a_pchField, size_t a_cchField,
2140 const char *a_pchValue, size_t a_cchValue) RT_OVERRIDE
2141 {
2142 int rc = RTCRestClientResponseBase::consumeHeader(a_uMatchWord, a_pchField, a_cchField, a_pchValue, a_cchValue);
2143 AssertRCReturn(rc, rc);
2144
2145#define MATCH_FIELD(a_sz) (sizeof(a_sz) - 1 == a_cchField && RTStrNICmpAscii(a_pchField, RT_STR_TUPLE(a_sz)) == 0)
2146 if (MATCH_FIELD("x-array"))
2147 {
2148 if (!m_pArray)
2149 {
2150 m_pArray = new (std::nothrow) RTCRestArray<RTCRestString>();
2151 AssertReturn(m_pArray, VERR_NO_MEMORY);
2152 return deserializeHeader(m_pArray, a_pchValue, a_cchValue, RTCRestObjectBase::kCollectionFormat_csv, "x-array");
2153 }
2154 }
2155 else if (a_cchField >= sizeof("x-map-") - 1 && RTStrNICmpAscii(a_pchField, RT_STR_TUPLE("x-map-")) == 0)
2156 {
2157 if (!m_pMap)
2158 {
2159 m_pMap = new (std::nothrow) RTCRestStringMap<RTCRestString>();
2160 AssertReturn(m_pMap, VERR_NO_MEMORY);
2161 }
2162 return deserializeHeaderIntoMap(m_pMap, a_pchField + 6, a_cchField - 6, a_pchValue, a_cchValue, 0, "x-map-");
2163 }
2164 else if (MATCH_FIELD("x-integer"))
2165 {
2166 if (!m_pInteger)
2167 {
2168 m_pInteger = new (std::nothrow) RTCRestInt64();
2169 AssertReturn(m_pInteger, VERR_NO_MEMORY);
2170 return deserializeHeader(m_pInteger, a_pchValue, a_cchValue, 0, "x-integer");
2171 }
2172 }
2173 else if (MATCH_FIELD("content-type"))
2174 {
2175 if (!m_pStrContentType)
2176 {
2177 m_pStrContentType = new (std::nothrow) RTCRestString();
2178 AssertReturn(m_pStrContentType, VERR_NO_MEMORY);
2179 return deserializeHeader(m_pStrContentType, a_pchValue, a_cchValue, 0, "content-type");
2180 }
2181 }
2182 else
2183 return VWRN_NOT_FOUND;
2184 RT_NOREF(a_uMatchWord);
2185 return addError(VERR_ALREADY_EXISTS, "Already have field '%.*s'!", a_cchField, a_pchField);
2186 }
2187
2188public:
2189 int pushHeader(const char *pszField, const char *pszValue)
2190 {
2191 size_t const cchField = strlen(pszField);
2192 void *pvFieldCopy = RTTestGuardedAllocTail(g_hTest, cchField);
2193 RTTESTI_CHECK_RET(pvFieldCopy, VERR_NO_MEMORY);
2194 memcpy(pvFieldCopy, pszField, cchField);
2195
2196 size_t const cchValue = strlen(pszValue);
2197 void *pvValueCopy = RTTestGuardedAllocTail(g_hTest, cchValue);
2198 RTTESTI_CHECK_RET(pvValueCopy, VERR_NO_MEMORY);
2199 memcpy(pvValueCopy, pszValue, cchValue);
2200
2201 uint32_t uWord = RTHTTP_MAKE_HDR_MATCH_WORD(cchField,
2202 cchField >= 1 ? RT_C_TO_LOWER(pszField[0]) : 0,
2203 cchField >= 2 ? RT_C_TO_LOWER(pszField[1]) : 0,
2204 cchField >= 3 ? RT_C_TO_LOWER(pszField[2]) : 0);
2205 int rc = consumeHeader(uWord, (const char *)pvFieldCopy, cchField, (const char *)pvValueCopy, cchValue);
2206 RTTestGuardedFree(g_hTest, pvValueCopy);
2207 RTTestGuardedFree(g_hTest, pvFieldCopy);
2208 return rc;
2209 }
2210};
2211
2212
2213void testClientResponseBase()
2214{
2215 RTTestSub(g_hTest, "RTCRestClientResponseBase");
2216 {
2217 TestResponse Resp1;
2218 RTTESTI_CHECK_RC(Resp1.pushHeader("content-type", "application/json; charset=utf-8"), VINF_SUCCESS);
2219 RTTESTI_CHECK(Resp1.getContentType().equals("application/json; charset=utf-8"));
2220 RTTESTI_CHECK(Resp1.m_pStrContentType && Resp1.m_pStrContentType->equals("application/json; charset=utf-8"));
2221
2222 RTTESTI_CHECK_RC(Resp1.pushHeader("content-typ2", "oopsy daisy"), VWRN_NOT_FOUND);
2223 RTTESTI_CHECK_RC(Resp1.pushHeader("content-type2", "oopsy daisy"), VWRN_NOT_FOUND);
2224 RTTESTI_CHECK(Resp1.getContentType().equals("application/json; charset=utf-8"));
2225 RTTESTI_CHECK(Resp1.m_pStrContentType && Resp1.m_pStrContentType->equals("application/json; charset=utf-8"));
2226
2227 RTTESTI_CHECK_RC(Resp1.pushHeader("x-integer", "398679406"), VINF_SUCCESS);
2228 RTTESTI_CHECK(Resp1.m_pInteger && Resp1.m_pInteger->m_iValue == 398679406);
2229
2230 RTTESTI_CHECK_RC(Resp1.pushHeader("x-array", "zero,one,two,three"), VINF_SUCCESS);
2231 RTTESTI_CHECK(Resp1.m_pArray && Resp1.m_pArray->size() == 4);
2232
2233 RTTESTI_CHECK_RC(Resp1.pushHeader("x-map-", "empty-key"), VINF_SUCCESS);
2234 RTTESTI_CHECK(Resp1.m_pMap && Resp1.m_pMap->size() == 1 && Resp1.m_pMap->get("") != NULL && Resp1.m_pMap->get("")->equals("empty-key"));
2235
2236 RTTESTI_CHECK_RC(Resp1.pushHeader("x-map-42", "key-is-42"), VINF_SUCCESS);
2237 RTTESTI_CHECK(Resp1.m_pMap && Resp1.m_pMap->size() == 2 && Resp1.m_pMap->get("42") != NULL && Resp1.m_pMap->get("42")->equals("key-is-42"));
2238 }
2239}
2240
2241
2242int main()
2243{
2244 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTRest-1", &g_hTest);
2245 if (rcExit == RTEXITCODE_SUCCESS )
2246 {
2247 testBool();
2248 testInteger<RTCRestInt64, int64_t, Int64Constants>();
2249 testInteger<RTCRestInt32, int32_t, Int32Constants>();
2250 testInteger<RTCRestInt16, int16_t, Int16Constants>();
2251 testDouble();
2252 testString("dummy", 1, 2);
2253 testDate();
2254 testArray();
2255 testStringMap();
2256 testClientRequestBase();
2257 testClientResponseBase();
2258
2259 rcExit = RTTestSummaryAndDestroy(g_hTest);
2260 }
2261 return rcExit;
2262}
2263
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette