|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <arrayfire.h> |
| 3 | +#include <af/dim4.hpp> |
| 4 | +#include <af/traits.hpp> |
| 5 | +#include <string> |
| 6 | +#include <vector> |
| 7 | +#include <testHelpers.hpp> |
| 8 | + |
| 9 | +using std::string; |
| 10 | +using std::vector; |
| 11 | + |
| 12 | +template<typename T> |
| 13 | +class Histogram : public ::testing::Test |
| 14 | +{ |
| 15 | + public: |
| 16 | + virtual void SetUp() {} |
| 17 | +}; |
| 18 | + |
| 19 | +// create a list of types to be tested |
| 20 | +typedef ::testing::Types<float, double, int, uint, char, uchar> TestTypes; |
| 21 | + |
| 22 | +// register the type list |
| 23 | +TYPED_TEST_CASE(Histogram, TestTypes); |
| 24 | + |
| 25 | +TYPED_TEST(Histogram,InvalidArgs) |
| 26 | +{ |
| 27 | + af::dim4 dims(1); |
| 28 | + vector<TypeParam> in(100,1); |
| 29 | + |
| 30 | + af_array inArray = 0; |
| 31 | + af_array outArray = 0; |
| 32 | + |
| 33 | + // square test file is 100x100 originally |
| 34 | + // usee new dimensions for this argument |
| 35 | + // unit test |
| 36 | + af::dim4 newDims(5,5,2,2); |
| 37 | + ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(), newDims.ndims(), newDims.get(), (af_dtype) af::dtype_traits<TypeParam>::af_type)); |
| 38 | + |
| 39 | + ASSERT_EQ(AF_ERR_ARG, af_histogram(&outArray,inArray,256,0,255)); |
| 40 | +} |
| 41 | + |
| 42 | +template<typename inType, typename outType> |
| 43 | +void histTest(string pTestFile, unsigned nbins, double minval, double maxval) |
| 44 | +{ |
| 45 | + af::dim4 dims(1); |
| 46 | + |
| 47 | + vector<inType> in; |
| 48 | + vector<vector<outType>> tests; |
| 49 | + ReadTests2<inType,uint,int>(pTestFile,dims,in,tests); |
| 50 | + |
| 51 | + af_array outArray = 0; |
| 52 | + af_array inArray = 0; |
| 53 | + outType *outData; |
| 54 | + ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<inType>::af_type)); |
| 55 | + |
| 56 | + ASSERT_EQ(AF_SUCCESS,af_histogram(&outArray,inArray,nbins,minval,maxval)); |
| 57 | + |
| 58 | + outData = new outType[dims.elements()]; |
| 59 | + |
| 60 | + ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray)); |
| 61 | + |
| 62 | + for (size_t testIter=0; testIter<tests.size(); ++testIter) { |
| 63 | + vector<outType> currGoldBar = tests[testIter]; |
| 64 | + size_t nElems = currGoldBar.size(); |
| 65 | + for (size_t elIter=0; elIter<nElems; ++elIter) { |
| 66 | + ASSERT_EQ(currGoldBar[elIter],outData[elIter])<< "at: " << elIter<< std::endl; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // cleanup |
| 71 | + delete[] outData; |
| 72 | + ASSERT_EQ(AF_SUCCESS, af_destroy_array(inArray)); |
| 73 | + ASSERT_EQ(AF_SUCCESS, af_destroy_array(outArray)); |
| 74 | +} |
| 75 | + |
| 76 | +TYPED_TEST(Histogram,256Bins0min255max_ones) |
| 77 | +{ |
| 78 | + histTest<TypeParam,uint>(string(TEST_DIR"/histogram/256bin1min1max.test"),256,0,255); |
| 79 | +} |
| 80 | + |
| 81 | +TYPED_TEST(Histogram,100Bins0min99max) |
| 82 | +{ |
| 83 | + histTest<TypeParam,uint>(string(TEST_DIR"/histogram/100bin0min99max.test"),100,0,99); |
| 84 | +} |
| 85 | + |
| 86 | +TYPED_TEST(Histogram,40Bins0min100max) |
| 87 | +{ |
| 88 | + histTest<TypeParam,uint>(string(TEST_DIR"/histogram/40bin0min100max.test"),40,0,100); |
| 89 | +} |
| 90 | + |
| 91 | +TYPED_TEST(Histogram,40Bins0min100max_Batch) |
| 92 | +{ |
| 93 | + histTest<TypeParam,uint>(string(TEST_DIR"/histogram/40bin0min100max_batch.test"),40,0,100); |
| 94 | +} |
| 95 | + |
| 96 | +TYPED_TEST(Histogram,256Bins0min255max_zeros) |
| 97 | +{ |
| 98 | + histTest<TypeParam,uint>(string(TEST_DIR"/histogram/256bin0min0max.test"),256,0,255); |
| 99 | +} |
0 commit comments