Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald-tests\memoryarena-tests.cpp
Line
Count
Source
1
//  Copyright 2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include <memory>
6
7
#include "catch.hpp"
8
9
#include "herald/herald.h"
10
11
1
TEST_CASE("memoryarena-pagesrequired","[memoryarena][pagesrequired]") {
12
1
  SECTION("memoryarena-pagesrequired") {
13
1
    auto req1 = herald::datatype::pagesRequired(2048,10);
14
1
    REQUIRE(205 == req1);
15
1
    auto req2 = herald::datatype::pagesRequired(2048,9);
16
1
    REQUIRE(228 == req2);
17
1
    auto req3 = herald::datatype::pagesRequired(2048,8);
18
1
    REQUIRE(256 == req3);
19
1
    auto req4 = herald::datatype::pagesRequired(2048,10);
20
1
    REQUIRE(205 == req4);
21
1
    auto req5 = herald::datatype::pagesRequired(2048,11);
22
1
    REQUIRE(187 == req5);
23
1
  }
24
1
}
25
26
1
TEST_CASE("memoryarena-size","[memoryarena][size]") {
27
1
  SECTION("memoryarena-size") {
28
1
    herald::datatype::MemoryArena<2048,10> arena;
29
1
    REQUIRE(sizeof(arena) == 2048 + ((205 + 9) / 8) + 2 + 2 + 2); // size of array, Size, PageSize
30
1
  }
31
1
}
32
33
1
TEST_CASE("memoryarena-set","[memoryarena][set]") {
34
1
  SECTION("memoryarena-set") {
35
1
    herald::datatype::MemoryArena<2048,10> arena;
36
1
    char value = 'G';
37
1
    char nonvalue = 'T';
38
1
    auto entry = arena.allocate(89);
39
90
    for (int i = 0;i < 89;
++i89
) {
40
89
      arena.set(entry,i,nonvalue);
41
89
    }
42
1
    arena.set(entry,0,value);
43
1
    arena.set(entry,5,value);
44
1
    arena.set(entry,88,value);
45
1
    REQUIRE(value == arena.get(entry,0));
46
1
    REQUIRE(nonvalue == arena.get(entry,1));
47
1
    REQUIRE(nonvalue == arena.get(entry,4));
48
1
    REQUIRE(value == arena.get(entry,5));
49
1
    REQUIRE(nonvalue == arena.get(entry,6));
50
1
    REQUIRE(nonvalue == arena.get(entry,87));
51
1
    REQUIRE(value == arena.get(entry,88));
52
1
  }
53
1
}
54
55
1
TEST_CASE("memoryarena-reserve","[memoryarena][reserve]") {
56
1
  SECTION("memoryarena-reserve") {
57
1
    herald::datatype::MemoryArena<2048,10> arena;
58
1
    REQUIRE(arena.pagesFree() == 205);
59
1
    auto entrySame = arena.allocate(10);
60
1
    REQUIRE(arena.pagesFree() == 204);
61
1
    arena.reserve(entrySame,10);
62
1
    REQUIRE(arena.pagesFree() == 204);
63
1
    REQUIRE(entrySame.byteLength == 10);
64
1
65
1
    auto entryExpanded = arena.allocate(10);
66
1
    REQUIRE(arena.pagesFree() == 203);
67
1
    arena.reserve(entryExpanded,20);
68
1
    REQUIRE(arena.pagesFree() == 202);
69
1
    REQUIRE(entryExpanded.byteLength == 20);
70
1
71
1
    auto entryShorter = arena.allocate(20);
72
1
    REQUIRE(arena.pagesFree() == 200);
73
1
    arena.reserve(entryShorter,10);
74
1
    REQUIRE(arena.pagesFree() == 200);
75
1
    REQUIRE(entryShorter.byteLength == 20);
76
1
  }
77
1
}
78
79
1
TEST_CASE("memoryarena-useall","[memoryarena][useall]") {
80
1
  SECTION("memoryarena-useall") {
81
1
    herald::datatype::MemoryArena<2048,10> arena;
82
1
    REQUIRE(arena.pagesFree() == 205);
83
1
    auto entry1 = arena.allocate(512);
84
1
    REQUIRE(entry1.startPageIndex == 0);
85
1
    REQUIRE(entry1.byteLength == 512);
86
1
    auto entry2 = arena.allocate(1024);
87
1
    REQUIRE(entry2.startPageIndex == 52);
88
1
    REQUIRE(entry2.byteLength == 1024);
89
1
    auto entry3 = arena.allocate(491);
90
1
    REQUIRE(entry3.startPageIndex == 155);
91
1
    REQUIRE(entry3.byteLength == 491); // not 512 as we have overhead due to page length of 10 bytes
92
1
    REQUIRE(arena.pagesFree() == 0); // used all pages (not bytes) exactly
93
1
    REQUIRE_THROWS([&arena](){
94
1
      auto entry4 = arena.allocate(1); // should fail to allocate
95
1
    }());
96
1
    arena.deallocate(entry1);
97
1
    REQUIRE(arena.pagesFree() == 52);
98
1
    arena.deallocate(entry3);
99
1
    REQUIRE(arena.pagesFree() == 102); // 52 + 50
100
1
    arena.deallocate(entry2);
101
1
    REQUIRE(arena.pagesFree() == 205);
102
1
  }
103
1
}