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


namespace {


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


    struct counters {
        std::size_t construct = {}, assign = {}, moved = {}, copied = {},
                    destruct = {};
    } count;
    struct counted {
        counted() { ++count.construct; }
        counted(counted const &) {
            ++count.construct;
            ++count.copied;
        }
        counted(counted &&) {
            ++count.construct;
            ++count.moved;
        }
        ~counted() { ++count.destruct; }

        counted &operator=(counted const &) {
            ++count.assign;
            return *this;
        }
        counted &operator=(counted &&) {
            ++count.moved;
            return *this;
        }
    };


    auto const cons = suite.test("construct & destroy", [](auto check) {
        count = {};
        felspar::memory::raw_memory<counted> c;
        check(count.construct) == 0u;
        check(count.copied) == 0u;
        check(count.moved) == 0u;
        check(count.assign) == 0u;
        check(count.destruct) == 0u;

        c.destroy_if(false);
        check(count.construct) == 0u;
        check(count.copied) == 0u;
        check(count.moved) == 0u;
        check(count.assign) == 0u;
        check(count.destruct) == 0u;

        c.emplace();
        check(count.construct) == 1u;
        check(count.copied) == 0u;
        check(count.moved) == 0u;
        check(count.assign) == 0u;
        check(count.destruct) == 0u;

        c.destroy_if(true);
        check(count.construct) == 1u;
        check(count.copied) == 0u;
        check(count.moved) == 0u;
        check(count.assign) == 0u;
        check(count.destruct) == 1u;
    });


    auto const assign = suite.test("assignment", [](auto check) {
        count = {};
        felspar::memory::raw_memory<counted> c;
        check(count.construct) == 0u;
        check(count.copied) == 0u;
        check(count.moved) == 0u;
        check(count.assign) == 0u;
        check(count.destruct) == 0u;

        c.assign_or_emplace(false, {});
        check(count.construct) == 3u;
        check(count.copied) == 0u;
        check(count.moved) == 2u;
        check(count.assign) == 0u;
        check(count.destruct) == 2u;

        c.assign_or_emplace(true, {});
        check(count.construct) == 4u;
        check(count.copied) == 0u;
        check(count.moved) == 3u;
        check(count.assign) == 0u;
        check(count.destruct) == 3u;

        c.destroy_if(true);
        check(count.construct) == 4u;
        check(count.copied) == 0u;
        check(count.moved) == 3u;
        check(count.assign) == 0u;
        check(count.destruct) == 4u;
    });


}