[mips] Whitespace cleanup in MIPS16 tests to reduce noise in following changes. NFC.
[oota-llvm.git] / unittests / Support / TrailingObjectsTest.cpp
1 //=== - llvm/unittest/Support/TrailingObjectsTest.cpp ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/TrailingObjects.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
15 namespace {
16 // This class, beyond being used by the test case, a nice
17 // demonstration of the intended usage of TrailingObjects, with a
18 // single trailing array.
19 class Class1 final : protected TrailingObjects<Class1, short> {
20   friend TrailingObjects;
21
22   unsigned NumShorts;
23
24 protected:
25   size_t numTrailingObjects(OverloadToken<short>) const { return NumShorts; }
26
27   Class1(int *ShortArray, unsigned NumShorts) : NumShorts(NumShorts) {
28     std::uninitialized_copy(ShortArray, ShortArray + NumShorts,
29                             getTrailingObjects<short>());
30   }
31
32 public:
33   static Class1 *create(int *ShortArray, unsigned NumShorts) {
34     void *Mem = ::operator new(totalSizeToAlloc<short>(NumShorts));
35     return new (Mem) Class1(ShortArray, NumShorts);
36   }
37
38   short get(unsigned Num) const { return getTrailingObjects<short>()[Num]; }
39
40   unsigned numShorts() const { return NumShorts; }
41
42   // Pull some protected members in as public, for testability.
43   using TrailingObjects::totalSizeToAlloc;
44   using TrailingObjects::additionalSizeToAlloc;
45   using TrailingObjects::getTrailingObjects;
46 };
47
48 // Here, there are two singular optional object types appended.
49 // Note that it fails to compile without the alignment spec.
50 class LLVM_ALIGNAS(8) Class2 final : protected TrailingObjects<Class2, double, short> {
51   friend TrailingObjects;
52
53   bool HasShort, HasDouble;
54
55 protected:
56   size_t numTrailingObjects(OverloadToken<short>) const {
57     return HasShort ? 1 : 0;
58   }
59   size_t numTrailingObjects(OverloadToken<double>) const {
60     return HasDouble ? 1 : 0;
61   }
62
63   Class2(bool HasShort, bool HasDouble)
64       : HasShort(HasShort), HasDouble(HasDouble) {}
65
66 public:
67   static Class2 *create(short S = 0, double D = 0.0) {
68     bool HasShort = S != 0;
69     bool HasDouble = D != 0.0;
70
71     void *Mem =
72         ::operator new(totalSizeToAlloc<double, short>(HasDouble, HasShort));
73     Class2 *C = new (Mem) Class2(HasShort, HasDouble);
74     if (HasShort)
75       *C->getTrailingObjects<short>() = S;
76     if (HasDouble)
77       *C->getTrailingObjects<double>() = D;
78     return C;
79   }
80
81   short getShort() const {
82     if (!HasShort)
83       return 0;
84     return *getTrailingObjects<short>();
85   }
86
87   double getDouble() const {
88     if (!HasDouble)
89       return 0.0;
90     return *getTrailingObjects<double>();
91   }
92
93   // Pull some protected members in as public, for testability.
94   using TrailingObjects::totalSizeToAlloc;
95   using TrailingObjects::additionalSizeToAlloc;
96   using TrailingObjects::getTrailingObjects;
97 };
98
99 TEST(TrailingObjects, OneArg) {
100   int arr[] = {1, 2, 3};
101   Class1 *C = Class1::create(arr, 3);
102   EXPECT_EQ(sizeof(Class1), sizeof(unsigned));
103   EXPECT_EQ(Class1::additionalSizeToAlloc<short>(1), sizeof(short));
104   EXPECT_EQ(Class1::additionalSizeToAlloc<short>(3), sizeof(short) * 3);
105
106   EXPECT_EQ(Class1::totalSizeToAlloc<short>(1), sizeof(Class1) + sizeof(short));
107   EXPECT_EQ(Class1::totalSizeToAlloc<short>(3),
108             sizeof(Class1) + sizeof(short) * 3);
109
110   EXPECT_EQ(C->getTrailingObjects<short>(), reinterpret_cast<short *>(C + 1));
111   EXPECT_EQ(C->get(0), 1);
112   EXPECT_EQ(C->get(2), 3);
113   delete C;
114 }
115
116 TEST(TrailingObjects, TwoArg) {
117   Class2 *C1 = Class2::create(4);
118   Class2 *C2 = Class2::create(0, 4.2);
119
120   EXPECT_EQ(sizeof(Class2), 8u); // due to alignment
121
122   EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(1, 0)),
123             sizeof(double));
124   EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(0, 1)),
125             sizeof(short));
126   EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(3, 1)),
127             sizeof(double) * 3 + sizeof(short));
128
129   EXPECT_EQ((Class2::totalSizeToAlloc<double, short>(1, 1)),
130             sizeof(Class2) + sizeof(double) + sizeof(short));
131
132   EXPECT_EQ(C1->getDouble(), 0);
133   EXPECT_EQ(C1->getShort(), 4);
134   EXPECT_EQ(C1->getTrailingObjects<double>(),
135             reinterpret_cast<double *>(C1 + 1));
136   EXPECT_EQ(C1->getTrailingObjects<short>(), reinterpret_cast<short *>(C1 + 1));
137
138   EXPECT_EQ(C2->getDouble(), 4.2);
139   EXPECT_EQ(C2->getShort(), 0);
140   EXPECT_EQ(C2->getTrailingObjects<double>(),
141             reinterpret_cast<double *>(C2 + 1));
142   EXPECT_EQ(C2->getTrailingObjects<short>(),
143             reinterpret_cast<short *>(reinterpret_cast<double *>(C2 + 1) + 1));
144   delete C1;
145   delete C2;
146 }
147 }