forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar.cpp
More file actions
119 lines (93 loc) · 2.78 KB
/
var.cpp
File metadata and controls
119 lines (93 loc) · 2.78 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************
* 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 <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <string>
#include <vector>
#include <testHelpers.hpp>
using std::string;
using std::vector;
using af::cdouble;
using af::cfloat;
template<typename T>
struct helperType {
typedef typename cond_type<is_same_type<T, uchar>::value,
uint,
T>::type type;
};
template<typename T>
struct varOutType {
typedef typename cond_type<is_same_type<T, char>::value,
int,
typename helperType<T>::type >::type type;
};
//////////////////////////////// CPP ////////////////////////////////////
// test var_all interface using cpp api
template<typename T>
void testCPPVar(T const_value, af::dim4 dims)
{
typedef typename varOutType<T>::type outType;
if (noDoubleTests<T>()) return;
using af::array;
using af::var;
vector<T> hundred(dims.elements(), const_value);
outType gold = outType(0);
array a(dims, &(hundred.front()));
outType output = var<outType>(a, false);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-3);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
output = var<outType>(a, true);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-3);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
gold = outType(2.5);
outType tmp[] = { outType(0), outType(1), outType(2), outType(3),
outType(4) };
array b(5, tmp);
output = var<outType>(b, false);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-3);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
gold = outType(2);
output = var<outType>(b, true);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-3);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
}
TEST(Var, CPP_f64)
{
testCPPVar<double>(2.1, af::dim4(10, 10, 1, 1));
}
TEST(Var, CPP_f32)
{
testCPPVar<float>(2.1f, af::dim4(10, 5, 2, 1));
}
TEST(Var, CPP_s32)
{
testCPPVar<int>(2, af::dim4(5, 5, 2, 2));
}
TEST(Var, CPP_u32)
{
testCPPVar<unsigned>(2, af::dim4(100, 1, 1, 1));
}
TEST(Var, CPP_s8)
{
testCPPVar<char>(2, af::dim4(5, 5, 2, 2));
}
TEST(Var, CPP_u8)
{
testCPPVar<uchar>(2, af::dim4(100, 1, 1, 1));
}
TEST(Var, CPP_cfloat)
{
testCPPVar<cfloat>(cfloat(2.1f), af::dim4(10, 5, 2, 1));
}
TEST(Var, CPP_cdouble)
{
testCPPVar<cdouble>(cdouble(2.1), af::dim4(10, 10, 1, 1));
}