forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_xarray_adaptor.cpp
203 lines (178 loc) · 5.37 KB
/
test_xarray_adaptor.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
/***************************************************************************
* 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 "gtest/gtest.h"
#include "xtensor/xarray.hpp"
#include "test_common.hpp"
namespace xt
{
using vec_type = std::vector<int>;
using adaptor_type = xarray_adaptor<vec_type, layout_type::dynamic>;
TEST(xarray_adaptor, shaped_constructor)
{
{
SCOPED_TRACE("row_major constructor");
row_major_result<> rm;
vec_type v;
adaptor_type a(v, rm.shape(), layout_type::row_major);
compare_shape(a, rm);
}
{
SCOPED_TRACE("column_major constructor");
column_major_result<> cm;
vec_type v;
adaptor_type a(v, cm.shape(), layout_type::column_major);
compare_shape(a, cm);
}
}
TEST(xarray_adaptor, strided_constructor)
{
central_major_result<> cmr;
vec_type v;
adaptor_type a(v, cmr.shape(), cmr.strides());
compare_shape(a, cmr);
}
TEST(xarray_adaptor, copy_semantic)
{
central_major_result<> res;
int value = 2;
vec_type v(res.size(), value);
adaptor_type a(v, res.shape(), res.strides());
{
SCOPED_TRACE("copy constructor");
adaptor_type b(a);
compare_shape(a, b);
EXPECT_EQ(a.storage(), b.storage());
}
{
SCOPED_TRACE("assignment operator");
row_major_result<> r;
vec_type v2(r.size(), 0);
adaptor_type c(v2, r.shape());
EXPECT_NE(a.storage(), c.storage());
c = a;
compare_shape(a, c);
EXPECT_EQ(a.storage(), c.storage());
}
}
TEST(xarray_adaptor, move_semantic)
{
central_major_result<> res;
int value = 2;
vec_type v(res.size(), value);
adaptor_type a(v, res.shape(), res.strides());
{
SCOPED_TRACE("move constructor");
adaptor_type tmp(a);
adaptor_type b(std::move(tmp));
compare_shape(a, b);
EXPECT_EQ(a.storage(), b.storage());
}
{
SCOPED_TRACE("move assignment");
row_major_result<> r;
vec_type v2(r.size(), 0);
adaptor_type c(v2, r.shape());
EXPECT_NE(a.storage(), c.storage());
adaptor_type tmp(a);
c = std::move(tmp);
compare_shape(a, c);
EXPECT_EQ(a.storage(), c.storage());
}
}
TEST(xarray_adaptor, resize)
{
vec_type v;
adaptor_type a(v);
test_resize(a);
}
TEST(xarray_adaptor, reshape)
{
vec_type v;
adaptor_type a(v);
test_reshape(a);
}
#if !(defined(XTENSOR_ENABLE_ASSERT) && defined(XTENSOR_DISABLE_EXCEPTIONS))
TEST(xarray_adaptor, access)
{
vec_type v;
adaptor_type a(v);
test_access(a);
}
#endif
TEST(xarray_adaptor, unchecked)
{
vec_type v;
adaptor_type a(v);
test_unchecked(a);
}
TEST(xarray_adaptor, at)
{
vec_type v;
adaptor_type a(v);
test_at(a);
}
TEST(xarray_adaptor, indexed_access)
{
vec_type v;
adaptor_type a(v);
test_indexed_access(a);
}
TEST(xarray_adaptor, broadcast_shape)
{
vec_type v;
adaptor_type a(v);
test_broadcast(a);
test_broadcast2(a);
}
TEST(xarray_adaptor, iterator)
{
vec_type v;
using adaptor_rm = xarray_adaptor<vec_type, layout_type::row_major>;
using adaptor_cm = xarray_adaptor<vec_type, layout_type::column_major>;
adaptor_rm arm(v);
adaptor_cm acm(v);
test_iterator(arm, acm);
}
TEST(xarray_adaptor, fill)
{
vec_type v;
adaptor_type a(v);
test_fill(a);
}
TEST(xarray_adaptor, xiterator)
{
vec_type v;
adaptor_type a(v);
test_xiterator(a);
}
TEST(xarray_adaptor, reverse_xiterator)
{
vec_type v;
adaptor_type a(v);
test_reverse_xiterator(a);
}
TEST(xarray_adaptor, adapt_std_array)
{
std::array<double, 9> a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
xt::xarray_adaptor<decltype(a)> ad(a, xt::dynamic_shape<std::size_t>{3, 3});
EXPECT_EQ(ad(1, 1), 5.);
ad = ad * 2;
EXPECT_EQ(ad(1, 1), 10.);
}
TEST(xarray_adaptor, iterator_types)
{
using vec_type = std::vector<int>;
using array_type = xarray_adaptor<vec_type>;
using const_array_type = xarray_adaptor<const vec_type>;
using iterator = vec_type::iterator;
using const_iterator = vec_type::const_iterator;
test_iterator_types<array_type, iterator, const_iterator>();
test_iterator_types<const_array_type, const_iterator, const_iterator>();
}
}