Skip to content

Commit 54ca763

Browse files
test pyarray assign traits
1 parent 1e922c5 commit 54ca763

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-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: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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_has_strides(T const&)
39+
{
40+
return xt::has_strides<T>::value;
41+
}
42+
43+
template <class T>
44+
xt::layout_type test_result_layout(T const& a1, T const& a2)
45+
{
46+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
47+
auto tmp2 = cos(a1);
48+
return (tmp1 + tmp2).layout();
49+
}
50+
51+
template <class T>
52+
bool test_linear_assign(T const& a1, T const& a2)
53+
{
54+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
55+
auto tmp2 = cos(a1);
56+
T res = tmp1 + tmp2;
57+
return xt::xassign_traits<T, decltype(tmp1 + tmp2)>::linear_assign(res, tmp1 + tmp2, true);
58+
}
59+
60+
template <class T>
61+
bool test_simd_linear_assign(T const& a1, T const& a2)
62+
{
63+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
64+
auto tmp2 = cos(a1);
65+
return xt::xassign_traits<T, decltype(tmp2)>::simd_linear_assign();
66+
}
67+
68+
template <class T>
69+
bool test_linear_static_layout(T const& a1, T const& a2)
70+
{
71+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
72+
auto tmp2 = cos(a1);
73+
return xt::detail::linear_static_layout<decltype(tmp1), decltype(tmp2)>();
74+
}
75+
76+
template <class T>
77+
bool test_contiguous_layout(T const& a1, T const& a2)
78+
{
79+
auto tmp1 = pow(sin((a2 - a1) / 2.), 2.);
80+
auto tmp2 = cos(a1);
81+
return decltype(tmp1)::contiguous_layout && decltype(tmp2)::contiguous_layout;
82+
}
83+
};
84+
85+
TEST_F(pyarray_traits, result_layout)
86+
{
87+
EXPECT_TRUE(d1.layout() == layout_type::row_major);
88+
EXPECT_TRUE(test_result_layout(d1, d2) == layout_type::row_major);
89+
90+
EXPECT_TRUE(r1.layout() == layout_type::row_major);
91+
EXPECT_TRUE(test_result_layout(r1, r2) == layout_type::row_major);
92+
93+
EXPECT_TRUE(c1.layout() == layout_type::column_major);
94+
EXPECT_TRUE(test_result_layout(c1, c2) == layout_type::column_major);
95+
}
96+
97+
TEST_F(pyarray_traits, has_strides)
98+
{
99+
EXPECT_TRUE(test_has_strides(d1));
100+
EXPECT_TRUE(test_has_strides(r1));
101+
EXPECT_TRUE(test_has_strides(c1));
102+
}
103+
104+
TEST_F(pyarray_traits, has_linear_assign)
105+
{
106+
EXPECT_TRUE(d2.has_linear_assign(d1.strides()));
107+
EXPECT_TRUE(r2.has_linear_assign(r1.strides()));
108+
EXPECT_TRUE(c2.has_linear_assign(c1.strides()));
109+
}
110+
111+
TEST_F(pyarray_traits, linear_assign)
112+
{
113+
EXPECT_TRUE(test_linear_assign(d1, d2));
114+
EXPECT_TRUE(test_linear_assign(r1, r2));
115+
EXPECT_TRUE(test_linear_assign(c1, c2));
116+
}
117+
118+
TEST_F(pyarray_traits, simd_linear_assign)
119+
{
120+
#ifdef XTENSOR_USE_XSIMD
121+
EXPECT_FALSE(test_simd_linear_assign(d1, d2));
122+
EXPECT_TRUE(test_simd_linear_assign(r1, r2));
123+
EXPECT_TRUE(test_simd_linear_assign(c1, c2));
124+
#else
125+
EXPECT_FALSE(test_simd_linear_assign(d1, d2));
126+
EXPECT_FALSE(test_simd_linear_assign(r1, r2));
127+
EXPECT_FALSE(test_simd_linear_assign(c1, c2));
128+
#endif
129+
}
130+
131+
TEST_F(pyarray_traits, linear_static_layout)
132+
{
133+
EXPECT_FALSE(test_linear_static_layout(d1, d2));
134+
EXPECT_TRUE(test_linear_static_layout(r1, r2));
135+
EXPECT_TRUE(test_linear_static_layout(c1, c2));
136+
}
137+
138+
TEST_F(pyarray_traits, contiguous_layout)
139+
{
140+
EXPECT_FALSE(test_contiguous_layout(d1, d2));
141+
EXPECT_TRUE(test_contiguous_layout(r1, r2));
142+
EXPECT_TRUE(test_contiguous_layout(c1, c2));
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy