forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xstrides.cpp
244 lines (204 loc) · 10.5 KB
/
test_xstrides.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***************************************************************************
* 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 "test_common_macros.hpp"
#include "xtensor/xarray.hpp"
#include "test_common.hpp"
namespace xt
{
TEST(xstrides, broadcast_shape)
{
using shape_type = std::vector<std::size_t>;
shape_type s1 = { 2, 4, 3 };
shape_type s2 = { 2, 1, 3 };
shape_type s3 = { 2, 0, 3 };
shape_type s4 = uninitialized_shape<shape_type>(3);
shape_type s5 = s2;
bool t1 = broadcast_shape(s1, s5);
EXPECT_EQ(s5, s1);
EXPECT_FALSE(t1);
shape_type s6 = s2;
bool t2 = broadcast_shape(s3, s6);
EXPECT_EQ(s6, s3);
EXPECT_FALSE(t2);
shape_type s7 = s3;
XT_EXPECT_ANY_THROW(broadcast_shape(s1, s7));
shape_type s8 = s4;
bool t3 = broadcast_shape(s1, s8);
EXPECT_EQ(s8, s1);
EXPECT_TRUE(t3);
shape_type s9 = s4;
bool t4 = broadcast_shape(s2, s9);
EXPECT_EQ(s9, s2);
EXPECT_TRUE(t4);
shape_type s10 = s4;
bool t5 = broadcast_shape(s3, s10);
EXPECT_EQ(s10, s3);
EXPECT_TRUE(t5);
}
TEST(xstrides, free_function_2d_row_major)
{
xt::xarray<int, xt::layout_type::row_major> a = xt::ones<int>({1, 3});
using stype = std::vector<std::ptrdiff_t>;
std::ptrdiff_t sof = sizeof(int);
EXPECT_EQ(xt::strides(a), stype({3, 1}));
EXPECT_EQ(xt::strides(a, xt::stride_type::normal), stype({3, 1}));
EXPECT_EQ(xt::strides(a, xt::stride_type::internal), stype({0, 1}));
EXPECT_EQ(xt::strides(a, xt::stride_type::bytes), stype({3 * sof, sof}));
EXPECT_TRUE(xt::strides(a, 0) == 3);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::normal) == 3);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::internal) == 0);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::bytes) == 3 * sof);
EXPECT_TRUE(xt::strides(a, 1) == 1);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::normal) == 1);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::internal) == 1);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::bytes) == sof);
}
TEST(xstrides, free_function_4d_row_major)
{
xt::xarray<int, xt::layout_type::row_major> a = xt::ones<int>({5, 4, 1, 4});
using stype = std::vector<std::ptrdiff_t>;
std::ptrdiff_t sof = sizeof(int);
EXPECT_EQ(xt::strides(a), stype({16, 4, 4, 1}));
EXPECT_EQ(xt::strides(a, xt::stride_type::normal), stype({16, 4, 4, 1}));
EXPECT_EQ(xt::strides(a, xt::stride_type::internal), stype({16, 4, 0, 1}));
EXPECT_EQ(xt::strides(a, xt::stride_type::bytes), stype({16 * sof, 4 * sof, 4 * sof, 1 * sof}));
EXPECT_TRUE(xt::strides(a, 0) == 16);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::normal) == 16);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::internal) == 16);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::bytes) == 16 * sof);
EXPECT_TRUE(xt::strides(a, 1) == 4);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::normal) == 4);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::internal) == 4);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::bytes) == 4 * sof);
EXPECT_TRUE(xt::strides(a, 2) == 4);
EXPECT_TRUE(xt::strides(a, 2, xt::stride_type::normal) == 4);
EXPECT_TRUE(xt::strides(a, 2, xt::stride_type::internal) == 0);
EXPECT_TRUE(xt::strides(a, 2, xt::stride_type::bytes) == 4 * sof);
EXPECT_TRUE(xt::strides(a, 3) == 1);
EXPECT_TRUE(xt::strides(a, 3, xt::stride_type::normal) == 1);
EXPECT_TRUE(xt::strides(a, 3, xt::stride_type::internal) == 1);
EXPECT_TRUE(xt::strides(a, 3, xt::stride_type::bytes) == sof);
}
TEST(xstrides, free_function_4d_column_major)
{
xt::xarray<int, xt::layout_type::column_major> a = xt::ones<int>({5, 4, 1, 4});
using stype = std::vector<std::ptrdiff_t>;
std::ptrdiff_t sof = sizeof(int);
EXPECT_EQ(xt::strides(a), stype({1, 5, 20, 20}));
EXPECT_EQ(xt::strides(a, xt::stride_type::normal), stype({1, 5, 20, 20}));
EXPECT_EQ(xt::strides(a, xt::stride_type::internal), stype({1, 5, 0, 20}));
EXPECT_EQ(xt::strides(a, xt::stride_type::bytes), stype({sof, 5 * sof, 20 * sof, 20 * sof}));
EXPECT_TRUE(xt::strides(a, 0) == 1);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::normal) == 1);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::internal) == 1);
EXPECT_TRUE(xt::strides(a, 0, xt::stride_type::bytes) == sof);
EXPECT_TRUE(xt::strides(a, 1) == 5);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::normal) == 5);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::internal) == 5);
EXPECT_TRUE(xt::strides(a, 1, xt::stride_type::bytes) == 5 * sof);
EXPECT_TRUE(xt::strides(a, 2) == 20);
EXPECT_TRUE(xt::strides(a, 2, xt::stride_type::normal) == 20);
EXPECT_TRUE(xt::strides(a, 2, xt::stride_type::internal) == 0);
EXPECT_TRUE(xt::strides(a, 2, xt::stride_type::bytes) == 20 * sof);
EXPECT_TRUE(xt::strides(a, 3) == 20);
EXPECT_TRUE(xt::strides(a, 3, xt::stride_type::normal) == 20);
EXPECT_TRUE(xt::strides(a, 3, xt::stride_type::internal) == 20);
EXPECT_TRUE(xt::strides(a, 3, xt::stride_type::bytes) == 20 * sof);
}
TEST(xstrides, unravel_from_strides)
{
SUBCASE("row_major strides")
{
row_major_result<> rm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::ptrdiff_t>(rm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_from_strides(offset, rm.strides(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
SUBCASE("column_major strides")
{
column_major_result<> cm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::ptrdiff_t>(cm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_from_strides(offset, cm.strides(), layout_type::column_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
SUBCASE("unit_major strides")
{
unit_shape_result<> um;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 0, 1 };
auto offset = element_offset<std::ptrdiff_t>(um.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_from_strides(offset, um.strides(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
}
TEST(xstrides, unravel_index)
{
SUBCASE("row_major strides")
{
row_major_result<> rm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::size_t>(rm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_index(offset, rm.shape(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
SUBCASE("column_major strides")
{
column_major_result<> cm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::size_t>(cm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_index(offset, cm.shape(), layout_type::column_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
SUBCASE("unit_major strides")
{
unit_shape_result<> um;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 0, 1 };
auto offset = element_offset<std::size_t>(um.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_index(offset, um.shape(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
}
TEST(xstrides, do_match_strides)
{
using vector_type = std::vector<std::size_t>;
vector_type shape_0 = { 2, 1, 4 };
vector_type strides_0 = { 4, 0, 1 };
EXPECT_TRUE(xt::do_strides_match(shape_0, strides_0, xt::layout_type::row_major, true));
vector_type strides_1 = { 4, 4, 1 };
EXPECT_TRUE(xt::do_strides_match(shape_0, strides_1, xt::layout_type::row_major, false));
vector_type strides_2 = { 1, 0, 2 };
EXPECT_TRUE(xt::do_strides_match(shape_0, strides_2, xt::layout_type::column_major, true));
vector_type strides_3 = { 1, 2, 2 };
EXPECT_TRUE(xt::do_strides_match(shape_0, strides_3, xt::layout_type::column_major, false));
vector_type shape_1 = { 2, 1, 2, 4 };
vector_type strides_4 = { 8, 8, 4, 1 };
EXPECT_TRUE(xt::do_strides_match(shape_1, strides_4, xt::layout_type::row_major, false));
vector_type strides_5 = { 8, 1, 8, 1 };
EXPECT_FALSE(xt::do_strides_match(shape_1, strides_5, xt::layout_type::row_major, false));
vector_type shape_2 = { 2, 2, 1, 4 };
vector_type strides_6 = { 1, 2, 4, 4 };
EXPECT_TRUE(xt::do_strides_match(shape_2, strides_6, xt::layout_type::column_major, false));
vector_type strides_7 = { 1, 2, 1, 4 };
EXPECT_FALSE(xt::do_strides_match(shape_2, strides_7, xt::layout_type::column_major, false));
}
TEST(xstrides, ravel_index)
{
xt::uvector<std::size_t> index = { 1, 1, 1 };
xt::uvector<std::size_t> shape = { 10, 20, 30 };
auto idx = xt::ravel_index(index, shape, xt::layout_type::row_major);
EXPECT_EQ(idx, 631);
}
}