-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
751 lines (672 loc) · 27.5 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//单元测试确保其他人修改代码后,原来的功能维持正确(这称为回归测试)。
//常用的单元测试框架有 xUnit 系列,如 C++ 的 Google Test、C# 的 NUnit。
//一般来说,软件开发是以周期进行的。例如,加入一个功能,再写关于该功能的单元测试。
//但也有另一种软件开发方法论,称为测试驱动开发TDD,它的主要循环步骤是:
//1.加入一个测试。
//2.编写、修改实现代码并运行所有测试,直到成功
//3.重构代码,加入新测试
//TDD 是先写测试再实现。好处是实现会刚好满足测试,而不会写了一些不需要的代码,或是没有被测试的代码。
//但无论我们是采用 TDD,或是先实现后测试,都应尽量加入足够覆盖率的单元测试。
//完全按照 TDD 的步骤来开发,会减慢开发进程。建议在这两种极端的工作方式取平衡。通常会在设计 API 后,先写部分测试再实现。
#ifdef _WINDOWS
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "leptjson.h"
static int main_ret = 0;
static int test_count = 0;
static int test_pass = 0;
#define EXPECT_EQ_BASE(equality, expect, actual, format) \
do {\
test_count++;\
if (equality)\
test_pass++;\
else {\
fprintf(stderr, "%s:%d: expect: " format " actual: " format "\n", __FILE__, __LINE__, expect, actual);\
main_ret = 1;\
}\
} while(0)
//如果 expect != actual(预期值!=实际值),便会输出错误信息
#define EXPECT_EQ_INT(expect, actual) EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%d")
#define EXPECT_EQ_DOUBLE(expect, actual) EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%.17g")
#define EXPECT_EQ_STRING(expect, actual, alength) \
EXPECT_EQ_BASE(sizeof(expect) - 1 == alength && memcmp(expect, actual, alength + 1) == 0, expect, actual, "%s")
#define EXPECT_TRUE(actual) EXPECT_EQ_BASE((actual) != 0, "true", "false", "%s")
#define EXPECT_FALSE(actual) EXPECT_EQ_BASE((actual) == 0, "false", "true", "%s")
#if defined(_MSC_VER)
#define EXPECT_EQ_SIZE_T(expect, actual) EXPECT_EQ_BASE((expect) == (actual), (size_t)expect, (size_t)actual, "%Iu")
#else
#define EXPECT_EQ_SIZE_T(expect, actual) EXPECT_EQ_BASE((expect) == (actual), (size_t)expect, (size_t)actual, "%zu")
#endif
//lept_parse() 只返回 LEPT_PARSE_OK:
//第一个返回 LEPT_PARSE_OK,所以是通过的。第二个测试因为 lept_parse() 没有把 v.type 改成 LEPT_NULL,造成失败。
//我们再实现 lept_parse() ,通过测试。
static void test_parse_null() {
lept_value v;
lept_init(&v);
lept_set_boolean(&v, 0);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "null"));
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
lept_free(&v);
}
static void test_parse_true() {
lept_value v;
lept_init(&v);
lept_set_boolean(&v, 0);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "true"));
EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(&v));
lept_free(&v);
}
static void test_parse_false() {
lept_value v;
lept_init(&v);
lept_set_boolean(&v, 1);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "false"));
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(&v));
lept_free(&v);
}
#define TEST_NUMBER(expect, json)\
do {\
lept_value v;\
lept_init(&v);\
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, json));\
EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(&v));\
EXPECT_EQ_DOUBLE(expect, lept_get_number(&v));\
lept_free(&v);\
} while(0)
//大部分情况下,测试案例不能穷举所有可能性。因此,除了加入一些典型的用例,我们也常会使用一些边界值,例如最大值等
static void test_parse_number() {
TEST_NUMBER(0.0, "0");
TEST_NUMBER(0.0, "-0");
TEST_NUMBER(0.0, "-0.0");
TEST_NUMBER(1.0, "1");
TEST_NUMBER(-1.0, "-1");
TEST_NUMBER(1.5, "1.5");
TEST_NUMBER(-1.5, "-1.5");
TEST_NUMBER(3.1416, "3.1416");
TEST_NUMBER(1E10, "1E10");
TEST_NUMBER(1e10, "1e10");
TEST_NUMBER(1E+10, "1E+10");
TEST_NUMBER(1E-10, "1E-10");
TEST_NUMBER(-1E10, "-1E10");
TEST_NUMBER(-1e10, "-1e10");
TEST_NUMBER(-1E+10, "-1E+10");
TEST_NUMBER(-1E-10, "-1E-10");
TEST_NUMBER(1.234E+10, "1.234E+10");
TEST_NUMBER(1.234E-10, "1.234E-10");
TEST_NUMBER(0.0, "1e-10000"); /* must underflow */
TEST_NUMBER(1.0000000000000002, "1.0000000000000002"); /* the smallest number > 1 */
TEST_NUMBER( 4.9406564584124654e-324, "4.9406564584124654e-324"); /* minimum denormal */
TEST_NUMBER(-4.9406564584124654e-324, "-4.9406564584124654e-324");
TEST_NUMBER( 2.2250738585072009e-308, "2.2250738585072009e-308"); /* Max subnormal double */
TEST_NUMBER(-2.2250738585072009e-308, "-2.2250738585072009e-308");
TEST_NUMBER( 2.2250738585072014e-308, "2.2250738585072014e-308"); /* Min normal positive double */
TEST_NUMBER(-2.2250738585072014e-308, "-2.2250738585072014e-308");
TEST_NUMBER( 1.7976931348623157e+308, "1.7976931348623157e+308"); /* Max double */
TEST_NUMBER(-1.7976931348623157e+308, "-1.7976931348623157e+308");
}
#define TEST_STRING(expect, json)\
do {\
lept_value v;\
lept_init(&v);\
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, json));\
EXPECT_EQ_INT(LEPT_STRING, lept_get_type(&v));\
EXPECT_EQ_STRING(expect, lept_get_string(&v), lept_get_string_length(&v));\
lept_free(&v);\
} while(0)
static void test_parse_string() {//字符串测试
TEST_STRING("", "\"\"");
TEST_STRING("Hello", "\"Hello\"");
TEST_STRING("Hello\nWorld", "\"Hello\\nWorld\"");
TEST_STRING("\" \\ / \b \f \n \r \t", "\"\\\" \\\\ \\/ \\b \\f \\n \\r \\t\"");
TEST_STRING("Hello\0World", "\"Hello\\u0000World\"");
TEST_STRING("\x24", "\"\\u0024\""); /* Dollar sign U+0024 */
TEST_STRING("\xC2\xA2", "\"\\u00A2\""); /* Cents sign U+00A2 */
TEST_STRING("\xE2\x82\xAC", "\"\\u20AC\""); /* Euro sign U+20AC */
TEST_STRING("\xF0\x9D\x84\x9E", "\"\\uD834\\uDD1E\""); /* G clef sign U+1D11E */
TEST_STRING("\xF0\x9D\x84\x9E", "\"\\ud834\\udd1e\""); /* G clef sign U+1D11E */
}
static void test_parse_array() {//数组测试
size_t i, j;
lept_value v;
lept_init(&v);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "[ ]"));
EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(&v));
EXPECT_EQ_SIZE_T(0, lept_get_array_size(&v));//验证 lept_get_array_size() 返回值是否正确
lept_free(&v);
lept_init(&v);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "[ null , false , true , 123 , \"abc\" ]"));
EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(&v));
EXPECT_EQ_SIZE_T(5, lept_get_array_size(&v));
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(lept_get_array_element(&v, 0)));
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(lept_get_array_element(&v, 1)));
EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(lept_get_array_element(&v, 2)));
EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(lept_get_array_element(&v, 3)));
EXPECT_EQ_INT(LEPT_STRING, lept_get_type(lept_get_array_element(&v, 4)));
EXPECT_EQ_DOUBLE(123.0, lept_get_number(lept_get_array_element(&v, 3)));
EXPECT_EQ_STRING("abc", lept_get_string(lept_get_array_element(&v, 4)), lept_get_string_length(lept_get_array_element(&v, 4)));
lept_free(&v);
lept_init(&v);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, "[ [ ] , [ 0 ] , [ 0 , 1 ] , [ 0 , 1 , 2 ] ]"));
EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(&v));
EXPECT_EQ_SIZE_T(4, lept_get_array_size(&v));
for (i = 0; i < 4; i++) {
lept_value* a = lept_get_array_element(&v, i);
EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(a));
EXPECT_EQ_SIZE_T(i, lept_get_array_size(a));
for (j = 0; j < i; j++) {
lept_value* e = lept_get_array_element(a, j);
EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(e));
EXPECT_EQ_DOUBLE((double)j, lept_get_number(e));
}
}
lept_free(&v);
}
static void test_parse_object() {
lept_value v;
size_t i;
lept_init(&v);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, " { } "));
EXPECT_EQ_INT(LEPT_OBJECT, lept_get_type(&v));
EXPECT_EQ_SIZE_T(0, lept_get_object_size(&v));
lept_free(&v);
lept_init(&v);
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v,
" { "
"\"n\" : null , "
"\"f\" : false , "
"\"t\" : true , "
"\"i\" : 123 , "
"\"s\" : \"abc\", "
"\"a\" : [ 1, 2, 3 ],"
"\"o\" : { \"1\" : 1, \"2\" : 2, \"3\" : 3 }"
" } "
));
EXPECT_EQ_INT(LEPT_OBJECT, lept_get_type(&v));
EXPECT_EQ_SIZE_T(7, lept_get_object_size(&v));
EXPECT_EQ_STRING("n", lept_get_object_key(&v, 0), lept_get_object_key_length(&v, 0));
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(lept_get_object_value(&v, 0)));
EXPECT_EQ_STRING("f", lept_get_object_key(&v, 1), lept_get_object_key_length(&v, 1));
EXPECT_EQ_INT(LEPT_FALSE, lept_get_type(lept_get_object_value(&v, 1)));
EXPECT_EQ_STRING("t", lept_get_object_key(&v, 2), lept_get_object_key_length(&v, 2));
EXPECT_EQ_INT(LEPT_TRUE, lept_get_type(lept_get_object_value(&v, 2)));
EXPECT_EQ_STRING("i", lept_get_object_key(&v, 3), lept_get_object_key_length(&v, 3));
EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(lept_get_object_value(&v, 3)));
EXPECT_EQ_DOUBLE(123.0, lept_get_number(lept_get_object_value(&v, 3)));
EXPECT_EQ_STRING("s", lept_get_object_key(&v, 4), lept_get_object_key_length(&v, 4));
EXPECT_EQ_INT(LEPT_STRING, lept_get_type(lept_get_object_value(&v, 4)));
EXPECT_EQ_STRING("abc", lept_get_string(lept_get_object_value(&v, 4)), lept_get_string_length(lept_get_object_value(&v, 4)));
EXPECT_EQ_STRING("a", lept_get_object_key(&v, 5), lept_get_object_key_length(&v, 5));
EXPECT_EQ_INT(LEPT_ARRAY, lept_get_type(lept_get_object_value(&v, 5)));
EXPECT_EQ_SIZE_T(3, lept_get_array_size(lept_get_object_value(&v, 5)));
for (i = 0; i < 3; i++) {
lept_value* e = lept_get_array_element(lept_get_object_value(&v, 5), i);
EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(e));
EXPECT_EQ_DOUBLE(i + 1.0, lept_get_number(e));
}
EXPECT_EQ_STRING("o", lept_get_object_key(&v, 6), lept_get_object_key_length(&v, 6));
{
lept_value* o = lept_get_object_value(&v, 6);
EXPECT_EQ_INT(LEPT_OBJECT, lept_get_type(o));
for (i = 0; i < 3; i++) {
lept_value* ov = lept_get_object_value(o, i);
EXPECT_TRUE('1' + i == lept_get_object_key(o, i)[0]);
EXPECT_EQ_SIZE_T(1, lept_get_object_key_length(o, i));
EXPECT_EQ_INT(LEPT_NUMBER, lept_get_type(ov));
EXPECT_EQ_DOUBLE(i + 1.0, lept_get_number(ov));
}
}
lept_free(&v);
}
//static void test_parse_expect_value() {
//lept_value v;
//v.type = LEPT_FALSE;
//EXPECT_EQ_INT(LEPT_PARSE_EXPECT_VALUE, lept_parse(&v, ""));
//EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
//v.type = LEPT_FALSE;
//EXPECT_EQ_INT(LEPT_PARSE_EXPECT_VALUE, lept_parse(&v, " "));
//EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
//}
#define TEST_PARSE_ERROR(error, json)\
do {\
lept_value v;\
lept_init(&v);\
v.type = LEPT_FALSE;\
EXPECT_EQ_INT(error, lept_parse(&v, json));\
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));\
lept_free(&v);\
} while(0)
static void test_parse_expect_value() {
TEST_PARSE_ERROR(LEPT_PARSE_EXPECT_VALUE, "");
TEST_PARSE_ERROR(LEPT_PARSE_EXPECT_VALUE, " ");
}
//不合法用例合集
static void test_parse_invalid_value() {
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "nul");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "?");
/* invalid number */
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "+0");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "+1");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, ".123"); /* at least one digit before '.' */
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "1."); /* at least one digit after '.' */
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "INF");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "inf");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "NAN");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "nan");
/* invalid value in array */
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "[1,]");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_VALUE, "[\"a\", nul]");
}
static void test_parse_root_not_singular() {
TEST_PARSE_ERROR(LEPT_PARSE_ROOT_NOT_SINGULAR, "null x");
/* invalid number */
TEST_PARSE_ERROR(LEPT_PARSE_ROOT_NOT_SINGULAR, "0123"); /* after zero should be '.' or nothing */
TEST_PARSE_ERROR(LEPT_PARSE_ROOT_NOT_SINGULAR, "0x0");
TEST_PARSE_ERROR(LEPT_PARSE_ROOT_NOT_SINGULAR, "0x123");
}
static void test_parse_number_too_big() {
TEST_PARSE_ERROR(LEPT_PARSE_NUMBER_TOO_BIG, "1e309");
TEST_PARSE_ERROR(LEPT_PARSE_NUMBER_TOO_BIG, "-1e309");
}
static void test_parse_miss_quotation_mark() {
TEST_PARSE_ERROR(LEPT_PARSE_MISS_QUOTATION_MARK, "\"");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_QUOTATION_MARK, "\"abc");
}
static void test_parse_invalid_string_escape() {
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\v\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\'\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\0\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_STRING_ESCAPE, "\"\\x12\"");
}
static void test_parse_invalid_string_char() {
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_STRING_CHAR, "\"\x01\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_STRING_CHAR, "\"\x1F\"");
}
static void test_parse_invalid_unicode_hex() {
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u0\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u01\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u012\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u/000\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\uG000\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u0/00\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u0G00\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u0/00\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u00G0\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u000/\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u000G\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_HEX, "\"\\u 123\"");
}
static void test_parse_invalid_unicode_surrogate() {
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_SURROGATE, "\"\\uD800\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_SURROGATE, "\"\\uDBFF\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_SURROGATE, "\"\\uD800\\\\\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_SURROGATE, "\"\\uD800\\uDBFF\"");
TEST_PARSE_ERROR(LEPT_PARSE_INVALID_UNICODE_SURROGATE, "\"\\uD800\\uE000\"");
}
static void test_parse_miss_comma_or_square_bracket() {
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_SQUARE_BRACKET, "[1");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_SQUARE_BRACKET, "[1}");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_SQUARE_BRACKET, "[1 2");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_SQUARE_BRACKET, "[[]");
}
static void test_parse_miss_key() {
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{:1,");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{1:1,");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{true:1,");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{false:1,");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{null:1,");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{[]:1,");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{{}:1,");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_KEY, "{\"a\":1,");
}
static void test_parse_miss_colon() {
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COLON, "{\"a\"}");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COLON, "{\"a\",\"b\"}");
}
static void test_parse_miss_comma_or_curly_bracket() {
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_CURLY_BRACKET, "{\"a\":1");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_CURLY_BRACKET, "{\"a\":1]");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_CURLY_BRACKET, "{\"a\":1 \"b\"");
TEST_PARSE_ERROR(LEPT_PARSE_MISS_COMMA_OR_CURLY_BRACKET, "{\"a\":{}");
}
static void test_parse() {
test_parse_null();
test_parse_true();
test_parse_false();
test_parse_number();
test_parse_string();
test_parse_array();
test_parse_object();
test_parse_expect_value();
test_parse_invalid_value();
test_parse_root_not_singular();
test_parse_number_too_big();
test_parse_miss_quotation_mark();
test_parse_invalid_string_escape();
test_parse_invalid_string_char();
test_parse_invalid_unicode_hex();
test_parse_invalid_unicode_surrogate();
test_parse_miss_comma_or_square_bracket();
test_parse_miss_key();
test_parse_miss_colon();
test_parse_miss_comma_or_curly_bracket();
}
#define TEST_ROUNDTRIP(json)\
do {\
lept_value v;\
char* json2;\
size_t length;\
lept_init(&v);\
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v, json));\
json2 = lept_stringify(&v, &length);\
EXPECT_EQ_STRING(json, json2, length);\
lept_free(&v);\
free(json2);\
} while(0)
static void test_stringify_number() {
TEST_ROUNDTRIP("0");
TEST_ROUNDTRIP("-0");
TEST_ROUNDTRIP("1");
TEST_ROUNDTRIP("-1");
TEST_ROUNDTRIP("1.5");
TEST_ROUNDTRIP("-1.5");
TEST_ROUNDTRIP("3.25");
TEST_ROUNDTRIP("1e+20");
TEST_ROUNDTRIP("1.234e+20");
TEST_ROUNDTRIP("1.234e-20");
TEST_ROUNDTRIP("1.0000000000000002"); /* the smallest number > 1 */
TEST_ROUNDTRIP("4.9406564584124654e-324"); /* minimum denormal */
TEST_ROUNDTRIP("-4.9406564584124654e-324");
TEST_ROUNDTRIP("2.2250738585072009e-308"); /* Max subnormal double */
TEST_ROUNDTRIP("-2.2250738585072009e-308");
TEST_ROUNDTRIP("2.2250738585072014e-308"); /* Min normal positive double */
TEST_ROUNDTRIP("-2.2250738585072014e-308");
TEST_ROUNDTRIP("1.7976931348623157e+308"); /* Max double */
TEST_ROUNDTRIP("-1.7976931348623157e+308");
}
static void test_stringify_string() {
TEST_ROUNDTRIP("\"\"");
TEST_ROUNDTRIP("\"Hello\"");
TEST_ROUNDTRIP("\"Hello\\nWorld\"");
TEST_ROUNDTRIP("\"\\\" \\\\ / \\b \\f \\n \\r \\t\"");
TEST_ROUNDTRIP("\"Hello\\u0000World\"");
}
static void test_stringify_array() {
TEST_ROUNDTRIP("[]");
TEST_ROUNDTRIP("[null,false,true,123,\"abc\",[1,2,3]]");
}
static void test_stringify_object() {
TEST_ROUNDTRIP("{}");
TEST_ROUNDTRIP("{\"n\":null,\"f\":false,\"t\":true,\"i\":123,\"s\":\"abc\",\"a\":[1,2,3],\"o\":{\"1\":1,\"2\":2,\"3\":3}}");
}
static void test_stringify() {//检测JSON生成器
//把一个 JSON 解析,然后再生成另一 JSON,逐字符比较两个 JSON 是否一模一样。这种测试称为往返测试
TEST_ROUNDTRIP("null");
TEST_ROUNDTRIP("false");
TEST_ROUNDTRIP("true");
test_stringify_number();
test_stringify_string();
test_stringify_array();
test_stringify_object();
}
#define TEST_EQUAL(json1, json2, equality) \
do {\
lept_value v1, v2;\
lept_init(&v1);\
lept_init(&v2);\
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v1, json1));\
EXPECT_EQ_INT(LEPT_PARSE_OK, lept_parse(&v2, json2));\
EXPECT_EQ_INT(equality, lept_is_equal(&v1, &v2));\
lept_free(&v1);\
lept_free(&v2);\
} while(0)
static void test_equal() {
TEST_EQUAL("true", "true", 1);
TEST_EQUAL("true", "false", 0);
TEST_EQUAL("false", "false", 1);
TEST_EQUAL("null", "null", 1);
TEST_EQUAL("null", "0", 0);
TEST_EQUAL("123", "123", 1);
TEST_EQUAL("123", "456", 0);
TEST_EQUAL("\"abc\"", "\"abc\"", 1);
TEST_EQUAL("\"abc\"", "\"abcd\"", 0);
TEST_EQUAL("[]", "[]", 1);
TEST_EQUAL("[]", "null", 0);
TEST_EQUAL("[1,2,3]", "[1,2,3]", 1);
TEST_EQUAL("[1,2,3]", "[1,2,3,4]", 0);
TEST_EQUAL("[[]]", "[[]]", 1);
TEST_EQUAL("{}", "{}", 1);
TEST_EQUAL("{}", "null", 0);
TEST_EQUAL("{}", "[]", 0);
TEST_EQUAL("{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2}", 1);
TEST_EQUAL("{\"a\":1,\"b\":2}", "{\"b\":2,\"a\":1}", 1);
TEST_EQUAL("{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":3}", 0);
TEST_EQUAL("{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2,\"c\":3}", 0);
TEST_EQUAL("{\"a\":{\"b\":{\"c\":{}}}}", "{\"a\":{\"b\":{\"c\":{}}}}", 1);
TEST_EQUAL("{\"a\":{\"b\":{\"c\":{}}}}", "{\"a\":{\"b\":{\"c\":[]}}}", 0);
}
static void test_copy() {
lept_value v1, v2;
lept_init(&v1);
lept_parse(&v1, "{\"t\":true,\"f\":false,\"n\":null,\"d\":1.5,\"a\":[1,2,3]}");
lept_init(&v2);
lept_copy(&v2, &v1);
EXPECT_TRUE(lept_is_equal(&v2, &v1));
lept_free(&v1);
lept_free(&v2);
}
static void test_move() {
lept_value v1, v2, v3;
lept_init(&v1);
lept_parse(&v1, "{\"t\":true,\"f\":false,\"n\":null,\"d\":1.5,\"a\":[1,2,3]}");
lept_init(&v2);
lept_copy(&v2, &v1);
lept_init(&v3);
lept_move(&v3, &v2);
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v2));
EXPECT_TRUE(lept_is_equal(&v3, &v1));
lept_free(&v1);
lept_free(&v2);
lept_free(&v3);
}
static void test_swap() {
lept_value v1, v2;
lept_init(&v1);
lept_init(&v2);
lept_set_string(&v1, "Hello", 5);
lept_set_string(&v2, "World!", 6);
lept_swap(&v1, &v2);
EXPECT_EQ_STRING("World!", lept_get_string(&v1), lept_get_string_length(&v1));
EXPECT_EQ_STRING("Hello", lept_get_string(&v2), lept_get_string_length(&v2));
lept_free(&v1);
lept_free(&v2);
}
static void test_access_null() {
lept_value v;
lept_init(&v);
lept_set_string(&v, "a", 1);
lept_set_null(&v);
EXPECT_EQ_INT(LEPT_NULL, lept_get_type(&v));
lept_free(&v);
}
static void test_access_boolean() {
lept_value v;
lept_init(&v);
lept_set_string(&v, "a", 1);
lept_set_boolean(&v, 1);
EXPECT_TRUE(lept_get_boolean(&v));
lept_set_boolean(&v, 0);
EXPECT_FALSE(lept_get_boolean(&v));
lept_free(&v);
}
static void test_access_number() {
lept_value v;
lept_init(&v);
lept_set_string(&v, "a", 1);
lept_set_number(&v, 1234.5);
EXPECT_EQ_DOUBLE(1234.5, lept_get_number(&v));
lept_free(&v);
}
//应用方的代码在调用 lept_parse() 之后,最终也应该调用 lept_free() 去释放内存。
//我们把之前的单元测试也加入此调用。
//如果不使用 lept_parse(),我们需要初始化值,那么就像以下的单元测试,先 lept_init(),最后 lept_free()。
static void test_access_string() {
lept_value v;
lept_init(&v);
lept_set_string(&v, "", 0);
EXPECT_EQ_STRING("", lept_get_string(&v), lept_get_string_length(&v));
lept_set_string(&v, "Hello", 5);
EXPECT_EQ_STRING("Hello", lept_get_string(&v), lept_get_string_length(&v));
lept_free(&v);
}
static void test_access_array() {
lept_value a, e;
size_t i, j;
lept_init(&a);
for (j = 0; j <= 5; j += 5) {
lept_set_array(&a, j);
EXPECT_EQ_SIZE_T(0, lept_get_array_size(&a));
EXPECT_EQ_SIZE_T(j, lept_get_array_capacity(&a));
for (i = 0; i < 10; i++) {
lept_init(&e);
lept_set_number(&e, i);
lept_move(lept_pushback_array_element(&a), &e);
lept_free(&e);
}
EXPECT_EQ_SIZE_T(10, lept_get_array_size(&a));
for (i = 0; i < 10; i++)
EXPECT_EQ_DOUBLE((double)i, lept_get_number(lept_get_array_element(&a, i)));
}
lept_popback_array_element(&a);
EXPECT_EQ_SIZE_T(9, lept_get_array_size(&a));
for (i = 0; i < 9; i++)
EXPECT_EQ_DOUBLE((double)i, lept_get_number(lept_get_array_element(&a, i)));
lept_erase_array_element(&a, 4, 0);
EXPECT_EQ_SIZE_T(9, lept_get_array_size(&a));
for (i = 0; i < 9; i++)
EXPECT_EQ_DOUBLE((double)i, lept_get_number(lept_get_array_element(&a, i)));
lept_erase_array_element(&a, 8, 1);
EXPECT_EQ_SIZE_T(8, lept_get_array_size(&a));
for (i = 0; i < 8; i++)
EXPECT_EQ_DOUBLE((double)i, lept_get_number(lept_get_array_element(&a, i)));
lept_erase_array_element(&a, 0, 2);
EXPECT_EQ_SIZE_T(6, lept_get_array_size(&a));
for (i = 0; i < 6; i++)
EXPECT_EQ_DOUBLE((double)i + 2, lept_get_number(lept_get_array_element(&a, i)));
#if 0
for (i = 0; i < 2; i++) {
lept_init(&e);
lept_set_number(&e, i);
lept_move(lept_insert_array_element(&a, i), &e);
lept_free(&e);
}
#endif
EXPECT_EQ_SIZE_T(8, lept_get_array_size(&a));
for (i = 0; i < 8; i++)
EXPECT_EQ_DOUBLE((double)i, lept_get_number(lept_get_array_element(&a, i)));
EXPECT_TRUE(lept_get_array_capacity(&a) > 8);
lept_shrink_array(&a);
EXPECT_EQ_SIZE_T(8, lept_get_array_capacity(&a));
EXPECT_EQ_SIZE_T(8, lept_get_array_size(&a));
for (i = 0; i < 8; i++)
EXPECT_EQ_DOUBLE((double)i, lept_get_number(lept_get_array_element(&a, i)));
lept_set_string(&e, "Hello", 5);
lept_move(lept_pushback_array_element(&a), &e); /* Test if element is freed */
lept_free(&e);
i = lept_get_array_capacity(&a);
lept_clear_array(&a);
EXPECT_EQ_SIZE_T(0, lept_get_array_size(&a));
EXPECT_EQ_SIZE_T(i, lept_get_array_capacity(&a)); /* capacity remains unchanged */
lept_shrink_array(&a);
EXPECT_EQ_SIZE_T(0, lept_get_array_capacity(&a));
lept_free(&a);
}
static void test_access_object() {
#if 0
lept_value o, v, *pv;
size_t i, j, index;
lept_init(&o);
for (j = 0; j <= 5; j += 5) {
lept_set_object(&o, j);
EXPECT_EQ_SIZE_T(0, lept_get_object_size(&o));
EXPECT_EQ_SIZE_T(j, lept_get_object_capacity(&o));
for (i = 0; i < 10; i++) {
char key[2] = "a";
key[0] += i;
lept_init(&v);
lept_set_number(&v, i);
lept_move(lept_set_object_value(&o, key, 1), &v);
lept_free(&v);
}
EXPECT_EQ_SIZE_T(10, lept_get_object_size(&o));
for (i = 0; i < 10; i++) {
char key[] = "a";
key[0] += i;
index = lept_find_object_index(&o, key, 1);
EXPECT_TRUE(index != LEPT_KEY_NOT_EXIST);
pv = lept_get_object_value(&o, index);
EXPECT_EQ_DOUBLE((double)i, lept_get_number(pv));
}
}
index = lept_find_object_index(&o, "j", 1);
EXPECT_TRUE(index != LEPT_KEY_NOT_EXIST);
lept_remove_object_value(&o, index);
index = lept_find_object_index(&o, "j", 1);
EXPECT_TRUE(index == LEPT_KEY_NOT_EXIST);
EXPECT_EQ_SIZE_T(9, lept_get_object_size(&o));
index = lept_find_object_index(&o, "a", 1);
EXPECT_TRUE(index != LEPT_KEY_NOT_EXIST);
lept_remove_object_value(&o, index);
index = lept_find_object_index(&o, "a", 1);
EXPECT_TRUE(index == LEPT_KEY_NOT_EXIST);
EXPECT_EQ_SIZE_T(8, lept_get_object_size(&o));
EXPECT_TRUE(lept_get_object_capacity(&o) > 8);
lept_shrink_object(&o);
EXPECT_EQ_SIZE_T(8, lept_get_object_capacity(&o));
EXPECT_EQ_SIZE_T(8, lept_get_object_size(&o));
for (i = 0; i < 8; i++) {
char key[] = "a";
key[0] += i + 1;
EXPECT_EQ_DOUBLE((double)i + 1, lept_get_number(lept_get_object_value(&o, lept_find_object_index(&o, key, 1))));
}
lept_set_string(&v, "Hello", 5);
lept_move(lept_set_object_value(&o, "World", 5), &v); /* Test if element is freed */
lept_free(&v);
pv = lept_find_object_value(&o, "World", 5);
EXPECT_TRUE(pv != NULL);
EXPECT_EQ_STRING("Hello", lept_get_string(pv), lept_get_string_length(pv));
i = lept_get_object_capacity(&o);
lept_clear_object(&o);
EXPECT_EQ_SIZE_T(0, lept_get_object_size(&o));
EXPECT_EQ_SIZE_T(i, lept_get_object_capacity(&o)); /* capacity remains unchanged */
lept_shrink_object(&o);
EXPECT_EQ_SIZE_T(0, lept_get_object_capacity(&o));
lept_free(&o);
#endif
}
static void test_access() {
test_access_null();
test_access_boolean();
test_access_number();
test_access_string();
test_access_array();
test_access_object();
}
int main() {
#ifdef _WINDOWS
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
test_parse();
test_stringify();
test_equal();
test_copy();
test_move();
test_swap();
test_access();
printf("%d/%d (%3.2f%%) passed\n", test_pass, test_count, test_pass * 100.0 / test_count);
return main_ret;
}