-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathring_helpers_test.cpp
More file actions
109 lines (92 loc) · 3.57 KB
/
Copy pathring_helpers_test.cpp
File metadata and controls
109 lines (92 loc) · 3.57 KB
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
103
104
105
106
107
108
109
#include "sintra/rings.h"
#include "test_utils.h"
#include <cstddef>
#include <cstdint>
#include <exception>
#include <iostream>
#include <string>
namespace {
constexpr std::string_view k_failure_prefix = "ring_helpers_test: ";
void test_aligned_capacity()
{
using element_t = std::uint32_t;
const std::size_t page_size = sintra::system_page_size();
const std::size_t requested = 1;
const std::size_t aligned = sintra::aligned_capacity<element_t>(requested);
sintra::test::require_true(aligned >= requested,
k_failure_prefix,
"aligned_capacity should not reduce the requested size");
sintra::test::require_true(aligned % 8 == 0,
k_failure_prefix,
"aligned_capacity must return a multiple of 8 elements");
if (page_size != 0) {
sintra::test::require_true((aligned * sizeof(element_t)) % page_size == 0,
k_failure_prefix,
"aligned_capacity must respect page alignment");
}
sintra::test::require_true(sintra::aligned_capacity<element_t>(0) == 0,
k_failure_prefix,
"aligned_capacity(0) should return 0");
const std::size_t runtime_aligned = sintra::aligned_capacity(requested, sizeof(element_t));
sintra::test::require_true(runtime_aligned == aligned,
k_failure_prefix,
"aligned_capacity overloads should match");
}
void test_write_commit()
{
using element_t = std::uint32_t;
const std::size_t capacity = sintra::aligned_capacity<element_t>(128);
sintra::test::require_true(capacity != 0,
k_failure_prefix,
"aligned_capacity returned 0 for a valid request");
const auto scratch_dir = sintra::test::unique_scratch_directory("ring_helpers");
const std::string directory = scratch_dir.string();
const std::string ring_name = "ring_helpers_ring";
sintra::Ring_W<element_t> writer(directory, ring_name, capacity);
sintra::Ring_R<element_t> reader(directory, ring_name, capacity, 2);
const element_t first_value = 42;
const auto seq1 = writer.write_commit(first_value);
sintra::test::require_true(seq1 == 1,
k_failure_prefix,
"write_commit should return the new leading sequence");
auto range1 = reader.start_reading(1);
sintra::test::require_true(range1.begin && range1.end,
k_failure_prefix,
"start_reading returned an invalid range");
sintra::test::require_true(range1.end - range1.begin == 1,
k_failure_prefix,
"expected a single element in range");
sintra::test::require_true(range1.begin[0] == first_value,
k_failure_prefix,
"unexpected value after write_commit");
reader.done_reading();
const element_t batch[] = {10, 11};
const auto seq2 = writer.write_commit(batch, 2);
sintra::test::require_true(seq2 == 3,
k_failure_prefix,
"write_commit should advance the leading sequence");
auto range2 = reader.start_reading(2);
sintra::test::require_true(range2.end - range2.begin == 2,
k_failure_prefix,
"expected two elements in range");
sintra::test::require_true(range2.begin[0] == batch[0],
k_failure_prefix,
"first batch element mismatch");
sintra::test::require_true(range2.begin[1] == batch[1],
k_failure_prefix,
"second batch element mismatch");
reader.done_reading();
}
} // namespace
int main()
{
try {
test_aligned_capacity();
test_write_commit();
}
catch (const std::exception& ex) {
std::cerr << "ring_helpers_test failed: " << ex.what() << std::endl;
return 1;
}
return 0;
}