forked from abseil/abseil-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cord_ring_test.cc
1495 lines (1343 loc) · 56.7 KB
/
cord_ring_test.cc
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 2020 The Abseil Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdlib>
#include <ctime>
#include <memory>
#include <random>
#include <sstream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/macros.h"
#include "absl/debugging/leak_check.h"
#include "absl/strings/internal/cord_internal.h"
#include "absl/strings/internal/cord_rep_ring.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
extern thread_local bool cord_ring;
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace {
using RandomEngine = std::mt19937_64;
using ::absl::cord_internal::CordRep;
using ::absl::cord_internal::CordRepConcat;
using ::absl::cord_internal::CordRepExternal;
using ::absl::cord_internal::CordRepFlat;
using ::absl::cord_internal::CordRepRing;
using ::absl::cord_internal::CordRepSubstring;
using ::absl::cord_internal::CONCAT;
using ::absl::cord_internal::EXTERNAL;
using ::absl::cord_internal::SUBSTRING;
using testing::ElementsAre;
using testing::ElementsAreArray;
using testing::Eq;
using testing::Ge;
using testing::Le;
using testing::Lt;
using testing::Ne;
using testing::SizeIs;
using index_type = CordRepRing::index_type;
enum InputShareMode { kPrivate, kShared, kSharedIndirect };
// TestParam class used by all test fixtures.
// Not all fixtures use all possible input combinations
struct TestParam {
TestParam() = default;
explicit TestParam(InputShareMode input_share_mode)
: input_share_mode(input_share_mode) {}
// Run the test with the 'rep under test' to be privately owned.
// Otherwise, the rep has a shared ref count of 2 or higher.
bool refcount_is_one = true;
// Run the test with the 'rep under test' being allocated with enough capacity
// to accommodate any modifications made to it. Otherwise, the rep has zero
// extra (reserve) capacity.
bool with_capacity = true;
// For test providing possibly shared input such as Append(.., CordpRep*),
// this field defines if that input is adopted with a refcount of one
// (privately owned / donated), or shared. For composite inputs such as
// 'substring of flat', we also have the 'shared indirect' value which means
// the top level node is not shared, but the contained child node is shared.
InputShareMode input_share_mode = kPrivate;
std::string ToString() const {
return absl::StrCat(refcount_is_one ? "Private" : "Shared",
with_capacity ? "" : "_NoCapacity",
(input_share_mode == kPrivate) ? ""
: (input_share_mode == kShared)
? "_SharedInput"
: "_IndirectSharedInput");
}
};
using TestParams = std::vector<TestParam>;
// Matcher validating when mutable copies are required / performed.
MATCHER_P2(EqIfPrivate, param, rep,
absl::StrCat("Equal 0x", absl::Hex(rep), " if private")) {
return param.refcount_is_one ? arg == rep : true;
}
// Matcher validating when mutable copies are required / performed.
MATCHER_P2(EqIfPrivateAndCapacity, param, rep,
absl::StrCat("Equal 0x", absl::Hex(rep),
" if private and capacity")) {
return (param.refcount_is_one && param.with_capacity) ? arg == rep : true;
}
// Matcher validating a shared ring was re-allocated. Should only be used for
// tests doing exactly one update as subsequent updates could return the
// original (freed and re-used) pointer.
MATCHER_P2(NeIfShared, param, rep,
absl::StrCat("Not equal 0x", absl::Hex(rep), " if shared")) {
return param.refcount_is_one ? true : arg != rep;
}
MATCHER_P2(EqIfInputPrivate, param, rep, "Equal if input is private") {
return param.input_share_mode == kPrivate ? arg == rep : arg != rep;
}
// Matcher validating the core in-variants of the CordRepRing instance.
MATCHER(IsValidRingBuffer, "RingBuffer is valid") {
std::stringstream ss;
if (!arg->IsValid(ss)) {
*result_listener << "\nERROR: " << ss.str() << "\nRING = " << *arg;
return false;
}
return true;
}
// Returns the flats contained in the provided CordRepRing
std::vector<string_view> ToFlats(const CordRepRing* r) {
std::vector<string_view> flats;
flats.reserve(r->entries());
index_type pos = r->head();
do {
flats.push_back(r->entry_data(pos));
} while ((pos = r->advance(pos)) != r->tail());
return flats;
}
class not_a_string_view {
public:
explicit not_a_string_view(absl::string_view s)
: data_(s.data()), size_(s.size()) {}
explicit not_a_string_view(const void* data, size_t size)
: data_(data), size_(size) {}
not_a_string_view remove_prefix(size_t n) const {
return not_a_string_view(static_cast<const char*>(data_) + n, size_ - n);
}
not_a_string_view remove_suffix(size_t n) const {
return not_a_string_view(data_, size_ - n);
}
const void* data() const { return data_; }
size_t size() const { return size_; }
private:
const void* data_;
size_t size_;
};
bool operator==(not_a_string_view lhs, not_a_string_view rhs) {
return lhs.data() == rhs.data() && lhs.size() == rhs.size();
}
std::ostream& operator<<(std::ostream& s, not_a_string_view rhs) {
return s << "{ data: " << rhs.data() << " size: " << rhs.size() << "}";
}
std::vector<not_a_string_view> ToRawFlats(const CordRepRing* r) {
std::vector<not_a_string_view> flats;
flats.reserve(r->entries());
index_type pos = r->head();
do {
flats.emplace_back(r->entry_data(pos));
} while ((pos = r->advance(pos)) != r->tail());
return flats;
}
// Returns the value contained in the provided CordRepRing
std::string ToString(const CordRepRing* r) {
std::string value;
value.reserve(r->length);
index_type pos = r->head();
do {
absl::string_view sv = r->entry_data(pos);
value.append(sv.data(), sv.size());
} while ((pos = r->advance(pos)) != r->tail());
return value;
}
// Creates a flat for testing
CordRep* MakeFlat(absl::string_view s, size_t extra = 0) {
CordRepFlat* flat = CordRepFlat::New(s.length() + extra);
memcpy(flat->Data(), s.data(), s.length());
flat->length = s.length();
return flat;
}
// Creates an external node for testing
CordRepExternal* MakeExternal(absl::string_view s) {
struct Rep : public CordRepExternal {
std::string s;
explicit Rep(absl::string_view s) : s(s) {
this->tag = EXTERNAL;
this->base = s.data();
this->length = s.length();
this->releaser_invoker = [](CordRepExternal* self) {
delete static_cast<Rep*>(self);
};
}
};
return new Rep(s);
}
CordRepExternal* MakeFakeExternal(size_t length) {
struct Rep : public CordRepExternal {
std::string s;
explicit Rep(size_t len) {
this->tag = EXTERNAL;
this->base = reinterpret_cast<const char*>(this->storage);
this->length = len;
this->releaser_invoker = [](CordRepExternal* self) {
delete static_cast<Rep*>(self);
};
}
};
return new Rep(length);
}
// Creates a flat or an external node for testing depending on the size.
CordRep* MakeLeaf(absl::string_view s, size_t extra = 0) {
if (s.size() <= absl::cord_internal::kMaxFlatLength) {
return MakeFlat(s, extra);
} else {
return MakeExternal(s);
}
}
// Creates a substring node
CordRepSubstring* MakeSubstring(size_t start, size_t len, CordRep* rep) {
auto* sub = new CordRepSubstring;
sub->tag = SUBSTRING;
sub->start = start;
sub->length = (len <= 0) ? rep->length - start + len : len;
sub->child = rep;
return sub;
}
// Creates a substring node removing the specified prefix
CordRepSubstring* RemovePrefix(size_t start, CordRep* rep) {
return MakeSubstring(start, rep->length - start, rep);
}
// Creates a substring node removing the specified suffix
CordRepSubstring* RemoveSuffix(size_t length, CordRep* rep) {
return MakeSubstring(0, rep->length - length, rep);
}
CordRepConcat* MakeConcat(CordRep* left, CordRep* right, int depth = 0) {
auto* concat = new CordRepConcat;
concat->tag = CONCAT;
concat->length = left->length + right->length;
concat->left = left;
concat->right = right;
concat->set_depth(depth);
return concat;
}
enum Composition { kMix, kAppend, kPrepend };
Composition RandomComposition() {
RandomEngine rng(GTEST_FLAG_GET(random_seed));
return (rng() & 1) ? kMix : ((rng() & 1) ? kAppend : kPrepend);
}
absl::string_view ToString(Composition composition) {
switch (composition) {
case kAppend:
return "Append";
case kPrepend:
return "Prepend";
case kMix:
return "Mix";
}
assert(false);
return "???";
}
constexpr const char* kFox = "The quick brown fox jumps over the lazy dog";
constexpr const char* kFoxFlats[] = {"The ", "quick ", "brown ",
"fox ", "jumps ", "over ",
"the ", "lazy ", "dog"};
constexpr const char* kAlphabet = "abcdefghijklmnopqrstuvwxyz";
CordRepRing* FromFlats(Span<const char* const> flats,
Composition composition = kAppend) {
if (flats.empty()) return nullptr;
CordRepRing* ring = nullptr;
switch (composition) {
case kAppend:
ring = CordRepRing::Create(MakeLeaf(flats.front()), flats.size() - 1);
for (int i = 1; i < flats.size(); ++i) {
ring = CordRepRing::Append(ring, MakeLeaf(flats[i]));
}
break;
case kPrepend:
ring = CordRepRing::Create(MakeLeaf(flats.back()), flats.size() - 1);
for (int i = static_cast<int>(flats.size() - 2); i >= 0; --i) {
ring = CordRepRing::Prepend(ring, MakeLeaf(flats[i]));
}
break;
case kMix:
size_t middle1 = flats.size() / 2, middle2 = middle1;
ring = CordRepRing::Create(MakeLeaf(flats[middle1]), flats.size() - 1);
if (!flats.empty()) {
if ((flats.size() & 1) == 0) {
ring = CordRepRing::Prepend(ring, MakeLeaf(flats[--middle1]));
}
for (int i = 1; i <= middle1; ++i) {
ring = CordRepRing::Prepend(ring, MakeLeaf(flats[middle1 - i]));
ring = CordRepRing::Append(ring, MakeLeaf(flats[middle2 + i]));
}
}
break;
}
EXPECT_THAT(ToFlats(ring), ElementsAreArray(flats));
return ring;
}
std::ostream& operator<<(std::ostream& s, const TestParam& param) {
return s << param.ToString();
}
std::string TestParamToString(const testing::TestParamInfo<TestParam>& info) {
return info.param.ToString();
}
class CordRingTest : public testing::Test {
public:
~CordRingTest() override {
for (CordRep* rep : unrefs_) {
CordRep::Unref(rep);
}
}
template <typename CordRepType>
CordRepType* NeedsUnref(CordRepType* rep) {
assert(rep);
unrefs_.push_back(rep);
return rep;
}
template <typename CordRepType>
CordRepType* Ref(CordRepType* rep) {
CordRep::Ref(rep);
return NeedsUnref(rep);
}
private:
std::vector<CordRep*> unrefs_;
};
class CordRingTestWithParam : public testing::TestWithParam<TestParam> {
public:
~CordRingTestWithParam() override {
for (CordRep* rep : unrefs_) {
CordRep::Unref(rep);
}
}
CordRepRing* CreateWithCapacity(CordRep* child, size_t extra_capacity) {
if (!GetParam().with_capacity) extra_capacity = 0;
CordRepRing* ring = CordRepRing::Create(child, extra_capacity);
ring->SetCapacityForTesting(1 + extra_capacity);
return RefIfShared(ring);
}
bool Shared() const { return !GetParam().refcount_is_one; }
bool InputShared() const { return GetParam().input_share_mode == kShared; }
bool InputSharedIndirect() const {
return GetParam().input_share_mode == kSharedIndirect;
}
template <typename CordRepType>
CordRepType* NeedsUnref(CordRepType* rep) {
assert(rep);
unrefs_.push_back(rep);
return rep;
}
template <typename CordRepType>
CordRepType* Ref(CordRepType* rep) {
CordRep::Ref(rep);
return NeedsUnref(rep);
}
template <typename CordRepType>
CordRepType* RefIfShared(CordRepType* rep) {
return Shared() ? Ref(rep) : rep;
}
template <typename CordRepType>
CordRepType* RefIfInputShared(CordRepType* rep) {
return InputShared() ? Ref(rep) : rep;
}
template <typename CordRepType>
CordRepType* RefIfInputSharedIndirect(CordRepType* rep) {
return InputSharedIndirect() ? Ref(rep) : rep;
}
private:
std::vector<CordRep*> unrefs_;
};
class CordRingCreateTest : public CordRingTestWithParam {
public:
static TestParams CreateTestParams() {
TestParams params;
params.emplace_back(InputShareMode::kPrivate);
params.emplace_back(InputShareMode::kShared);
return params;
}
};
class CordRingSubTest : public CordRingTestWithParam {
public:
static TestParams CreateTestParams() {
TestParams params;
for (bool refcount_is_one : {true, false}) {
TestParam param;
param.refcount_is_one = refcount_is_one;
params.push_back(param);
}
return params;
}
};
class CordRingBuildTest : public CordRingTestWithParam {
public:
static TestParams CreateTestParams() {
TestParams params;
for (bool refcount_is_one : {true, false}) {
for (bool with_capacity : {true, false}) {
TestParam param;
param.refcount_is_one = refcount_is_one;
param.with_capacity = with_capacity;
params.push_back(param);
}
}
return params;
}
};
class CordRingCreateFromTreeTest : public CordRingTestWithParam {
public:
static TestParams CreateTestParams() {
TestParams params;
params.emplace_back(InputShareMode::kPrivate);
params.emplace_back(InputShareMode::kShared);
params.emplace_back(InputShareMode::kSharedIndirect);
return params;
}
};
class CordRingBuildInputTest : public CordRingTestWithParam {
public:
static TestParams CreateTestParams() {
TestParams params;
for (bool refcount_is_one : {true, false}) {
for (bool with_capacity : {true, false}) {
for (InputShareMode share_mode : {kPrivate, kShared, kSharedIndirect}) {
TestParam param;
param.refcount_is_one = refcount_is_one;
param.with_capacity = with_capacity;
param.input_share_mode = share_mode;
params.push_back(param);
}
}
}
return params;
}
};
INSTANTIATE_TEST_SUITE_P(WithParam, CordRingSubTest,
testing::ValuesIn(CordRingSubTest::CreateTestParams()),
TestParamToString);
INSTANTIATE_TEST_SUITE_P(
WithParam, CordRingCreateTest,
testing::ValuesIn(CordRingCreateTest::CreateTestParams()),
TestParamToString);
INSTANTIATE_TEST_SUITE_P(
WithParam, CordRingCreateFromTreeTest,
testing::ValuesIn(CordRingCreateFromTreeTest::CreateTestParams()),
TestParamToString);
INSTANTIATE_TEST_SUITE_P(
WithParam, CordRingBuildTest,
testing::ValuesIn(CordRingBuildTest::CreateTestParams()),
TestParamToString);
INSTANTIATE_TEST_SUITE_P(
WithParam, CordRingBuildInputTest,
testing::ValuesIn(CordRingBuildInputTest::CreateTestParams()),
TestParamToString);
TEST_P(CordRingCreateTest, CreateFromFlat) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
CordRepRing* result = NeedsUnref(CordRepRing::Create(MakeFlat(str1)));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(str1.size()));
EXPECT_THAT(ToFlats(result), ElementsAre(str1));
}
TEST_P(CordRingCreateTest, CreateFromRing) {
CordRepRing* ring = RefIfShared(FromFlats(kFoxFlats));
CordRepRing* result = NeedsUnref(CordRepRing::Create(ring));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivate(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(ToFlats(result), ElementsAreArray(kFoxFlats));
}
TEST_P(CordRingCreateFromTreeTest, CreateFromSubstringRing) {
CordRepRing* ring = RefIfInputSharedIndirect(FromFlats(kFoxFlats));
CordRep* sub = RefIfInputShared(MakeSubstring(2, 11, ring));
CordRepRing* result = NeedsUnref(CordRepRing::Create(sub));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfInputPrivate(GetParam(), ring));
EXPECT_THAT(ToString(result), string_view(kFox).substr(2, 11));
}
TEST_F(CordRingTest, CreateWithIllegalExtraCapacity) {
#if defined(ABSL_HAVE_EXCEPTIONS)
CordRep* flat = NeedsUnref(MakeFlat("Hello world"));
try {
CordRepRing::Create(flat, CordRepRing::kMaxCapacity);
GTEST_FAIL() << "expected std::length_error exception";
} catch (const std::length_error&) {
}
#elif defined(GTEST_HAS_DEATH_TEST)
CordRep* flat = NeedsUnref(MakeFlat("Hello world"));
EXPECT_DEATH(CordRepRing::Create(flat, CordRepRing::kMaxCapacity), ".*");
#endif
}
TEST_P(CordRingCreateFromTreeTest, CreateFromSubstringOfFlat) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
auto* flat = RefIfInputShared(MakeFlat(str1));
auto* child = RefIfInputSharedIndirect(MakeSubstring(4, 20, flat));
CordRepRing* result = NeedsUnref(CordRepRing::Create(child));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(20));
EXPECT_THAT(ToFlats(result), ElementsAre(str1.substr(4, 20)));
}
TEST_P(CordRingCreateTest, CreateFromExternal) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
auto* child = RefIfInputShared(MakeExternal(str1));
CordRepRing* result = NeedsUnref(CordRepRing::Create(child));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(str1.size()));
EXPECT_THAT(ToFlats(result), ElementsAre(str1));
}
TEST_P(CordRingCreateFromTreeTest, CreateFromSubstringOfExternal) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
auto* external = RefIfInputShared(MakeExternal(str1));
auto* child = RefIfInputSharedIndirect(MakeSubstring(1, 24, external));
CordRepRing* result = NeedsUnref(CordRepRing::Create(child));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(24));
EXPECT_THAT(ToFlats(result), ElementsAre(str1.substr(1, 24)));
}
TEST_P(CordRingCreateFromTreeTest, CreateFromSubstringOfLargeExternal) {
auto* external = RefIfInputShared(MakeFakeExternal(1 << 20));
auto str = not_a_string_view(external->base, 1 << 20)
.remove_prefix(1 << 19)
.remove_suffix(6);
auto* child =
RefIfInputSharedIndirect(MakeSubstring(1 << 19, (1 << 19) - 6, external));
CordRepRing* result = NeedsUnref(CordRepRing::Create(child));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(str.size()));
EXPECT_THAT(ToRawFlats(result), ElementsAre(str));
}
TEST_P(CordRingBuildInputTest, CreateFromConcat) {
CordRep* flats[] = {MakeFlat("abcdefgh"), MakeFlat("ijklm"),
MakeFlat("nopqrstuv"), MakeFlat("wxyz")};
auto* left = MakeConcat(RefIfInputSharedIndirect(flats[0]), flats[1]);
auto* right = MakeConcat(flats[2], RefIfInputSharedIndirect(flats[3]));
auto* concat = RefIfInputShared(MakeConcat(left, right));
CordRepRing* result = NeedsUnref(CordRepRing::Create(concat));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(26));
EXPECT_THAT(ToString(result), Eq(kAlphabet));
}
TEST_P(CordRingBuildInputTest, CreateFromSubstringConcat) {
for (size_t off = 0; off < 26; ++off) {
for (size_t len = 1; len < 26 - off; ++len) {
CordRep* flats[] = {MakeFlat("abcdefgh"), MakeFlat("ijklm"),
MakeFlat("nopqrstuv"), MakeFlat("wxyz")};
auto* left = MakeConcat(RefIfInputSharedIndirect(flats[0]), flats[1]);
auto* right = MakeConcat(flats[2], RefIfInputSharedIndirect(flats[3]));
auto* concat = MakeConcat(left, right);
auto* child = RefIfInputShared(MakeSubstring(off, len, concat));
CordRepRing* result = NeedsUnref(CordRepRing::Create(child));
ASSERT_THAT(result, IsValidRingBuffer());
ASSERT_THAT(result->length, Eq(len));
ASSERT_THAT(ToString(result), string_view(kAlphabet).substr(off, len));
}
}
}
TEST_P(CordRingCreateTest, Properties) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
CordRepRing* result = NeedsUnref(CordRepRing::Create(MakeFlat(str1), 120));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->head(), Eq(0));
EXPECT_THAT(result->tail(), Eq(1));
EXPECT_THAT(result->capacity(), Ge(120 + 1));
EXPECT_THAT(result->capacity(), Le(2 * 120 + 1));
EXPECT_THAT(result->entries(), Eq(1));
EXPECT_THAT(result->begin_pos(), Eq(0));
}
TEST_P(CordRingCreateTest, EntryForNewFlat) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
CordRep* child = MakeFlat(str1);
CordRepRing* result = NeedsUnref(CordRepRing::Create(child, 120));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->entry_child(0), Eq(child));
EXPECT_THAT(result->entry_end_pos(0), Eq(str1.length()));
EXPECT_THAT(result->entry_data_offset(0), Eq(0));
}
TEST_P(CordRingCreateTest, EntryForNewFlatSubstring) {
absl::string_view str1 = "1234567890abcdefghijklmnopqrstuvwxyz";
CordRep* child = MakeFlat(str1);
CordRep* substring = MakeSubstring(10, 26, child);
CordRepRing* result = NeedsUnref(CordRepRing::Create(substring, 1));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->entry_child(0), Eq(child));
EXPECT_THAT(result->entry_end_pos(0), Eq(26));
EXPECT_THAT(result->entry_data_offset(0), Eq(10));
}
TEST_P(CordRingBuildTest, AppendFlat) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRepRing* ring = CreateWithCapacity(MakeExternal(str1), 1);
CordRepRing* result = NeedsUnref(CordRepRing::Append(ring, MakeFlat(str2)));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(result->length, Eq(str1.size() + str2.size()));
EXPECT_THAT(ToFlats(result), ElementsAre(str1, str2));
}
TEST_P(CordRingBuildTest, PrependFlat) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRepRing* ring = CreateWithCapacity(MakeExternal(str1), 1);
CordRepRing* result = NeedsUnref(CordRepRing::Prepend(ring, MakeFlat(str2)));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(result->length, Eq(str1.size() + str2.size()));
EXPECT_THAT(ToFlats(result), ElementsAre(str2, str1));
}
TEST_P(CordRingBuildTest, AppendString) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRepRing* ring = CreateWithCapacity(MakeExternal(str1), 1);
CordRepRing* result = NeedsUnref(CordRepRing::Append(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(result->length, Eq(str1.size() + str2.size()));
EXPECT_THAT(ToFlats(result), ElementsAre(str1, str2));
}
TEST_P(CordRingBuildTest, AppendStringHavingExtra) {
absl::string_view str1 = "1234";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRepRing* ring = CreateWithCapacity(MakeFlat(str1, 26), 0);
CordRepRing* result = NeedsUnref(CordRepRing::Append(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(str1.size() + str2.size()));
EXPECT_THAT(result, EqIfPrivate(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
}
TEST_P(CordRingBuildTest, AppendStringHavingPartialExtra) {
absl::string_view str1 = "1234";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Create flat with at least one extra byte. We don't expect to have sized
// alloc and capacity rounding to grant us enough to not make it partial.
auto* flat = MakeFlat(str1, 1);
size_t avail = flat->flat()->Capacity() - flat->length;
ASSERT_THAT(avail, Lt(str2.size())) << " adjust test for larger flats!";
// Construct the flats we do expect using all of `avail`.
absl::string_view str1a = str2.substr(0, avail);
absl::string_view str2a = str2.substr(avail);
CordRepRing* ring = CreateWithCapacity(flat, 1);
CordRepRing* result = NeedsUnref(CordRepRing::Append(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(str1.size() + str2.size()));
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
if (GetParam().refcount_is_one) {
EXPECT_THAT(ToFlats(result), ElementsAre(StrCat(str1, str1a), str2a));
} else {
EXPECT_THAT(ToFlats(result), ElementsAre(str1, str2));
}
}
TEST_P(CordRingBuildTest, AppendStringHavingExtraInSubstring) {
absl::string_view str1 = "123456789_1234";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRep* flat = RemovePrefix(10, MakeFlat(str1, 26));
CordRepRing* ring = CreateWithCapacity(flat, 0);
CordRepRing* result = NeedsUnref(CordRepRing::Append(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivate(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(result->length, Eq(4 + str2.size()));
if (GetParam().refcount_is_one) {
EXPECT_THAT(ToFlats(result), ElementsAre(StrCat("1234", str2)));
} else {
EXPECT_THAT(ToFlats(result), ElementsAre("1234", str2));
}
}
TEST_P(CordRingBuildTest, AppendStringHavingSharedExtra) {
absl::string_view str1 = "123456789_1234";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int shared_type = 0; shared_type < 2; ++shared_type) {
SCOPED_TRACE(absl::StrCat("Shared extra type ", shared_type));
// Create a flat that is shared in some way.
CordRep* flat = nullptr;
CordRep* flat1 = nullptr;
if (shared_type == 0) {
// Shared flat
flat = CordRep::Ref(MakeFlat(str1.substr(10), 100));
} else if (shared_type == 1) {
// Shared flat inside private substring
flat1 = CordRep::Ref(MakeFlat(str1));
flat = RemovePrefix(10, flat1);
} else {
// Private flat inside shared substring
flat = CordRep::Ref(RemovePrefix(10, MakeFlat(str1, 100)));
}
CordRepRing* ring = CreateWithCapacity(flat, 1);
CordRepRing* result = NeedsUnref(CordRepRing::Append(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(result->length, Eq(4 + str2.size()));
EXPECT_THAT(ToFlats(result), ElementsAre("1234", str2));
CordRep::Unref(shared_type == 1 ? flat1 : flat);
}
}
TEST_P(CordRingBuildTest, AppendStringWithExtra) {
absl::string_view str1 = "1234";
absl::string_view str2 = "1234567890";
absl::string_view str3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRepRing* ring = CreateWithCapacity(MakeExternal(str1), 1);
CordRepRing* result = NeedsUnref(CordRepRing::Append(ring, str2, 26));
result = CordRepRing::Append(result, str3);
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(str1.size() + str2.size() + str3.size()));
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(ToFlats(result), ElementsAre(str1, StrCat(str2, str3)));
}
TEST_P(CordRingBuildTest, PrependString) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Use external rep to avoid appending to first flat
CordRepRing* ring = CreateWithCapacity(MakeExternal(str1), 1);
CordRepRing* result = NeedsUnref(CordRepRing::Prepend(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
if (GetParam().with_capacity && GetParam().refcount_is_one) {
EXPECT_THAT(result, Eq(ring));
} else {
EXPECT_THAT(result, Ne(ring));
}
EXPECT_THAT(result->length, Eq(str1.size() + str2.size()));
EXPECT_THAT(ToFlats(result), ElementsAre(str2, str1));
}
TEST_P(CordRingBuildTest, PrependStringHavingExtra) {
absl::string_view str1 = "abcdefghijklmnopqrstuvwxyz1234";
absl::string_view str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRep* flat = RemovePrefix(26, MakeFlat(str1));
CordRepRing* ring = CreateWithCapacity(flat, 0);
CordRepRing* result = NeedsUnref(CordRepRing::Prepend(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivate(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(result->length, Eq(4 + str2.size()));
if (GetParam().refcount_is_one) {
EXPECT_THAT(ToFlats(result), ElementsAre(StrCat(str2, "1234")));
} else {
EXPECT_THAT(ToFlats(result), ElementsAre(str2, "1234"));
}
}
TEST_P(CordRingBuildTest, PrependStringHavingSharedExtra) {
absl::string_view str1 = "123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
absl::string_view str2 = "abcdefghij";
absl::string_view str1a = str1.substr(10);
for (int shared_type = 1; shared_type < 2; ++shared_type) {
SCOPED_TRACE(absl::StrCat("Shared extra type ", shared_type));
// Create a flat that is shared in some way.
CordRep* flat = nullptr;
CordRep* flat1 = nullptr;
if (shared_type == 1) {
// Shared flat inside private substring
flat = RemovePrefix(10, flat1 = CordRep::Ref(MakeFlat(str1)));
} else {
// Private flat inside shared substring
flat = CordRep::Ref(RemovePrefix(10, MakeFlat(str1, 100)));
}
CordRepRing* ring = CreateWithCapacity(flat, 1);
CordRepRing* result = NeedsUnref(CordRepRing::Prepend(ring, str2));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result->length, Eq(str1a.size() + str2.size()));
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(ToFlats(result), ElementsAre(str2, str1a));
CordRep::Unref(shared_type == 1 ? flat1 : flat);
}
}
TEST_P(CordRingBuildTest, PrependStringWithExtra) {
absl::string_view str1 = "1234";
absl::string_view str2 = "1234567890";
absl::string_view str3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
CordRepRing* ring = CreateWithCapacity(MakeExternal(str1), 1);
CordRepRing* result = NeedsUnref(CordRepRing::Prepend(ring, str2, 26));
ASSERT_THAT(result, IsValidRingBuffer());
result = CordRepRing::Prepend(result, str3);
EXPECT_THAT(result->length, Eq(str1.size() + str2.size() + str3.size()));
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(ToFlats(result), ElementsAre(StrCat(str3, str2), str1));
}
TEST_P(CordRingBuildTest, AppendPrependStringMix) {
const auto& flats = kFoxFlats;
CordRepRing* ring = CreateWithCapacity(MakeFlat(flats[4]), 8);
CordRepRing* result = ring;
for (int i = 1; i <= 4; ++i) {
result = CordRepRing::Prepend(result, flats[4 - i]);
result = CordRepRing::Append(result, flats[4 + i]);
}
NeedsUnref(result);
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
EXPECT_THAT(ToString(result), kFox);
}
TEST_P(CordRingBuildTest, AppendPrependStringMixWithExtra) {
const auto& flats = kFoxFlats;
CordRepRing* ring = CreateWithCapacity(MakeFlat(flats[4], 100), 8);
CordRepRing* result = ring;
for (int i = 1; i <= 4; ++i) {
result = CordRepRing::Prepend(result, flats[4 - i], 100);
result = CordRepRing::Append(result, flats[4 + i], 100);
}
NeedsUnref(result);
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivateAndCapacity(GetParam(), ring));
if (GetParam().refcount_is_one) {
EXPECT_THAT(ToFlats(result),
ElementsAre("The quick brown fox ", "jumps over the lazy dog"));
} else {
EXPECT_THAT(ToFlats(result), ElementsAre("The quick brown fox ", "jumps ",
"over the lazy dog"));
}
}
TEST_P(CordRingBuildTest, AppendPrependStringMixWithPrependedExtra) {
const auto& flats = kFoxFlats;
CordRep* flat = MakeFlat(StrCat(std::string(50, '.'), flats[4]), 50);
CordRepRing* ring = CreateWithCapacity(RemovePrefix(50, flat), 0);
CordRepRing* result = ring;
for (int i = 1; i <= 4; ++i) {
result = CordRepRing::Prepend(result, flats[4 - i], 100);
result = CordRepRing::Append(result, flats[4 + i], 100);
}
result = NeedsUnref(result);
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivate(GetParam(), ring));
if (GetParam().refcount_is_one) {
EXPECT_THAT(ToFlats(result), ElementsAre(kFox));
} else {
EXPECT_THAT(ToFlats(result), ElementsAre("The quick brown fox ", "jumps ",
"over the lazy dog"));
}
}
TEST_P(CordRingSubTest, SubRing) {
auto composition = RandomComposition();
SCOPED_TRACE(ToString(composition));
auto flats = MakeSpan(kFoxFlats);
string_view all = kFox;
for (size_t offset = 0; offset < all.size() - 1; ++offset) {
CordRepRing* ring = RefIfShared(FromFlats(flats, composition));
CordRepRing* result = CordRepRing::SubRing(ring, offset, 0);
EXPECT_THAT(result, nullptr);
for (size_t len = 1; len < all.size() - offset; ++len) {
ring = RefIfShared(FromFlats(flats, composition));
result = NeedsUnref(CordRepRing::SubRing(ring, offset, len));
ASSERT_THAT(result, IsValidRingBuffer());
ASSERT_THAT(result, EqIfPrivate(GetParam(), ring));
ASSERT_THAT(result, NeIfShared(GetParam(), ring));
ASSERT_THAT(ToString(result), Eq(all.substr(offset, len)));
}
}
}
TEST_P(CordRingSubTest, SubRingFromLargeExternal) {
auto composition = RandomComposition();
std::string large_string(1 << 20, '.');
const char* flats[] = {
"abcdefghijklmnopqrstuvwxyz",
large_string.c_str(),
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
};
std::string buffer = absl::StrCat(flats[0], flats[1], flats[2]);
absl::string_view all = buffer;
for (size_t offset = 0; offset < 30; ++offset) {
CordRepRing* ring = RefIfShared(FromFlats(flats, composition));
CordRepRing* result = CordRepRing::SubRing(ring, offset, 0);
EXPECT_THAT(result, nullptr);
for (size_t len = all.size() - 30; len < all.size() - offset; ++len) {
ring = RefIfShared(FromFlats(flats, composition));
result = NeedsUnref(CordRepRing::SubRing(ring, offset, len));
ASSERT_THAT(result, IsValidRingBuffer());
ASSERT_THAT(result, EqIfPrivate(GetParam(), ring));
ASSERT_THAT(result, NeIfShared(GetParam(), ring));
auto str = ToString(result);
ASSERT_THAT(str, SizeIs(len));
ASSERT_THAT(str, Eq(all.substr(offset, len)));
}
}
}
TEST_P(CordRingSubTest, RemovePrefix) {
auto composition = RandomComposition();
SCOPED_TRACE(ToString(composition));
auto flats = MakeSpan(kFoxFlats);
string_view all = kFox;
CordRepRing* ring = RefIfShared(FromFlats(flats, composition));
CordRepRing* result = CordRepRing::RemovePrefix(ring, all.size());
EXPECT_THAT(result, nullptr);
for (size_t len = 1; len < all.size(); ++len) {
ring = RefIfShared(FromFlats(flats, composition));
result = NeedsUnref(CordRepRing::RemovePrefix(ring, len));
ASSERT_THAT(result, IsValidRingBuffer());
EXPECT_THAT(result, EqIfPrivate(GetParam(), ring));
ASSERT_THAT(result, NeIfShared(GetParam(), ring));
EXPECT_THAT(ToString(result), Eq(all.substr(len)));
}
}
TEST_P(CordRingSubTest, RemovePrefixFromLargeExternal) {
CordRepExternal* external1 = MakeFakeExternal(1 << 20);
CordRepExternal* external2 = MakeFakeExternal(1 << 20);
CordRepRing* ring = CordRepRing::Create(external1, 1);
ring = CordRepRing::Append(ring, external2);
CordRepRing* result = NeedsUnref(CordRepRing::RemovePrefix(ring, 1 << 16));
EXPECT_THAT(
ToRawFlats(result),