Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtest_flat_tree.cpp
More file actions
1581 lines (1323 loc) · 41.5 KB
/
Copy pathtest_flat_tree.cpp
File metadata and controls
1581 lines (1323 loc) · 41.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
/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com)
*
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE.txt)
*/
#include <boost/redis/adapter/adapt.hpp>
#include <boost/redis/resp3/flat_tree.hpp>
#include <boost/redis/resp3/node.hpp>
#include <boost/redis/resp3/parser.hpp>
#include <boost/redis/resp3/type.hpp>
#include <boost/assert/source_location.hpp>
#include <boost/config.hpp> // for a safe #include <version>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/span.hpp>
#include "print_node.hpp"
#include <algorithm>
#include <array>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <limits>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#if (__cpp_lib_ranges >= 201911L) && (__cpp_lib_concepts >= 202002L)
#define BOOST_REDIS_TEST_RANGE_CONCEPTS
#include <ranges>
#endif
using boost::redis::adapter::adapt2;
using boost::redis::adapter::result;
using boost::redis::resp3::tree;
using boost::redis::resp3::flat_tree;
using boost::redis::generic_flat_response;
using boost::redis::resp3::type;
using boost::redis::resp3::detail::deserialize;
using boost::redis::resp3::node;
using boost::redis::resp3::node_view;
using boost::redis::resp3::parser;
using boost::redis::resp3::to_string;
using boost::redis::response;
using boost::system::error_code;
namespace {
void add_nodes(
flat_tree& to,
std::string_view data,
boost::source_location loc = BOOST_CURRENT_LOCATION)
{
error_code ec;
deserialize(data, adapt2(to), ec);
if (!BOOST_TEST_EQ(ec, error_code{}))
std::cerr << "Called from " << loc << std::endl;
}
bool parse_checked(
flat_tree& to,
parser& p,
std::string_view data,
boost::source_location loc = BOOST_CURRENT_LOCATION)
{
error_code ec;
auto adapter = adapt2(to);
bool done = boost::redis::resp3::parse(p, data, adapter, ec);
if (!BOOST_TEST_EQ(ec, error_code{}))
std::cerr << "Called from " << loc << std::endl;
return done;
}
void check_nodes(
const flat_tree& tree,
boost::span<const node_view> expected,
boost::source_location loc = BOOST_CURRENT_LOCATION)
{
if (!BOOST_TEST_ALL_EQ(tree.begin(), tree.end(), expected.begin(), expected.end()))
std::cerr << "Called from " << loc << std::endl;
}
// --- Adding nodes ---
// Adding nodes works, even when reallocations happen.
// Empty nodes don't cause trouble
void test_add_nodes()
{
flat_tree t;
// Add a bunch of nodes. Single allocation. Some nodes are empty.
add_nodes(t, "*2\r\n+hello\r\n+world\r\n");
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
// Capacity will have raised to 512 bytes, at least. Add some more without reallocations
add_nodes(t, "$3\r\nbye\r\n");
expected_nodes.push_back({type::blob_string, 1u, 0u, "bye"});
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 13u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 2u);
// Add nodes above the first reallocation threshold. Node strings are still valid
const std::string long_value(600u, 'a');
add_nodes(t, "+" + long_value + "\r\n");
expected_nodes.push_back({type::simple_string, 1u, 0u, long_value});
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 613u);
BOOST_TEST_EQ(t.data_capacity(), 1024u);
BOOST_TEST_EQ(t.get_reallocs(), 2u);
BOOST_TEST_EQ(t.get_total_msgs(), 3u);
// Add some more nodes, still within the reallocation threshold
add_nodes(t, "+some_other_value\r\n");
expected_nodes.push_back({type::simple_string, 1u, 0u, "some_other_value"});
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 629u);
BOOST_TEST_EQ(t.data_capacity(), 1024u);
BOOST_TEST_EQ(t.get_reallocs(), 2u);
BOOST_TEST_EQ(t.get_total_msgs(), 4u);
// Add some more, causing another reallocation
add_nodes(t, "+" + long_value + "\r\n");
expected_nodes.push_back({type::simple_string, 1u, 0u, long_value});
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 1229u);
BOOST_TEST_EQ(t.data_capacity(), 2048u);
BOOST_TEST_EQ(t.get_reallocs(), 3u);
BOOST_TEST_EQ(t.get_total_msgs(), 5u);
}
// Strings are really copied into the object
void test_add_nodes_copies()
{
flat_tree t;
// Place the message in dynamic memory
constexpr std::string_view const_msg = "+some_long_value_for_a_node\r\n";
std::unique_ptr<char[]> data{new char[100]{}};
std::copy(const_msg.begin(), const_msg.end(), data.get());
// Add nodes pointing into this message
add_nodes(t, data.get());
// Invalidate the original message
data.reset();
// Check
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "some_long_value_for_a_node"},
};
check_nodes(t, expected_nodes);
}
// Reallocations happen only when we would exceed capacity
void test_add_nodes_capacity_limit()
{
flat_tree t;
// Add a node to reach capacity 512
add_nodes(t, "+hello\r\n");
BOOST_TEST_EQ(t.data_size(), 5u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
// Fill the rest of the capacity
add_nodes(t, "+" + std::string(507u, 'b') + "\r\n");
BOOST_TEST_EQ(t.data_size(), 512u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
// Adding an empty node here doesn't change capacity
add_nodes(t, "_\r\n");
BOOST_TEST_EQ(t.data_size(), 512u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
// Adding more data causes a reallocation
add_nodes(t, "+a\r\n");
BOOST_TEST_EQ(t.data_size(), 513u);
BOOST_TEST_EQ(t.data_capacity(), 1024);
// Same goes for the next capacity limit
add_nodes(t, "+" + std::string(511u, 'c') + "\r\n");
BOOST_TEST_EQ(t.data_size(), 1024);
BOOST_TEST_EQ(t.data_capacity(), 1024);
// Reallocation
add_nodes(t, "+u\r\n");
BOOST_TEST_EQ(t.data_size(), 1025u);
BOOST_TEST_EQ(t.data_capacity(), 2048u);
// This would continue
add_nodes(t, "+" + std::string(1024u, 'd') + "\r\n");
BOOST_TEST_EQ(t.data_size(), 2049u);
BOOST_TEST_EQ(t.data_capacity(), 4096u);
}
// It's no problem if a node is big enough to surpass several reallocation limits
void test_add_nodes_big_node()
{
flat_tree t;
// Add a bunch of nodes. Single allocation. Some nodes are empty.
const std::string long_value(1500u, 'h');
add_nodes(t, "+" + long_value + "\r\n");
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, long_value},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 1500u);
BOOST_TEST_EQ(t.data_capacity(), 2048u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// Flat trees have a temporary area (tmp) where nodes are stored while
// messages are being parsed. Nodes in the tmp area are not part of the representation
// until they are committed when the message has been fully parsed
void test_add_nodes_tmp()
{
flat_tree t;
parser p;
// Add part of a message, but not all of it.
// These nodes are stored but are not part of the user-facing representation
BOOST_TEST_NOT(parse_checked(t, p, "*2\r\n+hello\r\n"));
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// Finish the message. Nodes will now show up
BOOST_TEST(parse_checked(t, p, "*2\r\n+hello\r\n+world\r\n"));
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
// We can repeat this cycle again
p.reset();
BOOST_TEST_NOT(parse_checked(t, p, ">2\r\n+good\r\n"));
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
BOOST_TEST(parse_checked(t, p, ">2\r\n+good\r\n+bye\r\n"));
expected_nodes.push_back({type::push, 2u, 0u, ""});
expected_nodes.push_back({type::simple_string, 1u, 1u, "good"});
expected_nodes.push_back({type::simple_string, 1u, 1u, "bye"});
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 17u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_total_msgs(), 2u);
}
// If there was an unfinished message when another message is started,
// the former is discarded
void test_add_nodes_existing_tmp()
{
flat_tree t;
parser p;
// Add part of a message
BOOST_TEST_NOT(parse_checked(t, p, ">3\r\n+some message\r\n"));
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// This message is abandoned, and another one is started
p.reset();
BOOST_TEST_NOT(parse_checked(t, p, "%66\r\n+abandoned\r\n"));
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// This happens again, but this time a complete message is added
add_nodes(t, "*2\r\n+hello\r\n+world\r\n");
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// The same works even if there is existing committed data
void test_add_nodes_existing_data_and_tmp()
{
flat_tree t;
parser p;
// Add a full message
add_nodes(t, "*2\r\n+hello\r\n+world\r\n");
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
// Add part of a message
p.reset();
BOOST_TEST_NOT(parse_checked(t, p, "%66\r\n+abandoned\r\n"));
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
// This message is abandoned, and replaced by a full one
add_nodes(t, "+complete message\r\n");
expected_nodes.push_back({type::simple_string, 1u, 0u, "complete message"});
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 26u);
BOOST_TEST_EQ(t.get_total_msgs(), 2u);
}
// --- Reserving space ---
// The usual case, calling it before using it
void test_reserve()
{
flat_tree t;
t.reserve(1024u, 5u);
check_nodes(t, {});
BOOST_TEST_GE(t.capacity(), 5u);
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.data_capacity(), 1024);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// Adding some nodes now works
add_nodes(t, "+hello\r\n");
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "hello"},
};
check_nodes(t, expected_nodes);
}
// Reserving space uses the same allocation thresholds
void test_reserve_not_power_of_2()
{
flat_tree t;
// First threshold at 512
t.reserve(200u, 5u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
// Second threshold at 1024
t.reserve(600u, 5u);
BOOST_TEST_EQ(t.data_capacity(), 1024u);
BOOST_TEST_EQ(t.get_reallocs(), 2u);
}
// Requesting a capacity below the current one does nothing
void test_reserve_below_current_capacity()
{
flat_tree t;
// Reserving with a zero capacity does nothing
t.reserve(0u, 0u);
BOOST_TEST_EQ(t.data_capacity(), 0u);
BOOST_TEST_EQ(t.get_reallocs(), 0u);
// Increase capacity
t.reserve(400u, 5u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
// Reserving again does nothing
t.reserve(400u, 5u);
t.reserve(512u, 5u);
t.reserve(0u, 5u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
}
// Reserving might reallocate. If there are nodes, strings remain valid
void test_reserve_with_data()
{
flat_tree t;
// Add a bunch of nodes, and then reserve
add_nodes(t, "*2\r\n+hello\r\n+world\r\n");
t.reserve(1000u, 10u);
// Check
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.data_capacity(), 1024u);
BOOST_TEST_EQ(t.get_reallocs(), 2u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// Reserve also handles the tmp area
void test_reserve_with_tmp()
{
flat_tree t;
parser p;
// Add a partial message, and then reserve
BOOST_TEST_NOT(parse_checked(t, p, "*2\r\n+hello\r\n"));
t.reserve(1000u, 10u);
// Finish the current message so nodes in the tmp area show up
BOOST_TEST(parse_checked(t, p, "*2\r\n+hello\r\n+world\r\n"));
// Check
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.data_capacity(), 1024u);
BOOST_TEST_EQ(t.get_reallocs(), 2u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// --- Clear ---
void test_clear()
{
flat_tree t;
// Add a bunch of nodes, then clear
add_nodes(t, "*2\r\n+hello\r\n+world\r\n");
t.clear();
// Nodes are no longer there, but memory hasn't been fred
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
}
// Clearing an empty tree doesn't cause trouble
void test_clear_empty()
{
flat_tree t;
t.clear();
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.data_capacity(), 0u);
BOOST_TEST_EQ(t.get_reallocs(), 0u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
}
// With clear, memory can be reused
// The response should be reusable.
void test_clear_reuse()
{
flat_tree t;
// First use
add_nodes(t, "~6\r\n+orange\r\n+apple\r\n+one\r\n+two\r\n+three\r\n+orange\r\n");
std::vector<node_view> expected_nodes{
{type::set, 6u, 0u, "" },
{type::simple_string, 1u, 1u, "orange"},
{type::simple_string, 1u, 1u, "apple" },
{type::simple_string, 1u, 1u, "one" },
{type::simple_string, 1u, 1u, "two" },
{type::simple_string, 1u, 1u, "three" },
{type::simple_string, 1u, 1u, "orange"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
// Second use
t.clear();
add_nodes(t, "*2\r\n+hello\r\n+world\r\n");
expected_nodes = {
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// Clear doesn't remove the tmp area
void test_clear_tmp()
{
flat_tree t;
parser p;
// Add a full message and part of another
add_nodes(t, ">2\r\n+orange\r\n+apple\r\n");
BOOST_TEST_NOT(parse_checked(t, p, "*2\r\n+hello\r\n"));
std::vector<node_view> expected_nodes{
{type::push, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "orange"},
{type::simple_string, 1u, 1u, "apple" },
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
// Clearing removes the user-facing representation
t.clear();
check_nodes(t, {});
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// The nodes in the tmp area are still alive. Adding the remaining yields the full message
BOOST_TEST(parse_checked(t, p, "*2\r\n+hello\r\n+world\r\n"));
expected_nodes = {
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// Clearing having only tmp area is safe
void test_clear_only_tmp()
{
flat_tree t;
parser p;
// Add part of a message
BOOST_TEST_NOT(parse_checked(t, p, "*2\r\n+hello\r\n"));
check_nodes(t, {});
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// Clearing here does nothing
t.clear();
check_nodes(t, {});
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// The nodes in the tmp area are still alive. Adding the remaining yields the full message
BOOST_TEST(parse_checked(t, p, "*2\r\n+hello\r\n+world\r\n"));
std::vector<node_view> expected_nodes = {
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// Clearing having tmp nodes but no data is also safe
void test_clear_only_tmp_nodes()
{
flat_tree t;
parser p;
// Add part of a message
BOOST_TEST_NOT(parse_checked(t, p, "*2\r\n"));
check_nodes(t, {});
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// Clearing here does nothing
t.clear();
check_nodes(t, {});
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
// The nodes in the tmp area are still alive. Adding the remaining yields the full message
BOOST_TEST(parse_checked(t, p, "*2\r\n+hello\r\n+world\r\n"));
std::vector<node_view> expected_nodes = {
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// --- Default ctor ---
void test_default_constructor()
{
flat_tree t;
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.get_reallocs(), 0u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
}
// --- Copy ctor ---
void test_copy_ctor()
{
// Setup
auto t = std::make_unique<flat_tree>();
add_nodes(*t, "*2\r\n+hello\r\n+world\r\n");
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
// Construct, then destroy the original copy
flat_tree t2{*t};
t.reset();
// Check
check_nodes(t2, expected_nodes);
BOOST_TEST_EQ(t2.data_size(), 10u);
BOOST_TEST_EQ(t2.data_capacity(), 512u);
BOOST_TEST_EQ(t2.get_reallocs(), 1u);
BOOST_TEST_EQ(t2.get_total_msgs(), 1u);
}
// Copying an empty tree doesn't cause problems
void test_copy_ctor_empty()
{
flat_tree t;
flat_tree t2{t};
check_nodes(t2, {});
BOOST_TEST_EQ(t2.data_size(), 0u);
BOOST_TEST_EQ(t2.data_capacity(), 0u);
BOOST_TEST_EQ(t2.get_reallocs(), 0u);
BOOST_TEST_EQ(t2.get_total_msgs(), 0u);
}
// Copying an object that has no elements but some capacity doesn't cause trouble
void test_copy_ctor_empty_with_capacity()
{
flat_tree t;
t.reserve(300u, 8u);
flat_tree t2{t};
check_nodes(t2, {});
BOOST_TEST_EQ(t2.data_size(), 0u);
BOOST_TEST_EQ(t2.data_capacity(), 0u);
BOOST_TEST_EQ(t2.get_reallocs(), 0u);
BOOST_TEST_EQ(t2.get_total_msgs(), 0u);
}
// Copying an object with more capacity than required adjusts its capacity
void test_copy_ctor_adjust_capacity()
{
// Setup
flat_tree t;
add_nodes(t, "+hello\r\n");
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "hello"},
};
// Cause reallocations
t.reserve(1000u, 10u);
t.reserve(2000u, 10u);
t.reserve(4000u, 10u);
// Copy
flat_tree t2{t};
// The target object has the minimum required capacity,
// and the number of reallocs has been reset
check_nodes(t2, expected_nodes);
BOOST_TEST_EQ(t2.data_size(), 5u);
BOOST_TEST_EQ(t2.data_capacity(), 512u);
BOOST_TEST_EQ(t2.get_reallocs(), 1u);
BOOST_TEST_EQ(t2.get_total_msgs(), 1u);
}
// Copying an object also copies its tmp area
void test_copy_ctor_tmp()
{
// Setup
flat_tree t;
parser p;
add_nodes(t, "+message\r\n");
BOOST_TEST_NOT(parse_checked(t, p, "*2\r\n+hello\r\n"));
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "message"},
};
// Copy. The copy has the tmp nodes but they're hidden in its tmp area
flat_tree t2{t};
check_nodes(t2, expected_nodes);
BOOST_TEST_EQ(t2.data_size(), 7u);
BOOST_TEST_EQ(t2.get_total_msgs(), 1u);
// Finishing the message in the copy works
BOOST_TEST(parse_checked(t2, p, "*2\r\n+hello\r\n+world\r\n"));
expected_nodes = {
{type::simple_string, 1u, 0u, "message"},
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello" },
{type::simple_string, 1u, 1u, "world" },
};
check_nodes(t2, expected_nodes);
BOOST_TEST_EQ(t2.data_size(), 17u);
BOOST_TEST_EQ(t2.get_total_msgs(), 2u);
}
// --- Move ctor ---
void test_move_ctor()
{
flat_tree t;
add_nodes(t, "*2\r\n+hello\r\n+world\r\n");
flat_tree t2{std::move(t)};
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t2, expected_nodes);
BOOST_TEST_EQ(t2.data_size(), 10u);
BOOST_TEST_EQ(t2.data_capacity(), 512u);
BOOST_TEST_EQ(t2.get_reallocs(), 1u);
BOOST_TEST_EQ(t2.get_total_msgs(), 1u);
}
// Moving an empty object doesn't cause trouble
void test_move_ctor_empty()
{
flat_tree t;
flat_tree t2{std::move(t)};
check_nodes(t2, {});
BOOST_TEST_EQ(t2.data_size(), 0u);
BOOST_TEST_EQ(t2.data_capacity(), 0u);
BOOST_TEST_EQ(t2.get_reallocs(), 0u);
BOOST_TEST_EQ(t2.get_total_msgs(), 0u);
}
// Moving an object with capacity but no data doesn't cause trouble
void test_move_ctor_with_capacity()
{
flat_tree t;
t.reserve(1000u, 10u);
flat_tree t2{std::move(t)};
check_nodes(t2, {});
BOOST_TEST_EQ(t2.data_size(), 0u);
BOOST_TEST_EQ(t2.data_capacity(), 1024u);
BOOST_TEST_EQ(t2.get_reallocs(), 1u);
BOOST_TEST_EQ(t2.get_total_msgs(), 0u);
}
// Moving an object also moves its tmp area
void test_move_ctor_tmp()
{
// Setup
flat_tree t;
parser p;
add_nodes(t, "+message\r\n");
BOOST_TEST_NOT(parse_checked(t, p, "*2\r\n+hello\r\n"));
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "message"},
};
// Move. The new object has the same tmp area
flat_tree t2{std::move(t)};
check_nodes(t2, expected_nodes);
BOOST_TEST_EQ(t2.data_size(), 7u);
BOOST_TEST_EQ(t2.get_total_msgs(), 1u);
// Finishing the message in the copy works
BOOST_TEST(parse_checked(t2, p, "*2\r\n+hello\r\n+world\r\n"));
expected_nodes = {
{type::simple_string, 1u, 0u, "message"},
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello" },
{type::simple_string, 1u, 1u, "world" },
};
check_nodes(t2, expected_nodes);
BOOST_TEST_EQ(t2.data_size(), 17u);
BOOST_TEST_EQ(t2.get_total_msgs(), 2u);
}
// --- Copy assignment ---
void test_copy_assign()
{
flat_tree t;
add_nodes(t, "+some_data\r\n");
auto t2 = std::make_unique<flat_tree>();
add_nodes(*t2, "*2\r\n+hello\r\n+world\r\n");
t = *t2;
// Delete the source object, to check that we copied the contents
t2.reset();
std::vector<node_view> expected_nodes{
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello"},
{type::simple_string, 1u, 1u, "world"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 10u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// The lhs is empty and doesn't have any capacity
void test_copy_assign_target_empty()
{
flat_tree t;
flat_tree t2;
add_nodes(t2, "+hello\r\n");
t = t2;
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "hello"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 5u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// If the target doesn't have enough capacity, a reallocation happens
void test_copy_assign_target_not_enough_capacity()
{
flat_tree t;
add_nodes(t, "+hello\r\n");
const std::string big_node(2000u, 'a');
flat_tree t2;
add_nodes(t2, "+" + big_node + "\r\n");
t = t2;
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, big_node},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 2000u);
BOOST_TEST_EQ(t.data_capacity(), 2048u);
BOOST_TEST_EQ(t.get_reallocs(), 2u); // initial + assignment
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// If the source of the assignment is empty, nothing bad happens
void test_copy_assign_source_empty()
{
flat_tree t;
add_nodes(t, "+hello\r\n");
flat_tree t2;
t = t2;
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.data_capacity(), 512u); // capacity is kept
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
}
// If the source of the assignment has capacity but no data, we're OK
void test_copy_assign_source_with_capacity()
{
flat_tree t;
add_nodes(t, "+hello\r\n");
flat_tree t2;
t2.reserve(1000u, 4u);
t2.reserve(4000u, 8u);
t = t2;
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.data_capacity(), 512u); // capacity is kept
BOOST_TEST_EQ(t.get_reallocs(), 1u); // not propagated
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
}
// If the source of the assignment has data with extra capacity
// and a reallocation is needed, the minimum amount of space is allocated
void test_copy_assign_source_with_extra_capacity()
{
flat_tree t;
flat_tree t2;
add_nodes(t2, "+hello\r\n");
t2.reserve(4000u, 8u);
t = t2;
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "hello"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 5u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
void test_copy_assign_both_empty()
{
flat_tree t;
flat_tree t2;
t = t2;
check_nodes(t, {});
BOOST_TEST_EQ(t.data_size(), 0u);
BOOST_TEST_EQ(t.data_capacity(), 0u);
BOOST_TEST_EQ(t.get_reallocs(), 0u);
BOOST_TEST_EQ(t.get_total_msgs(), 0u);
}
// Self-assignment doesn't cause trouble
void test_copy_assign_self()
{
flat_tree t;
add_nodes(t, "+hello\r\n");
const auto& tref = t;
t = tref;
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "hello"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 5u);
BOOST_TEST_EQ(t.data_capacity(), 512u);
BOOST_TEST_EQ(t.get_reallocs(), 1u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
}
// Copy assignment also assigns the tmp area
void test_copy_assign_tmp()
{
parser p;
flat_tree t;
add_nodes(t, "+some_data\r\n");
flat_tree t2;
add_nodes(t2, "+message\r\n");
BOOST_TEST_NOT(parse_checked(t2, p, "*2\r\n+hello\r\n"));
// Assigning also copies where the tmp area starts
t = t2;
std::vector<node_view> expected_nodes{
{type::simple_string, 1u, 0u, "message"},
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 7u);
BOOST_TEST_EQ(t.get_total_msgs(), 1u);
// The tmp area was also copied
BOOST_TEST(parse_checked(t, p, "*2\r\n+hello\r\n+world\r\n"));
expected_nodes = {
{type::simple_string, 1u, 0u, "message"},
{type::array, 2u, 0u, "" },
{type::simple_string, 1u, 1u, "hello" },
{type::simple_string, 1u, 1u, "world" },
};
check_nodes(t, expected_nodes);
BOOST_TEST_EQ(t.data_size(), 17u);
BOOST_TEST_EQ(t.get_total_msgs(), 2u);
}
// --- Move assignment ---
void test_move_assign()
{
flat_tree t;
add_nodes(t, "+some_data\r\n");
flat_tree t2;
add_nodes(t2, "*2\r\n+hello\r\n+world\r\n");
t = std::move(t2);
std::vector<node_view> expected_nodes{
You can’t perform that action at this time.
