forked from tcbrindle/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bounds_checked.cpp
142 lines (116 loc) · 4.15 KB
/
test_bounds_checked.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <vector>
#include "test_utils.hpp"
TEST_CASE("C array bounds checking")
{
{
int arr[] = {0, 1, 2, 3, 4};
auto seq = flux::ref(arr);
SUBCASE("Can read from in-bounds indices")
{
auto cur = seq.first();
REQUIRE(seq[cur] == 0);
REQUIRE(seq.move_at(cur) == 0);
}
SUBCASE("Can advance within bounds")
{
auto cur = seq.first();
for (; !seq.is_last(cur); seq.inc(cur)) {}
REQUIRE(seq.is_last(cur));
}
SUBCASE("Reading past the end is an error")
{
auto cur = seq.last();
REQUIRE_THROWS_AS(seq[cur], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(cur), flux::unrecoverable_error);
}
SUBCASE("Reading before the start is an error")
{
auto cur = seq.first() - 1;
REQUIRE_THROWS_AS(seq[cur], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(cur), flux::unrecoverable_error);
}
SUBCASE("Can decrement within bounds")
{
auto cur = seq.last();
while(seq.dec(cur) != seq.first()) {}
REQUIRE(cur == seq.first());
}
SUBCASE("Random reads are an error")
{
REQUIRE_THROWS_AS(seq[100], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(100), flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq[-100], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(-100), flux::unrecoverable_error);
}
SUBCASE("Views are bounds checked as well")
{
auto first = seq.begin();
auto last = seq.end();
REQUIRE_THROWS_AS(*last, flux::unrecoverable_error);
REQUIRE_THROWS_AS(first[10], flux::unrecoverable_error);
}
}
}
TEST_CASE("vector bounds checking")
{
std::vector<int> vec{0, 1, 2, 3, 4};
auto seq = flux::ref(vec);
SUBCASE("Can read from in-bounds indices")
{
auto cur = seq.first();
REQUIRE(seq[cur] == 0);
REQUIRE(seq.move_at(cur) == 0);
}
SUBCASE("Can advance within bounds")
{
auto cur = seq.first();
for (; !seq.is_last(cur); seq.inc(cur)) {}
REQUIRE(seq.is_last(cur));
}
SUBCASE("Reading past the end is an error")
{
auto cur = seq.last();
REQUIRE_THROWS_AS(seq[cur], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(cur), flux::unrecoverable_error);
}
SUBCASE("Reading before the start is an error")
{
auto cur = seq.first() - 1;
REQUIRE_THROWS_AS(seq[cur], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(cur), flux::unrecoverable_error);
}
SUBCASE("Can decrement within bounds")
{
auto cur = seq.last();
while(seq.dec(cur) != seq.first()) {}
REQUIRE(cur == seq.first());
}
SUBCASE("Random reads are an error")
{
REQUIRE_THROWS_AS(seq[100], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(100), flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq[-100], flux::unrecoverable_error);
REQUIRE_THROWS_AS(seq.move_at(-100), flux::unrecoverable_error);
}
SUBCASE("Storage invalidation is okay")
{
auto vec2 = vec;
auto cur = flux::next(vec2, flux::first(vec2), 2);
REQUIRE(flux::read_at(vec2, cur) == 2);
vec2.resize(vec2.capacity() + 1); // force a realloc
REQUIRE(flux::read_at(vec2, cur) == 2);
vec2.clear();
vec2.shrink_to_fit(); // for good measure
REQUIRE_THROWS_AS(flux::read_at(vec2, cur), flux::unrecoverable_error);
}
SUBCASE("Range interface is bounds checked as well")
{
auto first = seq.begin();
auto last = seq.end();
REQUIRE_THROWS_AS(*last, flux::unrecoverable_error);
REQUIRE_THROWS_AS(first[10], flux::unrecoverable_error);
}
}