From ac2949394abcfeb0be8c9addf47504a34bfbee0a Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Fri, 30 Sep 2016 22:40:23 +0200 Subject: [PATCH] Add tests for std::function --- tstl/test_suite/function.cpp | 62 ++++++++++++++++++++++++++++++++++++ tstl/test_suite/test.cpp | 2 ++ 2 files changed, 64 insertions(+) create mode 100644 tstl/test_suite/function.cpp diff --git a/tstl/test_suite/function.cpp b/tstl/test_suite/function.cpp new file mode 100644 index 00000000..de858667 --- /dev/null +++ b/tstl/test_suite/function.cpp @@ -0,0 +1,62 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2016. +// Distributed under the terms of the MIT License. +// (See accompanying file LICENSE or copy at +// http://www.opensource.org/licenses/MIT) +//======================================================================= + +#include +#include +#include + +#include + +#include "test.hpp" + +namespace { + +int foo(int& ref){ + ++ref; + return ref; +} + +void test_function_ptr(){ + std::function f(foo); + + int a = 4; + auto c = f(a); + + check(c == 5, "function: function_ptr error"); + check(a == 5, "function: function_ptr error"); +} + +void test_lambda(){ + auto l = [](int& ref){ ++ref; return ref; }; + std::function f(l); + + int a = 2; + auto c = f(a); + + check(c == 3, "function: lambda error"); + check(a == 3, "function: lambda error"); +} + +void test_lambda_state(){ + int a = 3; + + auto l = [&a](){ ++a; return a; }; + std::function f(l); + + auto c = f(); + + check(c == 4, "function: lambda error"); + check(a == 4, "function: lambda error"); +} + +} //end of anonymous namespace + +void function_tests(){ + test_function_ptr(); + test_lambda(); + test_lambda_state(); +} diff --git a/tstl/test_suite/test.cpp b/tstl/test_suite/test.cpp index 00c246f4..29d29070 100644 --- a/tstl/test_suite/test.cpp +++ b/tstl/test_suite/test.cpp @@ -11,6 +11,7 @@ #include "test.hpp" void string_tests(); +void function_tests(); void tuple_tests(); void vector_tests(); void list_tests(); @@ -28,6 +29,7 @@ int main(){ vector_tests(); shared_ptr_tests(); list_tests(); + function_tests(); printf("All tests finished\n");