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
|
/*******************************************************
* 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 <gtest/gtest.h>
#include <af/array.h>
#include <af/arith.h>
#include <af/data.h>
#include <testHelpers.hpp>
using namespace af;
using std::vector;
template<typename T>
class Constant : public ::testing::Test { };
typedef ::testing::Types<float, af::cfloat, double, af::cdouble, int, unsigned, char, uchar, uintl, intl, short, ushort> TestTypes;
TYPED_TEST_CASE(Constant, TestTypes);
template<typename T>
void ConstantCPPCheck(T value) {
if (noDoubleTests<T>()) return;
const int num = 1000;
T val = value;
dtype dty = (dtype) dtype_traits<T>::af_type;
af::array in = constant(val, num, dty);
vector<T> h_in(num);
in.host(&h_in.front());
for (int i = 0; i < num; i++) {
ASSERT_EQ(h_in[i], val);
}
}
template<typename T>
void ConstantCCheck(T value) {
if (noDoubleTests<T>()) return;
const int num = 1000;
typedef typename af::dtype_traits<T>::base_type BT;
BT val = ::real(value);
dtype dty = (dtype) dtype_traits<T>::af_type;
af_array out;
dim_t dim[] = {(dim_t)num};
ASSERT_EQ(AF_SUCCESS, af_constant(&out, val, 1, dim, dty));
vector<T> h_in(num);
af_get_data_ptr(&h_in.front(), out);
for (int i = 0; i < num; i++) {
ASSERT_EQ(::real(h_in[i]), val);
}
}
template<typename T>
void IdentityCPPCheck() {
if (noDoubleTests<T>()) return;
int num = 1000;
dtype dty = (dtype) dtype_traits<T>::af_type;
array out = af::identity(num, num, dty);
vector<T> h_in(num*num);
out.host(&h_in.front());
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if(j == i)
ASSERT_EQ(h_in[i * num + j], T(1));
else
ASSERT_EQ(h_in[i * num + j], T(0));
}
}
num = 100;
out = af::identity(num, num, num, dty);
h_in.resize(num*num*num);
out.host(&h_in.front());
for (int h = 0; h < num; h++) {
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if(j == i)
ASSERT_EQ(h_in[i * num + j], T(1));
else
ASSERT_EQ(h_in[i * num + j], T(0));
}
}
}
}
template<typename T>
void IdentityCCheck() {
if (noDoubleTests<T>()) return;
static const int num = 1000;
dtype dty = (dtype) dtype_traits<T>::af_type;
af_array out;
dim_t dim[] = {(dim_t)num, (dim_t)num};
ASSERT_EQ(AF_SUCCESS, af_identity(&out, 2, dim, dty));
vector<T> h_in(num*num);
af_get_data_ptr(&h_in.front(), out);
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if(j == i)
ASSERT_EQ(h_in[i * num + j], T(1));
else
ASSERT_EQ(h_in[i * num + j], T(0));
}
}
}
template<typename T>
void IdentityCPPError() {
if (noDoubleTests<T>()) return;
static const int num = 1000;
dtype dty = (dtype) dtype_traits<T>::af_type;
try {
array out = af::identity(num, 0, 10, dty);
}
catch(const af::exception &ex) {
SUCCEED();
return;
}
FAIL() << "Failed to throw an exception";
}
TYPED_TEST(Constant, basicCPP)
{
ConstantCPPCheck<TypeParam>(5);
}
TYPED_TEST(Constant, basicC)
{
ConstantCCheck<TypeParam>(5);
}
TYPED_TEST(Constant, IdentityC)
{
IdentityCCheck<TypeParam>();
}
TYPED_TEST(Constant, IdentityCPP)
{
IdentityCPPCheck<TypeParam>();
}
TYPED_TEST(Constant, IdentityCPPError)
{
IdentityCPPError<TypeParam>();
}
|