From f0016d978ad3cc6792efa7d7afeab59ebf1219f7 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 16 Jul 2012 21:51:06 -0600 Subject: [PATCH] Add test to make sure simultaneous ChaiScript instantiations work --- CMakeLists.txt | 4 ++ unittests/simultaneous_chaiscript_test.cpp | 60 ++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 unittests/simultaneous_chaiscript_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 08029b1..0d9b474 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -200,6 +200,10 @@ if(BUILD_TESTING) target_link_libraries(set_state_test ${LIBS}) add_test(NAME Set_State_Test COMMAND set_state_test) + add_executable(simultaneous_chaiscript_test unittests/simultaneous_chaiscript_test.cpp) + target_link_libraries(simultaneous_chaiscript_test ${LIBS}) + add_test(NAME Simultaneous_Chaiscript_Test COMMAND simultaneous_chaiscript_test) + if (MULTITHREAD_SUPPORT_ENABLED) add_executable(multithreaded_test unittests/multithreaded_test.cpp) target_link_libraries(multithreaded_test ${LIBS}) diff --git a/unittests/simultaneous_chaiscript_test.cpp b/unittests/simultaneous_chaiscript_test.cpp new file mode 100644 index 0000000..238f70a --- /dev/null +++ b/unittests/simultaneous_chaiscript_test.cpp @@ -0,0 +1,60 @@ +#include + +int dosomething(int i) +{ + return i + 2; +} + +int dosomethingelse(int i) +{ + return i * 2; +} + + + +int main() +{ + chaiscript::ChaiScript chai; + chai.add(chaiscript::fun(&dosomething), "dosomething"); + chai.add(chaiscript::var(1), "i"); + + for (int i = 0; i < 10; ++i) + { + chaiscript::ChaiScript chai2; + chai2.add(chaiscript::fun(&dosomethingelse), "dosomethingelse"); + + std::stringstream ss; + ss << i; + + if (chai.eval("dosomething(" + ss.str() + ")") != i + 2) + { + return EXIT_FAILURE; + } + + if (chai2.eval("dosomethingelse(" + ss.str() + ")") != i * 2) + { + return EXIT_FAILURE; + } + + try { + chai2.eval("dosomething(1)"); + return EXIT_FAILURE; // should not get here + } catch (const chaiscript::exception::eval_error &) { + // nothing to do, expected case + } + + try { + chai2.eval("i"); + return EXIT_FAILURE; // should not get here + } catch (const chaiscript::exception::eval_error &) { + // nothing to do, expected case + } + + try { + chai.eval("dosomethingelse(1)"); + return EXIT_FAILURE; // should not get here + } catch (const chaiscript::exception::eval_error &) { + // nothing to do, expected case + } + } +}