summaryrefslogtreecommitdiff
path: root/src/mongo/base/clonable_ptr_test.cpp
blob: 4c4bfff3d35e8b5da46657f3b4efc965f67ab46e (plain)
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
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/base/clonable_ptr.h"

#include <functional>
#include <memory>
#include <tuple>

#include "mongo/unittest/unittest.h"

#include <boost/lexical_cast.hpp>

namespace {

template <typename Test>
void runSyntaxTest(Test&& t) {
    if (false)
        t();
}

// These testing helper classes model various kinds of class which should be compatible with
// `mongo::clonable_ptr`.  The basic use cases satisfied by each class are described in each class's
// documentation.

// This class models the `Clonable` concept, and is used to test the simple case of `clonable_ptr`.
class ClonableTest {
private:
    std::string data =
        "This is the string data which is stored to make Functor Clonable need a complicated copy "
        "ctor.";

public:
    std::unique_ptr<ClonableTest> clone() const {
        return std::make_unique<ClonableTest>();
    }
};

// This class provides a member structure which models `CloneFactory<AltClonableTest>`.  The member
// structure is available under the expected member name of `clone_factory_type`.  The
// `CloneFactory` is stateless.
class AltClonableTest {
private:
    std::string data =
        "This is the string data which is stored to make Functor Clonable need a complicated copy "
        "ctor.";

public:
    struct clone_factory_type {
        std::unique_ptr<AltClonableTest> operator()(const AltClonableTest&) const {
            return std::make_unique<AltClonableTest>();
        }
    };
};

// This class requires a companion cloning function models `CloneFactory<Alt2ClonableTest>`.  There
// is an attendant specialization of the `mongo::clonable_traits` metafunction to provide the clone
// factory for this type.  That `CloneFactory` is stateless.
class Alt2ClonableTest {
private:
    std::string data =
        "This is the string data which is stored to make Functor Clonable need a complicated copy "
        "ctor.";
};
}  // namespace

namespace mongo {
// This specialization of the `mongo::clonable_traits` metafunction provides a model of a stateless
// `CloneFactory<Alt2ClonableTest>`
template <>
struct clonable_traits<::Alt2ClonableTest> {
    struct clone_factory_type {
        std::unique_ptr<Alt2ClonableTest> operator()(const Alt2ClonableTest&) const {
            return std::make_unique<Alt2ClonableTest>();
        }
    };
};
}  // namespace mongo

namespace {
// This class uses a stateful cloning function provided by the `getCloningFunction` static member.
// This stateful `CloneFactory<FunctorClonable>` must be passed to constructors of the
// `cloning_ptr`.
class FunctorClonable {
private:
    std::string data =
        "This is the string data which is stored to make Functor Clonable need a complicated copy "
        "ctor.";

public:
    using CloningFunctionType =
        std::function<std::unique_ptr<FunctorClonable>(const FunctorClonable&)>;

    static CloningFunctionType getCloningFunction() {
        return [](const FunctorClonable& c) { return std::make_unique<FunctorClonable>(c); };
    }
};


// This class uses a stateful cloning function provided by the `getCloningFunction` static member.
// This stateful `CloneFactory<FunctorWithDynamicStateClonable>` must be passed to constructors of
// the `cloning_ptr`.  The `CloneFactory` for this type dynamically updates its internal state.
// This is used to test cloning of objects that have dynamically changing clone factories.
class FunctorWithDynamicStateClonable {
private:
    std::string data =
        "This is the string data which is stored to make Functor Clonable need a complicated copy "
        "ctor.";

public:
    FunctorWithDynamicStateClonable(const FunctorWithDynamicStateClonable&) = delete;
    FunctorWithDynamicStateClonable() = default;

    FunctorWithDynamicStateClonable(const std::string& s) : data(s) {}

    using CloningFunctionType = std::function<std::unique_ptr<FunctorWithDynamicStateClonable>(
        const FunctorWithDynamicStateClonable&)>;

    static CloningFunctionType getCloningFunction() {
        return [calls = 0](const FunctorWithDynamicStateClonable& c) mutable {
            return std::make_unique<FunctorWithDynamicStateClonable>(
                c.data + boost::lexical_cast<std::string>(++calls));
        };
    }
};

// This class models `Clonable`, with a return from clone which is
// `Constructible<std::unique_ptr<RawPointerClonable>>` but isn't
// `std::unique_ptr<RawPointerClonable>`.  This is used to test that the `clonable_ptr` class does
// not expect `RawPointerClonable::clone() const` to return a model of
// `UniquePtr<RawPointerClonable>`
class RawPointerClonable {
public:
    RawPointerClonable* clone() const {
        return new RawPointerClonable;
    }
};

// This class models `Clonable`, with a return from clone which is
// `Constructible<std::unique_ptr<UniquePtrClonable>>` because it is
// `std::unique_ptr<UniquePtrClonable>`.  This is used to test that the `clonable_ptr` class can
// use a `UniquePtrClonable::clone() const` that returns a model of
// `UniquePtr<UniquePtrClonable>`
class UniquePtrClonable {
public:
    std::unique_ptr<UniquePtrClonable> clone() const {
        return std::make_unique<UniquePtrClonable>();
    }
};

TEST(ClonablePtrTest, syntax_smoke_test) {
// TODO: Either add a compressed pair type for optimization, or wait for MSVC to get this feature by
//       default.  MSVC doesn't make its tuple compressed, which causes this test to fail on MSVC.
#ifndef _MSC_VER
    {
        mongo::clonable_ptr<ClonableTest> p;

        p = std::make_unique<ClonableTest>();

        mongo::clonable_ptr<ClonableTest> p2 = p;

        ASSERT_TRUE(p != p2);

        static_assert(sizeof(p) == sizeof(ClonableTest*),
                      "`mongo::clonable_ptr< T >` should be `sizeof` a pointer when there is no "
                      "CloneFactory");
    }
#endif

    {
        mongo::clonable_ptr<AltClonableTest> p;

        p = std::make_unique<AltClonableTest>();

        mongo::clonable_ptr<AltClonableTest> p2 = p;

        ASSERT_TRUE(p != p2);
    }

    {
        mongo::clonable_ptr<Alt2ClonableTest> p;

        p = std::make_unique<Alt2ClonableTest>();

        mongo::clonable_ptr<Alt2ClonableTest> p2 = p;

        ASSERT_TRUE(p != p2);
    }

    {
        mongo::clonable_ptr<FunctorClonable, FunctorClonable::CloningFunctionType> p{
            FunctorClonable::getCloningFunction()};

        p = std::make_unique<FunctorClonable>();

        mongo::clonable_ptr<FunctorClonable, FunctorClonable::CloningFunctionType> p2 = p;

        ASSERT_TRUE(p != p2);

        mongo::clonable_ptr<FunctorClonable, FunctorClonable::CloningFunctionType> p3{
            FunctorClonable::getCloningFunction()};

        auto tmp = std::make_unique<FunctorClonable>();
        p3 = std::move(tmp);

        ASSERT_TRUE(p != p2);
        ASSERT_TRUE(p2 != p3);
        ASSERT_TRUE(p != p3);
    }
}

// These tests check that all expected valid syntactic forms of use for the
// `mongo::clonable_ptr<Clonable>` are valid.  These tests assert nothing but provide a single
// unified place to check the syntax of this component.  Build failures in these parts indicate that
// a change to the component has broken an expected valid syntactic usage.  Any expected valid usage
// which is not in this list should be added.
namespace SyntaxTests {
template <typename Clonable>
void construction() {
    // Test default construction
    { mongo::clonable_ptr<Clonable>{}; }

    // Test construction from a nullptr
    { mongo::clonable_ptr<Clonable>{nullptr}; }

    // Test construction from a Clonable pointer.
    {
        Clonable* const local = nullptr;
        mongo::clonable_ptr<Clonable>{local};
    }

    // Test move construction.
    { std::ignore = mongo::clonable_ptr<Clonable>{mongo::clonable_ptr<Clonable>{}}; }

    // Test copy construction.
    {
        mongo::clonable_ptr<Clonable> a;
        mongo::clonable_ptr<Clonable> b{a};
    }

    // Test move assignment.
    {
        mongo::clonable_ptr<Clonable> a;
        a = mongo::clonable_ptr<Clonable>{};
    }

    // Test copy assignment.
    {
        mongo::clonable_ptr<Clonable> a;
        mongo::clonable_ptr<Clonable> b;
        b = a;
    }

    // Test unique pointer construction
    { mongo::clonable_ptr<Clonable>{std::make_unique<Clonable>()}; }

    // Test unique pointer construction (conversion)
    {
        auto acceptor = [](const mongo::clonable_ptr<Clonable>&) {};
        acceptor(std::make_unique<Clonable>());
    }

    // Test non-conversion pointer construction
    { static_assert(!std::is_convertible<Clonable*, mongo::clonable_ptr<Clonable>>::value, ""); }

    // Test conversion unique pointer construction
    {
        static_assert(
            std::is_convertible<std::unique_ptr<Clonable>, mongo::clonable_ptr<Clonable>>::value,
            "");
    }
}

// Tests that syntactic forms that require augmented construction are proper
template <typename Clonable, typename CloneFactory>
void augmentedConstruction() {
    // Test default construction
    {
        static_assert(
            !std::is_default_constructible<mongo::clonable_ptr<Clonable, CloneFactory>>::value, "");
    }

    // Test Clone Factory construction
    { mongo::clonable_ptr<Clonable, CloneFactory>{Clonable::getCloningFunction()}; }

// TODO: Revist this when MSVC's enable-if and deletion on ctors works.
#ifndef _MSC_VER
    // Test non-construction from a nullptr
    {
        static_assert(!std::is_constructible<mongo::clonable_ptr<Clonable, CloneFactory>,
                                             std::nullptr_t>::value,
                      "");
    }
#endif

    // Test construction from a nullptr with factory
    { mongo::clonable_ptr<Clonable, CloneFactory>{nullptr, Clonable::getCloningFunction()}; }

// TODO: Revist this when MSVC's enable-if and deletion on ctors works.
#ifndef _MSC_VER
    // Test construction from a raw Clonable pointer.
    {
        static_assert(
            !std::is_constructible<mongo::clonable_ptr<Clonable, CloneFactory>, Clonable*>::value,
            "");
    }
#endif


    // Test initialization of a raw Clonable pointer with factory, using reset.
    {
        Clonable* const local = nullptr;
        mongo::clonable_ptr<Clonable, CloneFactory> p{Clonable::getCloningFunction()};
        p.reset(local);
    }

    // Test move construction.
    {
        std::ignore = mongo::clonable_ptr<Clonable, CloneFactory>{
            mongo::clonable_ptr<Clonable, CloneFactory>{Clonable::getCloningFunction()}};
    }

    // Test copy construction.
    {
        mongo::clonable_ptr<Clonable, CloneFactory> a{Clonable::getCloningFunction()};
        mongo::clonable_ptr<Clonable, CloneFactory> b{a};
    }

    // Test augmented copy construction.
    {
        mongo::clonable_ptr<Clonable, CloneFactory> a{Clonable::getCloningFunction()};
        mongo::clonable_ptr<Clonable, CloneFactory> b{a, Clonable::getCloningFunction()};
    }


    // Test move assignment.
    {
        mongo::clonable_ptr<Clonable, CloneFactory> a{Clonable::getCloningFunction()};
        a = mongo::clonable_ptr<Clonable, CloneFactory>{Clonable::getCloningFunction()};
    }

    // Test copy assignment.
    {
        mongo::clonable_ptr<Clonable, CloneFactory> a{Clonable::getCloningFunction()};
        mongo::clonable_ptr<Clonable, CloneFactory> b{Clonable::getCloningFunction()};
        b = a;
    }

    // Test unique pointer construction
    {
        mongo::clonable_ptr<Clonable, CloneFactory>{std::make_unique<Clonable>(),
                                                    Clonable::getCloningFunction()};
    }

    // Test augmented unique pointer construction
    {
        mongo::clonable_ptr<Clonable, CloneFactory>{std::make_unique<Clonable>(),
                                                    Clonable::getCloningFunction()};
    }

    // Test non-conversion pointer construction
    {
        static_assert(
            !std::is_convertible<mongo::clonable_ptr<Clonable, CloneFactory>, Clonable*>::value,
            "");
    }

    // Test non-conversion from factory
    {
        static_assert(
            !std::is_convertible<mongo::clonable_ptr<Clonable, CloneFactory>, CloneFactory>::value,
            "");
    }

    // Test conversion unique pointer construction
    {
        static_assert(!std::is_convertible<std::unique_ptr<Clonable>,
                                           mongo::clonable_ptr<Clonable, CloneFactory>>::value,
                      "");
    }
}

template <typename Clonable>
void pointerOperations() {
    mongo::clonable_ptr<Clonable> a;

    // Test `.get()` functionality:
    {
        Clonable* p = a.get();
        (void)p;
    }

    // Test `->` functionality
    {
        // We don't actually want to call the dtor, but we want the compiler to check that we
        // have.
        if (false) {
            a->~Clonable();
        }
    }

    // Test `*` functionality
    Clonable& r = *a;
    (void)r;

    // Test reset functionality
    {
        a.reset();
        a.reset(nullptr);
        a.reset(new Clonable);
    }
}

template <typename Clonable>
void equalityOperations() {
    mongo::clonable_ptr<Clonable> a;
    mongo::clonable_ptr<Clonable> b;

    std::unique_ptr<Clonable> ua;

    // Test equality expressions
    {
        (void)(a == a);
        (void)(a == b);
        (void)(b == a);

        (void)(a == ua);
        (void)(ua == a);

        (void)(nullptr == a);
        (void)(a == nullptr);
    }

    // Test inequality expressions
    {
        (void)(a != a);
        (void)(a != b);
        (void)(b != a);

        (void)(a != ua);
        (void)(ua != a);

        (void)(nullptr == a);
        (void)(a == nullptr);
    }
}
}  // namespace SyntaxTests

TEST(ClonablePtrSyntaxTests, construction) {
    runSyntaxTest(SyntaxTests::construction<ClonableTest>);
    runSyntaxTest(SyntaxTests::construction<AltClonableTest>);
    runSyntaxTest(SyntaxTests::construction<Alt2ClonableTest>);
    runSyntaxTest(SyntaxTests::construction<RawPointerClonable>);
    runSyntaxTest(SyntaxTests::construction<UniquePtrClonable>);
}

TEST(ClonablePtrSyntaxTests, augmentedConstruction) {
    runSyntaxTest(
        SyntaxTests::augmentedConstruction<FunctorClonable, FunctorClonable::CloningFunctionType>);
    runSyntaxTest(
        SyntaxTests::augmentedConstruction<FunctorWithDynamicStateClonable,
                                           FunctorWithDynamicStateClonable::CloningFunctionType>);
}

TEST(ClonablePtrSyntaxTests, pointerOperations) {
    runSyntaxTest(SyntaxTests::pointerOperations<ClonableTest>);
    runSyntaxTest(SyntaxTests::pointerOperations<AltClonableTest>);
    runSyntaxTest(SyntaxTests::pointerOperations<Alt2ClonableTest>);
    runSyntaxTest(SyntaxTests::pointerOperations<RawPointerClonable>);
    runSyntaxTest(SyntaxTests::pointerOperations<UniquePtrClonable>);
}

TEST(ClonablePtrSyntaxTests, equalityOperations) {
    runSyntaxTest(SyntaxTests::equalityOperations<ClonableTest>);
    runSyntaxTest(SyntaxTests::equalityOperations<AltClonableTest>);
    runSyntaxTest(SyntaxTests::equalityOperations<Alt2ClonableTest>);
    runSyntaxTest(SyntaxTests::equalityOperations<RawPointerClonable>);
    runSyntaxTest(SyntaxTests::equalityOperations<UniquePtrClonable>);
}

namespace BehaviorTests {
class DetectDestruction {
public:
    static int activeCount;

    static void resetCount() {
        activeCount = 0;
    }

    ~DetectDestruction() {
        --activeCount;
    }

    DetectDestruction(const DetectDestruction&) {
        ++activeCount;
    }

    DetectDestruction& operator=(const DetectDestruction&) = delete;

    DetectDestruction(DetectDestruction&&) = delete;
    DetectDestruction& operator=(DetectDestruction&&) = delete;

    DetectDestruction() {
        ++activeCount;
    }

    std::unique_ptr<DetectDestruction> clone() const {
        return std::make_unique<DetectDestruction>(*this);
    }
};

int DetectDestruction::activeCount = 0;

struct DestructionGuard {
    DestructionGuard(const DestructionGuard&) = delete;
    DestructionGuard& operator=(const DestructionGuard&) = delete;

    DestructionGuard() {
        DetectDestruction::resetCount();
    }

    ~DestructionGuard() {
        ASSERT_EQ(DetectDestruction::activeCount, 0);
    }
};

TEST(ClonablePtrTest, basic_construction_test) {
    // Do not default construct the object
    {
        DestructionGuard check;
        mongo::clonable_ptr<DetectDestruction> p;
        ASSERT_EQ(DetectDestruction::activeCount, 0);
    }

    // Do not make unnecessary copies of the object from ptr
    {
        DestructionGuard check;
        mongo::clonable_ptr<DetectDestruction> p{new DetectDestruction};
        ASSERT_EQ(DetectDestruction::activeCount, 1);
    }

    // Do not make unnecessary copies of the object from unique_ptr
    {
        DestructionGuard check;
        mongo::clonable_ptr<DetectDestruction> p{std::make_unique<DetectDestruction>()};
        ASSERT_EQ(DetectDestruction::activeCount, 1);
    }
    {
        DestructionGuard check;
        mongo::clonable_ptr<DetectDestruction> p = std::make_unique<DetectDestruction>();
        ASSERT_EQ(DetectDestruction::activeCount, 1);
    }

    // Two separate constructions are unlinked
    {
        DestructionGuard check;

        mongo::clonable_ptr<DetectDestruction> p1{std::make_unique<DetectDestruction>()};
        ASSERT_EQ(DetectDestruction::activeCount, 1);

        {
            mongo::clonable_ptr<DetectDestruction> p2{std::make_unique<DetectDestruction>()};
            ASSERT_EQ(DetectDestruction::activeCount, 2);
        }
        ASSERT_EQ(DetectDestruction::activeCount, 1);
    }

    // Two separate constructions can have opposite order and be unlinked
    {
        DestructionGuard check;

        auto p1 = std::make_unique<mongo::clonable_ptr<DetectDestruction>>(
            std::make_unique<DetectDestruction>());
        ASSERT_EQ(DetectDestruction::activeCount, 1);

        auto p2 = std::make_unique<mongo::clonable_ptr<DetectDestruction>>(
            std::make_unique<DetectDestruction>());
        ASSERT_EQ(DetectDestruction::activeCount, 2);

        p1.reset();
        ASSERT_EQ(DetectDestruction::activeCount, 1);

        p2.reset();
        ASSERT_EQ(DetectDestruction::activeCount, 0);
    }
}

// TODO: Bring in an "equivalence class for equality predicate testing" framework.
// Equals and Not Equals need to be tested independently -- It is not valid to assume that equals
// and not equals are correctly implemented as complimentary predicates.  Equality must be
// reflexive, symmetric and transitive.  This requres several instances that all have the same
// value.  Simply testing "2 == 2" and "3 != 2" is insufficient.  Every combination of position and
// equality must be tested to come out as expected.
//
// Consider that with equality it is important to make sure that `a == b` has the same meaning as `b
// == a`.  It is also necessary to check that `a == b` and `b == c` and `a == c` is true, when all
// three are equal, and to do so in reverse: `b == a` and `c == b` and `c == a`.  Further, the
// relationships above have to hold for multiple cases.  Similar cases need to be tested for
// inequality.
//
// Further, equality is an incredibly important operation to test completely and thoroughly --
// besides being a critical element in code using any value modeling type, it also is the keystone
// in any testing schedule for a copyable and movable value type.  Almost all testing of behavior
// relies upon being able to detect fundamental differences in value.  In order to provide this
// correctly, we provide a full battery of tests for equality in all mathematically relevant
// situations.  For this equality testing schedule to be correct, we require a mechanism to
// initialize objects (and references to those objects) which have predictable value.  These
// predictable values are then used to test known equality expressions for the correct evaluation.
//
// All other tests can then just use equality to verify that an object is in the desired state.
// This greatly simplifies testing and also makes tests more precise.
TEST(ClonablePtrTest, basicEqualityTest) {
    DestructionGuard check;

    mongo::clonable_ptr<DetectDestruction> n1;
    mongo::clonable_ptr<DetectDestruction> n2;
    mongo::clonable_ptr<DetectDestruction> n3;

    mongo::clonable_ptr<DetectDestruction> a = std::make_unique<DetectDestruction>();
    mongo::clonable_ptr<DetectDestruction> b = std::make_unique<DetectDestruction>();
    mongo::clonable_ptr<DetectDestruction> c = std::make_unique<DetectDestruction>();

    const mongo::clonable_ptr<DetectDestruction>& ap = a;
    const mongo::clonable_ptr<DetectDestruction>& bp = b;
    const mongo::clonable_ptr<DetectDestruction>& cp = c;
    const mongo::clonable_ptr<DetectDestruction>& ap2 = a;
    const mongo::clonable_ptr<DetectDestruction>& bp2 = b;
    const mongo::clonable_ptr<DetectDestruction>& cp2 = c;

    // Equals operator

    // Identity checks

    ASSERT(n1 == n1);
    ASSERT(n2 == n2);
    ASSERT(n3 == n3);

    ASSERT(a == a);
    ASSERT(b == b);
    ASSERT(c == c);

    // Same value checks.  (Because unique pointers should never be the same value, we have to use
    // references.)

    ASSERT(n1 == n2);
    ASSERT(n1 == n3);

    ASSERT(n2 == n1);
    ASSERT(n2 == n3);

    ASSERT(n3 == n1);
    ASSERT(n3 == n2);

    ASSERT(a == ap);
    ASSERT(a == ap2);
    ASSERT(ap == a);
    ASSERT(ap == ap2);
    ASSERT(ap2 == a);
    ASSERT(ap2 == ap);

    ASSERT(b == bp);
    ASSERT(b == bp2);
    ASSERT(bp == b);
    ASSERT(bp == bp2);
    ASSERT(bp2 == b);
    ASSERT(bp2 == bp);

    ASSERT(c == cp);
    ASSERT(c == cp2);
    ASSERT(cp == c);
    ASSERT(cp == cp2);
    ASSERT(cp2 == c);
    ASSERT(cp2 == cp);

    // Different value checks:

    ASSERT(!(a == n1));
    ASSERT(!(b == n1));
    ASSERT(!(c == n1));
    ASSERT(!(a == n2));
    ASSERT(!(b == n2));
    ASSERT(!(c == n2));

    ASSERT(!(n1 == a));
    ASSERT(!(n1 == b));
    ASSERT(!(n1 == c));
    ASSERT(!(n2 == a));
    ASSERT(!(n2 == b));
    ASSERT(!(n2 == c));

    ASSERT(!(a == b));
    ASSERT(!(a == c));

    ASSERT(!(b == a));
    ASSERT(!(b == c));

    ASSERT(!(c == a));
    ASSERT(!(c == b));

    // Not Equals operator

    // Identity checks

    ASSERT(!(n1 != n1));
    ASSERT(!(n2 != n2));
    ASSERT(!(n3 != n3));

    ASSERT(!(a != a));
    ASSERT(!(b != b));
    ASSERT(!(c != c));

    // Same value checks.  (Because unique pointers should never be the same value, we have to use
    // references.)

    ASSERT(!(n1 != n2));
    ASSERT(!(n1 != n3));

    ASSERT(!(n2 != n1));
    ASSERT(!(n2 != n3));

    ASSERT(!(n3 != n1));
    ASSERT(!(n3 != n2));

    ASSERT(!(a != ap));
    ASSERT(!(a != ap2));
    ASSERT(!(ap != a));
    ASSERT(!(ap != ap2));
    ASSERT(!(ap2 != a));
    ASSERT(!(ap2 != ap));

    ASSERT(!(b != bp));
    ASSERT(!(b != bp2));
    ASSERT(!(bp != b));
    ASSERT(!(bp != bp2));
    ASSERT(!(bp2 != b));
    ASSERT(!(bp2 != bp));

    ASSERT(!(c != cp));
    ASSERT(!(c != cp2));
    ASSERT(!(cp != c));
    ASSERT(!(cp != cp2));
    ASSERT(!(cp2 != c));
    ASSERT(!(cp2 != cp));

    // Different value checks:

    ASSERT(a != n1);
    ASSERT(b != n1);
    ASSERT(c != n1);
    ASSERT(a != n2);
    ASSERT(b != n2);
    ASSERT(c != n2);

    ASSERT(n1 != a);
    ASSERT(n1 != b);
    ASSERT(n1 != c);
    ASSERT(n2 != a);
    ASSERT(n2 != b);
    ASSERT(n2 != c);

    ASSERT(a != b);
    ASSERT(a != c);

    ASSERT(b != a);
    ASSERT(b != c);

    ASSERT(c != a);
    ASSERT(c != b);
}

// TODO: all other forms of equality with other types (`std::nullptr_t` and `std::unique_ptr< T >`)
// need testing still.

TEST(ClonablePtrTest, ownershipStabilityTest) {
    {
        DestructionGuard check;

        auto ptr_init = std::make_unique<DetectDestruction>();
        const auto* rp = ptr_init.get();

        mongo::clonable_ptr<DetectDestruction> cp = std::move(ptr_init);

        ASSERT(rp == cp.get());

        mongo::clonable_ptr<DetectDestruction> cp2 = std::move(cp);

        ASSERT(rp == cp2.get());

        mongo::clonable_ptr<DetectDestruction> cp3;

        ASSERT(nullptr == cp3);

        cp3 = std::move(cp2);

        ASSERT(rp == cp3.get());
    }

    {
        auto ptr_init = std::make_unique<DetectDestruction>();
        const auto* rp = ptr_init.get();

        mongo::clonable_ptr<DetectDestruction> cp{ptr_init.release()};

        ASSERT(rp == cp.get());

        mongo::clonable_ptr<DetectDestruction> cp2 = std::move(cp);

        ASSERT(rp == cp2.get());

        mongo::clonable_ptr<DetectDestruction> cp3;

        ASSERT(nullptr == cp3.get());

        cp3 = std::move(cp2);

        ASSERT(rp == cp3.get());
    }
}

class ClonableObject {
private:
    int value = 0;

public:
    // ClonableObject( const ClonableObject & ) { abort(); }
    ClonableObject() = default;
    explicit ClonableObject(const int v) : value(v) {}

    std::unique_ptr<ClonableObject> clone() const {
        return std::make_unique<ClonableObject>(*this);
    }

    auto make_equality_lens() const -> decltype(std::tie(this->value)) {
        return std::tie(value);
    }
};

bool operator==(const ClonableObject& lhs, const ClonableObject& rhs) {
    return lhs.make_equality_lens() == rhs.make_equality_lens();
}

bool operator!=(const ClonableObject& lhs, const ClonableObject& rhs) {
    return !(lhs == rhs);
}

TEST(ClonablePtrTest, noObjectCopySemanticTest) {
    mongo::clonable_ptr<ClonableObject> p;

    mongo::clonable_ptr<ClonableObject> p2 = p;
    ASSERT(p == p2);

    mongo::clonable_ptr<ClonableObject> p3;

    p3 = p;
    ASSERT(p == p3);
}

TEST(ClonablePtrTest, objectCopySemanticTest) {
    mongo::clonable_ptr<ClonableObject> p = std::make_unique<ClonableObject>(1);
    mongo::clonable_ptr<ClonableObject> q = std::make_unique<ClonableObject>(2);
    ASSERT(p != q);
    ASSERT(*p != *q);

    mongo::clonable_ptr<ClonableObject> p2 = p;
    ASSERT(p != p2);
    ASSERT(*p == *p2);

    mongo::clonable_ptr<ClonableObject> q2 = q;
    ASSERT(q2 != q);
    ASSERT(q2 != p);
    ASSERT(q2 != p2);
    ASSERT(*q2 == *q);

    q2 = p2;
    ASSERT(q2 != q);
    ASSERT(q2 != p);
    ASSERT(q2 != p2);
    ASSERT(*q2 == *p2);
}

class Interface {
public:
    virtual ~Interface() = default;
    virtual void consumeText(const std::string& message) = 0;
    virtual std::string produceText() = 0;

    std::unique_ptr<Interface> clone() const {
        return std::unique_ptr<Interface>{this->clone_impl()};
    }

private:
    virtual Interface* clone_impl() const = 0;
};

class GeneratorImplementation : public Interface {
private:
    const std::string root;
    int generation = 0;

    GeneratorImplementation* clone_impl() const {
        return new GeneratorImplementation{*this};
    }

public:
    explicit GeneratorImplementation(const std::string& m) : root(m) {}

    void consumeText(const std::string&) override {}

    std::string produceText() override {
        return root + boost::lexical_cast<std::string>(++generation);
    }
};

class StorageImplementation : public Interface {
private:
    std::string store;

    StorageImplementation* clone_impl() const {
        return new StorageImplementation{*this};
    }

public:
    void consumeText(const std::string& m) override {
        store = m;
    }
    std::string produceText() override {
        return store;
    }
};


TEST(ClonablePtrSimpleTest, simpleUsageExample) {
    mongo::clonable_ptr<Interface> source;
    mongo::clonable_ptr<Interface> sink;

    mongo::clonable_ptr<Interface> instance = std::make_unique<StorageImplementation>();

    sink = instance;

    ASSERT(instance.get() != sink.get());

    instance = std::make_unique<GeneratorImplementation>("base message");


    source = std::move(instance);


    sink->consumeText(source->produceText());
}

}  // namespace BehaviorTests
}  // namespace