From f3090c38576adae7e8d3d592f9de50fea803c22b Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 12 Apr 2015 19:20:02 -0600 Subject: [PATCH] Add test for number of moves/copies made For #165 --- CMakeLists.txt | 5 ++ unittests/future.chai | 4 +- unittests/object_copy_count_test.cpp | 80 ++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 unittests/object_copy_count_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e325f2..cdc105a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -317,6 +317,11 @@ if(BUILD_TESTING) target_link_libraries(object_lifetime_test2 ${LIBS}) add_test(NAME Object_Lifetime_Test2 COMMAND object_lifetime_test2) + add_executable(object_copy_count_test unittests/object_copy_count_test.cpp) + target_link_libraries(object_copy_count_test ${LIBS}) + add_test(NAME Object_Copy_Test COMMAND object_copy_count_test) + + add_executable(function_ordering_test unittests/function_ordering_test.cpp) target_link_libraries(function_ordering_test ${LIBS}) add_test(NAME Function_Ordering_Test COMMAND function_ordering_test) diff --git a/unittests/future.chai b/unittests/future.chai index 750c27d..b73fe52 100644 --- a/unittests/future.chai +++ b/unittests/future.chai @@ -7,8 +7,8 @@ var func = fun(){ } -var fut1 := async(func); -var fut2 := async(func); +var fut1 = async(func); +var fut2 = async(func); // simply executing without crashing is good enough for this test diff --git a/unittests/object_copy_count_test.cpp b/unittests/object_copy_count_test.cpp new file mode 100644 index 0000000..8b75d3e --- /dev/null +++ b/unittests/object_copy_count_test.cpp @@ -0,0 +1,80 @@ +#include + +class Test +{ + public: + Test() + { + std::cout << "Test()\n"; + ++constructcount(); + } + + Test(const Test &) + { + std::cout << "Test(const Test &)\n"; + ++copycount(); + } + + Test(Test &&) + { + std::cout << "Test(Test &&)\n"; + ++movecount(); + } + + ~Test() + { + std::cout << "~Test()\n"; + ++destructcount(); + } + + static int& constructcount() + { + static int c = 0; + return c; + } + + static int& copycount() + { + static int c = 0; + return c; + } + + static int& movecount() + { + static int c = 0; + return c; + } + + static int& destructcount() + { + static int c = 0; + return c; + } +}; + +Test create() +{ + return Test(); +} + +int main() +{ + chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module()); + + m->add(chaiscript::user_type(), "Test"); + m->add(chaiscript::constructor(), "Test"); + m->add(chaiscript::constructor(), "Test"); + m->add(chaiscript::fun(&create), "create"); + + chaiscript::ChaiScript chai; + chai.add(m); + + chai.eval(" { auto i = create(); } "); + + if (Test::destructcount() == 2 && Test::copycount() == 0 && Test::movecount() == 1 && Test::constructcount() == 1) + { + return EXIT_SUCCESS; + } else { + return EXIT_FAILURE; + } +}