File: test_line.cc

package info (click to toggle)
brlaser 6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 248 kB
  • sloc: cpp: 1,308; makefile: 3; sh: 3
file content (150 lines) | stat: -rw-r--r-- 4,097 bytes parent folder | download | duplicates (5)
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
// This file is part of the brlaser printer driver.
//
// Copyright 2014 Peter De Wachter
//
// brlaser is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// brlaser is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with brlaser.  If not, see <http://www.gnu.org/licenses/>.

#include "lest.hpp"
#include <assert.h>
#include <stdint.h>
#include <vector>
#include "../src/line.h"

typedef std::vector<uint8_t> vec;


uint8_t sub(uint8_t offset, uint8_t count) {
  assert(offset < 16);
  assert(count < 8);
  return (offset << 3) | count;
}

uint8_t rep(uint8_t offset, uint8_t count) {
  assert(offset < 4);
  assert(count < 32);
  return 128 | (offset << 5) | count;
}


const lest::test specification[] = {
  "Don't crash on zero-length lines",
  [] {
    EXPECT(( encode_line(vec{}) == vec{0xFF} ));
    EXPECT(( encode_line(vec{}, vec{}) == vec{0xFF} ));
  },

  "Encoding an initial blank line",
  [] {
    EXPECT(( encode_line(vec{0,0,0}) == vec{0xFF} ));
  },

  "Encoding an initial non-blank line",
  [] {
    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} ));
  },

  "Encoding a repeated line",
  [] {
    EXPECT(( encode_line(vec{1,2,3}, vec{1,2,3}) == vec{0} ));
  },

  "Using a subsitute command",
  [] {
    EXPECT(( encode_line(vec{0,0,1,2,3,0,0}, vec(7)) == vec{1,sub(2,2),1,2,3} ));
  },

  "Using a repeat command",
  [] {
    EXPECT(( encode_line(vec{0,0,1,1,0,0}, vec(6)) == vec{1,rep(2,0),1} ));
  },

  "Repeat command followed by substitute command",
  [] {
    EXPECT(( encode_line(vec{1,1,1,2,3}, vec(5)) == vec{2,rep(0,1),1,sub(0,1),2,3} ));
  },

  "Substitute comand followed by repeat command",
  [] {
    EXPECT(( encode_line(vec{3,2,1,1,1}, vec(5)) == vec{2,sub(0,1),3,2,rep(0,1),1} ));
  },

  "Substitute with an unmodified byte in the middle",
  [] {
    EXPECT(( encode_line(vec{1,2,3,0,1,2,3}, vec(7)) == vec{1,sub(0,6),1,2,3,0,1,2,3} ));
  },

  "Substitue with two unmodified bytes in the middle",
  [] {
    EXPECT(( encode_line(vec{1,2,3,0,0,1,2,3}, vec(8)) == vec{2,sub(0,2),1,2,3,sub(2,2),1,2,3} ));
  },

  "Repeat with an unmodified byte in the middle",
  [] {
    EXPECT(( encode_line(vec{1,1,1,0,1,1,1}, vec(7)) == vec{2,rep(0,1),1,rep(1,1),1} ));
  },

  "254 edits needed for a single line",
  [] {
    vec line, result;
    for (int i = 0; i < 254; ++i)
      line.insert(line.end(), {0,0,1});
    result.push_back(254);
    for (int i = 0; i < 254; ++i)
      result.insert(result.end(), {sub(2,0),1});
    EXPECT(( encode_line(line, vec(line.size())) == result ));
  },

  "Give up if more than 254 edits needed...",
  [] {
    vec line, result;
    for (int i = 0; i < 255; ++i)
      line.insert(line.end(), {0,0,1});
    result.push_back(254);
    for (int i = 0; i < 253; ++i)
      result.insert(result.end(), {sub(2,0),1});
    result.insert(result.end(), {sub(2,3),1,0,0,1});
    EXPECT(( encode_line(line, vec(line.size())) == result ));
  },

  "Repeat command with overflow bytes",
  [] {
    vec line(3, 0);
    line.insert(line.end(), 512, 1);
    vec ref(line.size(), 0);
    vec expected{1,rep(3,31),0,255,224,1};
    EXPECT(encode_line(line, ref) == expected);
  },

  "Substitute command with overflow bytes",
  [] {
    vec expected{1,sub(15,7),255,0,255,237};
    vec line(270, 0);
    for (int i = 0; i < 250; ++i) {
      expected.insert(expected.end(), {1,2});
      line.insert(line.end(), {1,2});
    }
    vec ref(line.size(), 0);
    EXPECT(encode_line(line, ref) == expected);
  },
};


int main() {
  return lest::run(specification);
}