Skip to content

Commit 5146d67

Browse files
author
Pradeep
committed
cpp wrapper unit tests
approx1, approx2, diff1, diff2, loadImage, info, randu, randn, sum, min, max, alltrue, anytrue, accum, regions, reoder, shift, tile, where, sort_index, sort_by_key, sort, rotate, resize, and regions.
1 parent a6cb86f commit 5146d67

19 files changed

Lines changed: 653 additions & 2 deletions

test/approx1.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,41 @@ void approx1ArgsTestPrecision(string pTestFile, const unsigned resultIdx, const
179179

180180
APPROX1_ARGSP(Approx1NearestArgsPrecision, approx1, 0, AF_INTERP_NEAREST);
181181
APPROX1_ARGSP(Approx1LinearArgsPrecision, approx1, 1, AF_INTERP_LINEAR);
182+
183+
184+
//////////////////////////////////////// CPP //////////////////////////////////
185+
//
186+
TEST(Approx1, CPP)
187+
{
188+
const unsigned resultIdx = 1;
189+
const af_interp_type method = AF_INTERP_LINEAR;
190+
191+
typedef typename af::dtype_traits<float>::base_type BT;
192+
vector<af::dim4> numDims;
193+
vector<vector<BT>> in;
194+
vector<vector<float>> tests;
195+
readTests<BT, float, float>(string(TEST_DIR"/approx/approx1.test"),numDims,in,tests);
196+
197+
af::dim4 idims = numDims[0];
198+
af::dim4 pdims = numDims[1];
199+
200+
af::array input(idims, &(in[0].front()));
201+
af::array pos(pdims, &(in[1].front()));
202+
203+
af::array output = approx1(input, pos, method, 0);
204+
205+
// Get result
206+
float* outData = new float[tests[resultIdx].size()];
207+
output.host((void*)outData);
208+
209+
// Compare result
210+
size_t nElems = tests[resultIdx].size();
211+
bool ret = true;
212+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
213+
ret = (std::abs(tests[resultIdx][elIter] - outData[elIter]) < 0.0001);
214+
ASSERT_EQ(true, ret) << tests[resultIdx][elIter] << "\t" << outData[elIter] << "at: " << elIter << std::endl;
215+
}
216+
217+
// Delete
218+
delete[] outData;
219+
}

test/approx2.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,41 @@ void approx2ArgsTestPrecision(string pTestFile, const unsigned resultIdx, const
195195

196196
APPROX2_ARGSP(Approx2NearestArgsPrecision, approx2, 0, AF_INTERP_NEAREST);
197197
APPROX2_ARGSP(Approx2LinearArgsPrecision, approx2, 1, AF_INTERP_LINEAR);
198+
199+
200+
//////////////////////////////////// CPP ////////////////////////////////////
201+
//
202+
TEST(Approx2, CPP)
203+
{
204+
const unsigned resultIdx = 1;
205+
206+
typedef typename af::dtype_traits<float>::base_type BT;
207+
vector<af::dim4> numDims;
208+
vector<vector<BT>> in;
209+
vector<vector<float>> tests;
210+
readTests<BT, float, float>(string(TEST_DIR"/approx/approx2.test"),numDims,in,tests);
211+
212+
af::dim4 idims = numDims[0];
213+
af::dim4 pdims = numDims[1];
214+
af::dim4 qdims = numDims[2];
215+
216+
af::array input(idims,&(in[0].front()));
217+
af::array pos0(pdims,&(in[1].front()));
218+
af::array pos1(qdims,&(in[2].front()));
219+
af::array output = af::approx2(input, pos0, pos1, AF_INTERP_LINEAR, 0);
220+
221+
// Get result
222+
float* outData = new float[tests[resultIdx].size()];
223+
output.host((void*)outData);
224+
225+
// Compare result
226+
size_t nElems = tests[resultIdx].size();
227+
bool ret = true;
228+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
229+
ret = (std::abs(tests[resultIdx][elIter] - outData[elIter]) < 0.0001);
230+
ASSERT_EQ(true, ret) << tests[resultIdx][elIter] << "\t" << outData[elIter] << "at: " << elIter << std::endl;
231+
}
232+
233+
// Delete
234+
delete[] outData;
235+
}

test/diff1.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,37 @@ TYPED_TEST(Diff1,InvalidArgs)
168168
{
169169
diff1ArgsTest<TypeParam>(string(TEST_DIR"/diff1/basic0.test"));
170170
}
171+
172+
////////////////////////////////////// CPP ////////////////////////////////////
173+
//
174+
TEST(Diff1, CPP)
175+
{
176+
const unsigned dim = 0;
177+
vector<af::dim4> numDims;
178+
179+
vector<vector<float>> in;
180+
vector<vector<float>> tests;
181+
readTests<float,float,int>(string(TEST_DIR"/diff1/matrix0.test"),numDims,in,tests);
182+
af::dim4 dims = numDims[0];
183+
184+
185+
af::array input(dims, &(in[0].front()));
186+
af::array output = af::diff1(input, dim);
187+
188+
// Get result
189+
float *outData = new float[dims.elements()];
190+
output.host((void*)outData);
191+
192+
// Compare result
193+
for (size_t testIter = 0; testIter < tests.size(); ++testIter) {
194+
vector<float> currGoldBar = tests[testIter];
195+
size_t nElems = currGoldBar.size();
196+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
197+
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter << std::endl;
198+
}
199+
}
200+
201+
// Delete
202+
delete[] outData;
203+
}
204+

test/diff2.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,34 @@ TYPED_TEST(Diff2,InvalidArgs)
165165
{
166166
diff2ArgsTest<TypeParam>(string(TEST_DIR"/diff2/basic0.test"));
167167
}
168+
169+
////////////////////////////////// CPP ////////////////////////////////////////
170+
//
171+
TEST(Diff2, CPP)
172+
{
173+
const unsigned dim = 1;
174+
vector<af::dim4> numDims;
175+
176+
vector<vector<float>> in;
177+
vector<vector<float>> tests;
178+
readTests<float,float,int>(string(TEST_DIR"/diff2/matrix1.test"),numDims,in,tests);
179+
af::dim4 dims = numDims[0];
180+
af::array input(dims, &(in[0].front()));
181+
af::array output = af::diff2(input, dim);
182+
183+
float *outData = new float[dims.elements()];
184+
output.host((void*)outData);
185+
186+
// Compare result
187+
for (size_t testIter = 0; testIter < tests.size(); ++testIter) {
188+
vector<float> currGoldBar = tests[testIter];
189+
size_t nElems = currGoldBar.size();
190+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
191+
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter << std::endl;
192+
}
193+
}
194+
195+
// Delete
196+
delete[] outData;
197+
}
198+

test/imageio.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,30 @@ TYPED_TEST(ImageIO,InvalidArgsWrongExt)
9393
{
9494
loadimageArgsTest(string(TEST_DIR"/imageio/image.wrongext"), true);
9595
}
96+
97+
////////////////////////////////// CPP //////////////////////////////////////
98+
//
99+
TEST(ImageIO, CPP)
100+
{
101+
vector<af::dim4> numDims;
102+
103+
vector<vector<float>> in;
104+
vector<vector<float>> tests;
105+
readTests<float, float, float>(string(TEST_DIR"/imageio/color_small.test"),numDims,in,tests);
106+
107+
af::dim4 dims = numDims[0];
108+
af::array img = af::loadImage(string(TEST_DIR"/imageio/color_small.png").c_str(), true);
109+
110+
// Get result
111+
float *imgData = new float[dims.elements()];
112+
img.host((void*)imgData);
113+
114+
// Compare result
115+
size_t nElems = in[0].size();
116+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
117+
ASSERT_EQ(in[0][elIter], imgData[elIter]) << "at: " << elIter << std::endl;
118+
}
119+
120+
// Delete
121+
delete[] imgData;
122+
}

test/info.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ void infoTest()
3535

3636
for(int d = 0; d < nDevices; d++) {
3737

38-
ASSERT_EQ(AF_SUCCESS, af_set_device(d));
39-
ASSERT_EQ(AF_SUCCESS, af_info());
38+
af::setDevice(d);
39+
af::info();
4040

4141
af_array outArray = 0;
4242
af::dim4 dims(32, 32, 1, 1);

test/random.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,14 @@ TYPED_TEST(Random,InvalidArgs)
133133
{
134134
randuArgsTest<TypeParam>();
135135
}
136+
137+
////////////////////////////////////// CPP /////////////////////////////////////
138+
//
139+
TEST(Random, CPP)
140+
{
141+
// TEST will fail if exception is thrown, which are thrown
142+
// when only wrong inputs are thrown on bad access happens
143+
af::dim4 dims(1, 2, 3, 1);
144+
af::array out1 = af::randu(dims);
145+
af::array out2 = af::randn(dims);
146+
}

test/reduce.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,67 @@ TEST(Reduce,Test_Reduce_Big1)
151151
1
152152
);
153153
}
154+
155+
/////////////////////////////////// CPP //////////////////////////////////
156+
//
157+
typedef af::array (*ReductionOp)(const af::array&, const int);
158+
159+
using af::sum;
160+
using af::min;
161+
using af::max;
162+
using af::alltrue;
163+
using af::anytrue;
164+
using af::count;
165+
166+
template<typename Ti, typename To, ReductionOp reduce>
167+
void cppReduceTest(string pTestFile)
168+
{
169+
vector<af::dim4> numDims;
170+
171+
vector<vector<int>> data;
172+
vector<vector<int>> tests;
173+
readTests<int,int,int> (pTestFile,numDims,data,tests);
174+
af::dim4 dims = numDims[0];
175+
176+
vector<Ti> in(data[0].begin(), data[0].end());
177+
178+
af::array input(dims, &in.front());
179+
180+
// Compare result
181+
for (int d = 0; d < (int)tests.size(); ++d) {
182+
183+
vector<To> currGoldBar(tests[d].begin(), tests[d].end());
184+
185+
// Run sum
186+
af::array output = reduce(input, d);
187+
188+
// Get result
189+
To *outData = new To[dims.elements()];
190+
output.host((void*)outData);
191+
192+
size_t nElems = currGoldBar.size();
193+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
194+
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter
195+
<< " for dim " << d << std::endl;
196+
}
197+
198+
// Delete
199+
delete[] outData;
200+
}
201+
}
202+
203+
204+
#define CPP_REDUCE_TESTS(FN, Ti, To) \
205+
TEST(Reduce, Test_##FN##_CPP) \
206+
{ \
207+
cppReduceTest<Ti, To, FN>( \
208+
string(TEST_DIR"/reduce/"#FN".test") \
209+
); \
210+
}
211+
212+
CPP_REDUCE_TESTS(sum, float, float);
213+
CPP_REDUCE_TESTS(min, float, float);
214+
CPP_REDUCE_TESTS(max, float, float);
215+
CPP_REDUCE_TESTS(anytrue, float, unsigned char);
216+
CPP_REDUCE_TESTS(alltrue, float, unsigned char);
217+
CPP_REDUCE_TESTS(count, float, unsigned);

test/regions.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,34 @@ void regionsTest(string pTestFile, af_connectivity_type connectivity, bool isSub
8484
REGIONS_INIT(Regions1, regions_8x8, 8, AF_CONNECTIVITY_8);
8585
REGIONS_INIT(Regions2, regions_128x128, 4, AF_CONNECTIVITY_4);
8686
REGIONS_INIT(Regions3, regions_128x128, 8, AF_CONNECTIVITY_8);
87+
88+
89+
///////////////////////////////////// CPP ////////////////////////////////
90+
//
91+
TEST(Regions, CPP)
92+
{
93+
vector<af::dim4> numDims;
94+
vector<vector<uchar>> in;
95+
vector<vector<float>> tests;
96+
readTests<uchar, float, unsigned>(string(TEST_DIR"/regions/regions_8x8_4.test"),numDims,in,tests);
97+
98+
af::dim4 idims = numDims[0];
99+
af::array input(idims, (float*)&(in[0].front()));
100+
af::array output = af::regions(input);
101+
102+
// Get result
103+
float* outData = new float[idims.elements()];
104+
output.host((void*)outData);
105+
106+
// Compare result
107+
for (size_t testIter = 0; testIter < tests.size(); ++testIter) {
108+
vector<float> currGoldBar = tests[testIter];
109+
size_t nElems = currGoldBar.size();
110+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
111+
ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter << std::endl;
112+
}
113+
}
114+
115+
// Delete
116+
delete[] outData;
117+
}

test/reorder.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,38 @@ void reorderTest(string pTestFile, const unsigned resultIdx,
118118
REORDER_INIT(Reorder3201, reorder4d,21, 3, 2, 0, 1);
119119
REORDER_INIT(Reorder3012, reorder4d,22, 3, 0, 1, 2);
120120
REORDER_INIT(Reorder3021, reorder4d,23, 3, 0, 2, 1);
121+
122+
////////////////////////////////// CPP ///////////////////////////////////
123+
//
124+
TEST(Reorder, CPP)
125+
{
126+
const unsigned resultIdx = 0;
127+
const unsigned x = 0;
128+
const unsigned y = 1;
129+
const unsigned z = 2;
130+
const unsigned w = 3;
131+
132+
vector<af::dim4> numDims;
133+
vector<vector<float>> in;
134+
vector<vector<float>> tests;
135+
readTests<float, float, int>(string(TEST_DIR"/reorder/reorder4d.test"),numDims,in,tests);
136+
137+
af::dim4 idims = numDims[0];
138+
139+
af::array input(idims, &(in[0].front()));
140+
af::array output = af::reorder(input, x, y, z, w);
141+
142+
// Get result
143+
float* outData = new float[tests[resultIdx].size()];
144+
output.host((void*)outData);
145+
146+
// Compare result
147+
size_t nElems = tests[resultIdx].size();
148+
for (size_t elIter = 0; elIter < nElems; ++elIter) {
149+
ASSERT_EQ(tests[resultIdx][elIter], outData[elIter]) << "at: " << elIter << std::endl;
150+
}
151+
152+
// Delete
153+
delete[] outData;
154+
}
155+

0 commit comments

Comments
 (0)