1
0
mirror of https://github.com/pdewacht/brlaser synced 2024-12-26 15:38:20 +01:00

Misc cleanups, mostly in the test code

This commit is contained in:
Peter De Wachter 2015-01-31 23:13:48 +01:00
parent 4f1a00d46a
commit 1474716b27
3 changed files with 47 additions and 26 deletions

View File

@ -25,13 +25,18 @@
class block {
public:
block(): line_bytes_(0) {
lines_.reserve(max_lines_per_block_);
}
bool empty() const {
return line_bytes_ == 0;
}
void add_line(std::vector<uint8_t> &&line) {
assert(!line.empty());
assert(line_fits(line.size()));
line_bytes_ += line.size();
lines_.push_back(line);
lines_.emplace_back(line);
}
bool line_fits(unsigned size) {
@ -40,7 +45,7 @@ class block {
}
void flush(FILE *f) {
if (line_bytes_) {
if (!empty()) {
fprintf(f, "%dw%c%c",
line_bytes_ + 2, 0,
static_cast<int>(lines_.size()));

View File

@ -24,7 +24,20 @@
typedef std::vector<uint8_t> vec;
const lest::test specification[] = {
"Block line limit",
"A block is created empty",
[] {
block b;
EXPECT(b.empty());
},
"Adding a line makes a block no longer empty",
[] {
block b;
b.add_line(vec{1});
EXPECT(!b.empty());
},
"A block can contain 128 lines",
[] {
block b;
for (int i = 0; i < 128; ++i) {
@ -34,7 +47,7 @@ const lest::test specification[] = {
EXPECT(!b.line_fits(1));
},
"Block size limit",
"A block has a size limit of about 16 kilobyte",
[] {
block b;
for (int i = 0; i < 16; ++i) {
@ -44,30 +57,33 @@ const lest::test specification[] = {
EXPECT(!b.line_fits(400));
},
"Flush",
"Flushing an empty block does nothing",
[] {
block b;
{
tempfile f;
b.flush(f.file());
EXPECT(f.data().empty());
}
for (uint8_t n = 0; n < 10; n += 2) {
vec line = {n, static_cast<uint8_t>(n+1)};
EXPECT(b.line_fits(line.size()));
b.add_line(std::move(line));
}
{
tempfile f;
b.flush(f.file());
EXPECT(( f.data() == vec{'1','2','w',0,5,0,1,2,3,4,5,6,7,8,9} ));
}
{
tempfile f;
b.flush(f.file());
EXPECT(f.data().empty());
}
tempfile f;
b.flush(f.file());
EXPECT(f.data().empty());
},
"Flush() writes the lines to a file with a proper header",
[] {
block b;
for (uint8_t n = 1; n < 6; ++n) {
b.add_line(vec{n, n});
}
tempfile f;
b.flush(f.file());
EXPECT(( f.data() == vec{'1','2','w',0,5,1,1,2,2,3,3,4,4,5,5} ));
},
"After flush() a block is empty again",
[] {
block b;
b.add_line(vec{1});
tempfile f;
b.flush(f.file());
EXPECT(b.empty());
}
};
int main() {

View File

@ -53,7 +53,7 @@ const lest::test specification[] = {
[] {
EXPECT(( encode_line(vec{1,2,3}) == (vec{1,sub(0,2),1,2,3}) ));
},
"Encoding a (non-initial) blank line",
[] {
EXPECT(( encode_line(vec{0,0,0}, vec{1,2,3}) == vec{0xFF} ));