-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-dmap.cpp
275 lines (258 loc) · 8.79 KB
/
test-dmap.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// test-dmap.cpp
// unittests
//
// Created by MK on 21.11.18.
// Copyright © 2018 Oliver Waldhorst. All rights reserved.
//
#include "catch.hpp"
#include <string.h>
#include <iostream>
#include "helper.hpp"
#include "constants.h"
#include "dmap.h"
#define BD_PATH "Test_Blockdevice.bin"
TEST_CASE("DMap.setAll / DMap.getAll", "[DMap]") {
SECTION("Completely empty/same Array"){
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
for (int i = 0; i < (DATA_BLOCKS + 1) / 8; i++) {
dMapArray[i] = 3;
readArray[i] = 1;
}
dmap.setAll(dMapArray);
dmap.getAll(readArray);
REQUIRE(memcmp(dMapArray, readArray, sizeof(dMapArray)) == 0);
}
SECTION("Random Array content"){
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
dmap.setAll(dMapArray);
dmap.getAll(readArray);
REQUIRE(memcmp(dMapArray, readArray, sizeof(dMapArray)) == 0);
}
}
TEST_CASE("DMap.clear", "[DMap]") {
SECTION("Clearing bit that is set") {
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
readArray[0] = dMapArray[0] = 0xff; // set first entry to the same value
dmap.setAll(dMapArray);
dMapArray[0] = 0x7f; // "clear" dmapArray[0]
dmap.clear(0); // clear dmap[0]
dmap.getAll(readArray); // read cleared dmap
REQUIRE(dMapArray[0] == readArray[0]);
}
SECTION("Clearing bit that is already clear") {
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
readArray[0] = dMapArray[0] = 0x7f;
dmap.setAll(dMapArray);
dmap.clear(0);
dmap.getAll(readArray);
REQUIRE(readArray[0] == dMapArray[0]);
}
SECTION("Clearing multiple bits in dMap") {
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
for (int i = 0; i < (DATA_BLOCKS + 1) / 8; i++) {
switch (i % 3) { // write multiple different values
case 0: dMapArray[i] = readArray[i] = 0xD2; break;
case 1: dMapArray[i] = readArray[i] = 0x39; break;
default: dMapArray[i] = readArray[i] = 0x76; break;
}
}
dmap.setAll(dMapArray);
for (int i = 0; i < (DATA_BLOCKS + 1) / 8; i++) {
dmap.clear((uint16_t)(i * 8 + (i % 8))); // with clear
uint8_t clearMask = ~((uint8_t) 1 << (7 - (i % 8))); // clear manually
dMapArray[i] &= clearMask;
}
dmap.getAll(readArray);
REQUIRE(memcmp(readArray, dMapArray, sizeof(dMapArray)) == 0);
}
}
TEST_CASE("DMap.set" , "[DMap]") {
SECTION("Setting empty bit") {
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
dMapArray[0] = 0;
dmap.setAll(dMapArray);
dMapArray[0] = 0x80;
dmap.set(0);
dmap.getAll(readArray);
REQUIRE(dMapArray[0] == readArray[0]);
}
SECTION("Setting bit that is already set") {
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
readArray[0] = 0;
dMapArray[0] = 0x80;
dmap.setAll(dMapArray);
dmap.set(0);
dmap.getAll(readArray);
REQUIRE(dMapArray[0] == readArray[0]);
}
SECTION("Setting multiple bits in dMap") {
DMap dmap = DMap();
uint8_t dMapArray[(DATA_BLOCKS + 1) / 8];
uint8_t readArray[(DATA_BLOCKS + 1) / 8];
for (int i = 0; i < (DATA_BLOCKS + 1) / 8; i++) {
switch (i % 3) { // write multiple different values
case 0: dMapArray[i] = readArray[i] = 0xD2; break;
case 1: dMapArray[i] = readArray[i] = 0x39; break;
default: dMapArray[i] = readArray[i] = 0x76; break;
}
}
dmap.setAll(dMapArray);
for (int i = 0; i < (DATA_BLOCKS + 1) / 8; i++) {
dmap.set((uint16_t)(i * 8 + (i % 8))); // with clear
uint8_t setMask = (uint8_t) 1 << (7 - (i % 8)); // clear manually
dMapArray[i] |= setMask;
}
dmap.getAll(readArray);
REQUIRE(memcmp(readArray, dMapArray, sizeof(dMapArray)) == 0);
}
}
TEST_CASE("DMap.getFreeBlock, with set blocks" , "[DMap]") {
SECTION("Initial values of dmap") {
DMap dmap = DMap();
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 0);
}
SECTION("The first n blocks are set") {
DMap dmap = DMap();
for (uint16_t i = 0; i < 234; i++) {
dmap.set(i);
}
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 234);
}
SECTION("Blocks are set with empty block between them") {
DMap dmap = DMap();
for (uint16_t i = 0; i < 51; i++) {
dmap.set(i);
}
for (uint16_t i = 52; i < 163; i++) {
dmap.set(i);
}
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 51);
}
SECTION("Some blocks are set, first block is free") {
DMap dmap = DMap();
for (uint16_t i = 43; i < 82; i++) {
dmap.set(i);
}
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 0);
}
}
TEST_CASE("DMap.getFreeBlock, with set and cleared blocks" , "[DMap]") {
SECTION("The first n blocks are set, one of them is cleared") {
DMap dmap = DMap();
for (uint16_t i = 0; i < 123; i++) {
dmap.set(i);
}
dmap.clear(43);
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 43);
}
SECTION("The first n blocks are set, the first is cleared") {
DMap dmap = DMap();
for (uint16_t i = 0; i < 51; i++) {
dmap.set(i);
}
dmap.clear(0);
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 0);
}
SECTION("The first n blocks are set, a block > n is cleared") {
DMap dmap = DMap();
for (uint16_t i = 0; i < 82; i++) {
dmap.set(i);
}
dmap.clear(425);
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 82);
}
SECTION("The first n blocks are set, many blocks are cleared") {
DMap dmap = DMap();
for (uint16_t i = 0; i < 351; i++) {
dmap.set(i);
}
dmap.clear(425);
dmap.clear(42);
dmap.clear(5456);
dmap.clear(33); // smallest number
dmap.clear(54);
for (uint16_t i = 101; i < 167; i++) {
dmap.clear(i);
}
dmap.clear(234);
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 33);
}
}
TEST_CASE("DMap.getFreeBlock, with setAll", "[DMap]") {
SECTION("Empty array") {
DMap dmap = DMap();
uint8_t dmapArray[(DATA_BLOCKS + 1) / 8];
for (int i = 0; i < (DATA_BLOCKS + 1) / 8; i++) {
dmapArray[i] = 0;
}
dmap.setAll(dmapArray);
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 0);
}
SECTION("First n blocks are set") {
DMap dmap = DMap();
uint8_t dmapArray[(DATA_BLOCKS + 1) / 8];
for (int i = 0; i < 67; i++) {
dmapArray[i] = (char) 0xFF;
}
dmapArray[67] = (char) 0xCF; // 11001111
dmap.setAll(dmapArray);
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
// (x * 8) - 1 is first free block for first x BYTES set.
REQUIRE(firstFreeBlock == 67 * 8 + 2);
}
SECTION("First blocks is empty") {
DMap dmap = DMap();
uint8_t dmapArray[(DATA_BLOCKS + 1) / 8];
dmapArray[0] = (char) 0;
dmap.setAll(dmapArray);
uint16_t firstFreeBlock;
dmap.getFreeBlock(&firstFreeBlock);
REQUIRE(firstFreeBlock == 0);
}
}
TEST_CASE("DMap.getFreeBlock, with full dmap", "[DMap]") {
SECTION("Full dmap with setAll") {
DMap dmap = DMap();
uint8_t dmapArray[(DATA_BLOCKS + 1) / 8];
for (int i = 0; i < (DATA_BLOCKS + 1) / 8; i++) {
dmapArray[i] = (char) 0xFF;
}
dmap.setAll(dmapArray);
uint16_t firstFreeBlock;
REQUIRE(dmap.getFreeBlock(&firstFreeBlock) < 0); // Error code
}
}