forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast.cpp
More file actions
99 lines (86 loc) · 2.79 KB
/
cast.cpp
File metadata and controls
99 lines (86 loc) · 2.79 KB
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
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/arith.h>
#include <af/array.h>
#include <af/data.h>
using af::cdouble;
using af::cfloat;
using af::dim4;
using af::dtype_traits;
const int num = 10;
template<typename Ti, typename To>
void cast_test() {
SUPPORTED_TYPE_CHECK(Ti);
SUPPORTED_TYPE_CHECK(To);
af_dtype ta = (af_dtype)dtype_traits<Ti>::af_type;
af_dtype tb = (af_dtype)dtype_traits<To>::af_type;
dim4 dims(num, 1, 1, 1);
af_array a, b;
af_randu(&a, dims.ndims(), dims.get(), ta);
af_err err = af_cast(&b, a, tb);
af_release_array(a);
af_release_array(b);
ASSERT_SUCCESS(err);
}
#define REAL_TO_TESTS(Ti, To) \
TEST(CAST_TEST, Test_Real_##Ti##_##To) { cast_test<Ti, To>(); }
#define REAL_TEST_INVOKE(Ti) \
REAL_TO_TESTS(Ti, float); \
REAL_TO_TESTS(Ti, cfloat); \
REAL_TO_TESTS(Ti, double); \
REAL_TO_TESTS(Ti, cdouble); \
REAL_TO_TESTS(Ti, char); \
REAL_TO_TESTS(Ti, int); \
REAL_TO_TESTS(Ti, unsigned); \
REAL_TO_TESTS(Ti, uchar); \
REAL_TO_TESTS(Ti, intl); \
REAL_TO_TESTS(Ti, uintl); \
REAL_TO_TESTS(Ti, short); \
REAL_TO_TESTS(Ti, ushort);
#define CPLX_TEST_INVOKE(Ti) \
REAL_TO_TESTS(Ti, cfloat); \
REAL_TO_TESTS(Ti, cdouble);
REAL_TEST_INVOKE(float)
REAL_TEST_INVOKE(double)
REAL_TEST_INVOKE(char)
REAL_TEST_INVOKE(int)
REAL_TEST_INVOKE(unsigned)
REAL_TEST_INVOKE(uchar)
REAL_TEST_INVOKE(intl)
REAL_TEST_INVOKE(uintl)
REAL_TEST_INVOKE(short)
REAL_TEST_INVOKE(ushort)
CPLX_TEST_INVOKE(cfloat)
CPLX_TEST_INVOKE(cdouble)
// Converting complex to real; expected to fail as this operation is
// not allowed. Use functions abs, real, image, arg, etc to make the
// conversion explicit.
template<typename Ti, typename To>
void cast_test_complex_real() {
SUPPORTED_TYPE_CHECK(Ti);
SUPPORTED_TYPE_CHECK(To);
af_dtype ta = (af_dtype)dtype_traits<Ti>::af_type;
af_dtype tb = (af_dtype)dtype_traits<To>::af_type;
dim4 dims(num, 1, 1, 1);
af_array a, b;
af_randu(&a, dims.ndims(), dims.get(), ta);
af_err err = af_cast(&b, a, tb);
ASSERT_EQ(err, AF_ERR_TYPE);
ASSERT_SUCCESS(af_release_array(a));
}
#define COMPLEX_REAL_TESTS(Ti, To) \
TEST(CAST_TEST, Test_Complex_To_Real_##Ti##_##To) { \
cast_test_complex_real<Ti, To>(); \
}
COMPLEX_REAL_TESTS(cfloat, float)
COMPLEX_REAL_TESTS(cfloat, double)
COMPLEX_REAL_TESTS(cdouble, float)
COMPLEX_REAL_TESTS(cdouble, double)