forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xadapt.cpp
174 lines (142 loc) · 4.88 KB
/
test_xadapt.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
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* 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/xadapt.hpp"
#include "xtensor/xstrides.hpp"
namespace xt
{
using vec_type = std::vector<int>;
TEST(xarray_adaptor, adapt)
{
vec_type v(4, 0);
using shape_type = std::vector<vec_type::size_type>;
shape_type s({2, 2});
auto a1 = adapt(v, s);
a1(0, 1) = 1;
EXPECT_EQ(1, v[a1.strides()[1]]);
shape_type str({2, 1});
auto a2 = adapt(v, s, str);
a2(1, 0) = 1;
EXPECT_EQ(1, v[2]);
}
TEST(xarray_adaptor, pointer_no_ownership)
{
size_t size = 4;
int* data = new int[size];
using shape_type = std::vector<vec_type::size_type>;
shape_type s({2, 2});
auto a1 = adapt(data, size, no_ownership(), s);
a1(0, 1) = 1;
EXPECT_EQ(1, data[a1.strides()[1]]);
shape_type str({2, 1});
auto a2 = adapt(data, size, no_ownership(), s, str);
a2(1, 0) = 1;
EXPECT_EQ(1, data[2]);
delete[] data;
}
TEST(xarray_adaptor, pointer_acquire_ownership)
{
size_t size = 4;
int* data = new int[size];
int* data2 = new int[size];
using shape_type = std::vector<vec_type::size_type>;
shape_type s({2, 2});
auto a1 = adapt(data, size, acquire_ownership(), s);
a1(0, 1) = 1;
EXPECT_EQ(1, data[a1.strides()[1]]);
shape_type str({2, 1});
auto a2 = adapt(data2, size, acquire_ownership(), s, str);
a2(1, 0) = 1;
EXPECT_EQ(1, data2[2]);
}
TEST(xtensor_adaptor, adapt)
{
vec_type v0(4, 0);
auto a0 = adapt(v0);
a0(0) = 1;
a0(3) = 3;
EXPECT_EQ(1, v0[0]);
EXPECT_EQ(3, v0[3]);
vec_type v(4, 0);
using shape_type = std::array<vec_type::size_type, 2>;
shape_type s = {2, 2};
auto a1 = adapt(v, s);
a1(0, 1) = 1;
EXPECT_EQ(1, v[a1.strides()[1]]);
shape_type str = {2, 1};
auto a2 = adapt(v, s, str);
a2(1, 0) = 1;
EXPECT_EQ(1, v[2]);
}
TEST(xtensor_adaptor, pointer_no_ownership)
{
size_t size = 4;
int* data = new int[size];
auto a0 = adapt(data, size, no_ownership());
a0(3) = 3;
EXPECT_EQ(3, data[3]);
using shape_type = std::array<vec_type::size_type, 2>;
shape_type s = {2, 2};
auto a1 = adapt(data, size, no_ownership(), s);
a1(0, 1) = 1;
EXPECT_EQ(1, data[a1.strides()[1]]);
shape_type str = {2, 1};
auto a2 = adapt(data, size, no_ownership(), s, str);
a2(1, 0) = 1;
EXPECT_EQ(1, data[2]);
delete[] data;
}
TEST(xtensor_adaptor, pointer_acquire_ownership)
{
size_t size = 4;
int* data0 = new int[size];
int* data1 = new int[size];
int* data2 = new int[size];
auto a0 = adapt(data0, size, acquire_ownership());
a0(3) = 3;
EXPECT_EQ(3, data0[3]);
using shape_type = std::array<vec_type::size_type, 2>;
shape_type s = {2, 2};
auto a1 = adapt(data1, size, acquire_ownership(), s);
a1(0, 1) = 1;
EXPECT_EQ(1, data1[a1.strides()[1]]);
shape_type str = {2, 1};
auto a2 = adapt(data2, size, acquire_ownership(), s, str);
a2(1, 0) = 1;
EXPECT_EQ(1, data2[2]);
}
TEST(xtensor_adaptor, move_pointer_acquire_ownership)
{
size_t size = 4;
int* data = new int[size];
int* data2 = new int[size];
using shape_type = std::array<vec_type::size_type, 2>;
shape_type s = {2, 2};
auto a1 = adapt(std::move(data), size, acquire_ownership(), s);
a1(0, 1) = 1;
EXPECT_EQ(1, data[a1.strides()[1]]);
shape_type str = {2, 1};
auto a2 = adapt(std::move(data2), size, acquire_ownership(), s, str);
a2(1, 0) = 1;
EXPECT_EQ(1, data2[2]);
}
TEST(xarray_adaptor, inplace_assignment)
{
size_t size = 1;
int data1 = 0;
int data2 = 1;
int data3;
using shape_type = std::vector<vec_type::size_type>;
shape_type s({1});
auto a1 = adapt(&data1, size, xt::no_ownership(), s);
auto a2 = adapt(&data2, size, xt::no_ownership(), s);
auto a3 = adapt(&data3, size, xt::no_ownership(), s);
a3 = a1 + a2;
EXPECT_EQ(1, data3);
}
}