File: var.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 (156 lines) | stat: -rw-r--r-- 4,448 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
/*******************************************************
 * 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;
using af::array;

template<typename T>
class Var : public ::testing::Test
{

};

typedef ::testing::Types< float, double, cfloat, cdouble, uint, int, uintl, intl, char, uchar, short, ushort> TestTypes;
TYPED_TEST_CASE(Var, TestTypes);

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 varOutType {
   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, short >::value ||
                               is_same_type<T, ushort>::value ||
                               is_same_type<T, uchar >::value ||
                               is_same_type<T, char  >::value,
                                              float,
                              typename elseType<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;
    if (noDoubleTests<outType>()) 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);
}

TYPED_TEST(Var, AllCPPSmall)
{
    testCPPVar<TypeParam>(2, af::dim4(10, 10, 1, 1));
}

TYPED_TEST(Var, AllCPPMedium)
{
    testCPPVar<TypeParam>(2, af::dim4(100, 100, 1, 1));
}

TYPED_TEST(Var, AllCPPLarge)
{
    testCPPVar<TypeParam>(2, af::dim4(1000, 1000, 1, 1));
}

TYPED_TEST(Var, DimCPPSmall)
{
    typedef typename varOutType<TypeParam>::type outType;

    if (noDoubleTests<TypeParam>()) return;
    if (noDoubleTests<outType>()) return;

    vector<af::dim4> numDims;
    vector<vector<TypeParam> > in;
    vector<vector<outType> > tests;

    readTests<TypeParam, outType, double> (TEST_DIR"/var/var.data",numDims,in,tests);

    for(size_t i = 0; i < in.size(); i++)
    {
        array input(numDims[i], &in[i].front(), afHost);

        array bout  = var(input, false);
        array nbout = var(input, true);

        array bout1  = var(input, false, 1);
        array nbout1 = var(input, true,  1);

        vector<vector<outType> > h_out(4);

        h_out[0].resize(bout.elements());
        h_out[1].resize(nbout.elements());
        h_out[2].resize(bout1.elements());
        h_out[3].resize(nbout1.elements());

        bout.host(  &h_out[0].front());
        nbout.host( &h_out[1].front());
        bout1.host( &h_out[2].front());
        nbout1.host(&h_out[3].front());

        for(size_t j = 0; j < tests.size(); j++) {
            for(size_t jj = 0; jj < tests[j].size(); jj++) {
                ASSERT_EQ(h_out[j][jj], tests[j][jj]);
            }
        }
    }
}