1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "mozilla/Pair.h"
8 #include "mozilla/TypeTraits.h"
9
10 using mozilla::IsSame;
11 using mozilla::MakePair;
12 using mozilla::Pair;
13
14 // Sizes aren't part of the guaranteed Pair interface, but we want to verify our
15 // attempts at compactness through EBO are moderately functional, *somewhere*.
16 #define INSTANTIATE(T1, T2, name, size) \
17 Pair<T1, T2> name##_1(T1(0), T2(0)); \
18 static_assert(sizeof(name##_1.first()) > 0, \
19 "first method should work on Pair<" #T1 ", " #T2 ">"); \
20 static_assert(sizeof(name##_1.second()) > 0, \
21 "second method should work on Pair<" #T1 ", " #T2 ">"); \
22 static_assert(sizeof(name##_1) == (size), \
23 "Pair<" #T1 ", " #T2 "> has an unexpected size"); \
24 Pair<T2, T1> name##_2(T2(0), T1(0)); \
25 static_assert(sizeof(name##_2.first()) > 0, \
26 "first method should work on Pair<" #T2 ", " #T1 ">"); \
27 static_assert(sizeof(name##_2.second()) > 0, \
28 "second method should work on Pair<" #T2 ", " #T1 ">"); \
29 static_assert(sizeof(name##_2) == (size), \
30 "Pair<" #T2 ", " #T1 "> has an unexpected size");
31
32 INSTANTIATE(int, int, prim1, 2 * sizeof(int));
33 INSTANTIATE(int, long, prim2, 2 * sizeof(long));
34
EmptyClassEmptyClass35 struct EmptyClass { explicit EmptyClass(int) {} };
NonEmptyNonEmpty36 struct NonEmpty { char mC; explicit NonEmpty(int) {} };
37
38 INSTANTIATE(int, EmptyClass, both1, sizeof(int));
39 INSTANTIATE(int, NonEmpty, both2, 2 * sizeof(int));
40 INSTANTIATE(EmptyClass, NonEmpty, both3, 1);
41
AA42 struct A { char dummy; explicit A(int) {} };
BB43 struct B : A { explicit B(int aI) : A(aI) {} };
44
45 INSTANTIATE(A, A, class1, 2);
46 INSTANTIATE(A, B, class2, 2);
47 INSTANTIATE(A, EmptyClass, class3, 1);
48
OtherEmptyOtherEmpty49 struct OtherEmpty : EmptyClass { explicit OtherEmpty(int aI) : EmptyClass(aI) {} };
50
51 // C++11 requires distinct objects of the same type, within the same "most
52 // derived object", to have different addresses. Pair allocates its elements as
53 // two bases, a base and a member, or two members. If the two elements have
54 // non-zero size or are unrelated, no big deal. But if they're both empty and
55 // related, something -- possibly both -- must be inflated. Exactly which are
56 // inflated depends which PairHelper specialization is used. We could
57 // potentially assert something about size for this case, but whatever we could
58 // assert would be very finicky. Plus it's two empty classes -- hardly likely.
59 // So don't bother trying to assert anything about this case.
60 //INSTANTIATE(EmptyClass, OtherEmpty, class4, ...something finicky...);
61
62 int
main()63 main()
64 {
65 A a(0);
66 B b(0);
67 const A constA(0);
68 const B constB(0);
69
70 // Check that MakePair generates Pair objects of the correct types.
71 static_assert(IsSame<decltype(MakePair(A(0), B(0))), Pair<A, B>>::value,
72 "MakePair should strip rvalue references");
73 static_assert(IsSame<decltype(MakePair(a, b)), Pair<A, B>>::value,
74 "MakePair should strip lvalue references");
75 static_assert(IsSame<decltype(MakePair(constA, constB)), Pair<A, B>>::value,
76 "MakePair should strip CV-qualifiers");
77
78 // Check that copy assignment and move assignment work.
79 a = constA;
80 a = A(0);
81
82 return 0;
83 }
84