mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-17 08:37:17 -04:00
Add tests for std::function
This commit is contained in:
parent
9e5d22f631
commit
ac2949394a
62
tstl/test_suite/function.cpp
Normal file
62
tstl/test_suite/function.cpp
Normal 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();
|
||||||
|
}
|
@ -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");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user