Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
scriptparser.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/timer/timer.hpp>
#include <ored/scripting/scriptparser.hpp>
#include <ored/scripting/astprinter.hpp>
#include <ored/scripting/asttoscriptconverter.hpp>
#include <ored/scripting/randomastgenerator.hpp>
#include <oret/toplevelfixture.hpp>
#include <iostream>
#include <iomanip>

Go to the source code of this file.

Functions

 BOOST_DATA_TEST_CASE (testScriptParsing, boost::unit_test::data::make(scriptData), testScript)
 
 BOOST_DATA_TEST_CASE (testRoundTrip, boost::unit_test::data::make(scriptData), testScript)
 
 BOOST_AUTO_TEST_CASE (testRandomRoundTrip)
 
 BOOST_AUTO_TEST_CASE (generateRandomScript, *boost::unit_test::disabled())
 
 BOOST_AUTO_TEST_CASE (testInteractive, *boost::unit_test::disabled())
 

Function Documentation

◆ BOOST_DATA_TEST_CASE() [1/2]

BOOST_DATA_TEST_CASE ( testScriptParsing  ,
boost::unit_test::data::make(scriptData)  ,
testScript   
)

Definition at line 538 of file scriptparser.cpp.

538 {
539
540 BOOST_TEST_MESSAGE("Testing Script Parser...");
541
542 ScriptParser parser(testScript.script);
543 if (parser.success()) {
544 BOOST_TEST_MESSAGE("Parsing succeeded\n" << to_string(parser.ast()));
545 } else {
546 BOOST_TEST_MESSAGE("Parsing failed\n" << parser.error());
547 }
548 BOOST_REQUIRE(parser.success());
549 BOOST_CHECK_EQUAL(to_string(parser.ast(), false), testScript.expectedAST);
550}
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
+ Here is the call graph for this function:

◆ BOOST_DATA_TEST_CASE() [2/2]

BOOST_DATA_TEST_CASE ( testRoundTrip  ,
boost::unit_test::data::make(scriptData)  ,
testScript   
)

Definition at line 552 of file scriptparser.cpp.

552 {
553
554 BOOST_TEST_MESSAGE("Testing Script Parser AST->Script->AST Roundtrip...");
555
556 ScriptParser parser(testScript.script);
557 BOOST_REQUIRE(parser.success());
558 std::string script = to_script(parser.ast());
559 BOOST_TEST_MESSAGE("Generated script:\n<<<<<<<<<<\n" + script + "\n>>>>>>>>>>");
560 ScriptParser parser2(script);
561 BOOST_REQUIRE(parser2.success());
562 BOOST_CHECK_EQUAL(to_string(parser.ast(), false), to_string(parser2.ast(), false));
563}
std::string script
std::string to_script(const ASTNodePtr root)
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [1/3]

BOOST_AUTO_TEST_CASE ( testRandomRoundTrip  )

Definition at line 565 of file scriptparser.cpp.

565 {
566
567 BOOST_TEST_MESSAGE("Testing Script Parser Random AST->Script->AST Roundtrip...");
568
569 std::vector<std::tuple<Size, Size, Size>> testSizes;
570 testSizes.push_back(std::make_tuple(1, 5, 1000));
571 testSizes.push_back(std::make_tuple(5, 5, 1000));
572 testSizes.push_back(std::make_tuple(10, 5, 1000));
573 testSizes.push_back(std::make_tuple(1, 10, 1000));
574 testSizes.push_back(std::make_tuple(5, 10, 1000));
575 testSizes.push_back(std::make_tuple(10, 10, 100));
576
577 for (auto const& t : testSizes) {
578 BOOST_TEST_MESSAGE("Testing Script Parser Random AST->Script->AST RoundTrip (len="
579 << std::get<0>(t) << ", "
580 << "dep=" << std::get<1>(t) << ", n=" << std::get<2>(t) << ")");
581 Size maxLen = 0;
582 double avgLen = 0.0, avgTiming = 0.0, maxTiming = 0.0;
583 for (Size i = 0; i < std::get<2>(t); ++i) {
584 auto ast = generateRandomAST(std::get<0>(t), std::get<1>(t), 42 + i);
585 std::string script = to_script(ast);
586 maxLen = std::max(script.length(), maxLen);
587 avgLen += static_cast<double>(script.length()) / static_cast<double>(std::get<2>(t));
588 boost::timer::cpu_timer timer;
589 ScriptParser parser(script);
590 if (!parser.success()) {
591 BOOST_TEST_MESSAGE("Error while parsing script: " << parser.error());
592 }
593 BOOST_REQUIRE(parser.success());
594 double timing = timer.elapsed().wall * 1e-9;
595 maxTiming = std::max(timing, maxTiming);
596 avgTiming += timing / static_cast<double>(std::get<2>(t));
597 BOOST_CHECK_EQUAL(to_string(ast, false), to_string(parser.ast(), false));
598 }
599 BOOST_TEST_MESSAGE("Finished, script size avg = " << avgLen << ", max = " << maxLen
600 << ", timing avg = " << avgTiming * 1E3
601 << " ms, max = " << maxTiming * 1E3 << " ms");
602 }
603}
ASTNodePtr generateRandomAST(const Size maxSequenceLength, const Size maxDepth, const Size seed)
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/3]

BOOST_AUTO_TEST_CASE ( generateRandomScript  ,
boost::unit_test::disabled() 
)

Definition at line 605 of file scriptparser.cpp.

605 {
606
607 // not a test, just for convenience, to be removed at some stage...
608
609 BOOST_TEST_MESSAGE("Creating randomg script based on LEN, DEP and SEED env variables");
610
611 if (getenv("LEN") && getenv("DEP") && getenv("SEED")) {
612 auto ast = generateRandomAST(atoi(getenv("LEN")), atoi(getenv("DEP")), atoi(getenv("SEED")));
613 std::string script = to_script(ast);
614 BOOST_TEST_MESSAGE("Generated script:\n<<<<<<<<<<\n" + script + "\n>>>>>>>>>>");
615 }
616}
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/3]

BOOST_AUTO_TEST_CASE ( testInteractive  ,
boost::unit_test::disabled() 
)

Definition at line 618 of file scriptparser.cpp.

618 {
619
620 // not a test, just for convenience, to be removed at some stage...
621
622 BOOST_TEST_MESSAGE("Running Script Parser on INPUT env variable...");
623
624 std::string script = "IF x==2 THEN y=1; ELSE z=2; END;";
625 if (auto c = getenv("INPUT"))
626 script = std::string(c);
627
628 ScriptParser parser(script);
629 if (parser.success()) {
630 std::cout << "Parsing succeeded\n" << to_string(parser.ast()) << std::endl;
631 } else {
632 std::cout << "Parsing failed\n" << parser.error();
633 }
634}
+ Here is the call graph for this function: