forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregions.cpp
More file actions
130 lines (105 loc) · 4.15 KB
/
Copy pathregions.cpp
File metadata and controls
130 lines (105 loc) · 4.15 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
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************
* 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/defines.h>
#include <af/traits.hpp>
#include <af/image.h>
#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 Regions : public ::testing::Test
{
public:
virtual void SetUp() {}
};
// create a list of types to be tested
typedef ::testing::Types<float, double, int, unsigned> TestTypes;
// register the type list
TYPED_TEST_CASE(Regions, TestTypes);
template<typename T>
void regionsTest(string pTestFile, af_connectivity connectivity, bool isSubRef = false, const vector<af_seq> * seqv = nullptr)
{
if (noDoubleTests<T>()) return;
vector<af::dim4> numDims;
vector<vector<uchar>> in;
vector<vector<T>> tests;
readTests<uchar, T, unsigned>(pTestFile,numDims,in,tests);
af::dim4 idims = numDims[0];
af_array inArray = 0;
af_array tempArray = 0;
af_array outArray = 0;
if (isSubRef) {
ASSERT_EQ(AF_SUCCESS, af_create_array(&tempArray, &(in[0].front()), idims.ndims(), idims.get(), (af_dtype) af::dtype_traits<uchar>::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()), idims.ndims(), idims.get(), (af_dtype) af::dtype_traits<uchar>::af_type));
}
ASSERT_EQ(AF_SUCCESS, af_regions(&outArray, inArray, connectivity, (af_dtype) af::dtype_traits<T>::af_type));
// Get result
T* outData = new T[idims.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_destroy_array(inArray);
if(outArray != 0) af_destroy_array(outArray);
if(tempArray != 0) af_destroy_array(tempArray);
}
#define REGIONS_INIT(desc, file, conn, conn_type) \
TYPED_TEST(Regions, desc) \
{ \
regionsTest<TypeParam>(string(TEST_DIR"/regions/"#file"_"#conn".test"), conn_type); \
}
REGIONS_INIT(Regions0, regions_8x8, 4, AF_CONNECTIVITY_4);
REGIONS_INIT(Regions1, regions_8x8, 8, AF_CONNECTIVITY_8);
REGIONS_INIT(Regions2, regions_128x128, 4, AF_CONNECTIVITY_4);
REGIONS_INIT(Regions3, regions_128x128, 8, AF_CONNECTIVITY_8);
///////////////////////////////////// CPP ////////////////////////////////
//
TEST(Regions, CPP)
{
if (noDoubleTests<float>()) return;
vector<af::dim4> numDims;
vector<vector<uchar>> in;
vector<vector<float>> tests;
readTests<uchar, float, unsigned>(string(TEST_DIR"/regions/regions_8x8_4.test"),numDims,in,tests);
af::dim4 idims = numDims[0];
af::array input(idims, (float*)&(in[0].front()));
af::array output = af::regions(input);
// Get result
float* outData = new float[idims.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;
}