|
| 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_ref.hpp" |
| 13 | + |
| 14 | +#include "xtensor/xarray.hpp" |
| 15 | +#include "xtensor/xview.hpp" |
| 16 | + |
| 17 | +#include "test_common.hpp" |
| 18 | + |
| 19 | +namespace xt |
| 20 | +{ |
| 21 | + using container_type = std::vector<npy_intp>; |
| 22 | + |
| 23 | + template <class T> |
| 24 | + using ndarray = pyarray_ref<T, xt::layout_type::row_major>; |
| 25 | + |
| 26 | + void test1 (ndarray<int>const& x) |
| 27 | + { |
| 28 | + ndarray<int> y = x; |
| 29 | + ndarray<int> z = xt::zeros<int>({10}); |
| 30 | + } |
| 31 | + |
| 32 | + double compute(ndarray<double> const& xs) |
| 33 | + { |
| 34 | + auto v = xt::view (xs, 0, xt::all()); |
| 35 | + return v(0); |
| 36 | + } |
| 37 | + |
| 38 | + TEST(pyarray_ref, initializer_constructor) |
| 39 | + { |
| 40 | + pyarray_ref<int> t |
| 41 | + {{{ 0, 1, 2}, |
| 42 | + { 3, 4, 5}, |
| 43 | + { 6, 7, 8}}, |
| 44 | + {{ 9, 10, 11}, |
| 45 | + {12, 13, 14}, |
| 46 | + {15, 16, 17}}}; |
| 47 | + |
| 48 | + EXPECT_EQ(t.dimension(), 3); |
| 49 | + EXPECT_EQ(t(0, 0, 1), 1); |
| 50 | + EXPECT_EQ(t.shape()[0], 2); |
| 51 | + } |
| 52 | + |
| 53 | + TEST(pyarray_ref, shaped_constructor) |
| 54 | + { |
| 55 | + { |
| 56 | + SCOPED_TRACE("row_major constructor"); |
| 57 | + row_major_result<> rm; |
| 58 | + pyarray_ref<int> ra(rm.m_shape); |
| 59 | + compare_shape(ra, rm); |
| 60 | + EXPECT_EQ(layout_type::row_major, ra.layout()); |
| 61 | + } |
| 62 | + |
| 63 | + { |
| 64 | + SCOPED_TRACE("column_major constructor"); |
| 65 | + column_major_result<> cm; |
| 66 | + pyarray_ref<int> ca(cm.m_shape, layout_type::column_major); |
| 67 | + compare_shape(ca, cm); |
| 68 | + EXPECT_EQ(layout_type::column_major, ca.layout()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + TEST(pyarray_ref, from_shape) |
| 73 | + { |
| 74 | + auto arr = pyarray_ref<double>::from_shape({5, 2, 6}); |
| 75 | + auto exp_shape = std::vector<std::size_t>{5, 2, 6}; |
| 76 | + EXPECT_TRUE(std::equal(arr.shape().begin(), arr.shape().end(), exp_shape.begin())); |
| 77 | + EXPECT_EQ(arr.shape().size(), 3); |
| 78 | + EXPECT_EQ(arr.size(), 5 * 2 * 6); |
| 79 | + } |
| 80 | + |
| 81 | + TEST(pyarray_ref, strided_constructor) |
| 82 | + { |
| 83 | + central_major_result<> cmr; |
| 84 | + pyarray_ref<int> cma(cmr.m_shape, cmr.m_strides); |
| 85 | + compare_shape(cma, cmr); |
| 86 | + } |
| 87 | + |
| 88 | + TEST(pyarray_ref, valued_constructor) |
| 89 | + { |
| 90 | + { |
| 91 | + SCOPED_TRACE("row_major valued constructor"); |
| 92 | + row_major_result<> rm; |
| 93 | + int value = 2; |
| 94 | + pyarray_ref<int> ra(rm.m_shape, value); |
| 95 | + compare_shape(ra, rm); |
| 96 | + std::vector<int> vec(ra.size(), value); |
| 97 | + EXPECT_TRUE(std::equal(vec.cbegin(), vec.cend(), ra.storage().cbegin())); |
| 98 | + } |
| 99 | + |
| 100 | + { |
| 101 | + SCOPED_TRACE("column_major valued constructor"); |
| 102 | + column_major_result<> cm; |
| 103 | + int value = 2; |
| 104 | + pyarray_ref<int> ca(cm.m_shape, value, layout_type::column_major); |
| 105 | + compare_shape(ca, cm); |
| 106 | + std::vector<int> vec(ca.size(), value); |
| 107 | + EXPECT_TRUE(std::equal(vec.cbegin(), vec.cend(), ca.storage().cbegin())); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + TEST(pyarray_ref, strided_valued_constructor) |
| 112 | + { |
| 113 | + central_major_result<> cmr; |
| 114 | + int value = 2; |
| 115 | + pyarray_ref<int> cma(cmr.m_shape, cmr.m_strides, value); |
| 116 | + compare_shape(cma, cmr); |
| 117 | + std::vector<int> vec(cma.size(), value); |
| 118 | + EXPECT_TRUE(std::equal(vec.cbegin(), vec.cend(), cma.storage().cbegin())); |
| 119 | + } |
| 120 | + |
| 121 | + TEST(pyarray_ref, copy_semantic) |
| 122 | + { |
| 123 | + central_major_result<> res; |
| 124 | + int value = 2; |
| 125 | + pyarray_ref<int> a(res.m_shape, res.m_strides, value); |
| 126 | + |
| 127 | + { |
| 128 | + SCOPED_TRACE("copy constructor"); |
| 129 | + pyarray_ref<int> b(a); |
| 130 | + compare_shape(a, b); |
| 131 | + EXPECT_EQ(a.storage(), b.storage()); |
| 132 | + a.data()[0] += 1; |
| 133 | + EXPECT_EQ(a.storage()[0], b.storage()[0]); |
| 134 | + } |
| 135 | + |
| 136 | + { |
| 137 | + SCOPED_TRACE("assignment operator"); |
| 138 | + row_major_result<> r; |
| 139 | + pyarray_ref<int> c(r.m_shape, 0); |
| 140 | + EXPECT_NE(a.storage(), c.storage()); |
| 141 | + c = a; |
| 142 | + compare_shape(a, c); |
| 143 | + EXPECT_EQ(a.storage(), c.storage()); |
| 144 | + a.data()[0] += 1; |
| 145 | + EXPECT_EQ(a.storage()[0], c.storage()[0]); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + TEST(pyarray_ref, move_semantic) |
| 150 | + { |
| 151 | + central_major_result<> res; |
| 152 | + int value = 2; |
| 153 | + pyarray_ref<int> a(res.m_shape, res.m_strides, value); |
| 154 | + |
| 155 | + { |
| 156 | + SCOPED_TRACE("move constructor"); |
| 157 | + pyarray_ref<int> tmp(a); |
| 158 | + pyarray_ref<int> b(std::move(tmp)); |
| 159 | + compare_shape(a, b); |
| 160 | + EXPECT_EQ(a.storage(), b.storage()); |
| 161 | + } |
| 162 | + |
| 163 | + { |
| 164 | + SCOPED_TRACE("move assignment"); |
| 165 | + row_major_result<> r; |
| 166 | + pyarray_ref<int> c(r.m_shape, 0); |
| 167 | + EXPECT_NE(a.storage(), c.storage()); |
| 168 | + pyarray_ref<int> tmp(a); |
| 169 | + c = std::move(tmp); |
| 170 | + compare_shape(a, c); |
| 171 | + EXPECT_EQ(a.storage(), c.storage()); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + TEST(pyarray_ref, extended_constructor) |
| 176 | + { |
| 177 | + xt::xarray<int> a1 = { { 1, 2 },{ 3, 4 } }; |
| 178 | + xt::xarray<int> a2 = { { 1, 2 },{ 3, 4 } }; |
| 179 | + pyarray_ref<int> c = a1 + a2; |
| 180 | + EXPECT_EQ(c(0, 0), a1(0, 0) + a2(0, 0)); |
| 181 | + EXPECT_EQ(c(0, 1), a1(0, 1) + a2(0, 1)); |
| 182 | + EXPECT_EQ(c(1, 0), a1(1, 0) + a2(1, 0)); |
| 183 | + EXPECT_EQ(c(1, 1), a1(1, 1) + a2(1, 1)); |
| 184 | + } |
| 185 | + |
| 186 | + TEST(pyarray_ref, resize) |
| 187 | + { |
| 188 | + pyarray_ref<int> a; |
| 189 | + test_resize(a); |
| 190 | + |
| 191 | + pyarray_ref<int> b = { {1, 2}, {3, 4} }; |
| 192 | + a.resize(b.shape()); |
| 193 | + EXPECT_EQ(a.shape(), b.shape()); |
| 194 | + } |
| 195 | + |
| 196 | + TEST(pyarray_ref, transpose) |
| 197 | + { |
| 198 | + pyarray_ref<int> a; |
| 199 | + test_transpose(a); |
| 200 | + } |
| 201 | + |
| 202 | + TEST(pyarray_ref, access) |
| 203 | + { |
| 204 | + pyarray_ref<int> a; |
| 205 | + test_access(a); |
| 206 | + } |
| 207 | + |
| 208 | + TEST(pyarray_ref, indexed_access) |
| 209 | + { |
| 210 | + pyarray_ref<int> a; |
| 211 | + test_indexed_access(a); |
| 212 | + } |
| 213 | + |
| 214 | + TEST(pyarray_ref, broadcast_shape) |
| 215 | + { |
| 216 | + pyarray_ref<int> a; |
| 217 | + test_broadcast(a); |
| 218 | + test_broadcast2(a); |
| 219 | + } |
| 220 | + |
| 221 | + TEST(pyarray_ref, iterator) |
| 222 | + { |
| 223 | + pyarray_ref<int> a; |
| 224 | + pyarray_ref<int> b; |
| 225 | + test_iterator(a, b); |
| 226 | + |
| 227 | + pyarray_ref<int, layout_type::row_major> c; |
| 228 | + bool truthy = std::is_same<decltype(c.begin()), int*>::value; |
| 229 | + EXPECT_TRUE(truthy); |
| 230 | + } |
| 231 | + |
| 232 | + TEST(pyarray_ref, initializer_list) |
| 233 | + { |
| 234 | + pyarray_ref<int> a0(1); |
| 235 | + pyarray_ref<int> a1({1, 2}); |
| 236 | + pyarray_ref<int> a2({{1, 2}, {2, 4}, {5, 6}}); |
| 237 | + EXPECT_EQ(1, a0()); |
| 238 | + EXPECT_EQ(2, a1(1)); |
| 239 | + EXPECT_EQ(4, a2(1, 1)); |
| 240 | + } |
| 241 | + |
| 242 | + TEST(pyarray_ref, zerod) |
| 243 | + { |
| 244 | + pyarray_ref<int> a; |
| 245 | + EXPECT_EQ(0, a()); |
| 246 | + } |
| 247 | + |
| 248 | + TEST(pyarray_ref, reshape) |
| 249 | + { |
| 250 | + pyarray_ref<int> a = {{1,2,3}, {4,5,6}}; |
| 251 | + auto ptr = a.data(); |
| 252 | + a.reshape({1, 6}); |
| 253 | + std::vector<std::size_t> sc1({1, 6}); |
| 254 | + EXPECT_TRUE(std::equal(sc1.begin(), sc1.end(), a.shape().begin()) && a.shape().size() == 2); |
| 255 | + EXPECT_EQ(ptr, a.data()); |
| 256 | + a.reshape({6}); |
| 257 | + std::vector<std::size_t> sc2 = {6}; |
| 258 | + EXPECT_TRUE(std::equal(sc2.begin(), sc2.end(), a.shape().begin()) && a.shape().size() == 1); |
| 259 | + EXPECT_EQ(ptr, a.data()); |
| 260 | + } |
| 261 | + |
| 262 | + TEST(pyarray_ref, view) |
| 263 | + { |
| 264 | + xt::pyarray_ref<int> arr = xt::zeros<int>({ 10 }); |
| 265 | + auto v = xt::view(arr, xt::all()); |
| 266 | + EXPECT_EQ(v(0), 0.); |
| 267 | + } |
| 268 | + |
| 269 | + TEST(pyarray_ref, zerod_copy) |
| 270 | + { |
| 271 | + xt::pyarray_ref<int> arr = 2; |
| 272 | + xt::pyarray_ref<int> arr2(arr); |
| 273 | + EXPECT_EQ(arr(), arr2()); |
| 274 | + } |
| 275 | +} |
0 commit comments