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
|
/*******************************************************
* 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 <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>
using std::vector;
using std::string;
using std::cout;
using std::endl;
using af::cfloat;
using af::cdouble;
template<typename T>
class Diff1 : public ::testing::Test
{
public:
virtual void SetUp() {
subMat0.push_back(af_make_seq(1, 4, 1));
subMat0.push_back(af_make_seq(0, 2, 1));
subMat0.push_back(af_make_seq(0, 1, 1));
subMat1.push_back(af_make_seq(0, 4, 1));
subMat1.push_back(af_make_seq(1, 3, 1));
subMat1.push_back(af_make_seq(1, 3, 1));
subMat2.push_back(af_make_seq(1, 5, 1));
subMat2.push_back(af_make_seq(0, 3, 1));
subMat2.push_back(af_make_seq(0, 2, 1));
}
vector<af_seq> subMat0;
vector<af_seq> subMat1;
vector<af_seq> subMat2;
};
// create a list of types to be tested
typedef ::testing::Types<float, cfloat, double, cdouble, int, unsigned, intl, uintl, char, unsigned char, short, ushort> TestTypes;
// register the type list
TYPED_TEST_CASE(Diff1, TestTypes);
template<typename T>
void diff1Test(string pTestFile, unsigned dim, bool isSubRef=false, const vector<af_seq> *seqv=NULL)
{
if (noDoubleTests<T>()) return;
vector<af::dim4> numDims;
vector<vector<T> > in;
vector<vector<T> > tests;
readTests<T,T,int>(pTestFile,numDims,in,tests);
af::dim4 dims = numDims[0];
T *outData;
af_array inArray = 0;
af_array outArray = 0;
af_array tempArray = 0;
// Get input array
if (isSubRef) {
ASSERT_EQ(AF_SUCCESS, af_create_array(&tempArray, &(in[0].front()), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<T>::af_type));
ASSERT_EQ(AF_SUCCESS, af_index(&inArray, tempArray, seqv->size(), &seqv->front()));
} else {
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<T>::af_type));
}
// Run diff1
ASSERT_EQ(AF_SUCCESS, af_diff1(&outArray, inArray, dim));
// Get result
outData = new T[dims.elements()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
// Compare result
for (size_t testIter = 0; testIter < tests.size(); ++testIter) {
vector<T> currGoldBar = tests[testIter];
size_t nElems = currGoldBar.size();
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter << std::endl;
}
}
// Delete
delete[] outData;
if(inArray != 0) af_release_array(inArray);
if(outArray != 0) af_release_array(outArray);
if(tempArray != 0) af_release_array(tempArray);
}
TYPED_TEST(Diff1,Vector0)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/vector0.test"), 0);
}
TYPED_TEST(Diff1,Matrix0)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/matrix0.test"), 0);
}
TYPED_TEST(Diff1,Matrix1)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/matrix1.test"), 1);
}
// Diff on 0 dimension
TYPED_TEST(Diff1,Basic0)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/basic0.test"), 0);
}
// Diff on 1 dimension
TYPED_TEST(Diff1,Basic1)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/basic1.test"), 1);
}
// Diff on 2 dimension
TYPED_TEST(Diff1,Basic2)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/basic2.test"), 2);
}
// Diff on 0 dimension subref
TYPED_TEST(Diff1,Subref0)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/subref0.test"), 0,true,&(this->subMat0));
}
// Diff on 1 dimension subref
TYPED_TEST(Diff1,Subref1)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/subref1.test"), 1,true,&(this->subMat1));
}
// Diff on 2 dimension subref
TYPED_TEST(Diff1,Subref2)
{
diff1Test<TypeParam>(string(TEST_DIR"/diff1/subref2.test"), 2,true,&(this->subMat2));
}
template<typename T>
void diff1ArgsTest(string pTestFile)
{
if (noDoubleTests<T>()) return;
vector<af::dim4> numDims;
vector<vector<T> > in;
vector<vector<T> > tests;
readTests<T,T,int>(pTestFile,numDims,in,tests);
af::dim4 dims = numDims[0];
af_array inArray = 0;
af_array outArray = 0;
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<T>::af_type));
ASSERT_EQ(AF_ERR_ARG, af_diff1(&outArray, inArray, -1));
ASSERT_EQ(AF_ERR_ARG, af_diff1(&outArray, inArray, 5));
if(inArray != 0) af_release_array(inArray);
if(outArray != 0) af_release_array(outArray);
}
TYPED_TEST(Diff1,InvalidArgs)
{
diff1ArgsTest<TypeParam>(string(TEST_DIR"/diff1/basic0.test"));
}
////////////////////////////////////// CPP ////////////////////////////////////
//
TEST(Diff1, CPP)
{
if (noDoubleTests<float>()) return;
const unsigned dim = 0;
vector<af::dim4> numDims;
vector<vector<float> > in;
vector<vector<float> > tests;
readTests<float,float,int>(string(TEST_DIR"/diff1/matrix0.test"),numDims,in,tests);
af::dim4 dims = numDims[0];
af::array input(dims, &(in[0].front()));
af::array output = af::diff1(input, dim);
// Get result
float *outData = new float[dims.elements()];
output.host((void*)outData);
// Compare result
for (size_t testIter = 0; testIter < tests.size(); ++testIter) {
vector<float> currGoldBar = tests[testIter];
size_t nElems = currGoldBar.size();
for (size_t elIter = 0; elIter < nElems; ++elIter) {
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter << std::endl;
}
}
// Delete
delete[] outData;
}
|