Skip to content

Commit 63a1abe

Browse files
committed
Now with tests
1 parent e5714f4 commit 63a1abe

20 files changed

+87
-57
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ before_install:
1616
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 90
1717
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 90
1818
script:
19-
- bazel build checktestdata
19+
- bazel test test

BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,14 @@ cc_library(
9292
srcs = [],
9393
hdrs = ["command.h"],
9494
)
95+
96+
sh_test(
97+
name = "test",
98+
srcs = ["test.sh"],
99+
data = [
100+
":checktestdata",
101+
] + glob([
102+
"tests/testprog*",
103+
"tests/testdata*",
104+
]),
105+
)

checktestdata.cc

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ class Reader : public Command {
139139
read_ = &Reader::readFloat;
140140
break;
141141
case checktestdataParser::STRING:
142+
string_ = std::move(*value);
143+
read_ = &Reader::readString;
142144
break;
143-
case checktestdataParser::REGEX: {
144-
const auto& str = std::get<Value::string>(value->eval().value_);
145-
regex_.emplace(str);
145+
case checktestdataParser::REGEX:
146+
string_ = std::move(*value);
146147
read_ = &Reader::readRegex;
147148
break;
148-
}
149149
default:
150150
throw std::logic_error("unimplemented read statement " +
151151
std::to_string(ctx->type->getType()));
@@ -158,8 +158,26 @@ class Reader : public Command {
158158
}
159159

160160
private:
161+
Expression string_;
162+
const void* last_string_value_ = nullptr;
161163
std::optional<RE2> regex_;
164+
165+
void readString(std::string_view& in) {
166+
const auto& s = std::get<Value::string>(string_.eval().value_);
167+
if (in.substr(0, s.size()) == s) {
168+
in.remove_prefix(s.size());
169+
}
170+
if (variable_) {
171+
variable_->assign(Value{s});
172+
}
173+
}
174+
162175
void readRegex(std::string_view& in) {
176+
const auto& s = std::get<Value::string>(string_.eval().value_);
177+
if (&s != last_string_value_) {
178+
last_string_value_ = &s;
179+
regex_.emplace(s);
180+
}
163181
const char* before = in.data();
164182
if (!RE2::Consume(&in, *regex_)) {
165183
throw InputMismatch{absl::StrCat("did not match regex ",
@@ -210,10 +228,12 @@ class Reader : public Command {
210228
if (peek(in) == '-') get(in);
211229
bool fail = true;
212230
int decimals = 0;
231+
int digits = 0;
213232
bool scientific = false;
214233
while (peek(in) >= '0' && peek(in) <= '9') {
215234
fail = false;
216235
get(in);
236+
++digits;
217237
}
218238
if (!fail && peek(in) == '.') {
219239
get(in);
@@ -232,11 +252,17 @@ class Reader : public Command {
232252
get(in);
233253
}
234254
}
235-
if ((mindecimals_ &&
236-
std::get<int64_t>(mindecimals_->eval().value_) > decimals) ||
237-
(maxdecimals_ &&
238-
std::get<int64_t>(maxdecimals_->eval().value_) < decimals)) {
239-
throw std::logic_error{"wrong number of decimal places in input"};
255+
if (mindecimals_) {
256+
int64_t mindecimals = mindecimals_->eval().toInt<uint32_t>();
257+
int64_t maxdecimals = maxdecimals_->eval().toInt<uint32_t>();
258+
if (scientific && (digits != 1 || before[0] == '0')) {
259+
throw std::logic_error{
260+
"In the FLOATP case a floating point number in scientific notation "
261+
"must have exactly one nonzero digit before the decimal point."};
262+
}
263+
if (mindecimals > decimals || maxdecimals < decimals) {
264+
throw std::logic_error{"wrong number of decimal places in input"};
265+
}
240266
}
241267
if (scientific_ && *scientific_ != scientific) {
242268
throw std::logic_error{"wrong float format"};
@@ -538,11 +564,13 @@ int main(int argc, char** argv) {
538564
checktestdataLexer lexer(&input);
539565
CommonTokenStream tokens(&lexer);
540566
checktestdataParser parser(&tokens);
541-
if (parser.getNumberOfSyntaxErrors()) {
542-
throw std::runtime_error{"failed to compile ctd"};
543-
}
544567
tree::ParseTree* tree = parser.main();
545-
// std::cout << tree->toStringTree(&parser) << std::endl;
568+
if (!lexer.hitEOF || parser.getNumberOfSyntaxErrors()) {
569+
throw std::runtime_error{absl::StrCat("failed to compile ctd ",
570+
lexer.hitEOF, " ",
571+
parser.getNumberOfSyntaxErrors())};
572+
}
573+
// std::cerr << tree->toStringTree(&parser) << std::endl;
546574
std::vector<std::thread> threads;
547575
for (int i = 2; i == 2 || i < argc; ++i) {
548576
threads.emplace_back([tree, i, argc, argv]() {

checktestdata.g4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ fragment ESCAPED_CHAR :
1111
| '\\r'
1212
| '\\b'
1313
| '\\\n'
14-
| '\\' [0-7][0-7][0-7];
14+
| '\\' [0-7][0-7][0-7]
15+
| '\\' [0-7][0-7]
16+
| '\\' [0-7];
1517
1618
main : command* EOF;
1719
command :

expression.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ Value parseString(const std::string& literal) {
3838
i += 1;
3939
char c2 = literal[i];
4040
if (c2 >= '0' && c2 <= '9') {
41-
i++;
42-
char c3 = literal[i];
43-
i++;
44-
char c4 = literal[i];
45-
result += static_cast<char>(c4 + 8 * (c3 + 8 * c2));
41+
for (; i < (int)literal.size() - 1 && literal[i] >= '0' &&
42+
literal[i] <= '9';
43+
++i) {
44+
c2 *= 8;
45+
c2 += literal[i];
46+
}
47+
result += c2;
4648
} else {
4749
switch (c2) {
4850
case '\\':

test.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
IFS=$'\n'
3+
progs=$(find tests/ | grep testprog | sort -V)
4+
for prog in $progs
5+
do
6+
for data in $(ls "$(echo $prog | sed -e 's/testprog\([0-9]*\)\..*/testdata\1/')".*)
7+
do
8+
echo checktestdata $prog $data
9+
./checktestdata $prog $data
10+
retval=$?
11+
if [[ ($prog == *.err*) || ($data == *.err*) ]]
12+
then
13+
retval=$[! $retval]
14+
fi
15+
if [ "$retval" -ne 0 ]
16+
then
17+
exit 1
18+
fi
19+
done
20+
done
21+
exit $retval

tests/testdata23.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
bar.*
1+
(?s)bar.*
22
barblaboo.*#!?$$^[a-z]

tests/testdata8.err1

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/testdata8.err2

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/testdata8.err3

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)