File: test_xbuffer_adaptor.cpp

package info (click to toggle)
xtensor 0.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,476 kB
  • sloc: cpp: 65,302; makefile: 202; python: 171; javascript: 8
file content (219 lines) | stat: -rw-r--r-- 6,426 bytes parent folder | download
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
/***************************************************************************
 * 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 "xtensor/xbuffer_adaptor.hpp"

#include "test_common_macros.hpp"

namespace xt
{
    using buffer_adaptor = xbuffer_adaptor<double*>;
    using allocator = std::allocator<double>;
    using owner_adaptor = xbuffer_adaptor<double*&, acquire_ownership>;

    TEST(xbuffer_adaptor, owner_destructor)
    {
        size_t size = 100;
        double* data = allocator{}.allocate(size);
        owner_adaptor adapt(data, size);
        EXPECT_EQ(data, adapt.data());
    }

    TEST(xbuffer_adaptor, owner_move)
    {
        size_t size = 100;
        double* data = allocator{}.allocate(size);
        owner_adaptor adapt(data, size);

        owner_adaptor adapt2(std::move(adapt));
        EXPECT_EQ(data, adapt2.data());
        EXPECT_EQ(size, adapt2.size());
        EXPECT_EQ(size_t(0), adapt.size());
    }

    TEST(xbuffer_adaptor, owner_copy_assign)
    {
        size_t size1 = 100;
        double* data1 = allocator{}.allocate(size1);
        data1[0] = 2.5;
        owner_adaptor adapt1(data1, size1);

        size_t size2 = 200;
        double* data2 = allocator{}.allocate(size2);
        data2[0] = 1.2;
        owner_adaptor adapt2(data2, size2);

        adapt1 = adapt2;
        EXPECT_EQ(adapt1.size(), adapt2.size());
        EXPECT_EQ(adapt1[0], adapt2[0]);
    }

    TEST(xbuffer_adaptor, owner_move_assign)
    {
        size_t size1 = 100;
        double* data1 = allocator{}.allocate(size1);
        data1[0] = 2.5;
        owner_adaptor adapt1(data1, size1);

        size_t size2 = 200;
        double* data2 = allocator{}.allocate(size2);
        double data2_ref = 1.2;
        data2[0] = data2_ref;
        owner_adaptor adapt2(data2, size2);

        adapt1 = std::move(adapt2);
        EXPECT_EQ(adapt1.size(), size2);
        EXPECT_EQ(adapt1[0], data2_ref);
    }

    class size_check_allocator : public std::allocator<size_t>
    {
    public:

        size_t* allocate(size_t n, const void* hint = 0)
        {
            size_t* res = std::allocator<size_t>::allocate(n, hint);
            // store the size into the result so we can
            // check if the size is correct when we deallocate.
            res[0] = n;
            return res;
        }

        void deallocate(size_t* p, size_t n)
        {
            EXPECT_EQ(p[0], n);
            return std::allocator<size_t>::deallocate(p, n);
        }
    };

    TEST(xbuffer_adaptor, owner_move_assign_check_size)
    {
        size_check_allocator custom_allocator;
        using owner_adaptor = xbuffer_adaptor<size_t*&, acquire_ownership, size_check_allocator>;
        size_t size1 = 100;
        size_t* data1 = custom_allocator.allocate(size1);
        owner_adaptor adapt1(data1, size1);

        size_t size2 = 200;
        size_t* data2 = custom_allocator.allocate(size2);
        owner_adaptor adapt2(data2, size2);

        adapt1 = adapt2;
        EXPECT_EQ(adapt1.size(), size2);
    }

    TEST(xbuffer_adaptor, owner_resize)
    {
        size_t size1 = 100;
        double* data1 = allocator{}.allocate(size1);
        owner_adaptor adapt(data1, size1);

        size_t size2 = 50;
        adapt.resize(size2);

        EXPECT_EQ(adapt.size(), size2);
    }

    TEST(xbuffer_adaptor, owner_iterating)
    {
        size_t size = 100;
        double* data = allocator{}.allocate(size);
        owner_adaptor adapt(data, size);

        std::fill(adapt.begin(), adapt.end(), 1.2);
        EXPECT_EQ(data[0], 1.2);
        EXPECT_EQ(data[size / 2], 1.2);
        EXPECT_EQ(data[size - 1], 1.2);
    }

    TEST(xbuffer_adaptor, no_owner_copy)
    {
        size_t size = 100;
        double* data = new double[size];
        buffer_adaptor adapt1(data, size);

        buffer_adaptor adapt2(adapt1);
        EXPECT_EQ(adapt1.size(), adapt2.size());
        EXPECT_EQ(adapt1.data(), adapt2.data());
        delete[] data;
    }

    TEST(xbuffer_adaptor, no_owner_move)
    {
        size_t size = 100;
        double* data = new double[size];
        buffer_adaptor adapt1(data, size);

        buffer_adaptor adapt2(std::move(adapt1));
        EXPECT_EQ(adapt1.size(), adapt2.size());
        EXPECT_EQ(adapt1.data(), adapt2.data());
        delete[] data;
    }

    TEST(xbuffer_adaptor, no_owner_copy_assign)
    {
        size_t size1 = 100;
        double* data1 = new double[size1];
        buffer_adaptor adapt1(data1, size1);

        size_t size2 = 200;
        double* data2 = new double[size2];
        buffer_adaptor adapt2(data2, size2);

        adapt1 = adapt2;
        EXPECT_EQ(adapt1.size(), adapt2.size());
        EXPECT_EQ(adapt1.data(), adapt2.data());

        delete[] data2;
        delete[] data1;
    }

    TEST(xbuffer_adaptor, no_owner_move_assign)
    {
        size_t size1 = 100;
        double* data1 = new double[size1];
        buffer_adaptor adapt1(data1, size1);

        size_t size2 = 200;
        double* data2 = new double[size2];
        buffer_adaptor adapt2(data2, size2);

        adapt1 = std::move(adapt2);
        EXPECT_EQ(adapt1.size(), adapt2.size());
        EXPECT_EQ(adapt1.data(), adapt2.data());

        delete[] data2;
        delete[] data1;
    }

    TEST(xbuffer_adaptor, no_owner_resize)
    {
        size_t size1 = 100;
        double* data1 = new double[size1];
        buffer_adaptor adapt(data1, size1);

        size_t size2 = 50;
        XT_EXPECT_THROW(adapt.resize(size2), std::runtime_error);
        EXPECT_EQ(adapt.size(), size1);
    }

    TEST(xbuffer_adaptor, no_owner_iterating)
    {
        size_t size = 100;
        double* data = new double[size];
        buffer_adaptor adapt(data, size);

        std::fill(adapt.begin(), adapt.end(), 1.2);
        EXPECT_EQ(data[0], 1.2);
        EXPECT_EQ(data[size / 2], 1.2);
        EXPECT_EQ(data[size - 1], 1.2);

        delete[] data;
    }
}