File: test_xoptional_assembly_storage.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 (199 lines) | stat: -rw-r--r-- 6,078 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
/***************************************************************************
 * 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/xio.hpp"
#include "xtensor/xoptional_assembly_storage.hpp"
#include "xtensor/xstorage.hpp"

#include "test_common_macros.hpp"

namespace xt
{
    using storage_type = uvector<int>;
    using flag_storage_type = uvector<bool>;

    TEST(xoptional_assembly_storage, constructor)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);
    }

    TEST(xoptional_assembly_storage, empty)
    {
        storage_type v1 = {};
        flag_storage_type f1 = {};
        auto stor1 = optional_assembly_storage(v1, f1);
        ASSERT_EQ(v1.empty(), stor1.empty());
    }

    TEST(xoptional_assembly_storage, size)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);
        ASSERT_EQ(stor.size(), std::size_t(4));
    }

    TEST(xoptional_assembly_storage, resize)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);
        ASSERT_EQ(stor.size(), std::size_t(4));
        stor.resize(std::size_t(5));
        ASSERT_EQ(stor.size(), std::size_t(5));
        stor.resize(std::size_t(2));
        ASSERT_EQ(stor.size(), std::size_t(2));
    }

    TEST(xoptional_assembly_storage, access)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);

        ASSERT_EQ(stor[0], xtl::missing<int>());
        stor[0].has_value() = true;
        ASSERT_EQ(stor[0], 56);
        ASSERT_EQ(stor[1], 2);
        stor[1] = 123;
        ASSERT_EQ(stor[1], 123);
        ASSERT_EQ(stor[2], 3);
    }

    TEST(xoptional_assembly_storage, front)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);
        ASSERT_EQ(stor[0], stor.front());
    }

    TEST(xoptional_assembly_storage, back)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);
        ASSERT_EQ(stor[3], stor.back());
    }

    TEST(xoptional_assembly_storage, iterator)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);

        auto it = stor.begin();
        auto it2 = stor.begin();
        it++;
        ASSERT_EQ(*it, 2);
        it->has_value() = false;
        ASSERT_EQ(*it, xtl::missing<int>());
        it += 2;
        ASSERT_EQ(*it, 5);
        it -= 2;
        ASSERT_EQ(*it, xtl::missing<int>());
        ASSERT_FALSE(it == stor.end());
        it += 3;
        ASSERT_TRUE(it == stor.end());
        it--;
        ASSERT_EQ(*it, 5);
        ASSERT_TRUE(it2 < it);
    }

    TEST(xoptional_assembly_storage, const_iterator)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);

        auto it = stor.cbegin();
        auto it2 = stor.cbegin();
        it++;
        ASSERT_EQ(*it, 2);
        it += 2;
        ASSERT_EQ(*it, 5);
        it -= 2;
        ASSERT_EQ(*it, 2);
        ASSERT_FALSE(it == stor.cend());
        it += 3;
        ASSERT_TRUE(it == stor.cend());
        it--;
        ASSERT_EQ(*it, 5);
        ASSERT_TRUE(it2 < it);
    }

    TEST(xoptional_assembly_storage, reverse_iterator)
    {
        storage_type v = {56, 2, 3, 5};
        flag_storage_type f = {false, true, true, true};
        auto stor = optional_assembly_storage(v, f);

        auto it = stor.rbegin();
        auto it2 = stor.rbegin();
        it++;
        ASSERT_EQ(*it, 3);
        it += 2;
        ASSERT_EQ(*it, xtl::missing<int>());
        it -= 2;
        ASSERT_EQ(*it, 3);
        ASSERT_FALSE(it == stor.rend());
        it += 3;
        ASSERT_TRUE(it == stor.rend());
        it--;
        ASSERT_EQ(*it, xtl::missing<int>());
        ASSERT_TRUE(it2 < it);
    }

    TEST(xoptional_assembly_storage, swap)
    {
        storage_type v1 = {56, 2, 3, 5};
        flag_storage_type f1 = {false, true, true, true};
        auto stor1 = optional_assembly_storage(v1, f1);

        storage_type v2 = {};
        flag_storage_type f2 = {};
        auto stor2 = optional_assembly_storage(v2, f2);

        stor2.swap(stor1);
        ASSERT_EQ(stor1.size(), size_t(0));
        ASSERT_EQ(stor2.size(), size_t(4));
    }

    TEST(xoptional_assembly_storage, operators)
    {
        storage_type v1 = {56, 2, 3, 5};
        flag_storage_type f1 = {false, true, true, true};
        auto stor1 = optional_assembly_storage(v1, f1);

        storage_type v2 = {56, 2, 3, 6};
        flag_storage_type f2 = {false, true, true, true};
        auto stor2 = optional_assembly_storage(v2, f2);

        ASSERT_TRUE(v1 == v1);
        ASSERT_FALSE(v1 != v1);
        ASSERT_TRUE(v1 != v2);
        ASSERT_FALSE(v1 == v2);

        ASSERT_FALSE(v1 < v1);
        ASSERT_TRUE(v1 < v2);
        ASSERT_TRUE(v1 <= v1);
        ASSERT_TRUE(v1 <= v2);

        ASSERT_FALSE(v1 > v1);
        ASSERT_TRUE(v2 > v1);
        ASSERT_TRUE(v1 >= v1);
        ASSERT_TRUE(v2 >= v1);

        swap(v1, v2);
        ASSERT_EQ(v1[3], 6);
        ASSERT_EQ(v2[3], 5);
    }
}