c++: style: improve tests

* tests/c++.at (C++ Variant-based Symbols Unit Tests): Provide better
assertions.
Use them.
Avoid useless Bison invocations.
This commit is contained in:
Akim Demaille
2018-12-23 19:45:07 +01:00
parent 9858165c52
commit 45cd7dfb7f

View File

@@ -103,7 +103,7 @@ AT_KEYWORDS([variant])
AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1]) AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
# Store strings and integers in a vector of strings. # Store strings and integers in a vector of strings.
AT_DATA_GRAMMAR([list.y], AT_DATA_GRAMMAR([list.yy],
[[%skeleton "lalr1.cc" [[%skeleton "lalr1.cc"
%define api.value.type variant %define api.value.type variant
%define parse.assert %define parse.assert
@@ -142,24 +142,33 @@ exp: "int" { $$.push_back ($1); }
]AT_YYERROR_DEFINE[ ]AT_YYERROR_DEFINE[
]AT_YYLEX_DEFINE[ ]AT_YYLEX_DEFINE[
template <typename Exp, typename Eff>
void assert_eq (const Exp& exp, const Eff& eff)
{
if (getenv ("DEBUG"))
std::cerr << "Assert: " << exp << " == " << eff << '\n';
if (exp != eff)
std::cerr << "Assertion failed: " << exp << " != " << eff << '\n';
}
int main() int main()
{ {
using yy::parser; using yy::parser;
// symbol_type: construction, accessor. // symbol_type: construction, accessor.
{ {
parser::symbol_type s = parser::make_INT(12); parser::symbol_type s = parser::make_INT (12);
std::cerr << s.value.as<int>() << '\n'; assert_eq (s.value.as<int> (), 12);
} }
// stack_symbol_type: construction, accessor. // stack_symbol_type: construction, accessor.
{ {
#if defined __cplusplus && 201103L <= __cplusplus #if 201103L <= YY_CPLUSPLUS
auto ss = parser::stack_symbol_type(1, parser::make_INT(123)); auto ss = parser::stack_symbol_type(1, parser::make_INT(123));
#else #else
parser::symbol_type s = parser::make_INT(123); parser::symbol_type s = parser::make_INT (123);
parser::stack_symbol_type ss(1, s); parser::stack_symbol_type ss(1, s);
#endif #endif
std::cerr << ss.value.as<int>() << '\n'; assert_eq (ss.value.as<int> (), 123);
} }
// Pushing on the stack. // Pushing on the stack.
@@ -171,7 +180,7 @@ int main()
const int int_reduction_state = 1; // Read list.output to find it. const int int_reduction_state = 1; // Read list.output to find it.
for (int i = 0; i < mucho; ++i) for (int i = 0; i < mucho; ++i)
{ {
#if defined __cplusplus && 201103L <= __cplusplus #if 201103L <= YY_CPLUSPLUS
st.push(parser::stack_symbol_type{int_reduction_state, st.push(parser::stack_symbol_type{int_reduction_state,
parser::make_INT (i)}); parser::make_INT (i)});
#else #else
@@ -182,19 +191,17 @@ int main()
} }
for (int i = mucho - 1; 0 <= i; --i) for (int i = mucho - 1; 0 <= i; --i)
{ {
assert (st[0].value.as<int>() == i); assert_eq (st[0].value.as<int>(), i);
st.pop (); st.pop ();
} }
} }
} }
]]) ]])
AT_BISON_CHECK([[-o list.cc list.yy]])
AT_FOR_EACH_CXX([ AT_FOR_EACH_CXX([
AT_FULL_COMPILE([list]) AT_COMPILE_CXX([list])
AT_PARSER_CHECK([./list], 0, [], AT_PARSER_CHECK([./list])
[12
123
])
]) ])
AT_BISON_OPTION_POPDEFS AT_BISON_OPTION_POPDEFS