[arrayfire] 216/408: TEST: Adding tests for select and replace

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Sep 21 19:12:01 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/sid
in repository arrayfire.

commit 04728842b5df3e29ab0eda3530614dab1a71a755
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Sat Aug 8 22:50:41 2015 -0400

    TEST: Adding tests for select and replace
---
 test/replace.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++
 test/select.cpp  | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 248 insertions(+)

diff --git a/test/replace.cpp b/test/replace.cpp
new file mode 100644
index 0000000..34316b3
--- /dev/null
+++ b/test/replace.cpp
@@ -0,0 +1,120 @@
+/*******************************************************
+ * Copyright (c) 2015, 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 <vector>
+#include <iostream>
+#include <string>
+#include <testHelpers.hpp>
+
+using std::vector;
+using namespace af;
+
+template<typename T>
+class Replace : public ::testing::Test
+{
+};
+
+typedef ::testing::Types<float, double, af::cfloat, af::cdouble, uint, int, intl, uintl, uchar, char> TestTypes;
+
+TYPED_TEST_CASE(Replace, TestTypes);
+
+template<typename T>
+void replaceTest(const dim4 &dims)
+{
+    if (noDoubleTests<T>()) return;
+    af::dtype ty = (af::dtype)af::dtype_traits<T>::af_type;
+
+    array a = randu(dims, ty);
+    array c = a.copy();
+    array cond = randu(dims, ty) > constant(0.3, dims, ty);
+    array b = randu(dims, ty);
+
+    replace(c, cond, b);
+
+    int num = (int)a.elements();
+
+    std::vector<T> ha(num);
+    std::vector<T> hb(num);
+    std::vector<T> hc(num);
+    std::vector<char> hcond(num);
+
+    a.host(&ha[0]);
+    b.host(&hb[0]);
+    c.host(&hc[0]);
+    cond.host(&hcond[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(hc[i], hcond[i] ? ha[i] : hb[i]);
+    }
+}
+
+template<typename T>
+void replaceScalarTest(const dim4 &dims)
+{
+    if (noDoubleTests<T>()) return;
+    af::dtype ty = (af::dtype)af::dtype_traits<T>::af_type;
+
+    array a = randu(dims, ty);
+    array c = a.copy();
+    array cond = randu(dims, ty) > constant(0.3, dims, ty);
+    double b = 3;
+
+    replace(c, cond, b);
+    int num = (int)a.elements();
+
+    std::vector<T> ha(num);
+    std::vector<T> hc(num);
+    std::vector<char> hcond(num);
+
+    a.host(&ha[0]);
+    c.host(&hc[0]);
+    cond.host(&hcond[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(hc[i], hcond[i] ? b : ha[i]);
+    }
+}
+
+TYPED_TEST(Replace, Simple)
+{
+    replaceTest<TypeParam>(dim4(1024, 1024));
+}
+
+TYPED_TEST(Replace, Scalar)
+{
+    replaceScalarTest<TypeParam>(dim4(5, 5));
+}
+
+TEST(Replace, NaN)
+{
+    dim4 dims(1000, 1250);
+    af::dtype ty = f32;
+
+    array a = randu(dims, ty);
+    a(seq(a.dims(0) / 2), span, span, span) = af::NaN;
+    array c = a.copy();
+    double b = 0;
+    replace(c, isNaN(c), b);
+
+    int num = (int)a.elements();
+
+    std::vector<float> ha(num);
+    std::vector<float> hc(num);
+
+    a.host(&ha[0]);
+    c.host(&hc[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(hc[i], std::isnan(ha[i]) ? b : ha[i]);
+    }
+}
diff --git a/test/select.cpp b/test/select.cpp
new file mode 100644
index 0000000..bc3e1f0
--- /dev/null
+++ b/test/select.cpp
@@ -0,0 +1,128 @@
+/*******************************************************
+ * Copyright (c) 2015, 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 <vector>
+#include <iostream>
+#include <string>
+#include <testHelpers.hpp>
+
+using std::vector;
+using namespace af;
+
+template<typename T>
+class Select : public ::testing::Test
+{
+};
+
+typedef ::testing::Types<float, double, af::cfloat, af::cdouble, uint, int, intl, uintl, uchar, char> TestTypes;
+TYPED_TEST_CASE(Select, TestTypes);
+
+template<typename T>
+void selectTest(const dim4 &dims)
+{
+    if (noDoubleTests<T>()) return;
+    af::dtype ty = (af::dtype)af::dtype_traits<T>::af_type;
+
+    array a = randu(dims, ty);
+    array cond = randu(dims, ty) > constant(0.3, dims, ty);
+    array b = randu(dims, ty);
+
+    array c = select(cond, a, b);
+
+    int num = (int)a.elements();
+
+    std::vector<T> ha(num);
+    std::vector<T> hb(num);
+    std::vector<T> hc(num);
+    std::vector<char> hcond(num);
+
+    a.host(&ha[0]);
+    b.host(&hb[0]);
+    c.host(&hc[0]);
+    cond.host(&hcond[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(hc[i], hcond[i] ? ha[i] : hb[i]);
+    }
+}
+
+template<typename T, bool is_right>
+void selectScalarTest(const dim4 &dims)
+{
+    if (noDoubleTests<T>()) return;
+    af::dtype ty = (af::dtype)af::dtype_traits<T>::af_type;
+
+    array a = randu(dims, ty);
+    array cond = randu(dims, ty) > constant(0.3, dims, ty);
+    double b = 3;
+
+    array c = is_right ? select(cond, a, b) : select(cond, b, a);
+
+    int num = (int)a.elements();
+
+    std::vector<T> ha(num);
+    std::vector<T> hc(num);
+    std::vector<char> hcond(num);
+
+    a.host(&ha[0]);
+    c.host(&hc[0]);
+    cond.host(&hcond[0]);
+
+    if (is_right) {
+        for (int i = 0; i < num; i++) {
+            ASSERT_EQ(hc[i], hcond[i] ? ha[i] : b);
+        }
+    } else {
+        for (int i = 0; i < num; i++) {
+            ASSERT_EQ(hc[i], hcond[i] ? b : ha[i]);
+        }
+    }
+}
+
+TYPED_TEST(Select, Simple)
+{
+    selectTest<TypeParam>(dim4(1024, 1024));
+}
+
+TYPED_TEST(Select, RightScalar)
+{
+    selectScalarTest<TypeParam, true>(dim4(1000, 1000));
+}
+
+TYPED_TEST(Select, LeftScalar)
+{
+    selectScalarTest<TypeParam, true>(dim4(1000, 1000));
+}
+
+TEST(Select, NaN)
+{
+    dim4 dims(1000, 1250);
+    af::dtype ty = f32;
+
+    array a = randu(dims, ty);
+    a(seq(a.dims(0) / 2), span, span, span) = af::NaN;
+    double b = 0;
+    array c = select(isNaN(a), b, a);
+
+    int num = (int)a.elements();
+
+    std::vector<float> ha(num);
+    std::vector<float> hc(num);
+
+    a.host(&ha[0]);
+    c.host(&hc[0]);
+
+    for (int i = 0; i < num; i++) {
+        ASSERT_EQ(hc[i], std::isnan(ha[i]) ? b : ha[i]);
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git



More information about the debian-science-commits mailing list