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

#include <array>
#include <cstdint>


namespace {


    auto const suite = felspar::testsuite("bitmap allocator");


    auto const nb = suite.test("nextbit", [](auto check) {
        namespace bm = felspar::memory::bitmap;
        check(bm::nextbit(std::uint8_t{0b0000'0000})) == 0u;
        check(bm::nextbit(std::uint8_t{0b0000'0111})) == 3u;
        check(bm::nextbit(std::uint8_t{0b1111'1110})) == 0u;
        check(bm::nextbit(std::uint8_t{0b1111'0111})) == 3u;
        check(bm::nextbit(std::uint8_t{0b1111'1111})) == 8u;
    });


    auto const al = suite.test("allocate", [](auto check) {
        std::array<std::byte, 32> memory{};
        std::uint32_t blocks{};

        namespace bm = felspar::memory::bitmap;

        check(bm::allocate(blocks, memory.data(), 1u)) == memory.data();
        check(blocks) == 1u;

        check(bm::allocate(blocks, memory.data(), 1u)) == memory.data() + 1;
        check(blocks) == 3u;

        bm::deallocate(memory.data(), blocks, memory.data(), 1u);
        check(blocks) == 2u;

        bm::deallocate(memory.data() + 1, blocks, memory.data(), 1u);
        check(blocks) == 0u;
    });


}