File: stdev.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (207 lines) | stat: -rw-r--r-- 6,179 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
/*******************************************************
 * Copyright (c) 2015, 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 <ctime>
#include <iostream>
#include <algorithm>
#include <testHelpers.hpp>

using namespace std;
using namespace af;

template<typename T>
class StandardDev : public ::testing::Test
{
    public:
        virtual void SetUp() {}
};

// create a list of types to be tested
typedef ::testing::Types<float, double, int, uint, intl, uintl, char, uchar> TestTypes;

// register the type list
TYPED_TEST_CASE(StandardDev, TestTypes);

template<typename T>
struct f32HelperType {
   typedef typename cond_type<is_same_type<T, double>::value,
                                             double,
                                             float>::type type;
};

template<typename T>
struct c32HelperType {
   typedef typename cond_type<is_same_type<T, cfloat>::value,
                                             cfloat,
                                             typename f32HelperType<T>::type >::type type;
};

template<typename T>
struct elseType {
   typedef typename cond_type< is_same_type<T, uintl>::value ||
                               is_same_type<T, intl> ::value,
                                              double,
                                              T>::type type;
};

template<typename T>
struct sdOutType {
   typedef typename cond_type< is_same_type<T, float>   ::value ||
                               is_same_type<T, int>     ::value ||
                               is_same_type<T, uint>    ::value ||
                               is_same_type<T, uchar>   ::value ||
                               is_same_type<T, short>   ::value ||
                               is_same_type<T, ushort>  ::value ||
                               is_same_type<T, char>    ::value,
                                              float,
                              typename elseType<T>::type>::type type;
};

template<typename T>
void stdevDimTest(string pFileName, dim_t dim=-1)
{
    typedef typename sdOutType<T>::type outType;
    if (noDoubleTests<T>()) return;
    if (noDoubleTests<outType>()) return;

    vector<af::dim4>      numDims;
    vector<vector<int> >       in;
    vector<vector<float> >  tests;

    readTestsFromFile<int,float>(pFileName, numDims, in, tests);

    af::dim4 dims = numDims[0];
    vector<T> input(in[0].begin(), in[0].end());

    af::array a(dims, &(input.front()));

    af::array b = stdev(a, dim);

    vector<outType> currGoldBar(tests[0].begin(), tests[0].end());

    size_t nElems    = currGoldBar.size();
    outType *outData = new outType[nElems];

    b.host((void*)outData);

    for (size_t elIter=0; elIter<nElems; ++elIter) {
        ASSERT_NEAR(::real(currGoldBar[elIter]), ::real(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
        ASSERT_NEAR(::imag(currGoldBar[elIter]), ::imag(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
    }

    // cleanup
    delete[] outData;
}

TYPED_TEST(StandardDev, Dim0)
{
    stdevDimTest<TypeParam>(string(TEST_DIR "/stdev/mat_10x10_dim0.test"), 0);
}

TYPED_TEST(StandardDev, Dim1)
{
    stdevDimTest<TypeParam>(string(TEST_DIR "/stdev/mat_10x10_dim1.test"), 1);
}

TYPED_TEST(StandardDev, Dim2)
{
    stdevDimTest<TypeParam>(string(TEST_DIR "/stdev/hypercube_10x10x5x5_dim2.test"), 2);
}

TYPED_TEST(StandardDev, Dim3)
{
    stdevDimTest<TypeParam>(string(TEST_DIR "/stdev/hypercube_10x10x5x5_dim3.test"), 3);
}

TEST(StandardDev, InvalidDim)
{
    ASSERT_THROW(af::stdev(af::array(), 5), af::exception);
}

TEST(StandardDev, InvalidType)
{
    ASSERT_THROW(af::stdev(constant(cdouble(1.0, -1.0), 10)), af::exception);
}

template<typename T>
void stdevDimIndexTest(string pFileName, dim_t dim=-1)
{
    typedef typename sdOutType<T>::type outType;
    if (noDoubleTests<T>()) return;
    if (noDoubleTests<outType>()) return;

    vector<af::dim4>      numDims;
    vector<vector<int> >       in;
    vector<vector<float> >  tests;

    readTestsFromFile<int,float>(pFileName, numDims, in, tests);

    af::dim4 dims = numDims[0];
    vector<T> input(in[0].begin(), in[0].end());

    af::array a(dims, &(input.front()));
    af::array b = a(seq(2,6), seq(1,7));

    af::array c = stdev(b, dim);

    vector<outType> currGoldBar(tests[0].begin(), tests[0].end());

    size_t nElems    = currGoldBar.size();
    outType *outData = new outType[nElems];

    c.host((void*)outData);

    for (size_t elIter=0; elIter<nElems; ++elIter) {
        ASSERT_NEAR(::real(currGoldBar[elIter]), ::real(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
        ASSERT_NEAR(::imag(currGoldBar[elIter]), ::imag(outData[elIter]), 1.0e-3)<< "at: " << elIter<< std::endl;
    }

    // cleanup
    delete[] outData;
}

TYPED_TEST(StandardDev, IndexedArrayDim0)
{
    stdevDimIndexTest<TypeParam>(string(TEST_DIR "/stdev/mat_10x10_seq2_6x1_7_dim0.test"), 0);
}

TYPED_TEST(StandardDev, IndexedArrayDim1)
{
    stdevDimIndexTest<TypeParam>(string(TEST_DIR "/stdev/mat_10x10_seq2_6x1_7_dim1.test"), 1);
}

TYPED_TEST(StandardDev, All)
{
    typedef typename sdOutType<TypeParam>::type outType;
    if (noDoubleTests<TypeParam>()) return;
    if (noDoubleTests<outType>()) return;

    vector<af::dim4>      numDims;
    vector<vector<int> >       in;
    vector<vector<float> >  tests;

    readTestsFromFile<int,float>(string(TEST_DIR "/stdev/mat_10x10_scalar.test"),
                                 numDims, in, tests);

    af::dim4 dims = numDims[0];
    vector<TypeParam> input(in[0].begin(), in[0].end());

    af::array a(dims, &(input.front()));
    outType b = stdev<outType>(a);

    vector<outType> currGoldBar(tests[0].begin(), tests[0].end());
    ASSERT_NEAR(::real(currGoldBar[0]), ::real(b), 1.0e-3);
    ASSERT_NEAR(::imag(currGoldBar[0]), ::imag(b), 1.0e-3);
}