TEST(MatrixManipulation, SNIPPET_matrix_manipulation_mesh) { //! [ex_matrix_manipulation_mesh] float hx[] = {1, 2, 3, 4}; float hy[] = {5, 6}; array x = array(4, hx); array y = array(2, hy); af_print(tile(x, 1, 2)); af_print(tile(y.T(), 4, 1)); //! [ex_matrix_manipulation_mesh] array outx = tile(x, 1, 2); array outy = tile(y.T(), 4, 1); ASSERT_EQ(4, outx.dims(0)); ASSERT_EQ(4, outy.dims(0)); ASSERT_EQ(2, outx.dims(1)); ASSERT_EQ(2, outy.dims(1)); vector<float> houtx(outx.elements()); outx.host(&houtx.front()); vector<float> houty(outy.elements()); outy.host(&houty.front()); for (unsigned i = 0; i < houtx.size(); i++) ASSERT_EQ(hx[i % 4], houtx[i]) << "At [" << i << "]"; for (unsigned i = 0; i < houty.size(); i++) ASSERT_EQ(hy[i > 3], houty[i]) << "At [" << i << "]"; }
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_join) { //! [ex_matrix_manipulation_join] float hA[] = {1, 2, 3, 4, 5, 6}; float hB[] = {10, 20, 30, 40, 50, 60, 70, 80, 90}; array A = array(3, 2, hA); array B = array(3, 3, hB); af_print(join(1, A, B)); // 3x5 matrix // array result = join(0, A, B); // fail: dimension mismatch //! [ex_matrix_manipulation_join] array out = join(1, A, B); vector<float> h_out(out.elements()); out.host(&h_out.front()); af_print(out); ASSERT_EQ(3, out.dims(0)); ASSERT_EQ(5, out.dims(1)); unsigned fdim = out.dims(0); unsigned sdim = out.dims(1); for (unsigned i = 0; i < sdim; i++) { for (unsigned j = 0; j < fdim; j++) { if (i < 2) { ASSERT_FLOAT_EQ(hA[i * fdim + j], h_out[i * fdim + j]) << "At [" << i << ", " << j << "]"; } else { ASSERT_FLOAT_EQ(hB[(i - 2) * fdim + j], h_out[i * fdim + j]) << "At [" << i << ", " << j << "]"; } } } }
///////////////////////////////////// CPP //////////////////////////////// // TEST(Transform, CPP) { if (noImageIOTests()) return; vector<dim4> inDims; vector<string> inFiles; vector<dim_t> goldDim; vector<string> goldFiles; vector<dim4> HDims; vector<vector<float> > HIn; vector<vector<float> > HTests; readTests<float, float, float>(TEST_DIR"/transform/tux_tmat.test",HDims,HIn,HTests); readImageTests(string(TEST_DIR"/transform/tux_nearest.test"), inDims, inFiles, goldDim, goldFiles); inFiles[0].insert(0,string(TEST_DIR"/transform/")); inFiles[1].insert(0,string(TEST_DIR"/transform/")); goldFiles[0].insert(0,string(TEST_DIR"/transform/")); array H = array(HDims[0][0], HDims[0][1], &(HIn[0].front())); array IH = array(HDims[0][0], HDims[0][1], &(HIn[0].front())); array scene_img = loadImage(inFiles[1].c_str(), false); array gold_img = loadImage(goldFiles[0].c_str(), false); array out_img = transform(scene_img, IH, inDims[0][0], inDims[0][1], AF_INTERP_NEAREST, false); dim4 outDims = out_img.dims(); dim4 goldDims = gold_img.dims(); vector<float> h_out_img(outDims[0] * outDims[1]); out_img.host(&h_out_img.front()); vector<float> h_gold_img(goldDims[0] * goldDims[1]); gold_img.host(&h_gold_img.front()); const dim_t n = gold_img.elements(); const float thr = 1.0f; // Maximum number of wrong pixels must be <= 0.01% of number of elements, // this metric is necessary due to rounding errors between different // backends for AF_INTERP_NEAREST and AF_INTERP_LOWER const size_t maxErr = n * 0.0001f; size_t err = 0; for (dim_t elIter = 0; elIter < n; elIter++) { err += fabs((int)h_out_img[elIter] - h_gold_img[elIter]) > thr; if (err > maxErr) { ASSERT_LE(err, maxErr) << "at: " << elIter << endl; } } }
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_tile) { //! [ex_matrix_manipulation_tile] float h[] = {1, 2, 3, 4}; array small_arr = array(2, 2, h); // 2x2 matrix af_print(small_arr); array large_arr = tile(small_arr, 2, 3); // produces 4x6 matrix: (2*2)x(2*3) af_print(large_arr); //! [ex_matrix_manipulation_tile] ASSERT_EQ(4, large_arr.dims(0)); ASSERT_EQ(6, large_arr.dims(1)); vector<float> h_large_arr(large_arr.elements()); large_arr.host(&h_large_arr.front()); unsigned fdim = large_arr.dims(0); unsigned sdim = large_arr.dims(1); for (unsigned i = 0; i < sdim; i++) { for (unsigned j = 0; j < fdim; j++) { ASSERT_FLOAT_EQ(h[(i % 2) * 2 + (j % 2)], h_large_arr[i * fdim + j]); } } }
TYPED_TEST(Reduce, Test_Any_Global) { if (noDoubleTests<TypeParam>()) return; // Input size test for(int i = 1; i < 1000; i+=100) { int num = 10 * i; vector<TypeParam> h_vals(num, (TypeParam)false); array a(2, num/2, &h_vals.front()); TypeParam res = af::anyTrue<TypeParam>(a); typed_assert_eq((TypeParam)false, res, false); h_vals[3] = true; a = array(2, num/2, &h_vals.front()); res = af::anyTrue<TypeParam>(a); typed_assert_eq((TypeParam)true, res, false); } // true value location test int num = 10000; vector<TypeParam> h_vals(num, (TypeParam)false); for(int i = 1; i < 10000; i+=100) { h_vals[i] = true; array a(2, num/2, &h_vals.front()); TypeParam res = af::anyTrue<TypeParam>(a); typed_assert_eq((TypeParam)true, res, false); h_vals[i] = false; } }
TEST(MatrixManipulation, SNIPPET_matrix_manipulation_moddims) { //! [ex_matrix_manipulation_moddims] int hA[] = {1, 2, 3, 4, 5, 6}; array A = array(3, 2, hA); af_print(A); // 2x3 matrix af_print(moddims(A, 2, 3)); // 2x3 matrix af_print(moddims(A, 6, 1)); // 6x1 column vector // moddims(A, 2, 2); // fail: wrong number of elements // moddims(A, 8, 8); // fail: wrong number of elements //! [ex_matrix_manipulation_moddims] }
TEST(Regions, NoComponentImage) { const int dim = 101; const int sz = dim*dim; vector<char> input(sz, 0); vector<float> gold(sz, 0.0f); array in = array(dim, dim, input.data()); array out = regions(in, AF_CONNECTIVITY_4); vector<float> output(sz); out.host((void*)output.data()); for (int i=0; i<sz; ++i) ASSERT_FLOAT_EQ(gold[i], output[i])<<" mismatch at i="<<i<<endl; }
TEST(StandardDev, InvalidDim) { ASSERT_THROW(stdev(array(), 5), exception); }