cstl-sys 2.4.0

Low level CSTL Rust bindings generated with bindgen
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <gtest/gtest.h>

#include <algorithm>
#include <cstdint>
#include <memory>
#include <vector>

#include "alloc.h"
#include "type.h"
#include "vector.h"

struct TestInt {
    TestInt() : value{new uint32_t{0xC0FFEE}} {}

    TestInt(uint32_t value) : value{new uint32_t{value}} {}

    TestInt(const TestInt& other) : value{new uint32_t{*other.value}} {}

    TestInt(TestInt&& other) noexcept : value{new uint32_t{*other.value}} {
        *other.value = 0xC0FFEE;
    }

    ~TestInt() {
        delete value;
    }

    TestInt& operator=(const TestInt& other) {
        delete value;
        value = new uint32_t{*other.value};
        return *this;
    }

    TestInt& operator=(TestInt&& other) noexcept {
        delete value;
        value = new uint32_t{*other.value};
        *other.value = 0xC0FFEE;
        return *this;
    }

    friend bool operator==(const TestInt& lhs, const TestInt& rhs) {
        return *lhs.value == *rhs.value;
    }

    uint32_t* value;
};

void destroy_testint(void* _first, void* _last) {
    auto first = reinterpret_cast<TestInt*>(_first);
    auto last  = reinterpret_cast<TestInt*>(_last);

    ASSERT_EQ(0, (uintptr_t)first % alignof(TestInt))
        << "`first` alignment not upheld";

    ASSERT_EQ(0, (uintptr_t)last % alignof(TestInt))
        << "`last` alignment not upheld";

    std::destroy(first, last);
}

void move_testint(void* _first, void* _last, void* _dest) {
    auto first = reinterpret_cast<TestInt*>(_first);
    auto last  = reinterpret_cast<TestInt*>(_last);
    auto dest  = reinterpret_cast<TestInt*>(_dest);

    ASSERT_EQ(0, (uintptr_t)first % alignof(TestInt))
        << "`first` alignment not upheld";

    ASSERT_EQ(0, (uintptr_t)last % alignof(TestInt))
        << "`last` alignment not upheld";

    ASSERT_EQ(0, (uintptr_t)dest % alignof(TestInt))
        << "`dest` alignment not upheld";;

    std::uninitialized_move(first, last, dest);
}

void copy_testint(const void* _first, const void* _last, void* _dest) {
    auto first = reinterpret_cast<const TestInt*>(_first);
    auto last  = reinterpret_cast<const TestInt*>(_last);
    auto dest  = reinterpret_cast<TestInt*>(_dest);

    ASSERT_EQ(0, (uintptr_t)first % alignof(TestInt))
        << "`first` alignment not upheld";

    ASSERT_EQ(0, (uintptr_t)last % alignof(TestInt))
        << "`last` alignment not upheld";

    ASSERT_EQ(0, (uintptr_t)dest % alignof(TestInt))
        << "`dest` alignment not upheld";;

    std::uninitialized_copy(first, last, dest);
}

void fill_testint(void* _first, void* _last, const void* _value) {
    const auto& value = *reinterpret_cast<const TestInt*>(_value);

    auto first = reinterpret_cast<TestInt*>(_first);
    auto last  = reinterpret_cast<TestInt*>(_last);

    ASSERT_EQ(0, (uintptr_t)first % alignof(TestInt))
        << "`first` alignment not upheld";

    std::uninitialized_fill(first, last, value);
}

class VectorTest : public testing::Test {
protected:
    VectorTest() : real_int{0xDEADBEEF}, cstl_vec{}, copy{},
        cstl_int{reinterpret_cast<const void*>(&real_int.value)},
        alloc{nullptr}, type{} {}

    ~VectorTest() {
        CSTL_vector_destroy(&cstl_vec, type, &copy.move_type.drop_type, alloc);
    }

    void SetUp() override {
        type = CSTL_define_type(sizeof(TestInt), alignof(TestInt));

        ASSERT_NE(nullptr, type);

        copy = {
            {
                {
                    &destroy_testint
                },
                &move_testint
            },
            &copy_testint,
            &fill_testint
        };

        CSTL_vector_construct(&cstl_vec);
    }

    void vector_expect_size(size_t size) {
        EXPECT_EQ(size, CSTL_vector_size(&cstl_vec, type))
            << "size of vector must be equal to " << size;
    
        CSTL_VectorIter first = CSTL_vector_begin(&cstl_vec, type);
        CSTL_VectorIter last  = CSTL_vector_end(&cstl_vec, type);
    
        EXPECT_EQ(size, CSTL_vector_iterator_distance(first, last))
            << "`[first, last)` must span exactly " << size << " elements";
    }

    void vector_assert_equal() {
        for (size_t i = 0; i < real_vec.size(); ++i) {
            ASSERT_LT(i, CSTL_vector_size(&cstl_vec, type))
                << "vector should be bigger; i=" << i;
            
            TestInt left  = real_vec[i];
            TestInt right = *(TestInt*)CSTL_vector_index(&cstl_vec, type, i);

            EXPECT_EQ(left, right) << "must equal if correctly reallocated; i=" << i;
        }
    }

    // Genuine STL vector:
    std::vector<TestInt> real_vec;
    const TestInt real_int;

    // CSTL vector:
    CSTL_VectorVal cstl_vec;
    CSTL_CopyType copy;
    const void* cstl_int;
    CSTL_Alloc* alloc;
    CSTL_Type type;
};

TEST_F(VectorTest, Default) {
    EXPECT_LT(0, CSTL_vector_max_size(type))
        << "max vector size must be greater than 0";

    vector_expect_size(0);
}

TEST_F(VectorTest, PushTen) {
    for (int i = 0; i < 10; ++i) {
        ASSERT_TRUE(CSTL_vector_copy_push_back(&cstl_vec, type, &copy, cstl_int, alloc))
            << "must return true on success";

        real_vec.push_back(real_int);
    }

    vector_expect_size(10);
    vector_assert_equal();
}

TEST_F(VectorTest, AssignCopies) {
    real_vec.assign(5, real_int);
    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(5);
    vector_assert_equal();

    // Will cause a reallocation:
    real_vec.assign(12, real_int);
    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 12, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(12);
    vector_assert_equal();

    // Will reuse storage and resize:
    real_vec.assign(7, real_int);
    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 7, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(7);
    vector_assert_equal();

    // Must fail and do nothing (too many elements):
    EXPECT_FALSE(CSTL_vector_assign_n(&cstl_vec, type, &copy, SIZE_MAX, cstl_int, alloc))
        << "must fail due to exceeding `CSTL_vector_max_size(&cstl_vec, type)`";

    vector_expect_size(7);
    vector_assert_equal();

    // Will destroy all elements:
    real_vec.assign(0, real_int);
    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 0, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(0);
}

TEST_F(VectorTest, AssignRange) {
    real_vec.assign(5, real_int);
    EXPECT_TRUE(CSTL_vector_copy_assign_range(&cstl_vec, type, &copy,
        (const void*)real_vec.data(), (const void*)(real_vec.data() + real_vec.size()), alloc))
        << "must return true on success";

    vector_expect_size(5);
    vector_assert_equal();

    // Will cause a reallocation:
    real_vec.assign(12, real_int);
    EXPECT_TRUE(CSTL_vector_copy_assign_range(&cstl_vec, type, &copy,
        (const void*)real_vec.data(), (const void*)(real_vec.data() + real_vec.size()), alloc))
        << "must return true on success";

    vector_expect_size(12);
    vector_assert_equal();

    // Will reuse storage and resize:
    real_vec.assign(7, real_int);
    EXPECT_TRUE(CSTL_vector_copy_assign_range(&cstl_vec, type, &copy,
        (const void*)real_vec.data(), (const void*)(real_vec.data() + real_vec.size()), alloc))
        << "must return true on success";

    vector_expect_size(7);
    vector_assert_equal();

    // Will destroy all elements:
    real_vec.assign(0, real_int);
    EXPECT_TRUE(CSTL_vector_copy_assign_range(&cstl_vec, type, &copy,
        (const void*)real_vec.data(), (const void*)(real_vec.data() + real_vec.size()), alloc))
        << "must return true on success";

    vector_expect_size(0);
}

TEST_F(VectorTest, VectorCopy) {
    CSTL_VectorVal other_vec;
    CSTL_vector_construct(&other_vec);

    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    EXPECT_TRUE(CSTL_vector_assign_n(&other_vec, type, &copy, 7, cstl_int, alloc))
        << "must return true on success";

    // Will expand storage.
    EXPECT_TRUE(CSTL_vector_copy_assign(&cstl_vec, type, &copy, &other_vec, alloc, alloc, false))
        << "must return true on success";

    EXPECT_EQ(CSTL_vector_size(&cstl_vec, type), CSTL_vector_size(&other_vec, type))
        << "vectors should have equal size after being copied";

    EXPECT_TRUE(CSTL_vector_assign_n(&other_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    // Will reuse storage.
    EXPECT_TRUE(CSTL_vector_copy_assign(&cstl_vec, type, &copy, &other_vec, alloc, alloc, false))
        << "must return true on success";

    EXPECT_EQ(CSTL_vector_size(&cstl_vec, type), CSTL_vector_size(&other_vec, type))
        << "vectors should have equal size after being copied";

    CSTL_vector_destroy(&other_vec, type, &copy.move_type.drop_type, alloc);
}

TEST_F(VectorTest, VectorMove) {
    CSTL_VectorVal other_vec;
    CSTL_vector_construct(&other_vec);

    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    EXPECT_TRUE(CSTL_vector_assign_n(&other_vec, type, &copy, 7, cstl_int, alloc))
        << "must return true on success";

    // Will expand storage.
    EXPECT_TRUE(CSTL_vector_move_assign(&cstl_vec, type, &copy.move_type, &other_vec, alloc, alloc, false))
        << "must return true on success";
    size_t left_size = CSTL_vector_size(&cstl_vec, type);
    EXPECT_TRUE(CSTL_vector_move_assign(&other_vec, type, &copy.move_type, &cstl_vec, alloc, alloc, false))
        << "must return true on success";
    size_t right_size = CSTL_vector_size(&other_vec, type);

    EXPECT_EQ(left_size, right_size) << "moving should keep size intact";

    EXPECT_TRUE(CSTL_vector_assign_n(&other_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    // Will reuse storage.
    EXPECT_TRUE(CSTL_vector_move_assign(&cstl_vec, type, &copy.move_type, &other_vec, alloc, alloc, false))
        << "must return true on success";
    left_size = CSTL_vector_size(&cstl_vec, type);
    EXPECT_TRUE(CSTL_vector_move_assign(&other_vec, type, &copy.move_type, &cstl_vec, alloc, alloc, false))
        << "must return true on success";
    right_size = CSTL_vector_size(&other_vec, type);

    EXPECT_EQ(left_size, right_size) << "moving should keep size intact";

    CSTL_vector_destroy(&other_vec, type, &copy.move_type.drop_type, alloc);
}

TEST_F(VectorTest, CheckedIndex) {
    for (int i = 0; i < 10; ++i) {
        ASSERT_EQ(nullptr, CSTL_vector_const_at(&cstl_vec, type, i))
            << "the end of the vector must not have accesible elements";

        ASSERT_TRUE(CSTL_vector_copy_push_back(&cstl_vec, type, &copy, cstl_int, alloc))
            << "must return true on success";

        real_vec.push_back(real_int);
    }

    vector_expect_size(10);
    vector_assert_equal();

    for (int i = 0; i < 10; ++i) {
        ASSERT_NE(nullptr, CSTL_vector_const_at(&cstl_vec, type, i))
            << "all vector elements must be accesible";
    }
}

TEST_F(VectorTest, FrontAndBack) {
    ASSERT_TRUE(CSTL_vector_copy_push_back(&cstl_vec, type, &copy, cstl_int, alloc))
        << "must return true on success";

    ASSERT_EQ(CSTL_vector_const_front(&cstl_vec), CSTL_vector_const_back(&cstl_vec, type))
        << "must equal when there is only one element";

    ASSERT_TRUE(CSTL_vector_copy_push_back(&cstl_vec, type, &copy, cstl_int, alloc))
        << "must return true on success";

    ASSERT_NE(CSTL_vector_const_front(&cstl_vec), CSTL_vector_const_back(&cstl_vec, type))
        << "must equal when there is only one element";
}

TEST_F(VectorTest, DataRange) {
    for (int i = 0; i < 10; ++i) {
        // The range `[data(), data() + size)` must be valid.
        const TestInt* first = (const TestInt*)CSTL_vector_data(&cstl_vec);
        const TestInt* last  = first + CSTL_vector_size(&cstl_vec, type);

        real_vec.assign(first, last);

        ASSERT_TRUE(CSTL_vector_copy_push_back(&cstl_vec, type, &copy, cstl_int, alloc))
            << "must return true on success";
    }

    vector_expect_size(10);
    vector_assert_equal();
}

TEST_F(VectorTest, Reserve) {
    CSTL_vector_reserve(&cstl_vec, type, &copy.move_type, 10, alloc);

    CSTL_VectorIter first = CSTL_vector_begin(&cstl_vec, type);

    for (int i = 0; i < 10; ++i) {
        ASSERT_TRUE(CSTL_vector_copy_push_back(&cstl_vec, type, &copy, cstl_int, alloc))
            << "must return true on success";

        ASSERT_TRUE(CSTL_vector_iterator_eq(first, CSTL_vector_begin(&cstl_vec, type)))
            << "not exceeding reserved capacity should not invalidate iterators";
    }
}

TEST_F(VectorTest, Clear) {
    real_vec.assign(5, real_int);
    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    real_vec.clear();
    CSTL_vector_clear(&cstl_vec, &copy.move_type.drop_type);

    vector_expect_size(0);
    vector_assert_equal();
}

TEST_F(VectorTest, EmplaceInsert) {
    real_vec.assign(10, real_int);

    for (int i = 0; i < 3; ++i) {
        CSTL_VectorIter first = CSTL_vector_begin(&cstl_vec, type);
        CSTL_VectorIter pos   = CSTL_vector_copy_insert(&cstl_vec, &copy, first, cstl_int, alloc);
        ASSERT_FALSE(CSTL_vector_iterator_eq(pos, CSTL_vector_end(&cstl_vec, type)))
            << "emplace must return a dereferenceable iterator";
    }

    for (int i = 0; i < 3; ++i) {
        CSTL_VectorIter last  = CSTL_vector_end(&cstl_vec, type);
        CSTL_VectorIter pos   = CSTL_vector_copy_insert(&cstl_vec, &copy, last, cstl_int, alloc);
        ASSERT_FALSE(CSTL_vector_iterator_eq(pos, CSTL_vector_end(&cstl_vec, type)))
            << "emplace must return a dereferenceable iterator";
    }

    CSTL_VectorIter first = CSTL_vector_begin(&cstl_vec, type);
    CSTL_VectorIter last  = CSTL_vector_end(&cstl_vec, type);
    CSTL_VectorIter mid   = CSTL_vector_iterator_add(first, CSTL_vector_iterator_distance(first, last) / 2);
    CSTL_VectorIter pos   = CSTL_vector_insert_n(&cstl_vec, &copy, mid, 4, cstl_int, alloc);
    
    ASSERT_FALSE(CSTL_vector_iterator_eq(pos, CSTL_vector_end(&cstl_vec, type)))
        << "insert must return a dereferenceable iterator";

    vector_expect_size(10);
    vector_assert_equal();
}

TEST_F(VectorTest, Erase) {
    real_vec.assign(5, real_int);

    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(5);
    vector_assert_equal();

    // Erase at front:
    real_vec.erase(real_vec.begin());
    CSTL_VectorIter first = CSTL_vector_begin(&cstl_vec, type);
    CSTL_VectorIter pos   = CSTL_vector_erase(&cstl_vec, &copy.move_type, first);
    ASSERT_TRUE(CSTL_vector_iterator_eq(pos, CSTL_vector_begin(&cstl_vec, type)))
        << "erasing the first element should return a compatible iterator";

    vector_expect_size(4);
    vector_assert_equal();

    // Erase at back:
    real_vec.erase(real_vec.end() - 1);
    CSTL_VectorIter last = CSTL_vector_iterator_sub(CSTL_vector_end(&cstl_vec, type), 1);
    CSTL_VectorIter end  = CSTL_vector_erase(&cstl_vec, &copy.move_type, last);
    ASSERT_TRUE(CSTL_vector_iterator_eq(end, CSTL_vector_end(&cstl_vec, type)))
        << "erasing the last element should return a past the end iterator";

    vector_expect_size(3);
    vector_assert_equal();
}

TEST_F(VectorTest, EraseRange) {
    real_vec.assign(5, real_int);

    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(5);
    vector_assert_equal();

    // Erase at `[1, 4)`:
    real_vec.erase(real_vec.begin() + 1, real_vec.end() - 1);
    CSTL_VectorIter first = CSTL_vector_iterator_add(CSTL_vector_begin(&cstl_vec, type), 1);
    CSTL_VectorIter last  = CSTL_vector_iterator_sub(CSTL_vector_end(&cstl_vec, type), 1);
    CSTL_vector_erase_range(&cstl_vec, &copy.move_type, first, last);

    vector_expect_size(2);
    vector_assert_equal();
}

TEST_F(VectorTest, Resize) {
    real_vec.assign(5, real_int);

    EXPECT_TRUE(CSTL_vector_assign_n(&cstl_vec, type, &copy, 5, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(5);
    vector_assert_equal();

    // Downsize:
    real_vec.resize(3, real_int);
    EXPECT_TRUE(CSTL_vector_resize(&cstl_vec, type, &copy, 3, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(3);
    vector_assert_equal();

    // Upsize:
    real_vec.resize(10, real_int);
    EXPECT_TRUE(CSTL_vector_resize(&cstl_vec, type, &copy, 10, cstl_int, alloc))
        << "must return true on success";

    vector_expect_size(10);
    vector_assert_equal();
}