-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathEncoderTests.cc
More file actions
1196 lines (1052 loc) · 42.5 KB
/
Copy pathEncoderTests.cc
File metadata and controls
1196 lines (1052 loc) · 42.5 KB
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// EncoderTests.cpp
//
// Copyright 2015-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
//
#include "FleeceTests.hh"
#include "Pointer.hh"
#include "JSONConverter.hh"
#include "KeyTree.hh"
#include "Path.hh"
#include "Internal.hh"
#include "jsonsl.h"
#include "mn_wordlist.h"
#include "NumConversion.hh"
#include <iostream>
#include "fleece/Fleece.hh"
#include <float.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
namespace fleece { namespace impl {
using namespace fleece::impl::internal;
class EncoderTests {
public:
EncoderTests()
:enc()
{ }
~EncoderTests() {
enc.reset();
}
Encoder enc;
alloc_slice result;
void endEncoding() {
enc.end();
result = enc.finish();
REQUIRE(result);
enc.reset();
}
template <bool WIDE>
uint32_t pointerOffset(const Value *v) const noexcept {
return v->_asPointer()->offset<WIDE>();
}
void checkOutput(const char *expected) {
endEncoding();
std::string hex;
constexpr size_t bufSize = 4;
for (size_t i = 0; i < result.size; i++) {
char str[bufSize];
snprintf(str, bufSize, "%02X", result[i]);
hex.append(str);
if (i % 2 && i != result.size-1)
hex.append(" ");
}
REQUIRE(hex == std::string(expected));
}
void checkReadBool(bool b) {
auto v = Value::fromData(result);
REQUIRE(v != nullptr);
REQUIRE(v->type() == kBoolean);
REQUIRE(v->asBool() == b);
REQUIRE(v->asInt() == (int64_t)b);
}
void checkRead(int64_t i) {
auto v = Value::fromData(result);
if (!v || v->type() != kNumber || !v->isInteger() || v->isUnsigned() || v->asInt() != i || v->asDouble() != (double)i) {
REQUIRE(v != nullptr);
REQUIRE(v->type() == kNumber);
REQUIRE(v->isInteger());
REQUIRE(!v->isUnsigned());
REQUIRE(v->asInt() == i);
REQUIRE(v->asDouble() == (double)i);
}
}
void checkReadU(uint64_t i) {
auto v = Value::fromData(result);
if (!v || v->type() != kNumber || !v->isInteger() || v->asUnsigned() != i || v->asDouble() != (double)i) {
REQUIRE(v != nullptr);
REQUIRE(v->type() == kNumber);
REQUIRE(v->isInteger());
REQUIRE(v->asUnsigned() == i);
REQUIRE(v->asDouble() == (double)i);
}
if (i >= (UINT64_MAX >> 1))
REQUIRE(v->isUnsigned());
}
void checkReadFloat(float f) {
auto v = Value::fromData(result);
REQUIRE(v != nullptr);
REQUIRE(v->type() == kNumber);
REQUIRE(!v->isDouble());
REQUIRE(v->asInt() == (int64_t)f);
REQUIRE(v->asFloat() == f);
REQUIRE(v->asDouble() == (double)f);
}
void checkReadDouble(double f) {
auto v = Value::fromData(result);
REQUIRE(v != nullptr);
REQUIRE(v->type() == kNumber);
REQUIRE(v->asInt() == (int64_t)f);
REQUIRE(v->asDouble() == f);
REQUIRE(v->asFloat() == (float)f);
}
void checkReadString(const char *str) {
auto v = Value::fromData(result);
REQUIRE(v != nullptr);
REQUIRE(v->type() == kString);
REQUIRE(v->asString() == slice(str, strlen(str)));
}
const Array* checkArray(uint32_t count) {
auto v = Value::fromData(result);
REQUIRE(v != nullptr);
REQUIRE(v->type() == kArray);
auto a = v->asArray();
REQUIRE(a != nullptr);
REQUIRE(a->count() == count);
return a;
}
const Dict* checkDict(uint32_t count) {
auto v = Value::fromData(result);
REQUIRE(v != nullptr);
REQUIRE(v->type() == kDict);
auto d = v->asDict();
REQUIRE(d != nullptr);
REQUIRE(d->count() == count);
return d;
}
void testArrayOfLength(unsigned length) {
enc.beginArray();
for (unsigned i = 0; i < length; ++i)
enc.writeUInt(i);
enc.endArray();
endEncoding();
// Check the contents:
auto a = checkArray(length);
for (unsigned i = 0; i < length; ++i) {
auto v = a->get(i);
if (!v || v->type() != kNumber || v->asUnsigned() != (uint64_t)i) {
REQUIRE(v);
REQUIRE(v->type() == kNumber);
REQUIRE(v->asUnsigned() == (uint64_t)i);
}
}
}
void checkReadAsJSON(const char *str) {
auto v = Value::fromData(result);
REQUIRE(v != nullptr);
CHECK(v->toJSONString() == std::string(str));
}
void checkJSONStr(std::string json,
slice expectedStr,
int expectedErr = JSONSL_ERROR_SUCCESS)
{
json = std::string("[\"") + json + std::string("\"]");
JSONConverter j(enc);
j.encodeJSON(slice(json));
REQUIRE(j.jsonError() == expectedErr);
if (j.jsonError()) {
enc.reset();
return;
}
endEncoding();
REQUIRE(expectedStr); // expected success
auto a = checkArray(1);
auto output = a->get(0)->asString();
REQUIRE(output == slice(expectedStr));
}
void checkJSONStr(std::string json,
const char *expectedStr,
int expectedErr = JSONSL_ERROR_SUCCESS)
{
checkJSONStr(json, (slice)expectedStr, expectedErr);
}
void lookupNameWithKey(const Dict* person, Dict::key &nameKey, std::string expectedName) {
REQUIRE(person);
const Value *name = person->get(nameKey);
REQUIRE(name);
std::string nameStr = (std::string)name->asString();
REQUIRE(nameStr == expectedName);
// Second lookup (using cache)
name = person->get(nameKey);
REQUIRE(name);
nameStr = (std::string)name->asString();
REQUIRE(nameStr == expectedName);
}
void writeKey(int key) {
enc.writeKey(key);
}
};
#pragma mark - TESTS
TEST_CASE_METHOD(EncoderTests, "Empty", "[Encoder]") {
REQUIRE(enc.isEmpty());
enc.beginArray();
REQUIRE(!enc.isEmpty());
enc.endArray();
Encoder enc2;
REQUIRE(enc2.isEmpty());
enc2 << 17;
REQUIRE(!enc2.isEmpty());
enc2.reset();
REQUIRE(enc2.isEmpty());
}
TEST_CASE_METHOD(EncoderTests, "Pointer", "[Encoder]") {
const uint8_t data[2] = {0x80, 0x02};
auto v = (const Value*)data;
REQUIRE(pointerOffset<false>(v) == 4u);
}
TEST_CASE_METHOD(EncoderTests, "Special", "[Encoder]") {
enc.writeNull(); checkOutput("3000");
enc.writeBool(false); checkOutput("3400"); checkReadBool(false);
enc.writeBool(true); checkOutput("3800"); checkReadBool(true);
}
TEST_CASE_METHOD(EncoderTests, "Ints", "[Encoder][Numeric]") {
enc.writeInt( 0); checkOutput("0000"); checkRead(0);
enc.writeInt( 128); checkOutput("0080"); checkRead(128);
enc.writeInt( 1234); checkOutput("04D2"); checkRead(1234);
enc.writeInt(-1234); checkOutput("0B2E"); checkRead(-1234);
enc.writeInt( 2047); checkOutput("07FF"); checkRead(2047);
enc.writeInt(-2048); checkOutput("0800"); checkRead(-2048);
enc.writeInt( 2048); checkOutput("1100 0800 8002"); checkRead(2048);
enc.writeInt(-2049); checkOutput("11FF F700 8002"); checkRead(-2049);
#if !FL_EMBEDDED // this takes too long on a puny microcontroller
for (int i = -66666; i <= 66666; ++i) {
enc.writeInt(i);
endEncoding();
checkRead(i);
}
for (unsigned i = 0; i <= 66666; ++i) {
enc.writeUInt(i);
endEncoding();
checkReadU(i);
}
#endif
enc.writeInt(12345678); checkOutput("134E 61BC 0000 8003"); checkRead(12345678);
enc.writeInt(-12345678);checkOutput("13B2 9E43 FF00 8003"); checkRead(-12345678);
enc.writeInt(0x223344); checkOutput("1244 3322 8002"); checkRead(0x223344);
enc.writeInt(0xBBCCDD); checkOutput("13DD CCBB 0000 8003"); checkRead(0xBBCCDD);
enc.writeInt(0x11223344556677); checkOutput("1677 6655 4433 2211 8004");
checkRead(0x11223344556677);
enc.writeInt(0x1122334455667788); checkOutput("1788 7766 5544 3322 1100 8005");
checkRead(0x1122334455667788);
enc.writeInt(-0x1122334455667788); checkOutput("1778 8899 AABB CCDD EE00 8005");
checkRead(-0x1122334455667788);
enc.writeUInt(0xCCBBAA9988776655); checkOutput("1F55 6677 8899 AABB CC00 8005");
checkReadU(0xCCBBAA9988776655);
enc.writeUInt(UINT64_MAX); checkOutput("1FFF FFFF FFFF FFFF FF00 8005");
checkReadU(UINT64_MAX);
for (int bits = 0; bits < 64; ++bits) {
int64_t i = 1LL << bits;
enc.writeInt(i); endEncoding(); checkRead(i);
if (bits < 63) {
enc.writeInt(-i); endEncoding(); checkRead(-i);
enc.writeInt(i - 1); endEncoding(); checkRead(i - 1);
enc.writeInt(1 - i); endEncoding(); checkRead(1 - i);
}
}
for (int bits = 0; bits < 64; ++bits) {
uint64_t i = 1LLU << bits;
enc.writeUInt(i); endEncoding(); checkReadU(i);
enc.writeUInt(i - 1); endEncoding(); checkReadU(i - 1);
}
}
TEST_CASE_METHOD(EncoderTests, "Floats", "[Encoder][Numeric]") {
enc.writeFloat( 0.5); checkOutput("2000 0000 003F 8003"); checkReadFloat( 0.5);
enc.writeFloat(-0.5); checkOutput("2000 0000 00BF 8003"); checkReadFloat(-0.5);
enc.writeFloat((float)M_PI); checkOutput("2000 DB0F 4940 8003"); checkReadFloat((float)M_PI);
enc.writeDouble(M_PI); checkOutput("2800 182D 4454 FB21 0940 8005"); checkReadDouble(M_PI);
#if 0 // Enable these if this becomes an option again
// Floats that get encoded as integers:
enc.writeFloat(0.0); checkOutput("0000"); checkReadFloat(0.0);
enc.writeFloat(-2048.0); checkOutput("0800"); checkReadFloat(-2048.0);
enc.writeFloat(0x223344); checkOutput("1244 3322 8002"); checkReadFloat(0x223344);
// Doubles that get encoded as integers:
enc.writeDouble(0.0); checkOutput("0000"); checkReadDouble(0.0);
enc.writeDouble(-2048.0); checkOutput("0800"); checkReadDouble(-2048.0);
enc.writeDouble(0x223344); checkOutput("1244 3322 8002"); checkReadDouble(0x223344);
#endif
// Doubles that get encoded as float:
enc.writeDouble( 0.5); checkOutput("2400 0000 003F 8003"); checkReadDouble( 0.5);
enc.writeDouble(-0.5); checkOutput("2400 0000 00BF 8003"); checkReadDouble(-0.5);
enc.writeDouble((float)M_PI); checkOutput("2400 DB0F 4940 8003"); checkReadDouble((float)M_PI);
// A long decimal that nevertheless has an exact float representation:
static_assert(307.79998779296875f == 307.8f);
enc.writeFloat(307.79998779296875);
checkOutput("2000 66E6 9943 8003");
checkReadFloat(307.79998779296875);
checkReadAsJSON("307.8");
// If we write the same as a double, it will be encoded as float but still act as a double:
static_assert(307.79998779296875 == 307.79998779296875f);
static_assert(307.79998779296875 != 307.8);
enc.writeDouble(307.79998779296875);
checkOutput("2400 66E6 9943 8003");
checkReadDouble(307.79998779296875);
checkReadAsJSON("307.79998779296875"); // <-- This used to fail; see #206
// This number is just barely not float-compatible (last digit is changed)
enc.writeDouble(307.7999877929688);
checkOutput("2800 0100 00C0 CC3C 7340 8005");
checkReadDouble(307.7999877929688);
checkReadAsJSON("307.7999877929688");
}
TEST_CASE_METHOD(EncoderTests, "Strings", "[Encoder]") {
enc.writeString(""); checkOutput("4000"); checkReadString("");
enc.writeString("a"); checkOutput("4161"); checkReadString("a");
enc.writeString("ab"); checkOutput("4261 6200 8002"); checkReadString("ab");
enc.writeString("abcdefghijklmn");
checkOutput("4E61 6263 6465 6667 6869 6A6B 6C6D 6E00 8008");
checkReadString("abcdefghijklmn");
enc.writeString("abcdefghijklmno");
checkOutput("4F0F 6162 6364 6566 6768 696A 6B6C 6D6E 6F00 8009");
checkReadString("abcdefghijklmno");
enc.writeString("abcdefghijklmnop");
checkOutput("4F10 6162 6364 6566 6768 696A 6B6C 6D6E 6F70 8009");
checkReadString("abcdefghijklmnop");
enc.writeString("müßchop"); checkOutput("496D C3BC C39F 6368 6F70 8005");
checkReadString("müßchop");
// Check a long string (long enough that length has multi-byte varint encoding):
char cstr[667];
memset(cstr, '@', 666);
cstr[666] = 0;
std::string str = cstr;
enc.writeString(str);
endEncoding();
checkReadString(cstr);
}
TEST_CASE_METHOD(EncoderTests, "Arrays", "[Encoder]") {
{
enc.beginArray();
enc.endArray();
checkOutput("6000");
checkArray(0);
}
{
enc.beginArray(1);
enc.writeNull();
enc.endArray();
checkOutput("6001 3000 8002");
auto a = checkArray(1);
auto v = a->get(0);
REQUIRE(v);
REQUIRE(v->type() == kNull);
}
{
enc.beginArray(2);
enc.writeString("a");
enc.writeString("hello");
enc.endArray();
checkOutput("4568 656C 6C6F 6002 4161 8005 8003");
// Check the contents:
auto a = checkArray(2);
auto v = a->get(0);
REQUIRE(v);
REQUIRE(v->type() == kString);
REQUIRE(v->asString() == "a"_sl);
v = a->get(1);
REQUIRE(v);
REQUIRE(v->type() == kString);
REQUIRE(v->asString() == "hello"_sl);
// Now use an iterator:
Array::iterator iter(a);
REQUIRE(iter);
REQUIRE(iter->type() == kString);
REQUIRE(iter->asString() == slice("a"));
++iter;
REQUIRE(iter);
REQUIRE(iter->type() == kString);
REQUIRE(iter->asString() == slice("hello"));
++iter;
REQUIRE(!iter);
REQUIRE(a->toJSON() == alloc_slice("[\"a\",\"hello\"]"));
}
#if 0
{
// Strings that can be inlined in a wide array:
enc.beginArray(2);
enc.writeString("ab");
enc.writeString("cde");
enc.endArray();
checkOutput("6802 4261 6200 4363 6465 8005");
}
#endif
}
TEST_CASE_METHOD(EncoderTests, "LongArrays", "[Encoder]") {
testArrayOfLength(0x7FE);
testArrayOfLength(0x7FF);
testArrayOfLength(0x800);
testArrayOfLength(0x801);
#if !FL_EMBEDDED
testArrayOfLength(0xFFFF);
#endif
}
TEST_CASE_METHOD(EncoderTests, "Dictionaries", "[Encoder]") {
{
enc.beginDictionary();
enc.endDictionary();
checkOutput("7000");
checkDict(0);
}
{
enc.beginDictionary();
enc.writeKey("f");
enc.writeInt(42);
enc.endDictionary();
checkOutput("7001 4166 002A 8003");
auto d = checkDict(1);
auto v = d->get(slice("f"));
REQUIRE(v);
REQUIRE(v->asInt() == 42ll);
REQUIRE(d->get(slice("barrr")) == (const Value*)nullptr);
REQUIRE(d->toJSON() == alloc_slice("{\"f\":42}"));
REQUIRE(d->toJSON<5>() == alloc_slice("{f:42}"));
}
{
enc.beginDictionary();
enc.writeKey("o-o");
enc.writeInt(42);
enc.endDictionary();
checkOutput("436F 2D6F 7001 8003 002A 8003");
auto d = checkDict(1);
auto v = d->get(slice("o-o"));
REQUIRE(v);
REQUIRE(v->asInt() == 42);
REQUIRE(d->get(slice("barrr")) == (const Value*)nullptr);
REQUIRE(d->toJSON() == alloc_slice("{\"o-o\":42}"));
REQUIRE(d->toJSON<5>() == alloc_slice("{\"o-o\":42}"));
}
}
#ifndef NDEBUG
TEST_CASE_METHOD(EncoderTests, "DictionaryNumericKeys", "[Encoder]") {
gDisableNecessarySharedKeysCheck = true;
{
enc.beginDictionary();
writeKey(0);
enc.writeInt(23);
writeKey(1);
enc.writeInt(42);
writeKey(2047);
enc.writeInt(-1);
enc.endDictionary();
checkOutput("7003 0000 0017 0001 002A 07FF 0FFF 8007");
auto d = checkDict(3);
auto v = d->get(0);
REQUIRE(v);
REQUIRE(v->asInt() == 23ll);
v = d->get(1);
REQUIRE(v);
REQUIRE(v->asInt() == 42ll);
v = d->get(2047);
REQUIRE(v);
REQUIRE(v->asInt() == -1ll);
REQUIRE(d->get(slice("barrr")) == (const Value*)nullptr);
REQUIRE(d->toJSON() == alloc_slice("{0:23,1:42,2047:-1}"));
}
gDisableNecessarySharedKeysCheck = false;
}
#endif
TEST_CASE_METHOD(EncoderTests, "Deep Nesting", "[Encoder]") {
for (int depth = 0; depth < 100; ++depth) {
enc.beginArray();
enc.writeInt(depth);
}
constexpr size_t bufSize = 20;
for (int depth = 0; depth < 100; ++depth) {
char str[bufSize];
snprintf(str, bufSize, "Hi there %d", depth);
enc.writeString(str);
enc.endArray();
}
endEncoding();
}
TEST_CASE_METHOD(EncoderTests, "SharedStrings", "[Encoder]") {
enc.beginArray(4);
enc.writeString("a");
enc.writeString("hello");
enc.writeString("a");
enc.writeString("hello");
enc.endArray();
checkOutput("4568 656C 6C6F 6004 4161 8005 4161 8007 8005");
auto a = checkArray(4);
REQUIRE(a->toJSON() == alloc_slice("[\"a\",\"hello\",\"a\",\"hello\"]"));
}
#if !FL_EMBEDDED
TEST_CASE("Widening Edge Case", "[Encoder]") {
// Tests an edge case in the Encoder's logic for widening an array/dict when a pointer
// reaches back 64KB. See couchbase/couchbase-lite-core#493
static constexpr size_t kMinStringLen = 60000, kMaxStringLen = 70000;
char *string = new char[kMaxStringLen];
memset(string, 'x', kMaxStringLen);
for (size_t stringLen = kMinStringLen; stringLen <= kMaxStringLen; ++stringLen) {
Encoder enc;
enc.beginArray();
enc.writeString("hi");
enc.writeString("there");
enc.writeString(slice{string, stringLen});
enc.endArray();
auto data = enc.finish();
}
delete [] string;
}
#endif
#pragma mark - JSON:
TEST_CASE_METHOD(EncoderTests, "JSONStrings", "[Encoder]") {
checkJSONStr("", "");
checkJSONStr("x", "x");
checkJSONStr("\\\"", "\"");
checkJSONStr("\"", nullptr, JSONConverter::kErrTruncatedJSON); // unterminated string
checkJSONStr("\\", nullptr, JSONConverter::kErrTruncatedJSON);
checkJSONStr("hi \\\"there\\\"", "hi \"there\"");
checkJSONStr("hi\\nthere", "hi\nthere");
checkJSONStr("H\\u0061ppy", "Happy");
checkJSONStr("H\\u0061", "Ha");
// Unicode escapes:
checkJSONStr("Price 50\\u00A2", "Price 50¢");
checkJSONStr("Price \\u20ac250", "Price €250");
checkJSONStr("Price \\uffff?", "Price \uffff?");
checkJSONStr("Price \\u20ac", "Price €");
checkJSONStr("!\\u0000!", "!\0!"_sl);
checkJSONStr("Price \\u20a", nullptr, JSONSL_ERROR_UESCAPE_TOOSHORT);
checkJSONStr("Price \\u20", nullptr, JSONSL_ERROR_UESCAPE_TOOSHORT);
checkJSONStr("Price \\u2", nullptr, JSONSL_ERROR_UESCAPE_TOOSHORT);
checkJSONStr("Price \\u", nullptr, JSONSL_ERROR_UESCAPE_TOOSHORT);
checkJSONStr("\\uzoop!", nullptr, JSONSL_ERROR_PERCENT_BADHEX);
// UTF-16 surrogate pair decoding:
checkJSONStr("lmao\\uD83D\\uDE1C!", "lmao😜!");
checkJSONStr("lmao\\uD83D", nullptr, JSONSL_ERROR_INVALID_CODEPOINT);
checkJSONStr("lmao\\uD83D\\n", nullptr, JSONSL_ERROR_INVALID_CODEPOINT);
checkJSONStr("lmao\\uD83D\\u", nullptr, JSONSL_ERROR_UESCAPE_TOOSHORT);
checkJSONStr("lmao\\uD83D\\u333", nullptr, JSONSL_ERROR_UESCAPE_TOOSHORT);
checkJSONStr("lmao\\uD83D\\u3333", nullptr, JSONSL_ERROR_INVALID_CODEPOINT);
checkJSONStr("lmao\\uDE1C\\uD83D!", nullptr, JSONSL_ERROR_INVALID_CODEPOINT);
}
TEST_CASE_METHOD(EncoderTests, "JSON", "[Encoder]") {
slice json("{\"\":\"hello\\nt\\\\here\","
"\"\\\"ironic\\\"\":[null,false,true,-100,0,100,123.456,6.02e+23,5e-06],"
"\"foo\":123}");
JSONConverter j(enc);
REQUIRE(j.encodeJSON(json));
endEncoding();
auto d = checkDict(3);
auto output = d->toJSON();
REQUIRE((slice)output == json);
}
TEST_CASE_METHOD(EncoderTests, "JSON parse numbers", "[Encoder]") {
slice json = "[9223372036854775807, -9223372036854775808, 18446744073709551615, "
"18446744073709551616, 602214076000000000000000, "
"-9999999999999999999]"_sl;
alloc_slice data = JSONConverter::convertJSON(json);
const Array *root = Value::fromTrustedData(data)->asArray();
CHECK(root->get(0)->isInteger());
CHECK(root->get(0)->asInt() == INT64_MAX);
CHECK(root->get(1)->isInteger());
CHECK(root->get(1)->asInt() == INT64_MIN);
CHECK(root->get(2)->isInteger());
CHECK(root->get(2)->asUnsigned() == UINT64_MAX);
CHECK(!root->get(3)->isInteger());
CHECK(root->get(3)->asDouble() == 18446744073709551616.0);
CHECK(!root->get(4)->isInteger());
CHECK(root->get(4)->asDouble() == 6.02214076e23);
CHECK(!root->get(5)->isInteger());
CHECK(root->get(5)->asDouble() == -9999999999999999999.0);
}
TEST_CASE_METHOD(EncoderTests, "JSONBinary", "[Encoder]") {
enc.beginArray();
enc.writeData(slice("not-really-binary"));
enc.endArray();
endEncoding();
auto json = Value::fromData(result)->toJSON();
REQUIRE(json == alloc_slice("[\"bm90LXJlYWxseS1iaW5hcnk=\"]"));
Writer w;
w.writeDecodedBase64(slice("bm90LXJlYWxseS1iaW5hcnk="));
REQUIRE(w.finish() == alloc_slice("not-really-binary"));
}
TEST_CASE_METHOD(EncoderTests, "Dump", "[Encoder]") {
std::string json = json5("{'foo':123,"
"'\"ironic\"':[null,false,true,-100,0,100,123.456,6.02e+23],"
"'':'hello\\nt\\\\here'}");
JSONConverter j(enc);
j.encodeJSON(slice(json));
endEncoding();
std::string dumped = Value::dump(result);
//std::cerr << dumped;
REQUIRE(dumped == std::string(
" 0000: 43 66 6f 6f : \"foo\"\n"
" 0004: 48 22 69 72…: \"\\\"ironic\\\"\"\n"
" 000e: 28 00 77 be…: 123.456\n"
" 0018: 28 00 61 d3…: 6.02e+23\n"
" 0022: 60 08 : Array [\n"
" 0024: 30 00 : null,\n"
" 0026: 34 00 : false,\n"
" 0028: 38 00 : true,\n"
" 002a: 0f 9c : -100,\n"
" 002c: 00 00 : 0,\n"
" 002e: 00 64 : 100,\n"
" 0030: 80 11 : &123.456 @000e,\n"
" 0032: 80 0d : &6.02e+23 @0018 ]\n"
" 0034: 4c 68 65 6c…: \"hello\\nt\\\\here\"\n"
" 0042: 70 03 : Dict {\n"
" 0044: 40 00 : \"\":\n"
" 0046: 80 09 : &\"hello\\nt\\\\here\" @0034,\n"
" 0048: 80 22 : &\"\\\"ironic\\\"\" @0004:\n"
" 004a: 80 14 : &Array @0022,\n"
" 004c: 80 26 : &\"foo\" @0000:\n"
" 004e: 00 7b : 123 }\n"
" 0050: 80 07 : &Dict @0042\n"));
}
TEST_CASE_METHOD(EncoderTests, "ConvertPeople", "[Encoder]") {
auto input = readTestFile(kBigJSONTestFileName);
enc.uniqueStrings(true);
JSONConverter jr(enc);
if (!jr.encodeJSON(input))
FAIL("JSON parse error at " << jr.errorPos());
#if 0
// Dump the string table and some statistics:
auto &strings = enc.strings();
strings.dump();
#endif
enc.end();
result = enc.finish();
#if FL_HAVE_TEST_FILES
REQUIRE(result.buf);
writeToFile(result, kTestFilesDir "1000people.fleece");
#endif
fprintf(stderr, "\nJSON size: %zu bytes; Fleece size: %zu bytes (%.2f%%)\n",
input.size, result.size, (result.size*100.0/input.size));
#ifndef NDEBUG
fprintf(stderr, "Narrow: %u, Wide: %u (total %u)\n", enc._numNarrow, enc._numWide, enc._numNarrow+enc._numWide);
fprintf(stderr, "Narrow count: %u, Wide count: %u (total %u)\n", enc._narrowCount, enc._wideCount, enc._narrowCount+enc._wideCount);
fprintf(stderr, "Used %u pointers to shared strings\n", enc._numSavedStrings);
#endif
}
#if FL_HAVE_TEST_FILES
TEST_CASE_METHOD(EncoderTests, "Encode To File", "[Encoder]") {
auto doc = readTestFile("1000people.fleece");
auto root = Value::fromTrustedData(doc)->asArray();
{
FILE *out = fopen(kTempDir"fleecetemp.fleece", "wb");
REQUIRE(out != nullptr);
Encoder fenc(out);
fenc.writeValue(root);
fenc.end();
fclose(out);
}
alloc_slice newDoc = readFile(kTempDir"fleecetemp.fleece");
REQUIRE(newDoc);
auto newRoot = Value::fromData(newDoc)->asArray();
REQUIRE(newRoot);
CHECK(newRoot->count() == root->count());
}
#endif
#if FL_HAVE_TEST_FILES
TEST_CASE_METHOD(EncoderTests, "FindPersonByIndexSorted", "[Encoder]") {
auto doc = readTestFile("1000people.fleece");
auto root = Value::fromTrustedData(doc)->asArray();
auto person = root->get(123)->asDict();
const Value *name = person->get(slice("name"));
REQUIRE(name);
std::string nameStr = (std::string)name->asString();
REQUIRE(nameStr == std::string("Concepcion Burns"));
}
#endif
TEST_CASE_METHOD(EncoderTests, "FindPersonByIndexKeyed", "[Encoder]") {
{
Dict::key nameKey(slice("name"));
// First build a small non-wide Dict:
enc.beginArray();
enc.beginDictionary();
enc.writeKey("f");
enc.writeInt(42);
enc.writeKey("name");
enc.writeString("Concepcion Burns");
enc.writeKey("x");
enc.writeBool(false);
enc.endDictionary();
enc.beginDictionary();
enc.writeKey("name");
enc.writeString("Carmen Miranda");
enc.writeKey("x");
enc.writeBool(false);
enc.endDictionary();
enc.beginDictionary();
enc.writeKey("nxme");
enc.writeString("Carmen Miranda");
enc.writeKey("x");
enc.writeBool(false);
enc.endDictionary();
enc.endArray();
endEncoding();
auto smol = Value::fromData(result)->asArray();
lookupNameWithKey(smol->get(0)->asDict(), nameKey, "Concepcion Burns");
lookupNameWithKey(smol->get(1)->asDict(), nameKey, "Carmen Miranda");
REQUIRE(smol->get(2)->asDict()->get(nameKey) == nullptr);
}
#if FL_HAVE_TEST_FILES
{
// Now try a wide Dict:
Dict::key nameKey(slice("name"));
auto doc = readTestFile("1000people.fleece");
auto root = Value::fromTrustedData(doc)->asArray();
auto person = root->get(123)->asDict();
lookupNameWithKey(person, nameKey, "Concepcion Burns");
person = root->get(3)->asDict();
lookupNameWithKey(person, nameKey, "Isabella Compton");
}
#endif
}
TEST_CASE_METHOD(EncoderTests, "Paths", "[Encoder]") {
auto input = readTestFile(kBigJSONTestFileName);
JSONConverter jr(enc);
jr.encodeJSON(input);
enc.end();
alloc_slice fleeceData = enc.finish();
const Value *root = Value::fromData(fleeceData);
CHECK(root->asArray()->count() == kBigJSONTestCount);
Path p1{"$[32].name"};
const Value *name = p1.eval(root);
REQUIRE(name);
REQUIRE(name->type() == kString);
REQUIRE(name->asString() == slice("Mendez Tran"));
Path p2{"[-1].name"};
name = p2.eval(root);
REQUIRE(name);
REQUIRE(name->type() == kString);
#if FL_HAVE_TEST_FILES
REQUIRE(name->asString() == slice("Marva Morse"));
#else // embedded test uses only 50 people, not 1000, so [-1] resolves differently
REQUIRE(name->asString() == slice("Tara Wall"));
#endif
}
TEST_CASE_METHOD(EncoderTests, "Resuse Encoder", "[Encoder]") {
enc.beginDictionary();
enc.writeKey("foo");
enc.writeInt(17);
enc.endDictionary();
auto data1 = enc.finish();
enc.beginDictionary();
enc.writeKey("bar");
enc.writeInt(23);
enc.endDictionary();
auto data2 = enc.finish();
enc.beginDictionary();
enc.writeKey("baz");
enc.writeInt(42);
enc.endDictionary();
auto data3 = enc.finish();
}
TEST_CASE_METHOD(EncoderTests, "Multi-Item", "[Encoder]") {
enc.suppressTrailer();
size_t pos[10];
unsigned n = 0;
enc.beginDictionary();
enc.writeKey("foo");
enc.writeInt(17);
enc.endDictionary();
pos[n++] = enc.finishItem();
enc.beginDictionary();
enc.writeKey("bar");
enc.writeInt(123456789);
enc.endDictionary();
pos[n++] = enc.finishItem();
enc.beginArray();
enc.writeBool(false);
enc.writeBool(true);
enc.endArray();
pos[n++] = enc.finishItem();
enc.writeString("LOL BUTTS"_sl);
pos[n++] = enc.finishItem();
enc.writeString("X"_sl);
pos[n++] = enc.finishItem();
enc.writeInt(17);
pos[n++] = enc.finishItem();
endEncoding();
pos[n] = result.size;
for (unsigned i = 0; i < n; i++)
CHECK(pos[i] < pos[i+1]);
CHECK(result.size == pos[n-1] + 2);
auto dict = (const Dict*)&result[pos[0]];
REQUIRE(dict->type() == kDict);
CHECK(dict->count() == 1);
REQUIRE(dict->get("foo"_sl));
CHECK(dict->get("foo"_sl)->asInt() == 17);
dict = (const Dict*)&result[pos[1]];
REQUIRE(dict->type() == kDict);
CHECK(dict->count() == 1);
REQUIRE(dict->get("bar"_sl));
CHECK(dict->get("bar"_sl)->asInt() == 123456789);
auto array = (const Array*)&result[pos[2]];
REQUIRE(array->type() == kArray);
REQUIRE(array->count() == 2);
CHECK(array->get(0)->type() == kBoolean);
CHECK(array->get(1)->type() == kBoolean);
auto str = (const Value*)&result[pos[3]];
REQUIRE(str->type() == kString);
CHECK(str->asString() == "LOL BUTTS"_sl);
str = (const Value*)&result[pos[4]];
REQUIRE(str->type() == kString);
CHECK(str->asString() == "X"_sl);
auto num = (const Value*)&result[pos[5]];
REQUIRE(num->type() == kNumber);
CHECK(num->asInt() == 17);
}
TEST_CASE_METHOD(EncoderTests, "Rewriting Values", "[Encoder]") {
CHECK(enc.lastValueWritten() == Encoder::PreWrittenValue::none); // no values yet
enc.beginArray();
enc.beginDictionary();
enc.writeKey("foo");
enc.writeInt(17);
CHECK(enc.lastValueWritten() == Encoder::PreWrittenValue::none); // it's inline
enc.endDictionary();
Encoder::PreWrittenValue wroteDict1 = enc.lastValueWritten();
REQUIRE(wroteDict1 != Encoder::PreWrittenValue::none);
enc.beginDictionary();
enc.writeKey("prev");
enc.writeValueAgain(wroteDict1);
enc.endDictionary();
enc.endArray();
checkOutput("4366 6F6F 7001 8003 0011 4470 7265 7600 7001 8004 8008 6002 800A 8005 8003");
Value::dump(result, std::cerr);
auto root = Value::fromData(result);
REQUIRE(root);
std::cerr << "JSON: " << root->toJSONString() << "\n";
CHECK(root->toJSONString() == "[{\"foo\":17},{\"prev\":{\"foo\":17}}]");
auto dict1 = root->asArray()->get(0);
auto dict2 = root->asArray()->get(1)->asDict()->get("prev");
std::cerr << "root[0] = " << (void*)dict1 << "\n";
std::cerr << "root[1].prev = " << (void*)dict2 << "\n";
CHECK(dict1 == dict2);
}
TEST_CASE_METHOD(EncoderTests, "Snip", "[Encoder]") {
enc.beginArray();
enc.beginDictionary();
enc.writeKey("part");
enc.writeInt(1);
enc.writeKey("name");
enc.writeString("Snip Test");
enc.endDictionary();
alloc_slice part1 = enc.snip();
REQUIRE(part1);
auto val1 = Value::fromData(part1);
REQUIRE(val1);
std::cerr << "--Part 1--\n";
Value::dump(part1, std::cerr);
CHECK(val1->toJSONString() == R"({"name":"Snip Test","part":1})");
enc.beginDictionary();
enc.writeKey("part");
enc.writeInt(2);
enc.writeKey("name");
enc.writeString("Snip Test");
enc.endDictionary();
enc.endArray();
endEncoding();
REQUIRE(result);
Retained<Doc> doc = new Doc(result, Doc::kUntrusted, nullptr, part1);
CHECK(doc->root()->toJSONString() == R"([{"name":"Snip Test","part":1},{"name":"Snip Test","part":2}])");
std::cerr << "--Part 2--\n";
Value::dump(result, std::cerr);
// Make sure the string "Snip Test" was encoded only once:
CHECK(doc->root()->asArray()->get(0)->asDict()->get("name") ==
doc->root()->asArray()->get(1)->asDict()->get("name"));
}
#pragma mark - KEY TREE:
TEST_CASE_METHOD(EncoderTests, "KeyTree", "[Encoder]") {
bool verbose = false;
char eeeeeeee[1024] = "";
memset(&eeeeeeee[0], 'e', sizeof(eeeeeeee)-1);
#if 1
const size_t n = sizeof(mn_words)/sizeof(char*);
const char* rawStrings[n];
memcpy(rawStrings, mn_words, sizeof(mn_words));
rawStrings[0] = eeeeeeee;
#else
const char* rawStrings[] = {"alphabetically first", "bravo", "charlie", "delta", eeeeeeee,
"foxtrot", "ganon in pig form as slain by link at the end of ocarina of time", "hi",
"i", "jodhpur",
"kale is not one of my favorite vegetables, too bitter, though I respect its high vitamin/mineral content",
"lemon", "maxwell edison, majoring in medicine, calls her on the phone", "naomi",
"obey", "purple", "quorum", "roger", "snake", "tango", "umpqua", "vector", "whiskey",
"xerxes", "yellow", "zebra"};
size_t n = sizeof(rawStrings)/sizeof(char*);
#endif
std::vector<slice> strings(n);
size_t totalLen = 0;