Skip to content

Commit 1900c23

Browse files
test pyarray assign traits
1 parent 9924e7b commit 1900c23

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ include_directories(${GTEST_INCLUDE_DIRS})
8686
set(XTENSOR_PYTHON_TESTS
8787
main.cpp
8888
test_pyarray.cpp
89+
test_pyarray_traits.cpp
8990
test_pytensor.cpp
9091
test_pyvectorize.cpp
9192
)

test/test_pyarray_traits.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/***************************************************************************
2+
* Copyright (c) Wolf Vollprecht, Johan Mabille and Sylvain Corlay *
3+
* Copyright (c) QuantStack *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#include "gtest/gtest.h"
11+
12+
#include "xtensor-python/pyarray.hpp"
13+
14+
15+
16+
namespace xt
17+
{
18+
namespace testing
19+
{
20+
class pyarray_traits: public ::testing::Test
21+
{
22+
protected:
23+
24+
using dynamic_type = xt::pyarray<double>;
25+
using row_major_type = xt::pyarray<double, xt::layout_type::row_major>;
26+
using column_major_type = xt::pyarray<double, xt::layout_type::column_major>;
27+
28+
dynamic_type d1 = {{0., 1.}, {0., 10.}, {0., 100.}};
29+
dynamic_type d2 = {{0., 2.}, {0., 20.}, {0., 200.}};
30+
31+
row_major_type r1 = {{0., 1.}, {0., 10.}, {0., 100.}};
32+
row_major_type r2 = {{0., 2.}, {0., 20.}, {0., 200.}};
33+
34+
column_major_type c1 = {{0., 1.}, {0., 10.}, {0., 100.}};
35+
column_major_type c2 = {{0., 2.}, {0., 20.}, {0., 200.}};
36+
37+
template <class T>
38+
bool test_linear_assign(T const& a1, T const& a2)
39+
{
40+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
41+
auto tmp2 = cos(a1);
42+
T res = tmp1 + tmp2;
43+
return xt::xassign_traits<T, decltype(tmp1 + tmp2)>::linear_assign(res, tmp1 + tmp2, true);
44+
}
45+
46+
template <class T>
47+
bool test_simd_linear_assign(T const& a1, T const& a2)
48+
{
49+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
50+
auto tmp2 = cos(a1);
51+
return xt::xassign_traits<T, decltype(tmp2)>::simd_linear_assign();
52+
}
53+
54+
template <class T>
55+
bool test_linear_static_layout(T const& a1, T const& a2)
56+
{
57+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
58+
auto tmp2 = cos(a1);
59+
return xt::detail::linear_static_layout<decltype(tmp1), decltype(tmp2)>();
60+
}
61+
62+
template <class T>
63+
bool test_contiguous_layout(T const& a1, T const& a2)
64+
{
65+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
66+
auto tmp2 = cos(a1);
67+
return decltype(tmp1)::contiguous_layout && decltype(tmp2)::contiguous_layout;
68+
}
69+
};
70+
71+
TEST_F(pyarray_traits, linear_assign)
72+
{
73+
EXPECT_TRUE(test_linear_assign(d1, d2));
74+
EXPECT_TRUE(test_linear_assign(r1, r2));
75+
EXPECT_TRUE(test_linear_assign(c1, c2));
76+
}
77+
78+
TEST_F(pyarray_traits, simd_linear_assign)
79+
{
80+
#ifdef XTENSOR_USE_XSIMD
81+
EXPECT_FALSE(test_simd_linear_assign(d1, d2));
82+
EXPECT_TRUE(test_simd_linear_assign(r1, r2));
83+
EXPECT_TRUE(test_simd_linear_assign(c1, c2));
84+
#else
85+
EXPECT_FALSE(test_simd_linear_assign(d1, d2));
86+
EXPECT_FALSE(test_simd_linear_assign(r1, r2));
87+
EXPECT_FALSE(test_simd_linear_assign(c1, c2));
88+
#endif
89+
}
90+
91+
TEST_F(pyarray_traits, linear_static_layout)
92+
{
93+
EXPECT_FALSE(test_linear_static_layout(d1, d2));
94+
EXPECT_TRUE(test_linear_static_layout(r1, r2));
95+
EXPECT_TRUE(test_linear_static_layout(c1, c2));
96+
}
97+
98+
TEST_F(pyarray_traits, contiguous_layout)
99+
{
100+
EXPECT_FALSE(test_contiguous_layout(d1, d2));
101+
EXPECT_TRUE(test_contiguous_layout(r1, r2));
102+
EXPECT_TRUE(test_contiguous_layout(c1, c2));
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)