LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
test_tpa.cxx
Go to the documentation of this file.
1#include <cstdint>
2
3#include <lfostest.h>
4
5namespace LFOS {
6 extern "C" {
7 #include <tpa.c>
8 }
9
10 class TpaTest : public ::testing::Test {
11 public:
14 }
15
16 virtual ~TpaTest() {
18 }
19
20 protected:
21 const size_t _tpa_page_size = 4096;
23 };
24
25 TEST_F(TpaTest, Empty) {
26 EXPECT_EQ(tpa_size(_tpa), 4096) << "Size of TPA with no entries correct";
27 EXPECT_EQ(tpa_entries(_tpa), 0) << "Entrycount of TPA with no entries correct";
28 EXPECT_EQ(tpa_length(_tpa), -1) << "Length of TPA with no entries correct";
29 }
30
31 TEST_F(TpaTest, BasicData) {
32 size_t idx = 0x1337;
33 uint64_t val = 23;
34 tpa_set(_tpa, idx, &val);
35
36 {
37 SCOPED_TRACE("Checking sizes with a single entry");
38
39 size_t page_num = (idx + tpa_entries_per_page(_tpa) - 1) / tpa_entries_per_page(_tpa);
40 size_t len = tpa_entries_per_page(_tpa) * page_num;
41
42 EXPECT_EQ(tpa_size(_tpa), 8192) << "Size of TPA with a single entry correct";
43 EXPECT_EQ(tpa_entries(_tpa), 1) << "Entrycount of TPA with a single entry correct";
44 EXPECT_EQ(tpa_length(_tpa), len) << "Length of TPA with no entries correct";
45 }
46
47 {
48 SCOPED_TRACE("Checking retrieval of known-good value");
49
50 void* entry = tpa_get(_tpa, idx);
51
52 EXPECT_NE(entry, (void*)0) << "Entry returned";
53 EXPECT_EQ(*(uint64_t*)entry, val) << "Correct entry value";
54 }
55
56 {
57 SCOPED_TRACE("Checking retrieval of known-bad value");
58
59 void* non_entry = tpa_get(_tpa, idx + 1);
60 EXPECT_EQ(non_entry, (void*)0) << "Not existing entry returns 0";
61 }
62
63 {
64 SCOPED_TRACE("Checking size after deleting all data");
65
66 tpa_set(_tpa, idx, 0);
67
68 EXPECT_EQ(tpa_size(_tpa), _tpa_page_size) << "Size of TPA with all entries deleted";
69 EXPECT_EQ(tpa_entries(_tpa), 0) << "Entrycount of TPA with all entries deleted";
70 EXPECT_EQ(tpa_length(_tpa), -1) << "Length of TPA with all entries deleted";
71 }
72 }
73
74 TEST_F(TpaTest, LotsOfData) {
75 uint64_t val = 42;
76
77 const size_t entry_count = (rand() / (double)RAND_MAX) * 256 * 1024;
78 RecordProperty("EntryCount", entry_count);
79
80 const size_t num_pages = (entry_count + tpa_entries_per_page(_tpa) - 1)
82 const size_t expected_tpa_size = _tpa_page_size // page for the tpa_t header
83 + (num_pages * _tpa_page_size); // all the pages
84 const size_t expected_tpa_len = num_pages * tpa_entries_per_page(_tpa);
85
86 for(size_t i = 0; i < entry_count; ++i) {
87 tpa_set(_tpa, i, &val);
88 }
89
90 EXPECT_EQ(tpa_size(_tpa), expected_tpa_size) << "Size of TPA with lots of entries";
91 EXPECT_EQ(tpa_entries(_tpa), entry_count) << "Entrycount of TPA with lots of entries";
92 EXPECT_EQ(tpa_length(_tpa), expected_tpa_len) << "Length of TPA with lots of entries";
93 }
94}
allocator_t kernel_alloc
Definition vm.c:737
unsigned long uint64_t
Definition arch.h:14
virtual ~TpaTest()
Definition test_tpa.cxx:16
tpa_t * _tpa
Definition test_tpa.cxx:22
const size_t _tpa_page_size
Definition test_tpa.cxx:21
size_t num_pages
Number of pages, where page size is 4096 bytes.
Definition loader.h:4
TEST_F(MessageQueueTest, PopFromEmptyQueue)
Definition test_mq.cxx:39
static void * entry
Definition syscalls.h:34
tpa_t * tpa_new(allocator_t *alloc, uint64_t entry_size, uint64_t page_size, tpa_t *tpa)
Definition tpa.c:48
void tpa_delete(tpa_t *tpa)
Definition tpa.c:61
size_t tpa_size(tpa_t *tpa)
Definition tpa.c:72
void * tpa_get(tpa_t *tpa, uint64_t idx)
Definition tpa.c:144
ssize_t tpa_length(tpa_t *tpa)
Definition tpa.c:87
void tpa_set(tpa_t *tpa, uint64_t idx, void *data)
Definition tpa.c:174
size_t tpa_entries(tpa_t *tpa)
Definition tpa.c:110
size_t tpa_entries_per_page(tpa_t *tpa)
Definition tpa.c:83
Header of a TPA.
Definition tpa.c:34