xo-interpreter2: utest: refactor to reduce repetition
This commit is contained in:
parent
1200a3080f
commit
6a73f49aae
1 changed files with 281 additions and 496 deletions
|
|
@ -40,6 +40,7 @@ namespace xo {
|
|||
using xo::mm::ArenaConfig;
|
||||
using xo::facet::FacetRegistry;
|
||||
using xo::facet::TypeRegistry;
|
||||
using xo::reflect::typeseq;
|
||||
using span_type = xo::scm::DVirtualSchematikaMachine::span_type;
|
||||
using Catch::Matchers::WithinAbs;
|
||||
|
||||
|
|
@ -73,7 +74,8 @@ namespace xo {
|
|||
|
||||
~VsmFixture() {}
|
||||
|
||||
bool log_memory_layout(scope * p_log) {
|
||||
bool log_memory_layout(scope * p_log)
|
||||
{
|
||||
auto visitor = [p_log](const MemorySizeInfo & info) {
|
||||
*p_log && (*p_log)(xtag("resource", info.resource_name_),
|
||||
xtag("used", info.used_),
|
||||
|
|
@ -90,10 +92,90 @@ namespace xo {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool session_with_complete_input(const char * input_text,
|
||||
std::function<bool (const VsmResultExt & x)> verify_result)
|
||||
{
|
||||
INFO(xtag("input_text", input_text));
|
||||
|
||||
bool eof_flag = false;
|
||||
|
||||
vsm_->begin_interactive_session();
|
||||
|
||||
VsmResultExt res = vsm_->read_eval_print(span_type::from_cstr(input_text),
|
||||
eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
REQUIRE(verify_result(res));
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ArenaShim aux_mm_;
|
||||
abox<AGCObject,DVirtualSchematikaMachine> vsm_;
|
||||
};
|
||||
|
||||
void vsm_std_utest_pattern(bool debug_flag,
|
||||
const std::string & testname,
|
||||
const char * input,
|
||||
std::function<bool (const VsmResultExt & x)> verify_fn,
|
||||
const VsmConfig & cfg = VsmConfig())
|
||||
{
|
||||
scope log(XO_DEBUG(debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, debug_flag, cfg);
|
||||
|
||||
vsm_fixture.session_with_complete_input(input, verify_fn);
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
}
|
||||
|
||||
void vsm_multi_utest_pattern(bool debug_flag,
|
||||
const std::string & testname,
|
||||
const char * input,
|
||||
std::vector<std::function<bool (const VsmResultExt &)>> verify_fns,
|
||||
bool eof_flag = false,
|
||||
const VsmConfig & cfg = VsmConfig())
|
||||
{
|
||||
scope log(XO_DEBUG(debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, debug_flag, cfg);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
|
||||
span_type remaining = span_type::from_cstr(input);
|
||||
|
||||
for (std::size_t i = 0; i < verify_fns.size(); ++i) {
|
||||
log && log(xtag("i_expr", i), xtag("input", remaining));
|
||||
|
||||
VsmResultExt res = vsm->read_eval_print(remaining, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
REQUIRE(verify_fns[i](res));
|
||||
|
||||
if (i + 1 == verify_fns.size()) {
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
} else {
|
||||
REQUIRE(res.remaining_.size() > 1);
|
||||
}
|
||||
|
||||
remaining = res.remaining_;
|
||||
}
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-ctor", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
|
@ -109,653 +191,356 @@ namespace xo {
|
|||
TEST_CASE("VirtualSchematikaMachine-const1", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
||||
constexpr bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x._typeseq() == typeseq::id<DFloat>());
|
||||
REQUIRE_THAT(x.data()->value(), WithinAbs(3.141592635, 1e-6));
|
||||
|
||||
bool eof_flag = false;
|
||||
return true;
|
||||
};
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res = vsm->read_eval_print(span_type::from_cstr("3.141592635;"), eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE_THAT(x.data()->value(), WithinAbs(3.141592635, 1e-6));
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname, "3.1415926535;", verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-const2", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
||||
constexpr bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x._typeseq() == typeseq::id<DInteger>());
|
||||
REQUIRE(x.data()->value() == 1011);
|
||||
|
||||
bool eof_flag = false;
|
||||
return true;
|
||||
};
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res = vsm->read_eval_print(span_type::from_cstr("1011;"), eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()));
|
||||
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE(x.data()->value() == 1011);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname, "1011;", verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-arith1", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
||||
constexpr bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
|
||||
bool eof_flag = false;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x._typeseq() == typeseq::id<DFloat>());
|
||||
REQUIRE(x.data()->value() == 1.570796325);
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res = vsm->read_eval_print(span_type::from_cstr("3.14159265 * 0.5;"), eof_flag);
|
||||
return true;
|
||||
};
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()));
|
||||
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE(x.data()->value() == 1.570796325);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"3.14159265 * 0.5;",
|
||||
verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-arith2", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
||||
constexpr bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
|
||||
bool eof_flag = false;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x.data()->value() == 6.2831853);
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res = vsm->read_eval_print(span_type::from_cstr("3.14159265 / 0.5;"), eof_flag);
|
||||
return true;
|
||||
};
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()));
|
||||
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE(x.data()->value() == 6.2831853);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"3.14159265 / 0.5;", verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-cmp1", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
||||
constexpr bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DBoolean>::from(*res.value());
|
||||
|
||||
bool eof_flag = false;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x.data()->value() == true);
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(span_type::from_cstr("123 == 123;"),
|
||||
eof_flag);
|
||||
return true;
|
||||
};
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()));
|
||||
|
||||
auto x = obj<AGCObject,DBoolean>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE(x.data()->value() == true);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"123 == 123;",
|
||||
verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-if", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
||||
constexpr bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DString>::from(*res.value());
|
||||
|
||||
bool eof_flag = false;
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x.data()->chars(), "equal") == 0);
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(span_type::from_cstr("if 123 == 123 then \"equal\" else \"notequal\";"),
|
||||
eof_flag);
|
||||
return true;
|
||||
};
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()));
|
||||
|
||||
auto x = obj<AGCObject,DString>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x.data()->chars(), "equal") == 0);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"if 123 == 123 then \"equal\" else \"notequal\";",
|
||||
verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-lambda1", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
|
||||
constexpr bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DClosure>::from(*res.value());
|
||||
|
||||
bool eof_flag = false;
|
||||
REQUIRE(x);
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(span_type::from_cstr("lambda (x : i64) -> i64 { x * x; }"),
|
||||
eof_flag);
|
||||
return true;
|
||||
};
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()));
|
||||
|
||||
auto x = obj<AGCObject,DClosure>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
//REQUIRE(x.data()->value() == 1.570796325);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"lambda (x : i64) -> i64 { x * x; }",
|
||||
verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-apply2", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = false;
|
||||
|
||||
bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 195);
|
||||
|
||||
bool eof_flag = false;
|
||||
return true;
|
||||
};
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(span_type::from_cstr
|
||||
("(lambda (x : i64, y : i64) { x * y; })(13, 15);"),
|
||||
eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()));
|
||||
|
||||
// currently get not-implemented error
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 195);
|
||||
|
||||
//log && log("runtime-error", xtag("ex.src", x->src_function()), xtag("ex.descr", x->error_descr()));
|
||||
|
||||
//REQUIRE(x.data()->value() == 1.570796325);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"(lambda (x : i64, y : i64) { x * y; })(13, 15);",
|
||||
verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-def1", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = false;
|
||||
|
||||
bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "foo") == 0);
|
||||
|
||||
bool eof_flag = false;
|
||||
return true;
|
||||
};
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(span_type::from_cstr("def foo = 3.14159;"),
|
||||
eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
// currently get not-implemented error
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "foo") == 0);
|
||||
|
||||
//log && log("runtime-error", xtag("ex.src", x->src_function()), xtag("ex.descr", x->error_descr()));
|
||||
|
||||
//REQUIRE(x.data()->value() == 1.570796325);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"def foo = 3.14159;",
|
||||
verify_fn);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-def2", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = true;
|
||||
|
||||
bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
|
||||
bool eof_flag = false;
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
|
||||
span_type input = span_type::from_cstr("def foo = 3.14159; foo;");
|
||||
|
||||
for (int i_expr = 0; i_expr < 2; ++i_expr) {
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
if (i_expr == 0) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "foo") == 0);
|
||||
|
||||
REQUIRE(res.remaining_.size() > 1);
|
||||
|
||||
input = res.remaining_;
|
||||
} else if (i_expr == 1) {
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE_THAT(x->value(), WithinAbs(3.14159, 1e-6));
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
input = res.remaining_;
|
||||
}
|
||||
}
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
|
||||
vsm_multi_utest_pattern(
|
||||
c_debug_flag, testname,
|
||||
"def foo = 3.14159; foo;",
|
||||
{
|
||||
[](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "foo") == 0);
|
||||
return true;
|
||||
},
|
||||
[](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE_THAT(x->value(), WithinAbs(3.14159, 1e-6));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-def3", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = true;
|
||||
|
||||
bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
|
||||
bool eof_flag = true;
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
|
||||
span_type input = span_type::from_cstr("def fact = lambda (n) { if (n == 0) then 1 else n * fact(n - 1) };");
|
||||
|
||||
for (int i_expr = 0; i_expr < 1; ++i_expr) {
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
if (i_expr == 0) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "fact") == 0);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
input = res.remaining_;
|
||||
}
|
||||
}
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_multi_utest_pattern(
|
||||
c_debug_flag, testname,
|
||||
"def fact = lambda (n) { if (n == 0) then 1 else n * fact(n - 1) };",
|
||||
{
|
||||
[](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "fact") == 0);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
/*eof_flag=*/true);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-if2", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = true;
|
||||
|
||||
bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
|
||||
bool eof_flag = true;
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
|
||||
span_type input = span_type::from_cstr("def n = 4; if (n == 4) then n * 3 else n * 5;");
|
||||
|
||||
for (int i_expr = 0; i_expr < 2; ++i_expr) {
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
if (i_expr == 0) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "n") == 0);
|
||||
input = res.remaining_;
|
||||
} else if (i_expr == 1) {
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 12);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
input = res.remaining_;
|
||||
}
|
||||
}
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_multi_utest_pattern(
|
||||
c_debug_flag, testname,
|
||||
"def n = 4; if (n == 4) then n * 3 else n * 5;",
|
||||
{
|
||||
[](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "n") == 0);
|
||||
return true;
|
||||
},
|
||||
[](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 12);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
/*eof_flag=*/true);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-fact0", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = false;
|
||||
|
||||
bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag);
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
|
||||
bool eof_flag = true;
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
|
||||
span_type input = span_type::from_cstr("def fact = lambda (n) { if (n == 0) then 1 else n * fact(n - 1) }; fact(4);");
|
||||
|
||||
for (int i_expr = 0; i_expr < 2; ++i_expr) {
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
if (i_expr == 0) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "fact") == 0);
|
||||
input = res.remaining_;
|
||||
} else if (i_expr == 1) {
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 24);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
input = res.remaining_;
|
||||
}
|
||||
}
|
||||
|
||||
log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_multi_utest_pattern(
|
||||
c_debug_flag, testname,
|
||||
"def fact = lambda (n) { if (n == 0) then 1 else n * fact(n - 1) }; fact(4);",
|
||||
{
|
||||
[](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DUniqueString>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(strcmp(x->chars(), "fact") == 0);
|
||||
return true;
|
||||
},
|
||||
[](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DInteger>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 24);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
/*eof_flag=*/true);
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-qliteral1", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = true;
|
||||
|
||||
bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 4.5);
|
||||
|
||||
bool eof_flag = false;
|
||||
return true;
|
||||
};
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
|
||||
span_type input = span_type::from_cstr("#q{ 4.5 };");
|
||||
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
auto x = obj<AGCObject,DFloat>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == 4.5);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
//log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"#q{ 4.5 };",
|
||||
verify_fn,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-qlist", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = false;
|
||||
|
||||
bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DList>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->size() == 2);
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
auto x0 = obj<AGCObject,DFloat>::from(x->at(0));
|
||||
REQUIRE(x0);
|
||||
REQUIRE(x0->value() == 4.5);
|
||||
|
||||
bool eof_flag = true;
|
||||
auto x1 = obj<AGCObject,DFloat>::from(x->at(1));
|
||||
REQUIRE(x1);
|
||||
REQUIRE(x1->value() == 7.2);
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
return true;
|
||||
};
|
||||
|
||||
span_type input = span_type::from_cstr("#q{ (4.5 7.2) };");
|
||||
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
auto x = obj<AGCObject,DList>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->size() == 2);
|
||||
auto x0 = obj<AGCObject,DFloat>::from(x->at(0));
|
||||
REQUIRE(x0);
|
||||
REQUIRE(x0->value() == 4.5);
|
||||
auto x1 = obj<AGCObject,DFloat>::from(x->at(1));
|
||||
REQUIRE(x1);
|
||||
REQUIRE(x1->value() == 7.2);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
//log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"#q{ (4.5 7.2) };",
|
||||
verify_fn,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-qarray", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = true;
|
||||
|
||||
bool c_debug_flag = true;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DArray>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->size() == 2);
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
auto x0 = obj<AGCObject,DFloat>::from(x->at(0));
|
||||
REQUIRE(x0);
|
||||
REQUIRE(x0->value() == 4.5);
|
||||
|
||||
bool eof_flag = true;
|
||||
auto x1 = obj<AGCObject,DFloat>::from(x->at(1));
|
||||
REQUIRE(x1);
|
||||
REQUIRE(x1->value() == 7.2);
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
return true;
|
||||
};
|
||||
|
||||
span_type input = span_type::from_cstr("#q{ [4.5 7.2] };");
|
||||
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
auto x = obj<AGCObject,DArray>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->size() == 2);
|
||||
auto x0 = obj<AGCObject,DFloat>::from(x->at(0));
|
||||
REQUIRE(x0);
|
||||
REQUIRE(x0->value() == 4.5);
|
||||
auto x1 = obj<AGCObject,DFloat>::from(x->at(1));
|
||||
REQUIRE(x1);
|
||||
REQUIRE(x1->value() == 7.2);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
//log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"#q{ [4.5 7.2] };",
|
||||
verify_fn,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
}
|
||||
|
||||
TEST_CASE("VirtualSchematikaMachine-report_memory_use", "[interpreter2][VSM]")
|
||||
{
|
||||
const auto & testname = Catch::getResultCapture().getCurrentTestName();
|
||||
constexpr bool c_debug_flag = false;
|
||||
|
||||
bool c_debug_flag = false;
|
||||
scope log(XO_DEBUG(c_debug_flag), xtag("test", testname));
|
||||
auto verify_fn = [](const VsmResultExt & res) {
|
||||
auto x = obj<AGCObject,DBoolean>::from(*res.value());
|
||||
|
||||
VsmFixture vsm_fixture(testname, c_debug_flag,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
auto & vsm = vsm_fixture.vsm_;
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == true);
|
||||
|
||||
bool eof_flag = true;
|
||||
return true;
|
||||
};
|
||||
|
||||
vsm->begin_interactive_session();
|
||||
|
||||
span_type input = span_type::from_cstr("report-memory-use();");
|
||||
|
||||
log && log(xtag("input", input));
|
||||
|
||||
VsmResultExt res
|
||||
= vsm->read_eval_print(input, eof_flag);
|
||||
|
||||
REQUIRE(res.is_value());
|
||||
REQUIRE(res.value());
|
||||
|
||||
log && log(xtag("res.tseq", res.value()->_typeseq()),
|
||||
xtag("res.type", TypeRegistry::id2name(res.value()->_typeseq())));
|
||||
|
||||
auto x = obj<AGCObject,DBoolean>::from(*res.value());
|
||||
REQUIRE(x);
|
||||
REQUIRE(x->value() == true);
|
||||
|
||||
REQUIRE(res.remaining_.size() == 1);
|
||||
REQUIRE(*res.remaining_.lo() == '\n');
|
||||
|
||||
//log && vsm_fixture.log_memory_layout(&log);
|
||||
vsm_std_utest_pattern(c_debug_flag, testname,
|
||||
"report-memory-use();",
|
||||
verify_fn,
|
||||
VsmConfig().with_parser_debug_flag(c_debug_flag));
|
||||
}
|
||||
} /*namespace ut*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue