/******************************************************* * 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 #include #include #include #include using namespace std; using namespace af; #define MINMAXOP(fn, ty) \ TEST(IndexedMinMaxTests, Test_##fn##_##ty##_0) \ { \ if (noDoubleTests()) return; \ dtype dty = (dtype)dtype_traits::af_type; \ const int nx = 10000; \ const int ny = 100; \ af::array in = randu(nx, ny, dty); \ af::array val, idx; \ fn(val, idx, in, 0); \ \ ty *h_in = in.host(); \ ty *h_in_st = h_in; \ ty *h_val = val.host(); \ uint *h_idx = idx.host(); \ for (int i = 0; i < ny; i++) { \ ty tmp = *fn##_element(h_in, h_in + nx); \ ASSERT_EQ(tmp, h_val[i]) \ << "for index" << i; \ ASSERT_EQ(h_in[h_idx[i]], tmp) \ << "for index" << i; \ h_in += nx; \ } \ delete[] h_in_st; \ delete[] h_val; \ delete[] h_idx; \ } \ TEST(IndexedMinMaxTests, Test_##fn##_##ty##_1) \ { \ if (noDoubleTests()) return; \ dtype dty = (dtype)dtype_traits::af_type; \ const int nx = 100; \ const int ny = 100; \ af::array in = randu(nx, ny, dty); \ af::array val, idx; \ fn(val, idx, in, 1); \ \ ty *h_in = in.host(); \ ty *h_val = val.host(); \ uint *h_idx = idx.host(); \ for (int i = 0; i < nx; i++) { \ ty val = h_val[i]; \ for (int j= 0; j < ny; j++) { \ ty tmp = fn(val, h_in[j * nx + i]); \ ASSERT_EQ(tmp, val); \ } \ ASSERT_EQ(val, h_in[h_idx[i] * nx + i]); \ } \ delete[] h_in; \ delete[] h_val; \ delete[] h_idx; \ } \ TEST(IndexedMinMaxTests, Test_##fn##_##ty##_all) \ { \ if (noDoubleTests()) return; \ dtype dty = (dtype)dtype_traits::af_type; \ const int num = 100000; \ af::array in = randu(num, dty); \ ty val; \ uint idx; \ fn(&val, &idx, in); \ ty *h_in = in.host(); \ ty tmp = *fn##_element(h_in, h_in + num); \ ASSERT_EQ(tmp, val); \ ASSERT_EQ(tmp, h_in[idx]); \ delete[] h_in; \ } \ MINMAXOP(min, float) MINMAXOP(min, double) MINMAXOP(min, int) MINMAXOP(min, uint) MINMAXOP(min, char) MINMAXOP(min, uchar) MINMAXOP(max, float) MINMAXOP(max, double) MINMAXOP(max, int) MINMAXOP(max, uint) MINMAXOP(max, char) MINMAXOP(max, uchar)