File size: 10,979 Bytes
be11144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <unittest/unittest.h>
#include <thrust/detail/alignment.h>

struct alignof_mock_0
{
    char a;
    char b;
}; // size: 2 * sizeof(char), alignment: sizeof(char)

struct alignof_mock_1
{
    int n;
    char c;
    // sizeof(int) - sizeof(char) bytes of padding
}; // size: 2 * sizeof(int), alignment: sizeof(int)

struct alignof_mock_2
{
    int n;
    char c;
    // sizeof(int) - sizeof(char) bytes of padding
}; // size: 2 * sizeof(int), alignment: sizeof(int)

struct alignof_mock_3
{
    char c;
    // sizeof(int) - sizeof(char) bytes of padding
    int n;
}; // size: 2 * sizeof(int), alignment: sizeof(int)

struct alignof_mock_4
{
    char c0;
    // sizeof(int) - sizeof(char) bytes of padding
    int n;
    char c1;
    // sizeof(int) - sizeof(char) bytes of padding
}; // size: 3 * sizeof(int), alignment: sizeof(int)

struct alignof_mock_5
{
    char c0;
    char c1;
    // sizeof(int) - 2 * sizeof(char) bytes of padding
    int n;
}; // size: 2 * sizeof(int), alignment: sizeof(int)

struct alignof_mock_6
{
    int n;
    char c0;
    char c1;
    // sizeof(int) - 2 * sizeof(char) bytes of padding
}; // size: 2 * sizeof(int), alignment: sizeof(int)

void test_alignof_mocks_sizes()
{
    ASSERT_EQUAL(sizeof(alignof_mock_0), 2 * sizeof(char));
    ASSERT_EQUAL(sizeof(alignof_mock_1), 2 * sizeof(int));
    ASSERT_EQUAL(sizeof(alignof_mock_2), 2 * sizeof(int));
    ASSERT_EQUAL(sizeof(alignof_mock_3), 2 * sizeof(int));
    ASSERT_EQUAL(sizeof(alignof_mock_4), 3 * sizeof(int));
    ASSERT_EQUAL(sizeof(alignof_mock_5), 2 * sizeof(int));
    ASSERT_EQUAL(sizeof(alignof_mock_6), 2 * sizeof(int));
}
DECLARE_UNITTEST(test_alignof_mocks_sizes);

void test_alignof()
{
    ASSERT_EQUAL(THRUST_ALIGNOF(bool)                  , sizeof(bool));
    ASSERT_EQUAL(THRUST_ALIGNOF(signed char)           , sizeof(signed char));
    ASSERT_EQUAL(THRUST_ALIGNOF(unsigned char)         , sizeof(unsigned char));
    ASSERT_EQUAL(THRUST_ALIGNOF(char)                  , sizeof(char));
    ASSERT_EQUAL(THRUST_ALIGNOF(short int)             , sizeof(short int));
    ASSERT_EQUAL(THRUST_ALIGNOF(unsigned short int)    , sizeof(unsigned short int));
    ASSERT_EQUAL(THRUST_ALIGNOF(int)                   , sizeof(int));
    ASSERT_EQUAL(THRUST_ALIGNOF(unsigned int)          , sizeof(unsigned int));
    ASSERT_EQUAL(THRUST_ALIGNOF(long int)              , sizeof(long int));
    ASSERT_EQUAL(THRUST_ALIGNOF(unsigned long int)     , sizeof(unsigned long int));
    ASSERT_EQUAL(THRUST_ALIGNOF(long long int)         , sizeof(long long int));
    ASSERT_EQUAL(THRUST_ALIGNOF(unsigned long long int), sizeof(unsigned long long int));
    ASSERT_EQUAL(THRUST_ALIGNOF(float)                 , sizeof(float));
    ASSERT_EQUAL(THRUST_ALIGNOF(double)                , sizeof(double));
    ASSERT_EQUAL(THRUST_ALIGNOF(long double)           , sizeof(long double));

    ASSERT_EQUAL(THRUST_ALIGNOF(alignof_mock_0), sizeof(char));
    ASSERT_EQUAL(THRUST_ALIGNOF(alignof_mock_1), sizeof(int));
    ASSERT_EQUAL(THRUST_ALIGNOF(alignof_mock_2), sizeof(int));
    ASSERT_EQUAL(THRUST_ALIGNOF(alignof_mock_3), sizeof(int));
    ASSERT_EQUAL(THRUST_ALIGNOF(alignof_mock_4), sizeof(int));
    ASSERT_EQUAL(THRUST_ALIGNOF(alignof_mock_5), sizeof(int));
    ASSERT_EQUAL(THRUST_ALIGNOF(alignof_mock_6), sizeof(int));
}
DECLARE_UNITTEST(test_alignof);

void test_alignment_of()
{
    ASSERT_EQUAL(
        thrust::detail::alignment_of<bool>::value
      , sizeof(bool)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<signed char>::value
      , sizeof(signed char)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<unsigned char>::value
      , sizeof(unsigned char)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<char>::value
      , sizeof(char)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<short int>::value
      , sizeof(short int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<unsigned short int>::value
      , sizeof(unsigned short int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<int>::value
      , sizeof(int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<unsigned int>::value
      , sizeof(unsigned int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<long int>::value
      , sizeof(long int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<unsigned long int>::value
      , sizeof(unsigned long int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<long long int>::value
      , sizeof(long long int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<unsigned long long int>::value
      , sizeof(unsigned long long int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<float>::value
      , sizeof(float)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<double>::value
      , sizeof(double)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<long double>::value
      , sizeof(long double)
    );

    ASSERT_EQUAL(
        thrust::detail::alignment_of<alignof_mock_0>::value
      , sizeof(char)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<alignof_mock_1>::value
      , sizeof(int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<alignof_mock_2>::value
      , sizeof(int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<alignof_mock_3>::value
      , sizeof(int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<alignof_mock_4>::value
      , sizeof(int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<alignof_mock_5>::value
      , sizeof(int)
    );
    ASSERT_EQUAL(
        thrust::detail::alignment_of<alignof_mock_6>::value
      , sizeof(int)
    );
}
DECLARE_UNITTEST(test_alignment_of);

template <std::size_t Align>
void test_aligned_type_instantiation()
{
    typedef typename thrust::detail::aligned_type<Align>::type type;
    ASSERT_GEQUAL(sizeof(type), 1lu);
    ASSERT_EQUAL(THRUST_ALIGNOF(type), Align);
    ASSERT_EQUAL(thrust::detail::alignment_of<type>::value, Align);
}

void test_aligned_type()
{
    test_aligned_type_instantiation<1>();
    test_aligned_type_instantiation<2>();
    test_aligned_type_instantiation<4>();
    test_aligned_type_instantiation<8>();
    test_aligned_type_instantiation<16>();
    test_aligned_type_instantiation<32>();
    test_aligned_type_instantiation<64>();
    test_aligned_type_instantiation<128>();
}
DECLARE_UNITTEST(test_aligned_type);

template <std::size_t Len, std::size_t Align>
void test_aligned_storage_instantiation(thrust::detail::true_type /* Align is valid */)
{
    typedef typename thrust::detail::aligned_storage<Len, Align>::type type;
    ASSERT_GEQUAL(sizeof(type), Len);
    ASSERT_EQUAL(THRUST_ALIGNOF(type), Align);
    ASSERT_EQUAL(thrust::detail::alignment_of<type>::value, Align);
}

template <std::size_t Len, std::size_t Align>
void test_aligned_storage_instantiation(thrust::detail::false_type /* Align is invalid */)
{
  // no-op -- alignment is > max_align_t and MSVC complains loudly.
}

template <std::size_t Len, std::size_t Align>
void test_aligned_storage_instantiation()
{
  typedef thrust::detail::integral_constant<
      bool, Align <= THRUST_ALIGNOF(thrust::detail::max_align_t)>
      ValidAlign;
  test_aligned_storage_instantiation<Len, Align>(ValidAlign());
}

template <std::size_t Len>
void test_aligned_storage_size()
{
    test_aligned_storage_instantiation<Len, 1>();
    test_aligned_storage_instantiation<Len, 2>();
    test_aligned_storage_instantiation<Len, 4>();
    test_aligned_storage_instantiation<Len, 8>();
    test_aligned_storage_instantiation<Len, 16>();
    test_aligned_storage_instantiation<Len, 32>();
    test_aligned_storage_instantiation<Len, 64>();
    test_aligned_storage_instantiation<Len, 128>();
}

void test_aligned_storage()
{
    test_aligned_storage_size<1>();
    test_aligned_storage_size<2>();
    test_aligned_storage_size<4>();
    test_aligned_storage_size<8>();
    test_aligned_storage_size<16>();
    test_aligned_storage_size<32>();
    test_aligned_storage_size<64>();
    test_aligned_storage_size<128>();
    test_aligned_storage_size<256>();
    test_aligned_storage_size<512>();
    test_aligned_storage_size<1024>();
    test_aligned_storage_size<2048>();
    test_aligned_storage_size<4096>();
    test_aligned_storage_size<8192>();
    test_aligned_storage_size<16384>();

    test_aligned_storage_size<3>();
    test_aligned_storage_size<5>();
    test_aligned_storage_size<7>();

    test_aligned_storage_size<17>();
    test_aligned_storage_size<42>();

    test_aligned_storage_size<10000>();
}
DECLARE_UNITTEST(test_aligned_storage);

void test_max_align_t()
{
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(bool)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(signed char)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(unsigned char)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(char)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(short int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(unsigned short int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(unsigned int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(long int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(unsigned long int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(long long int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(unsigned long long int)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(float)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(double)
    );
    ASSERT_GEQUAL(
        THRUST_ALIGNOF(thrust::detail::max_align_t)
      , THRUST_ALIGNOF(long double)
    );
}
DECLARE_UNITTEST(test_max_align_t);

void test_aligned_reinterpret_cast()
{
    thrust::detail::aligned_type<1>* a1 = 0;

    thrust::detail::aligned_type<2>* a2 = 0;

    // Cast to type with stricter (larger) alignment requirement.
    a2 = thrust::detail::aligned_reinterpret_cast<
        thrust::detail::aligned_type<2>*
    >(a1);

    // Cast to type with less strict (smaller) alignment requirement.
    a1 = thrust::detail::aligned_reinterpret_cast<
        thrust::detail::aligned_type<1>*
    >(a2);
}
DECLARE_UNITTEST(test_aligned_reinterpret_cast);