File: small_vector_test.cpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (405 lines) | stat: -rw-r--r-- 11,473 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------

#include <gtest/gtest.h>

#include <string>
#include <utility>

#include <seqan3/range/container/concept.hpp>
#include <seqan3/range/container/small_vector.hpp>
#include <seqan3/std/ranges>
#include <seqan3/test/cereal.hpp>

#include "container_test_template.hpp"

using small_vector_over_dna4_t = seqan3::small_vector<seqan3::dna4, 1000>;
INSTANTIATE_TYPED_TEST_SUITE_P(small_vector, container_over_dna4_test, small_vector_over_dna4_t, );

// standard construction.
TEST(small_vector, standard_construction)
{
    EXPECT_TRUE((std::is_default_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_nothrow_default_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_copy_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_trivially_copy_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_nothrow_copy_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_move_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_trivially_move_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_nothrow_move_constructible_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_copy_assignable_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_trivially_copy_assignable_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_nothrow_copy_assignable_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_move_assignable_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_trivially_move_assignable_v<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::is_nothrow_move_assignable_v<seqan3::small_vector<char, 4>>));
}

TEST(small_vector, concepts)
{
    EXPECT_TRUE((seqan3::reservible_container<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::ranges::random_access_range<seqan3::small_vector<char, 4>>));
    EXPECT_TRUE((std::ranges::contiguous_range<seqan3::small_vector<char, 4>>));
}

TEST(small_vector, construct_from_array)
{
    // Deduce value type and N
    EXPECT_TRUE((std::same_as<decltype(seqan3::small_vector{std::array{'h','e','l','l','o'}}),
                 seqan3::small_vector<char, 5>>));

    // construct from different sized array (size has to be smaller)
    EXPECT_TRUE((std::same_as<decltype(seqan3::small_vector<char, 10>{std::array{'h','e','l','l','o'}}),
                 seqan3::small_vector<char, 10>>));
}

TEST(small_vector, construct_from_built_in_array)
{
    // Deduce value type and N
    int arr[3] = {1, 2, 3};
    EXPECT_TRUE((std::same_as<decltype(seqan3::small_vector{arr}), seqan3::small_vector<int, 3>>));

    // Deduce value type and N
    EXPECT_TRUE((std::same_as<decltype(seqan3::small_vector<int, 5>{arr}), seqan3::small_vector<int, 5>>));

    // char const *
    EXPECT_TRUE((std::same_as<decltype(seqan3::small_vector{"hi"}), seqan3::small_vector<char, 3>>));

    // parameter pack
    EXPECT_TRUE((std::same_as<decltype(seqan3::small_vector<char, 3>{'A', 'C', 'X'}), seqan3::small_vector<char, 3>>));
}

// ---------------------------------------------------------------------------------------------------------------------
// constexpr tests
// ---------------------------------------------------------------------------------------------------------------------
constexpr bool comparison_test()
{
    seqan3::small_vector<char, 20> t1{'A', 'C', 'C', 'G', 'T'};
    seqan3::small_vector<char, 20> t2{'A', 'C', 'C', 'G', 'T'};
    seqan3::small_vector<char, 2> t3{'A', 'C'};
    seqan3::small_vector<char, 20> t4{'A', 'G', 'C', 'G', 'T'};

    bool res = t1 == t2;
    res = res && (t1 <= t2);
    res = res && (t1 >= t2);
    res = res && (t1 != t3);

    res = res && (t3 <  t1);
    res = res && (t3 <= t1);
    res = res && (t1 <  t4);
    res = res && (t1 <= t4);

    res = res && (t1 >  t3);
    res = res && (t1 >= t3);
    res = res && (t4 >  t1);
    res = res && (t4 >= t1);

    return true;
}


TEST(small_vector, comparison)
{
    constexpr bool b = comparison_test();
    EXPECT_TRUE(b);
}

constexpr bool begin_end_test()
{
    std::array src{'h','e','l','l','o'};
    seqan3::small_vector vec{src};

    auto it_s = src.begin();
    auto it_v = vec.begin();

    for (; it_v != vec.end(); ++it_s, ++it_v)
        if (*it_v != *it_s)
            return false;

    return true;
}

constexpr bool cbegin_cend_test()
{
    std::array src{'h','e','l','l','o'};
    seqan3::small_vector vec{src};

    auto it_s = src.cbegin();
    auto it_v = vec.cbegin();

    for (; it_v != vec.cend(); ++it_s, ++it_v)
        if (*it_v != *it_s)
            return false;

    return true;
}

TEST(small_vector, iterator)
{
    constexpr bool b_const = begin_end_test();
    EXPECT_TRUE(b_const);

    constexpr bool cb_const = cbegin_cend_test();
    EXPECT_TRUE(cb_const);
}

TEST(small_string, size_and_maxsize)
{
    // auto deduction -> capacity == size
    {
        constexpr seqan3::small_vector vec{"hello"};
        constexpr auto size = vec.size();
        constexpr auto msize = vec.max_size();

        EXPECT_EQ(size, 6u); // incl. null character
        EXPECT_EQ(msize, 6u);
    }

    // capacity != size
    {
        constexpr seqan3::small_vector<char, 10> vec{'h','e','l','l','o'};
        constexpr auto size = vec.size();
        constexpr auto msize = vec.max_size();

        EXPECT_EQ(size, 5u);
        EXPECT_EQ(msize, 10u);
    }
}

constexpr bool swap_test()
{
    seqan3::small_vector<char, 20> t0{};
    seqan3::small_vector<char, 20> t1{"AC"};

    t0.swap(t1);
    return (t0 == seqan3::small_vector<char, 20>{"AC"}) && (t1 == seqan3::small_vector<char, 20>{});
}

TEST(small_vector, swap)
{
    constexpr bool res = swap_test();
    EXPECT_TRUE(res);
}

constexpr bool assign_test()
{
    seqan3::small_vector<char, 20> t0{'C', 'C'};
    seqan3::small_vector<char, 20> t1{'A', 'C', 'C', 'G', 'T'};

    // n * value
    seqan3::small_vector<char, 20> t3;
    t3.assign(2, 'C');
    bool res = (t3 == t0);

    // from another container's sub-range
    seqan3::small_vector<char, 20> t4;
    t4.assign(t1.cbegin(), t1.cend());
    res = res && (t4 == t1);

    // initializer list
    seqan3::small_vector<char, 20> t5, t6;
    t5.assign({'A', 'C', 'C', 'G', 'T'});
    t6 = {'A', 'C', 'C', 'G', 'T'};
    res = res && (t5 == t1);
    res = res && (t6 == t1);

    // direct from another container
    seqan3::small_vector<char, 20> t7;
    t7.assign(std::array<char, 5>{'A', 'C', 'C', 'G', 'T'});
    res = res && (t7 == t1);

    return res;
}

TEST(small_vector, assign)
{
    constexpr bool res = assign_test();
    EXPECT_TRUE(res);
}

constexpr bool element_access_test()
{
    seqan3::small_vector<char, 20> t1{'A', 'C', 'C', 'G', 'T'};
    seqan3::small_vector<char, 20> const t2{'A', 'C', 'C', 'G', 'T'};

    // at() cannot be constexpr because it throws

    // []
    bool res = (t1[0] == 'A');
    res = res && (t2[0] == 'A');
    // front
    res = res && (t1.front() == 'A');
    res = res && (t2.front() == 'A');

    // back
    res = res && (t1.back() == 'T');
    res = res && (t2.back() == 'T');

    // mutability
    t1[0] = 'T';
    res = res && (t1 == seqan3::small_vector<char, 20>{'T', 'C', 'C', 'G', 'T'});
    res = res && !(t1 == seqan3::small_vector<char, 20>{'T', 'C', 'C'}); // for code coverage

    t1.front() = 'C';
    res = res && (t1 == seqan3::small_vector<char, 20>{'C', 'C', 'C', 'G', 'T'});

    t1.back() = 'G';
    res = res && (t1 == seqan3::small_vector<char, 20>{'C', 'C', 'C', 'G', 'G'});

    // data()
    res = res && (*t1.data() == 'C');
    res = res && (*t2.data() == 'A');

    return res;
}

TEST(small_vector, element_access)
{
    constexpr bool res = element_access_test();
    EXPECT_TRUE(res);
}

constexpr bool clear_test()
{
    seqan3::small_vector<char, 20> t0{};
    seqan3::small_vector<char, 20> t1{'A', 'C', 'C', 'G', 'T'};

    t1.clear();

    return t0 == t1;
}

TEST(small_vector, clear)
{
    constexpr bool res = clear_test();
    EXPECT_TRUE(res);
}

constexpr bool insert_test()
{
    seqan3::small_vector<char, 20> t0{};
    seqan3::small_vector<char, 20> t1{'A', 'C', 'C', 'G', 'T'};

    // position, value
    t0.insert(t0.cend(), 'A');
    t0.insert(t0.cend(), 'C');
    t0.insert(t0.cend(), 'G');
    t0.insert(t0.cend(), 'T');
    t0.insert(t0.cbegin() + 1, 'C');
    bool res = (t0 == t1);

    // position, n times values
    t0.clear();
    t0.insert(t0.cend(), 2, 'C');
    t0.insert(t0.cend(), 1, 'G');
    t0.insert(t0.cend(), 1, 'T');
    t0.insert(t0.cbegin(), 1, 'A');
    res = res && (t0 == t1);

    // iterator pair
    t0.clear();
    t0.insert(t0.cend(), t1.begin() + 1, t1.begin() + 3);

    t0.insert(t0.cend(),   t1.cend() - 2, t1.cend());
    t0.insert(t0.cbegin(), t1.cbegin(), t1.cbegin() + 1);
    res = res && (t0 == t1);

    // initializer list
    t0.clear();
    t0.insert(t0.cend(), {'A', 'C', 'G', 'T'});
    t0.insert(t0.cbegin() + 1, 'C');
    res = res && (t0 == t1);

    return res;
}

TEST(small_vector, insert)
{
    constexpr bool res = insert_test();
    EXPECT_TRUE(res);
}

constexpr bool erase_test()
{
    seqan3::small_vector<char, 20> t1{'A', 'C', 'C', 'G', 'T'};

    // one element
    t1.erase(t1.begin());
    bool res = (t1 == (seqan3::small_vector<char, 20>{'C', 'C', 'G', 'T'}));

    // range
    t1.erase(t1.begin() + 1, t1.begin() + 3);
    res = res && (t1 == (seqan3::small_vector<char, 20>{'C', 'T'}));

    return res;
}

TEST(small_vector, erase)
{
    constexpr bool res = erase_test();
    EXPECT_TRUE(res);
}

constexpr bool push_pop_test()
{
    seqan3::small_vector<char, 20> t0{};

    // push_back
    t0.push_back('A');
    bool res = (t0 ==  (seqan3::small_vector<char, 20>{'A'}));
    t0.push_back('C');
    res = res && (t0 == (seqan3::small_vector<char, 20>{'A', 'C'}));

    // pop_back
    t0.pop_back();
    res = res && (t0 == (seqan3::small_vector<char, 20>{'A'}));
    t0.pop_back();
    res = res && (t0 == (seqan3::small_vector<char, 20>{}));

    return res;
}

TEST(small_vector, push_pop)
{
    constexpr bool res = push_pop_test();
    EXPECT_TRUE(res);
}

constexpr bool resize_test()
{
    seqan3::small_vector<int, 20> t0{};

    // enlarge without values
    t0.resize(3);
    bool res = (t0 == (seqan3::small_vector<int, 20>{0, 0, 0}));

    // enlarge with value
    t0.resize(5, 11);
    res = res && (t0 == (seqan3::small_vector<int, 20>{0, 0, 0, 11, 11}));

    // shrink with value (no effect)
    t0.resize(4, 500);
    res = res && (t0 == (seqan3::small_vector<int, 20>{0, 0, 0, 11}));

    // shrink without value
    t0.resize(2);
    res = res && (t0 == (seqan3::small_vector<int, 20>{0, 0}));

    return res;
}

TEST(small_vector, resize)
{
    constexpr bool res = resize_test();
    EXPECT_TRUE(res);
}

TEST(small_vector, serialisation)
{
    seqan3::small_vector hello{std::array{'h','e','l','l','o'}};
    seqan3::test::do_serialisation(hello);
}