Add tests for std::function

This commit is contained in:
Baptiste Wicht 2016-09-30 22:40:23 +02:00
parent 9e5d22f631
commit ac2949394a
No known key found for this signature in database
GPG Key ID: C5566B6C7F884532
2 changed files with 64 additions and 0 deletions

View File

@ -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 <cstdio>
#include <cstring>
#include <new>
#include <function.hpp>
#include "test.hpp"
namespace {
int foo(int& ref){
++ref;
return ref;
}
void test_function_ptr(){
std::function<int(int&)> 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<int(int&)> 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<int()> 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();
}

View File

@ -11,6 +11,7 @@
#include "test.hpp" #include "test.hpp"
void string_tests(); void string_tests();
void function_tests();
void tuple_tests(); void tuple_tests();
void vector_tests(); void vector_tests();
void list_tests(); void list_tests();
@ -28,6 +29,7 @@ int main(){
vector_tests(); vector_tests();
shared_ptr_tests(); shared_ptr_tests();
list_tests(); list_tests();
function_tests();
printf("All tests finished\n"); printf("All tests finished\n");