v8 147.4.0

Rust bindings to V8
Documentation
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
// Copyright 2025 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 "absl/container/linked_hash_set.h"

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/container/internal/hash_generator_testing.h"
#include "absl/container/internal/hash_policy_testing.h"
#include "absl/container/internal/heterogeneous_lookup_testing.h"
#include "absl/container/internal/test_instance_tracker.h"
#include "absl/container/internal/unordered_set_constructor_test.h"
#include "absl/container/internal/unordered_set_lookup_test.h"
#include "absl/container/internal/unordered_set_members_test.h"
#include "absl/container/internal/unordered_set_modifiers_test.h"
#include "absl/strings/string_view.h"

namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
namespace {

using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::Pointee;

template <class T>
using Set =
    linked_hash_set<T, StatefulTestingHash, StatefulTestingEqual, Alloc<T>>;

using SetTypes =
    ::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;

INSTANTIATE_TYPED_TEST_SUITE_P(LinkedHashSet, ConstructorTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(LinkedHashSet, LookupTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(LinkedHashSet, MembersTest, SetTypes);
INSTANTIATE_TYPED_TEST_SUITE_P(LinkedHashSet, ModifiersTest, SetTypes);

// Tests that the range constructor works.
TEST(LinkedHashSetTest, RangeConstruct) {
  const auto items = {1, 2, 3};
  EXPECT_THAT(linked_hash_set<int>(items.begin(), items.end()),
              ElementsAre(1, 2, 3));
}

// Tests that copying works.
TEST(LinkedHashSetTest, Copy) {
  linked_hash_set<int> m{4, 8, 15, 16, 23, 42};
  auto copy = m;

  auto found = copy.find(8);
  ASSERT_TRUE(found != copy.end());
  for (auto iter = copy.begin(); iter != copy.end(); ++iter) {
    if (iter == found) return;
  }
  FAIL() << "Copied set's find method returned an invalid iterator.";
}

// Tests that assignment works.
TEST(LinkedHashSetTest, Assign) {
  linked_hash_set<int> m{2, 3};
  linked_hash_set<int> n{4};

  n = m;
  EXPECT_TRUE(n.contains(2));
  auto found = n.find(2);
  ASSERT_TRUE(found != n.end());
  for (auto iter = n.begin(); iter != n.end(); ++iter) {
    if (iter == found) return;
  }
  FAIL() << "Assigned set's find method returned an invalid iterator.";
}

// Tests that self-assignment works.
TEST(LinkedHashSetTest, SelfAssign) {
  linked_hash_set<int> a{1, 2, 3};
  auto& a_ref = a;
  a = a_ref;

  EXPECT_TRUE(a.contains(2));
  auto found = a.find(2);
  ASSERT_TRUE(found != a.end());
  for (auto iter = a.begin(); iter != a.end(); ++iter) {
    if (iter == found) return;
  }
  FAIL() << "Assigned set's find method returned an invalid iterator.";
}

// Tests that move constructor works.
TEST(LinkedHashSetTest, Move) {
  // Use unique_ptr as an example of a non-copyable type.
  linked_hash_set<std::unique_ptr<int>> m;
  m.insert(std::make_unique<int>(2));
  m.insert(std::make_unique<int>(3));
  linked_hash_set<std::unique_ptr<int>> n = std::move(m);
  EXPECT_THAT(n, ElementsAre(Pointee(2), Pointee(3)));
}

// Tests that self-moving works.
TEST(LinkedHashSetTest, SelfMove) {
  linked_hash_set<int> a{1, 2, 3};
  auto& a_ref = a;
  a = std::move(a_ref);
  EXPECT_THAT(a, ElementsAre(1, 2, 3));
}

struct IntUniquePtrHash {
  size_t operator()(const std::unique_ptr<int>& p) const {
    return static_cast<size_t>(*p);
  }
};

struct IntUniquePtrEq {
  size_t operator()(const std::unique_ptr<int>& a,
                    const std::unique_ptr<int>& b) const {
    return *a == *b;
  }
};

// Pretty artificial for a set, but unique_ptr is a convenient move-only type.
TEST(LinkedHashSetTest, CanInsertMoveOnly) {
  linked_hash_set<std::unique_ptr<int>, IntUniquePtrHash, IntUniquePtrEq> s;
  std::vector<int> data = {4, 8, 15, 16, 23, 42};
  for (int x : data) s.insert(std::make_unique<int>(x));
  EXPECT_EQ(s.size(), data.size());
  for (const std::unique_ptr<int>& elt : s) {
    EXPECT_TRUE(s.contains(elt));
    EXPECT_TRUE(s.find(elt) != s.end());
  }
}

TEST(LinkedHashSetTest, CanMoveMoveOnly) {
  linked_hash_set<std::unique_ptr<int>, IntUniquePtrHash, IntUniquePtrEq> s;
  std::vector<int> data = {4, 8, 15, 16, 23, 42};
  for (int x : data) s.insert(std::make_unique<int>(x));
  linked_hash_set<std::unique_ptr<int>, IntUniquePtrHash, IntUniquePtrEq> ss =
      std::move(s);
  EXPECT_EQ(ss.size(), data.size());
}

TEST(LinkedHashSetTest, CanEmplaceMoveOnly) {
  linked_hash_set<std::unique_ptr<int>, IntUniquePtrHash, IntUniquePtrEq> s;
  std::vector<int> data = {4, 8, 15, 16, 23, 42};
  for (const int x : data) {
    s.emplace(new int{x});
  }
  EXPECT_EQ(s.size(), data.size());
  for (const std::unique_ptr<int>& elt : s) {
    EXPECT_TRUE(s.contains(elt));
    EXPECT_TRUE(s.find(elt) != s.end());
  }
}

TEST(LinkedHashSetTest, CanInsertTransparent) {
  linked_hash_set<std::string> s;
  s.insert(absl::string_view("foo"));
  s.insert(absl::string_view("bar"));
  s.insert(absl::string_view("foo"));
  EXPECT_THAT(s, ElementsAre("foo", "bar"));
}

// Tests that iteration from begin() to end() works
TEST(LinkedHashSetTest, Iteration) {
  linked_hash_set<int> m;
  EXPECT_TRUE(m.begin() == m.end());

  m.insert(2);
  m.insert(1);
  m.insert(3);

  linked_hash_set<int>::iterator i = m.begin();
  ASSERT_TRUE(m.begin() == i);
  ASSERT_TRUE(m.end() != i);
  EXPECT_EQ(2, *i);

  ++i;
  ASSERT_TRUE(m.end() != i);
  EXPECT_EQ(1, *i);

  ++i;
  ASSERT_TRUE(m.end() != i);
  EXPECT_EQ(3, *i);

  ++i;
  ASSERT_TRUE(m.end() == i);
}

// Tests that reverse iteration from rbegin() to rend() works
TEST(LinkedHashSetTest, ReverseIteration) {
  linked_hash_set<int> m;
  EXPECT_TRUE(m.rbegin() == m.rend());

  m.insert(2);
  m.insert(1);
  m.insert(3);

  linked_hash_set<int>::reverse_iterator i = m.rbegin();
  ASSERT_TRUE(m.rbegin() == i);
  ASSERT_TRUE(m.rend() != i);
  EXPECT_EQ(3, *i);

  ++i;
  ASSERT_TRUE(m.rend() != i);
  EXPECT_EQ(1, *i);

  ++i;
  ASSERT_TRUE(m.rend() != i);
  EXPECT_EQ(2, *i);

  ++i;
  ASSERT_TRUE(m.rend() == i);
}

// Tests that clear() works
TEST(LinkedHashSetTest, Clear) {
  linked_hash_set<int> m{2, 1, 3};
  ASSERT_EQ(3, m.size());

  m.clear();
  EXPECT_EQ(0, m.size());
  EXPECT_FALSE(m.contains(1));
  EXPECT_TRUE(m.find(1) == m.end());

  // Make sure we can call it on an empty set.
  m.clear();
  EXPECT_EQ(0, m.size());
}

// Tests that size() works.
TEST(LinkedHashSetTest, Size) {
  linked_hash_set<int> m;
  EXPECT_EQ(0, m.size());
  m.insert(2);
  EXPECT_EQ(1, m.size());
  m.insert(11);
  EXPECT_EQ(2, m.size());
  m.insert(0);
  EXPECT_EQ(3, m.size());
  m.insert(0);
  EXPECT_EQ(3, m.size());
  m.clear();
  EXPECT_EQ(0, m.size());
}

// Tests empty()
TEST(LinkedHashSetTest, Empty) {
  linked_hash_set<int> m;
  ASSERT_TRUE(m.empty());
  m.insert(2);
  ASSERT_FALSE(m.empty());
  m.clear();
  ASSERT_TRUE(m.empty());
}

TEST(LinkedHashSetTest, Erase) {
  linked_hash_set<int> m;
  ASSERT_EQ(0, m.size());
  EXPECT_EQ(0, m.erase(2));  // Nothing to erase yet

  m.insert(2);
  ASSERT_EQ(1, m.size());
  EXPECT_EQ(1, m.erase(2));
  EXPECT_EQ(0, m.size());
  EXPECT_TRUE(m.empty());

  EXPECT_EQ(0, m.erase(2));  // Make sure nothing bad happens if we repeat.
  EXPECT_EQ(0, m.size());
  EXPECT_TRUE(m.empty());
}

TEST(LinkedHashSetTest, Erase2) {
  linked_hash_set<int> m;
  ASSERT_EQ(0, m.size());
  EXPECT_EQ(0, m.erase(2));  // Nothing to erase yet

  m.insert(2);
  m.insert(1);
  m.insert(3);
  m.insert(4);
  ASSERT_EQ(4, m.size());

  // Erase middle two
  EXPECT_EQ(1, m.erase(1));
  EXPECT_EQ(1, m.erase(3));

  EXPECT_EQ(2, m.size());

  // Make sure we can still iterate over everything that's left.
  linked_hash_set<int>::iterator it = m.begin();
  ASSERT_TRUE(it != m.end());
  EXPECT_EQ(2, *it);
  ++it;
  ASSERT_TRUE(it != m.end());
  EXPECT_EQ(4, *it);
  ++it;
  ASSERT_TRUE(it == m.end());

  EXPECT_EQ(0, m.erase(1));  // Make sure nothing bad happens if we repeat.
  ASSERT_EQ(2, m.size());

  EXPECT_EQ(1, m.erase(2));
  EXPECT_EQ(1, m.erase(4));
  ASSERT_EQ(0, m.size());
  EXPECT_TRUE(m.empty());

  EXPECT_EQ(0, m.erase(1));  // Make sure nothing bad happens if we repeat.
  ASSERT_EQ(0, m.size());
  EXPECT_TRUE(m.empty());
}

// Test that erase(iter,iter) and erase(iter) compile and work.
TEST(LinkedHashSetTest, Erase3) {
  linked_hash_set<int> m;

  m.insert(1);
  m.insert(2);
  m.insert(3);
  m.insert(4);

  // Erase middle two
  linked_hash_set<int>::iterator it2 = m.find(2);
  linked_hash_set<int>::iterator it4 = m.find(4);
  EXPECT_EQ(m.erase(it2, it4), m.find(4));
  EXPECT_FALSE(m.contains(2));
  EXPECT_TRUE(m.find(2) == m.end());
  EXPECT_FALSE(m.contains(3));
  EXPECT_TRUE(m.find(3) == m.end());
  EXPECT_EQ(2, m.size());

  // Make sure we can still iterate over everything that's left.
  linked_hash_set<int>::iterator it = m.begin();
  ASSERT_TRUE(it != m.end());
  EXPECT_EQ(1, *it);
  ++it;
  ASSERT_TRUE(it != m.end());
  EXPECT_EQ(4, *it);
  ++it;
  ASSERT_TRUE(it == m.end());

  // Erase first one using an iterator.
  EXPECT_EQ(m.erase(m.begin()), m.find(4));
  EXPECT_FALSE(m.contains(1));
  EXPECT_TRUE(m.find(1) == m.end());

  // Only the last element should be left.
  EXPECT_TRUE(m.contains(4));
  it = m.begin();
  ASSERT_TRUE(it != m.end());
  EXPECT_EQ(4, *it);
  ++it;
  ASSERT_TRUE(it == m.end());
}

// Test all types of insertion
TEST(LinkedHashSetTest, Insertion) {
  linked_hash_set<int> m;
  ASSERT_EQ(0, m.size());
  std::pair<linked_hash_set<int>::iterator, bool> result;

  result = m.insert(2);
  ASSERT_EQ(1, m.size());
  EXPECT_TRUE(result.second);
  EXPECT_EQ(2, *result.first);
  EXPECT_TRUE(m.contains(2));
  EXPECT_TRUE(m.find(2) != m.end());

  result = m.insert(1);
  ASSERT_EQ(2, m.size());
  EXPECT_TRUE(result.second);
  EXPECT_EQ(1, *result.first);
  EXPECT_TRUE(m.contains(1));
  EXPECT_TRUE(m.find(1) != m.end());

  result = m.insert(3);
  linked_hash_set<int>::iterator result_iterator = result.first;
  ASSERT_EQ(3, m.size());
  EXPECT_TRUE(result.second);
  EXPECT_EQ(3, *result.first);
  EXPECT_TRUE(m.contains(3));
  EXPECT_TRUE(m.find(3) != m.end());

  result = m.insert(3);
  EXPECT_EQ(3, m.size());
  EXPECT_FALSE(result.second) << "No insertion should have occurred.";
  EXPECT_TRUE(result_iterator == result.first)
      << "Duplicate insertion should have given us the original iterator.";
  EXPECT_TRUE(m.contains(3));
  EXPECT_TRUE(m.find(3) != m.end());

  std::vector<int> v = {3, 4, 5};
  m.insert(v.begin(), v.end());
  // Expect 4 and 5 inserted, 3 not inserted.
  EXPECT_EQ(5, m.size());
  EXPECT_TRUE(m.contains(4));
  EXPECT_NE(m.find(4), m.end());
  EXPECT_TRUE(m.contains(5));
  EXPECT_NE(m.find(5), m.end());
}

TEST(LinkedHashSetTest, HintedInsertionMoveable) {
  linked_hash_set<int> m = {1, 3};
  m.insert(m.find(3), 2);
  EXPECT_THAT(m, ElementsAre(1, 2, 3));
}

TEST(LinkedHashSetTest, HintedInsertionReference) {
  linked_hash_set<int> m = {1, 3};
  const int val = 2;
  m.insert(m.find(3), val);
  EXPECT_THAT(m, ElementsAre(1, 2, 3));
}

TEST(LinkedHashSetTest, HintedEmplaceMoveable) {
  linked_hash_set<int> m = {1, 3};
  m.emplace_hint(m.find(3), 2);
  EXPECT_THAT(m, ElementsAre(1, 2, 3));
}

TEST(LinkedHashSetTest, HintedEmplaceReference) {
  linked_hash_set<int> m = {1, 3};
  const int val = 2;
  m.emplace_hint(m.find(3), val);
  EXPECT_THAT(m, ElementsAre(1, 2, 3));
}

// Test front accessors.
TEST(LinkedHashSetTest, Front) {
  linked_hash_set<int> m;

  m.insert(222);
  m.insert(111);
  m.insert(333);

  EXPECT_EQ(3, m.size());
  EXPECT_EQ(222, m.front());
  m.pop_front();
  EXPECT_EQ(2, m.size());
  EXPECT_EQ(111, m.front());
  m.pop_front();
  EXPECT_EQ(1, m.size());
  EXPECT_EQ(333, m.front());
  m.pop_front();
  EXPECT_TRUE(m.empty());
}

// Test back accessors.
TEST(LinkedHashSetTest, Back) {
  linked_hash_set<int> m;

  m.insert(222);
  m.insert(111);
  m.insert(333);

  EXPECT_EQ(3, m.size());
  EXPECT_EQ(333, m.back());
  m.pop_back();
  EXPECT_EQ(2, m.size());
  EXPECT_EQ(111, m.back());
  m.pop_back();
  EXPECT_EQ(1, m.size());
  EXPECT_EQ(222, m.back());
  m.pop_back();
  EXPECT_TRUE(m.empty());
}

TEST(LinkedHashSetTest, Find) {
  linked_hash_set<int> m;

  EXPECT_TRUE(m.end() == m.find(1))
      << "We shouldn't find anything in an empty set.";

  m.insert(2);
  EXPECT_TRUE(m.end() == m.find(1))
      << "We shouldn't find an element that doesn't exist in the set.";

  std::pair<linked_hash_set<int>::iterator, bool> result = m.insert(1);
  ASSERT_TRUE(result.second);
  ASSERT_TRUE(m.end() != result.first);
  EXPECT_TRUE(result.first == m.find(1))
      << "We should have found an element we know exists in the set.";
  EXPECT_EQ(1, *result.first);

  // Check that a follow-up insertion doesn't affect our original
  m.insert(3);
  linked_hash_set<int>::iterator it = m.find(1);
  ASSERT_TRUE(m.end() != it);
  EXPECT_EQ(1, *it);

  m.clear();
  EXPECT_TRUE(m.end() == m.find(1))
      << "We shouldn't find anything in a set that we've cleared.";
}

TEST(LinkedHashSetTest, Contains) {
  linked_hash_set<int> m;

  EXPECT_FALSE(m.contains(1)) << "The empty set shouldn't contain anything.";

  m.insert(2);
  EXPECT_FALSE(m.contains(1))
      << "contains() should not return true for an element that doesn't exist "
      << "in the set.";

  m.insert(1);
  EXPECT_TRUE(m.contains(1))
      << "contains() should return true for an element we know exists in the "
      << "set.";

  m.clear();
  EXPECT_FALSE(m.contains(1))
      << "A set that we've cleared shouldn't contain anything.";
}

TEST(LinkedHashSetTest, Swap) {
  linked_hash_set<int> m1;
  linked_hash_set<int> m2;
  m1.insert(1);
  m1.insert(2);
  m2.insert(3);
  ASSERT_EQ(2, m1.size());
  ASSERT_EQ(1, m2.size());
  m1.swap(m2);
  ASSERT_EQ(1, m1.size());
  ASSERT_EQ(2, m2.size());
}

TEST(LinkedHashSetTest, SelfSwap) {
  linked_hash_set<int> a{1, 2, 3};
  using std::swap;
  swap(a, a);
  EXPECT_THAT(a, ElementsAre(1, 2, 3));
}

TEST(LinkedHashSetTest, InitializerList) {
  linked_hash_set<int> m{1, 3};
  ASSERT_EQ(2, m.size());
  EXPECT_TRUE(m.contains(1));
  linked_hash_set<int>::iterator it = m.find(1);
  ASSERT_TRUE(m.end() != it);
  EXPECT_EQ(1, *it);
  it = m.find(3);
  EXPECT_TRUE(m.contains(3));
  ASSERT_TRUE(m.end() != it);
  EXPECT_EQ(3, *it);
}

TEST(LinkedHashSetTest, CustomHashAndEquality) {
  struct CustomIntHash {
    size_t operator()(int x) const { return 0; }
  };
  struct CustomIntEq {
    bool operator()(int x, int y) const { return abs(x) == abs(y); }
  };
  linked_hash_set<int, CustomIntHash, CustomIntEq> m;
  m.insert(1);
  EXPECT_EQ(1, m.size());
  m.insert(2);
  EXPECT_EQ(2, m.size());
  EXPECT_FALSE(m.insert(-2).second);
  EXPECT_EQ(2, m.size());
  EXPECT_TRUE(m.contains(-1));
  EXPECT_TRUE(m.find(-1) != m.end());
}

TEST(LinkedHashSetTest, EqualRange) {
  linked_hash_set<int> m{3, 1};
  const auto& const_m = m;

  EXPECT_THAT(m.equal_range(2), testing::Pair(m.end(), m.end()));
  EXPECT_THAT(const_m.equal_range(2),
              testing::Pair(const_m.end(), const_m.end()));

  EXPECT_THAT(m.equal_range(1), testing::Pair(m.find(1), ++m.find(1)));
  EXPECT_THAT(const_m.equal_range(1),
              testing::Pair(const_m.find(1), ++const_m.find(1)));
}

TEST(LinkedHashSetTest, ReserveWorks) {
  linked_hash_set<int> m;
  EXPECT_EQ(0, m.size());
  EXPECT_EQ(0.0, m.load_factor());
  m.reserve(10);
  EXPECT_LE(10, m.capacity());
  EXPECT_EQ(0, m.size());
  EXPECT_EQ(0.0, m.load_factor());
  m.insert(1);
  m.insert(2);
  EXPECT_LE(10, m.capacity());
  EXPECT_EQ(2, m.size());
  EXPECT_LT(0.0, m.load_factor());
}

TEST(LinkedHashSetTest, HeterogeneousTests) {
  absl::test_internal::InstanceTracker tracker;

  linked_hash_set<ExpensiveType, HeterogeneousHash, HeterogeneousEqual> set;
  ExpensiveType one(1);
  tracker.ResetCopiesMovesSwaps();
  set.insert(one);
  // Two instances: 'one' var and an instance in the set.
  EXPECT_EQ(2, tracker.instances());
  EXPECT_EQ(1, tracker.copies());
  EXPECT_EQ(0, tracker.moves());

  tracker.ResetCopiesMovesSwaps();
  set.insert(one);
  // No construction since key==1 exists.
  EXPECT_EQ(2, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(0, tracker.moves());

  tracker.ResetCopiesMovesSwaps();
  set.emplace(CheapType(1));
  // No construction since key==1 exists.
  EXPECT_EQ(2, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(0, tracker.moves());

  tracker.ResetCopiesMovesSwaps();
  set.emplace(CheapType(2));
  // Construction since key==2 doesn't exist in the set.
  EXPECT_EQ(3, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(0, tracker.moves());
  EXPECT_THAT(set, ElementsAre(HasExpensiveValue(1), HasExpensiveValue(2)));

  // find
  tracker.ResetCopiesMovesSwaps();
  auto itr = set.find(CheapType(1));
  ASSERT_NE(itr, set.end());
  EXPECT_EQ(1, itr->value());
  // contains
  EXPECT_TRUE(set.contains(CheapType(2)));
  // count
  EXPECT_EQ(1, set.count(CheapType(2)));
  // equal_range
  auto eq_itr_pair = set.equal_range(CheapType(2));
  ASSERT_NE(eq_itr_pair.first, set.end());
  EXPECT_EQ(2, eq_itr_pair.first->value());
  // No construction for find, contains, count or equal_range.
  EXPECT_EQ(3, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(0, tracker.moves());

  // emplace
  tracker.ResetCopiesMovesSwaps();
  set.emplace(3);
  // Just one construction.
  EXPECT_EQ(4, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(0, tracker.moves());

  tracker.ResetCopiesMovesSwaps();
  set.emplace(3);
  // No additional construction since key==3 exists.
  EXPECT_EQ(4, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(0, tracker.moves());
  EXPECT_THAT(set, ElementsAre(HasExpensiveValue(1), HasExpensiveValue(2),
                               HasExpensiveValue(3)));

  // Test std::move() using insert().
  ExpensiveType four(4);
  tracker.ResetCopiesMovesSwaps();
  set.insert(std::move(four));
  // Two constructions (regular and move).
  EXPECT_EQ(6, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(1, tracker.moves());

  EXPECT_THAT(set, ElementsAre(HasExpensiveValue(1), HasExpensiveValue(2),
                               HasExpensiveValue(3), HasExpensiveValue(4)));

  tracker.ResetCopiesMovesSwaps();
  set.erase(CheapType(1));
  // No construction and instance reduced by one.
  EXPECT_EQ(5, tracker.instances());
  EXPECT_EQ(0, tracker.copies());
  EXPECT_EQ(0, tracker.moves());
  EXPECT_THAT(set, ElementsAre(HasExpensiveValue(2), HasExpensiveValue(3),
                               HasExpensiveValue(4)));
}

TEST(LinkedHashSetTest, HeterogeneousStringViewLookup) {
  linked_hash_set<std::string> set;
  set.insert("foo");
  set.insert("bar");
  set.insert("blah");

  {
    absl::string_view lookup("foo");
    auto itr = set.find(lookup);
    ASSERT_NE(itr, set.end());
    EXPECT_EQ("foo", *itr);
  }

  // Not found.
  {
    absl::string_view lookup("foobar");
    EXPECT_EQ(set.end(), set.find(lookup));
  }

  {
    absl::string_view lookup("blah");
    auto itr = set.find(lookup);
    ASSERT_NE(itr, set.end());
    EXPECT_EQ("blah", *itr);
  }
}

TEST(LinkedHashSetTest, EmplaceString) {
  std::vector<std::string> v = {"a", "b"};
  linked_hash_set<absl::string_view> hs(v.begin(), v.end());
  EXPECT_THAT(hs, ElementsAreArray(v));
}

TEST(LinkedHashSetTest, BitfieldArgument) {
  union {
    int n : 1;
  };
  n = 0;
  linked_hash_set<int> s = {n};
  s.insert(n);
  s.insert(s.end(), n);
  s.insert({n});
  s.erase(n);
  s.count(n);
  s.find(n);
  s.contains(n);
  s.equal_range(n);
}

TEST(LinkedHashSetTest, MergeExtractInsert) {
  struct Hash {
    size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
  };
  struct Eq {
    bool operator()(const std::unique_ptr<int>& a,
                    const std::unique_ptr<int>& b) const {
      return *a == *b;
    }
  };
  linked_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
  set1.insert(std::make_unique<int>(7));
  set1.insert(std::make_unique<int>(17));

  set2.insert(std::make_unique<int>(7));
  set2.insert(std::make_unique<int>(19));

  EXPECT_THAT(set1, ElementsAre(Pointee(7), Pointee(17)));
  EXPECT_THAT(set2, ElementsAre(Pointee(7), Pointee(19)));

  set1.merge(set2);

  EXPECT_THAT(set1, ElementsAre(Pointee(7), Pointee(17), Pointee(19)));
  EXPECT_THAT(set2, ElementsAre(Pointee(7)));

  auto node = set1.extract(std::make_unique<int>(7));
  EXPECT_TRUE(node);
  EXPECT_THAT(node.value(), Pointee(7));
  EXPECT_THAT(set1, ElementsAre(Pointee(17), Pointee(19)));

  auto insert_result = set2.insert(std::move(node));
  EXPECT_FALSE(node);
  EXPECT_FALSE(insert_result.inserted);
  EXPECT_TRUE(insert_result.node);
  EXPECT_THAT(insert_result.node.value(), Pointee(7));
  EXPECT_EQ(**insert_result.position, 7);
  EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
  EXPECT_THAT(set2, ElementsAre(Pointee(7)));

  node = set1.extract(std::make_unique<int>(17));
  EXPECT_TRUE(node);
  EXPECT_THAT(node.value(), Pointee(17));
  EXPECT_THAT(set1, ElementsAre(Pointee(19)));

  node.value() = std::make_unique<int>(23);

  insert_result = set2.insert(std::move(node));
  EXPECT_FALSE(node);
  EXPECT_TRUE(insert_result.inserted);
  EXPECT_FALSE(insert_result.node);
  EXPECT_EQ(**insert_result.position, 23);
  EXPECT_THAT(set2, ElementsAre(Pointee(7), Pointee(23)));
}

TEST(LinkedHashSet, ExtractInsert) {
  linked_hash_set<int> s = {1, 7, 2, 9};
  auto node = s.extract(1);
  EXPECT_TRUE(node);
  EXPECT_EQ(node.value(), 1);
  EXPECT_THAT(s, ElementsAre(7, 2, 9));
  EXPECT_FALSE(s.contains(1));

  node.value() = 17;
  s.insert(std::move(node));
  EXPECT_FALSE(node);
  EXPECT_THAT(s, ElementsAre(7, 2, 9, 17));
  EXPECT_TRUE(s.contains(17));

  node = s.extract(s.find(9));
  EXPECT_TRUE(node);
  EXPECT_EQ(node.value(), 9);
  EXPECT_THAT(s, ElementsAre(7, 2, 17));
  EXPECT_FALSE(s.contains(9));
}

TEST(LinkedHashSet, Merge) {
  linked_hash_set<int> m = {1, 7, 3, 6, 10};
  linked_hash_set<int> src = {1, 2, 9, 10, 4, 16};

  m.merge(src);

  EXPECT_THAT(m, ElementsAre(1, 7, 3, 6, 10, 2, 9, 4, 16));
  for (int i : {1, 7, 3, 6, 10, 2, 9, 4, 16}) {
    EXPECT_TRUE(m.contains(i));
  }
  EXPECT_THAT(src, ElementsAre(1, 10));
  for (int i : {1, 10}) {
    EXPECT_TRUE(src.contains(i));
  }
  for (int i : {2, 9, 4, 16}) {
    EXPECT_FALSE(src.contains(i));
  }
}

TEST(LinkedHashSet, EraseRange) {
  linked_hash_set<int> set = {1, 2, 3, 4, 5, 25, 36, 7, 8, 9, 81};
  auto start = set.find(3);
  auto end = set.find(8);
  auto itr = set.erase(start, end);
  ASSERT_NE(itr, set.end());
  EXPECT_THAT(*itr, 8);
  EXPECT_THAT(set, ElementsAre(1, 2, 8, 9, 81));
  for (int i : {1, 2, 8, 9, 81}) {
    EXPECT_TRUE(set.contains(i));
  }
  for (int i : {3, 4, 5, 25, 36, 7}) {
    EXPECT_FALSE(set.contains(i));
  }
}

TEST(LinkedHashSet, InsertInitializerList) {
  linked_hash_set<int> set;
  set.insert({1, 7, 2, 9, 3, 29});
  EXPECT_THAT(set, ElementsAre(1, 7, 2, 9, 3, 29));
  for (int i : {1, 7, 2, 9, 3, 29}) {
    EXPECT_TRUE(set.contains(i));
  }
}

struct CountedHash {
  explicit CountedHash(int* count) : count(count) {}
  size_t operator()(int value) const {
    ++(*count);
    return value;
  }
  int* count = nullptr;
};

// Makes a set too big for small object optimization.  Counts the number of
// hashes in `count`, but leaves `count` set to 0.
linked_hash_set<int, CountedHash> MakeNonSmallSet(int* count) {
  const int kFirstKey = -1000;
  linked_hash_set<int, CountedHash> s(0, CountedHash(count));
  for (int i = kFirstKey; i < kFirstKey + 100; ++i) {
    s.insert(i);
  }
  *count = 0;
  return s;
}

constexpr bool BuildHasDebugModeRehashes() {
#if !defined(NDEBUG) || defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
    defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
  return true;
#else
  return false;
#endif
}

TEST(LinkedHashSetTest, HashCountInOptBuilds) {
  if (BuildHasDebugModeRehashes()) {
    GTEST_SKIP() << "Only run under NDEBUG: `assert` statements and sanitizer "
                    "rehashing may cause redundant hashing.";
  }

  using Set = linked_hash_set<int, CountedHash>;
  {
    int count = 0;
    Set s = MakeNonSmallSet(&count);
    s.insert(1);
    EXPECT_EQ(count, 1);
    s.erase(1);
    EXPECT_EQ(count, 2);
  }
  {
    int count = 0;
    Set s = MakeNonSmallSet(&count);
    s.insert(3);
    EXPECT_EQ(count, 1);
    auto node = s.extract(3);
    EXPECT_EQ(count, 2);
    s.insert(std::move(node));
    EXPECT_EQ(count, 3);
  }
  {
    int count = 0;
    Set s = MakeNonSmallSet(&count);
    s.emplace(5);
    EXPECT_EQ(count, 1);
  }
  {
    int src_count = 0, dst_count = 0;
    Set src = MakeNonSmallSet(&src_count);
    Set dst = MakeNonSmallSet(&dst_count);
    src.insert(7);
    dst.merge(src);
    EXPECT_LE(src_count, 200);
    EXPECT_LE(dst_count, 200);
  }
}

}  // namespace
}  // namespace container_internal
ABSL_NAMESPACE_END
}  // namespace absl