utest_variadic_vector.cpp 4.35 KB
Newer Older
1 2 3 4
#define VARIADIC_VECTOR_DEBUG

#include "Utils/variadic_container.hpp"

5
#include "inria/checker.hpp"
6 7 8 9 10 11

class utest_variadic_vector {

    using vector_t = variadic_vector<std::allocator<char>, char, int, float, double>;
    using value_t  = typename vector_t::value_type;

12
    inria::checker_t check;
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

    template<std::size_t I, std::size_t J, class Vec>
    static unsigned long get_byte_offset(const Vec& v) {
        return (unsigned long)(((char*)std::get<J>(v._data_tuple))
                               - ((char*)std::get<I>(v._data_tuple)));
    }

    void test_standard_allocation() {
        std::size_t v_cap = 10;

        vector_t v = vector_t();
        v.reserve(v_cap);

        check.equal(v._capacity, v_cap, LOCUS);
        check.equal(get_byte_offset<0,1>(v), v_cap * sizeof(char),  LOCUS);
        check.equal(get_byte_offset<1,2>(v), v_cap * sizeof(int),   LOCUS);
        check.equal(get_byte_offset<2,3>(v), v_cap * sizeof(float), LOCUS);
    };

    void test_constructor() {
        // Test default constructor
        {
            vector_t v = vector_t();

            check.equal(v.size(),     0u, LOCUS);
            check.equal(v.capacity(), 0u, LOCUS);
        }

        // Test constructor, multiple default constructions
        {
            std::size_t v_size = 10;

            vector_t v(v_size);

            check.equal     (v.size(),     v_size, LOCUS);
            check.greater_eq(v.capacity(), v_size, LOCUS);
            for(std::size_t i = 0; i < v_size; ++i) {
                check.equal(v[i], value_t{}, LOCUS);
            }
        }

        // Test constructor from duplicated value
        {
            std::size_t v_size = 5;
            value_t ref{'a', 1, 3.14, 1e-5};

            vector_t v(v_size, ref);

            check.equal     (v.size(),     v_size, LOCUS);
            check.greater_eq(v.capacity(), v_size, LOCUS);
            for(std::size_t i = 0; i < v_size; ++i) {
                check.equal(v[i], ref, LOCUS);
            }
        }

        // Test constructor from range
        {
            std::initializer_list<value_t> l = {{'b',1,3.14,1e-5}, {'c',2,2.71,2e-6}};

            vector_t v(std::begin(l), std::end(l));

            std::size_t i = 0;
            for(auto ref : l) {
                check.equal(v[i], ref, LOCUS);
                ++i;
            }
        }

        // Test copy constructor
        {
            value_t ref{'a', 1, 3.14, 1e-5};
            vector_t v_ref(5, ref);

            vector_t v(v_ref);

            check.equal(v.size(), v_ref.size(), LOCUS);

            for(std::size_t i = 0; i < v.size(); ++i) {
                check.equal(v[i], ref,      LOCUS);
                check.equal(v[i], v_ref[i], LOCUS);
            }

        }
        // Test move constructor
        {
            value_t ref{'a', 1, 3.14, 1e-5};
            vector_t v_source(5, ref);
            auto v_source_data = v_source.data();
            std::size_t v_source_size = v_source.size();
            std::size_t v_source_capacity = v_source.capacity();

            vector_t v(std::move(v_source));

            check.equal(v.data(),     v_source_data, LOCUS);
            check.equal(v.size(),     v_source_size, LOCUS);
            check.equal(v.capacity(), v_source_capacity, LOCUS);

            check.equal(v_source.size(),     0u, LOCUS);
            check.equal(v_source.capacity(), 0u, LOCUS);

            for(std::size_t i = 0; i < v.size(); ++i) {
                check.equal(v[i], ref, LOCUS);
            }
        }
    }


    void test_assignment_operator() {
        // Test copy assignment
        {
            value_t ref{'a', 1, 3.14, 1e-5};
            const vector_t v_source(5, ref);

            vector_t v = vector_t();
            check.equal(v.size(),     0u, LOCUS);
            check.equal(v.capacity(), 0u, LOCUS);

            v = v_source;

            check.different(v.data(), v_source.data(), LOCUS);
            check.equal(v.size(),     v_source.size(), LOCUS);
            check.equal(v.capacity(), v_source.capacity(), LOCUS);

            for(std::size_t i = 0; i < v.size(); ++i) {
                check.equal(v[i], ref, LOCUS);
            }
        }
    }

public:
    bool run() {
        test_standard_allocation();
        test_constructor();

        check.print_summary();
        return check.ok();
    }
};

int main() {
    return utest_variadic_vector{}.run() ? EXIT_SUCCESS : EXIT_FAILURE;
}