1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*******************************************************
* 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, short, ushort> 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 b = randu(dims, ty);
if (a.isinteger()) {
a = (a % (1 << 30)).as(ty);
b = (b % (1 << 30)).as(ty);
}
array c = a.copy();
array cond = randu(dims, ty) > a;
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);
if (a.isinteger()) {
a = (a % (1 << 30)).as(ty);
}
array c = a.copy();
array cond = randu(dims, ty) > a;
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] ? T(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();
float 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]);
}
}
TEST(Replace, ISSUE_1249)
{
dim4 dims(2, 3, 4);
array cond = af::randu(dims) > 0.5;
array a = af::randu(dims);
array b = a.copy();
replace(b, !cond, a - a * 0.9);
array c = a - a * cond * 0.9;
int num = (int)dims.elements();
std::vector<float> hb(num);
std::vector<float> hc(num);
b.host(&hb[0]);
c.host(&hc[0]);
for (int i = 0; i < num; i++) {
ASSERT_EQ(hc[i], hb[i]) << "at " << i;
}
}
TEST(Replace, 4D)
{
dim4 dims(2, 3, 4, 2);
array cond = af::randu(dims) > 0.5;
array a = af::randu(dims);
array b = a.copy();
replace(b, !cond, a - a * 0.9);
array c = a - a * cond * 0.9;
int num = (int)dims.elements();
std::vector<float> hb(num);
std::vector<float> hc(num);
b.host(&hb[0]);
c.host(&hc[0]);
for (int i = 0; i < num; i++) {
ASSERT_EQ(hc[i], hb[i]) << "at " << i;
}
}
|