|
| 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_static_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_dynamic_simd_linear_assign(T const& a1, T const& a2) |
| 70 | + { |
| 71 | + auto tmp1 = pow(sin((a2 - a1) / 2.), 2.); |
| 72 | + auto tmp2 = cos(a1); |
| 73 | + return xt::xassign_traits<T, decltype(tmp2)>::simd_linear_assign(a1, tmp2); |
| 74 | + } |
| 75 | + |
| 76 | + template <class T> |
| 77 | + bool test_linear_static_layout(T const& a1, T const& a2) |
| 78 | + { |
| 79 | + auto tmp1 = pow(sin((a2 - a1) / 2.), 2.); |
| 80 | + auto tmp2 = cos(a1); |
| 81 | + return xt::detail::linear_static_layout<decltype(tmp1), decltype(tmp2)>(); |
| 82 | + } |
| 83 | + |
| 84 | + template <class T> |
| 85 | + bool test_contiguous_layout(T const& a1, T const& a2) |
| 86 | + { |
| 87 | + auto tmp1 = pow(sin((a2 - a1) / 2.), 2.); |
| 88 | + auto tmp2 = cos(a1); |
| 89 | + return decltype(tmp1)::contiguous_layout && decltype(tmp2)::contiguous_layout; |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + TEST_F(pyarray_traits, result_layout) |
| 94 | + { |
| 95 | + EXPECT_TRUE(d1.layout() == layout_type::row_major); |
| 96 | + EXPECT_TRUE(test_result_layout(d1, d2) == layout_type::row_major); |
| 97 | + |
| 98 | + EXPECT_TRUE(r1.layout() == layout_type::row_major); |
| 99 | + EXPECT_TRUE(test_result_layout(r1, r2) == layout_type::row_major); |
| 100 | + |
| 101 | + EXPECT_TRUE(c1.layout() == layout_type::column_major); |
| 102 | + EXPECT_TRUE(test_result_layout(c1, c2) == layout_type::column_major); |
| 103 | + } |
| 104 | + |
| 105 | + TEST_F(pyarray_traits, has_strides) |
| 106 | + { |
| 107 | + EXPECT_TRUE(test_has_strides(d1)); |
| 108 | + EXPECT_TRUE(test_has_strides(r1)); |
| 109 | + EXPECT_TRUE(test_has_strides(c1)); |
| 110 | + } |
| 111 | + |
| 112 | + TEST_F(pyarray_traits, has_linear_assign) |
| 113 | + { |
| 114 | + EXPECT_TRUE(d2.has_linear_assign(d1.strides())); |
| 115 | + EXPECT_TRUE(r2.has_linear_assign(r1.strides())); |
| 116 | + EXPECT_TRUE(c2.has_linear_assign(c1.strides())); |
| 117 | + } |
| 118 | + |
| 119 | + TEST_F(pyarray_traits, linear_assign) |
| 120 | + { |
| 121 | + EXPECT_TRUE(test_linear_assign(d1, d2)); |
| 122 | + EXPECT_TRUE(test_linear_assign(r1, r2)); |
| 123 | + EXPECT_TRUE(test_linear_assign(c1, c2)); |
| 124 | + } |
| 125 | + |
| 126 | + TEST_F(pyarray_traits, static_simd_linear_assign) |
| 127 | + { |
| 128 | +#ifdef XTENSOR_USE_XSIMD |
| 129 | + EXPECT_FALSE(test_static_simd_linear_assign(d1, d2)); |
| 130 | + EXPECT_TRUE(test_static_simd_linear_assign(r1, r2)); |
| 131 | + EXPECT_TRUE(test_static_simd_linear_assign(c1, c2)); |
| 132 | +#else |
| 133 | + EXPECT_FALSE(test_static_simd_linear_assign(d1, d2)); |
| 134 | + EXPECT_FALSE(test_static_simd_linear_assign(r1, r2)); |
| 135 | + EXPECT_FALSE(test_static_simd_linear_assign(c1, c2)); |
| 136 | +#endif |
| 137 | + } |
| 138 | + |
| 139 | + TEST_F(pyarray_traits, dynamic_simd_linear_assign) |
| 140 | + { |
| 141 | +#ifdef XTENSOR_USE_XSIMD |
| 142 | + EXPECT_TRUE(test_dynamic_simd_linear_assign(d1, d2)); |
| 143 | + EXPECT_TRUE(test_dynamic_simd_linear_assign(r1, r2)); |
| 144 | + EXPECT_TRUE(test_dynamic_simd_linear_assign(c1, c2)); |
| 145 | +#else |
| 146 | + EXPECT_FALSE(test_dynamic_simd_linear_assign(d1, d2)); |
| 147 | + EXPECT_FALSE(test_dynamic_simd_linear_assign(r1, r2)); |
| 148 | + EXPECT_FALSE(test_dynamic_simd_linear_assign(c1, c2)); |
| 149 | +#endif |
| 150 | + } |
| 151 | + |
| 152 | + TEST_F(pyarray_traits, linear_static_layout) |
| 153 | + { |
| 154 | + EXPECT_FALSE(test_linear_static_layout(d1, d2)); |
| 155 | + EXPECT_TRUE(test_linear_static_layout(r1, r2)); |
| 156 | + EXPECT_TRUE(test_linear_static_layout(c1, c2)); |
| 157 | + } |
| 158 | + |
| 159 | + TEST_F(pyarray_traits, contiguous_layout) |
| 160 | + { |
| 161 | + EXPECT_FALSE(test_contiguous_layout(d1, d2)); |
| 162 | + EXPECT_TRUE(test_contiguous_layout(r1, r2)); |
| 163 | + EXPECT_TRUE(test_contiguous_layout(c1, c2)); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments