File: test_vec.cpp

package info (click to toggle)
rkcommon 1.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,276 kB
  • sloc: cpp: 33,504; sh: 31; makefile: 13
file content (439 lines) | stat: -rw-r--r-- 9,966 bytes parent folder | download
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
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "../catch.hpp"

#include "rkcommon/math/vec.h"

using namespace rkcommon::math;

#define RUN_FOR_FP_TYPES(name) \
  name<vec2f>();               \
  name<vec2d>();               \
  name<vec3f>();               \
  name<vec3fa>();              \
  name<vec3d>();               \
  name<vec4f>();               \
  name<vec4d>()

#define RUN_FOR_INT_TYPES(name) \
  name<vec2c>();                \
  name<vec2s>();                \
  name<vec2i>();                \
  name<vec2l>();                \
  name<vec3c>();                \
  name<vec3s>();                \
  name<vec3i>();                \
  name<vec3ia>();               \
  name<vec3l>();                \
  name<vec4c>();                \
  name<vec4s>();                \
  name<vec4i>();                \
  name<vec4l>()

#define RUN_FOR_ALL_TYPES(name) \
  RUN_FOR_FP_TYPES(name);       \
  RUN_FOR_INT_TYPES(name)

template <typename T>
inline void test_unary_op()
{
  REQUIRE((T(-1)) == -T(1));
}

TEST_CASE("Vector unary operators", "[vec]")
{
  RUN_FOR_ALL_TYPES(test_unary_op);
}

#define unary_functor_test(op, v)  \
  template <typename T>            \
  inline void test_##op()          \
  {                                \
    using Scalar = typename T::scalar_t;   \
    REQUIRE(op(T(v)) == T(op(Scalar(v)))); \
  }

unary_functor_test(rcp, 0.5);
unary_functor_test(rcp_safe, 0.5);
unary_functor_test(abs, -1);
unary_functor_test(sin, 1.0);
unary_functor_test(cos, 1.0);
#undef unary_functor_test

TEST_CASE("Vector unary functors", "[vec]")
{
  SECTION("Test rcp")
  {
    RUN_FOR_FP_TYPES(test_rcp);
  }

  SECTION("Test rcp_safe")
  {
    RUN_FOR_FP_TYPES(test_rcp_safe);
  }

  SECTION("Test abs")
  {
    RUN_FOR_ALL_TYPES(test_abs);
  }

  SECTION("Test sin")
  {
    RUN_FOR_FP_TYPES(test_sin);
  }

  SECTION("Test cos")
  {
    RUN_FOR_FP_TYPES(test_cos);
  }
}

#define binary_op_test(name, op, a, b, r) \
  template <typename T>                   \
  inline void test_binary_##name()        \
  {                                       \
    using Scalar = typename T::scalar_t;  \
    REQUIRE(T(a) op T(b) == T(r));        \
    REQUIRE(T(a) op Scalar(b) == T(r));   \
    REQUIRE(Scalar(a) op T(b) == T(r));   \
  }

binary_op_test(add, +, 1, 2, 3);
binary_op_test(sub, -, 3, 2, 1);
binary_op_test(mul, *, 2, 2, 4);
binary_op_test(div, /, 6, 2, 3);
binary_op_test(mod, %, 5, 3, 2);
#undef binary_op_test

TEST_CASE("Vector binary arithmetic operators", "[vec]")
{
  SECTION("Test add")
  {
    RUN_FOR_ALL_TYPES(test_binary_add);
  }

  SECTION("Test sub")
  {
    RUN_FOR_ALL_TYPES(test_binary_sub);
  }

  SECTION("Test mul")
  {
    RUN_FOR_ALL_TYPES(test_binary_mul);
  }

  SECTION("Test div")
  {
    RUN_FOR_ALL_TYPES(test_binary_div);
  }

  SECTION("Test mod")
  {
    RUN_FOR_INT_TYPES(test_binary_mod);
  }
}

#define assign_op_test(name, op, a, b, r) \
  template <typename T>                   \
  inline void test_assign_##name()        \
  {                                       \
    T a1(a), a2(a);                       \
    a1 op T(b);                           \
    a2 op b;                              \
    REQUIRE(a1 == T(r));                  \
    REQUIRE(a2 == T(r));                  \
  }

assign_op_test(add, +=, 1, 2, 3);
assign_op_test(sub, -=, 3, 2, 1);
assign_op_test(mul, *=, 2, 2, 4);
assign_op_test(div, /=, 6, 2, 3);
assign_op_test(mod, %=, 5, 3, 2);
#undef assign_op_test

TEST_CASE("Vector arithmetic assignment operators", "[vec]")
{
  SECTION("Test add")
  {
    RUN_FOR_ALL_TYPES(test_assign_add);
  }

  SECTION("Test sub")
  {
    RUN_FOR_ALL_TYPES(test_assign_sub);
  }

  SECTION("Test mul")
  {
    RUN_FOR_ALL_TYPES(test_assign_mul);
  }

  SECTION("Test div")
  {
    RUN_FOR_ALL_TYPES(test_assign_div);
  }

  SECTION("Test mod")
  {
    RUN_FOR_INT_TYPES(test_assign_mod);
  }
}

#define compare_op_test(name, op, a, b) \
  template <typename T>                 \
  inline void test_compare_##name()     \
  {                                     \
    REQUIRE(T(a) op T(b));              \
  }

compare_op_test(equal, ==, 1, 1);
compare_op_test(notEqual, !=, 1, 2);
#undef compare_op_test

template <typename T>
inline void test_compare_anyLessThan()
{
  REQUIRE(anyLessThan(T(1), T(2)));
}

TEST_CASE("Vector comparison operators", "[vec]")
{
  SECTION("Test equal")
  {
    RUN_FOR_ALL_TYPES(test_compare_equal);
  }

  SECTION("Test not equal")
  {
    RUN_FOR_ALL_TYPES(test_compare_notEqual);
  }

  SECTION("Test anyLessThan")
  {
    RUN_FOR_ALL_TYPES(test_compare_anyLessThan);
  }
}

template <typename T>
inline void test_dot()
{
  using Scalar = typename T::scalar_t;
  REQUIRE(dot(T(0), T(0)) == Scalar(0));
}

TEST_CASE("Vector dot functions", "[vec]")
{
  RUN_FOR_FP_TYPES(test_dot);
}

template <typename T>
inline void test_length()
{
  using Scalar = typename T::scalar_t;
  REQUIRE(length(T(0)) == Scalar(0));
}

TEST_CASE("Vector length functions", "[vec]")
{
  RUN_FOR_FP_TYPES(test_length);
}

template <typename T>
inline void test_cross()
{
  REQUIRE(cross(T(1, 0, 0), T(0, 1, 0)) == T(0, 0, 1));
}

TEST_CASE("Vector cross product", "[vec]")
{
  test_cross<vec3f>();
  test_cross<vec3d>();
}

template <typename T>
inline void test_normalize()
{
  using Scalar = typename T::scalar_t;
  REQUIRE(abs(Scalar(1) - length(normalize(T(1)))) <= Scalar(ulp));
  REQUIRE(abs(Scalar(1) - length(safe_normalize(T(1)))) <= Scalar(ulp));
}

TEST_CASE("Vector normalize", "[vec]")
{
  RUN_FOR_FP_TYPES(test_normalize);
}

template <typename T>
inline void test_interpolate()
{
  using Scalar = typename T::scalar_t;
  using Vector = vec_t<Scalar, 3>;
  REQUIRE(interpolate_uv(Vector(0.3, 0.2, 0.1), T(1), T(2), T(3)) == T(1));
}

TEST_CASE("Vector interpolation", "[vec]")
{
  RUN_FOR_FP_TYPES(test_interpolate);
}

#define binary_func_test(name, a, b, r)  \
  template <typename T>                  \
  inline void test_binary_##name()       \
  {                                      \
    using Scalar = typename T::scalar_t; \
    REQUIRE(name(T(a), T(b)) == T(r));   \
  }

binary_func_test(min, 1, 2, 1);
binary_func_test(max, 1, 2, 2);
binary_func_test(divRoundUp, 3, 2, 2);
#undef binary_func_test

TEST_CASE("Vector binary functors", "[vec]")
{
  SECTION("Test min")
  {
    RUN_FOR_ALL_TYPES(test_binary_min);
  }

  SECTION("Test max")
  {
    RUN_FOR_ALL_TYPES(test_binary_max);
  }

  SECTION("Test divRoundUp")
  {
    RUN_FOR_ALL_TYPES(test_binary_divRoundUp);
  }
}

#define reduce_test(dim, op, r, ...)           \
  template <typename T>                        \
  inline void test_reduce_##op##_##dim()       \
  {                                            \
    REQUIRE(reduce_##op(T(__VA_ARGS__)) == r); \
  }

reduce_test(2, add, 3, 1, 2);
reduce_test(2, mul, 4, 2, 2);
reduce_test(2, min, 1, 1, 2);
reduce_test(2, max, 2, 1, 2);

reduce_test(3, add, 6, 1, 2, 3);
reduce_test(3, mul, 8, 2, 2, 2);
reduce_test(3, min, 1, 1, 2, 3);
reduce_test(3, max, 3, 1, 2, 3);

reduce_test(4, add, 10, 1, 2, 3, 4);
reduce_test(4, mul, 16, 2, 2, 2, 2);
reduce_test(4, min, 1, 1, 2, 3, 4);
reduce_test(4, max, 4, 1, 2, 3, 4);
#undef reduce_test

TEST_CASE("Vector reductions", "[vec]")
{
  SECTION("Test reduce_add")
  {
    test_reduce_add_2<vec2c>();
    test_reduce_add_2<vec2s>();
    test_reduce_add_2<vec2i>();
    test_reduce_add_2<vec2l>();
    test_reduce_add_2<vec2f>();
    test_reduce_add_2<vec2d>();

    test_reduce_add_3<vec3c>();
    test_reduce_add_3<vec3s>();
    test_reduce_add_3<vec3i>();
    test_reduce_add_3<vec3ia>();
    test_reduce_add_3<vec3l>();
    test_reduce_add_3<vec3f>();
    test_reduce_add_3<vec3fa>();
    test_reduce_add_3<vec3d>();

    test_reduce_add_4<vec4c>();
    test_reduce_add_4<vec4s>();
    test_reduce_add_4<vec4i>();
    test_reduce_add_4<vec4l>();
    test_reduce_add_4<vec4f>();
    test_reduce_add_4<vec4d>();
  }

  SECTION("Test reduce_mul")
  {
    test_reduce_mul_2<vec2c>();
    test_reduce_mul_2<vec2s>();
    test_reduce_mul_2<vec2i>();
    test_reduce_mul_2<vec2l>();
    test_reduce_mul_2<vec2f>();
    test_reduce_mul_2<vec2d>();

    test_reduce_mul_3<vec3c>();
    test_reduce_mul_3<vec3s>();
    test_reduce_mul_3<vec3i>();
    test_reduce_mul_3<vec3ia>();
    test_reduce_mul_3<vec3l>();
    test_reduce_mul_3<vec3f>();
    test_reduce_mul_3<vec3fa>();
    test_reduce_mul_3<vec3d>();

    test_reduce_mul_4<vec4c>();
    test_reduce_mul_4<vec4s>();
    test_reduce_mul_4<vec4i>();
    test_reduce_mul_4<vec4l>();
    test_reduce_mul_4<vec4f>();
    test_reduce_mul_4<vec4d>();
  }

  SECTION("Test reduce_min")
  {
    test_reduce_min_2<vec2c>();
    test_reduce_min_2<vec2s>();
    test_reduce_min_2<vec2i>();
    test_reduce_min_2<vec2l>();
    test_reduce_min_2<vec2f>();
    test_reduce_min_2<vec2d>();

    test_reduce_min_3<vec3c>();
    test_reduce_min_3<vec3s>();
    test_reduce_min_3<vec3i>();
    test_reduce_min_3<vec3ia>();
    test_reduce_min_3<vec3l>();
    test_reduce_min_3<vec3f>();
    test_reduce_min_3<vec3fa>();
    test_reduce_min_3<vec3d>();

    test_reduce_min_4<vec4c>();
    test_reduce_min_4<vec4s>();
    test_reduce_min_4<vec4i>();
    test_reduce_min_4<vec4l>();
    test_reduce_min_4<vec4f>();
    test_reduce_min_4<vec4d>();
  }

  SECTION("Test reduce_max")
  {
    test_reduce_max_2<vec2c>();
    test_reduce_max_2<vec2s>();
    test_reduce_max_2<vec2i>();
    test_reduce_max_2<vec2l>();
    test_reduce_max_2<vec2f>();
    test_reduce_max_2<vec2d>();

    test_reduce_max_3<vec3c>();
    test_reduce_max_3<vec3s>();
    test_reduce_max_3<vec3i>();
    test_reduce_max_3<vec3ia>();
    test_reduce_max_3<vec3l>();
    test_reduce_max_3<vec3f>();
    test_reduce_max_3<vec3fa>();
    test_reduce_max_3<vec3d>();

    test_reduce_max_4<vec4c>();
    test_reduce_max_4<vec4s>();
    test_reduce_max_4<vec4i>();
    test_reduce_max_4<vec4l>();
    test_reduce_max_4<vec4f>();
    test_reduce_max_4<vec4d>();
  }
}