forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.cpp
More file actions
149 lines (117 loc) · 4.34 KB
/
Copy pathhistogram.cpp
File metadata and controls
149 lines (117 loc) · 4.34 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************
* 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;
template<typename T>
class Histogram : public ::testing::Test
{
public:
virtual void SetUp() {}
};
// create a list of types to be tested
typedef ::testing::Types<float, double, int, uint, char, uchar> TestTypes;
// register the type list
TYPED_TEST_CASE(Histogram, TestTypes);
TYPED_TEST(Histogram,InvalidArgs)
{
if (noDoubleTests<TypeParam>()) return;
af::dim4 dims(1);
vector<TypeParam> in(100,1);
af_array inArray = 0;
af_array outArray = 0;
// square test file is 100x100 originally
// use new dimensions for this argument
// unit test
af::dim4 newDims(5,5,2,2);
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(), newDims.ndims(), newDims.get(), (af_dtype) af::dtype_traits<TypeParam>::af_type));
ASSERT_EQ(AF_ERR_SIZE, af_histogram(&outArray,inArray,256,0,255));
}
template<typename inType, typename outType>
void histTest(string pTestFile, unsigned nbins, double minval, double maxval)
{
if (noDoubleTests<inType>()) return;
if (noDoubleTests<outType>()) return;
vector<af::dim4> numDims;
vector<vector<inType>> in;
vector<vector<outType>> tests;
readTests<inType,uint,int>(pTestFile,numDims,in,tests);
af::dim4 dims = numDims[0];
af_array outArray = 0;
af_array inArray = 0;
outType *outData;
ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &(in[0].front()), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<inType>::af_type));
ASSERT_EQ(AF_SUCCESS,af_histogram(&outArray,inArray,nbins,minval,maxval));
outData = new outType[dims.elements()];
ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));
for (size_t testIter=0; testIter<tests.size(); ++testIter) {
vector<outType> 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;
}
}
// cleanup
delete[] outData;
ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray));
ASSERT_EQ(AF_SUCCESS, af_destroy_array(outArray));
}
TYPED_TEST(Histogram,256Bins0min255max_ones)
{
histTest<TypeParam,uint>(string(TEST_DIR"/histogram/256bin1min1max.test"),256,0,255);
}
TYPED_TEST(Histogram,100Bins0min99max)
{
histTest<TypeParam,uint>(string(TEST_DIR"/histogram/100bin0min99max.test"),100,0,99);
}
TYPED_TEST(Histogram,40Bins0min100max)
{
histTest<TypeParam,uint>(string(TEST_DIR"/histogram/40bin0min100max.test"),40,0,100);
}
TYPED_TEST(Histogram,40Bins0min100max_Batch)
{
histTest<TypeParam,uint>(string(TEST_DIR"/histogram/40bin0min100max_batch.test"),40,0,100);
}
TYPED_TEST(Histogram,256Bins0min255max_zeros)
{
histTest<TypeParam,uint>(string(TEST_DIR"/histogram/256bin0min0max.test"),256,0,255);
}
/////////////////////////////////// CPP //////////////////////////////////
//
TEST(Histogram, CPP)
{
if (noDoubleTests<float>()) return;
if (noDoubleTests<int>()) return;
const unsigned nbins = 100;
const double minval = 0.0;
const double maxval = 99.0;
vector<af::dim4> numDims;
vector<vector<float>> in;
vector<vector<uint>> tests;
readTests<float,uint,int>(string(TEST_DIR"/histogram/100bin0min99max.test"),numDims,in,tests);
af::array input(numDims[0], &(in[0].front()));
af::array output = histogram(input, nbins, minval, maxval);
uint *outData = new uint[output.elements()];
output.host((void*)outData);
for (size_t testIter=0; testIter<tests.size(); ++testIter) {
vector<uint> 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;
}
}
// cleanup
delete[] outData;
}