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
|
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "xtensor/xarray.hpp"
#include "xtensor/xtensor.hpp"
#include "test_common.hpp"
#include "test_common_macros.hpp"
namespace xt
{
using array_type = xarray<double>;
using tensor_type = xtensor<double, 2>;
using array_shape = array_type::shape_type;
using tensor_shape = tensor_type::shape_type;
TEST(xtensor_semantic, tensor_plus_tensor)
{
tensor_shape s = {3, 2};
tensor_type t1(s, 3.2);
tensor_type t2(s, 2.5);
tensor_type res = t1 + t2;
EXPECT_EQ(res(0, 0), t1(0, 0) + t2(0, 0));
}
TEST(xtensor_semantic, tensor_plus_array)
{
tensor_shape s1 = {3, 2};
tensor_type t1(s1, 3.2);
array_shape s2 = {3, 2};
array_type t2(s2, 2.5);
tensor_type res = t1 + t2;
EXPECT_EQ(res(0, 0), t1(0, 0) + t2(0, 0));
}
TEST(xtensor_semantic, array_plus_tensor)
{
tensor_shape s1 = {3, 2};
tensor_type t1(s1, 3.2);
array_shape s2 = {3, 2};
array_type t2(s2, 2.5);
array_type res = t1 + t2;
EXPECT_EQ(res(0, 0), t1(0, 0) + t2(0, 0));
}
TEST(xtensor_semantic, tensor_cast)
{
using int8_tensor = xtensor<int8_t, 2>;
using int32_tensor = xtensor<int32_t, 2>;
using double_tensor = xtensor<double, 2>;
int8_tensor i8t = {{int8_t(0), int8_t(1)}, {int8_t(2), int8_t(3)}, {int8_t(4), int8_t(5)}};
int32_tensor i32t = {{int32_t(0), int32_t(1)}, {int8_t(2), int8_t(3)}, {int8_t(4), int8_t(5)}};
double_tensor dt = {{0., 1.}, {2., 3.}, {4., 5.}};
int32_tensor i32res = i8t;
EXPECT_EQ(i32res, i32t);
double_tensor dres = i32t;
EXPECT_EQ(dres, dt);
}
TEST(xtensor_semantic, broadcasting_single_element)
{
xtensor<int, 2> t = xt::zeros<int>({1, 1});
EXPECT_EQ(t.backstrides().size(), 2u);
EXPECT_EQ(t.backstrides()[0], 0u);
EXPECT_EQ(t.backstrides()[1], 0u);
}
}
|