stable_vector.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
#include <felspar/memory/stable_vector.hpp>
#include <felspar/test.hpp>


namespace {


    auto const suite = felspar::testsuite("stable_vector");


    auto const construction = suite.test("construction", [](auto check) {
        felspar::memory::stable_vector<int, 8> sv1, sv2(20, 123);
        check(sv1.empty()) == true;
        check(sv2.empty()) == false;
        check(sv1.size()) == 0u;
        check(sv2.size()) == 20u;
        check(sv1.capacity()) == 0u;
        check(sv2.capacity()) == 24u;
        for (std::size_t index{}; index < 20; ++index) {
            check(sv2[index]) == 123;
        }
    });


    auto const mutation = suite.test("mutation", [](auto check) {
        felspar::memory::stable_vector<int, 8> sv;
        for (int index{}; index < 20; ++index) {
            sv.push_back(index);
            check(sv[index]) == index;
        }
        check(sv.capacity()) == 24u;
    });


    auto const clear = suite.test("clear", [](auto check) {
        felspar::memory::stable_vector<int, 8> sv(20, 123);
        sv.clear();
        check(sv.capacity()) == 24u;
        check(sv.empty()) == true;
        for (int index{}; index < 10; ++index) {
            sv.push_back(index);
            check(sv[index]) == index;
        }
        check(sv.size()) == 10u;
        check(sv.capacity()) == 24u;
    });


    auto const reserve = suite.test("reserve", [](auto check) {
        felspar::memory::stable_vector<int, 8> sv;
        sv.reserve(0);
        check(sv.capacity()) == 0u;
        sv.reserve(1);
        check(sv.capacity()) == 8u;
        sv.reserve(20);
        check(sv.capacity()) == 24u;
    });


}