VirtualBox

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

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

IPRT/rest: More client request base tests. Added RTHttpGetHeaderCount and RTHttpGetByOrdinal for testing purposes. bugref:9167

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