/****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl http://www.etlcpp.com Copyright(c) 2014 jwellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #include "UnitTest++.h" #include "etl/delegate.h" namespace { //***************************************************************************** const int VALUE1 = 1; const int VALUE2 = 2; bool function_called = false; bool parameter_correct = false; //***************************************************************************** // Test data structure. //***************************************************************************** struct Data { int d; }; Data data; //***************************************************************************** // The free function taking no parameters. //***************************************************************************** void free_void() { function_called = true; } //***************************************************************************** // The free function taking an int parameter. //***************************************************************************** void free_int(int i, int j) { function_called = true; parameter_correct = (i == VALUE1) && (j == VALUE2); } //***************************************************************************** // The free function taking a Data reference parameter. //***************************************************************************** void free_reference(const Data& data, int j) { function_called = true; parameter_correct = (data.d == VALUE1) && (j = VALUE2); } //***************************************************************************** // The test class with member functions. //***************************************************************************** class Test { public: //******************************************* // void void member_void() { function_called = true; } void member_void_const() const { function_called = true; } //******************************************* // int void member_int(int i, int j) { function_called = true; parameter_correct = (i == VALUE1) && (j == VALUE2); } void member_int_const(int i, int j) const { function_called = true; parameter_correct = (i == VALUE1) && (j == VALUE2); } //******************************************* // reference void member_reference(const Data& data, int j) { function_called = true; parameter_correct = (data.d == VALUE1) && (j = VALUE2); } void member_reference_const(const Data& data, int j) const { function_called = true; parameter_correct = (data.d == VALUE1) && (j = VALUE2); } //******************************************* // static static void member_static(const Data& data, int j) { function_called = true; parameter_correct = (data.d == VALUE1) && (j = VALUE2); } //******************************************* // operator() void operator()() { function_called = true; } void operator()() const { function_called = true; } }; Test test_static; const Test const_test_static; } //***************************************************************************** // Initialises the test results. //***************************************************************************** struct SetupFixture { SetupFixture() { function_called = false; parameter_correct = false; } }; namespace { SUITE(test_delegate) { //************************************************************************* TEST_FIXTURE(SetupFixture, test_is_valid_false) { etl::delegate d; CHECK(!d.is_valid()); CHECK(!d); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_is_valid_true) { etl::delegate d([] {}); CHECK(d.is_valid()); CHECK(d); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_free_void) { etl::delegate d = etl::delegate::create(); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_free_int) { etl::delegate d = etl::delegate::create(); d(VALUE1, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_free_reference) { etl::delegate d = etl::delegate::create(); Data data; data.d = VALUE1; d(data, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_lambda_int) { etl::delegate d([](int i, int j) { function_called = true; parameter_correct = (i == VALUE1) && (j == VALUE2); }); d(VALUE1, VALUE2); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_lambda_int_create) { auto lambda = [](int i, int j) { function_called = true; parameter_correct = (i == VALUE1) && (j == VALUE2); }; etl::delegate d(lambda); d(VALUE1, VALUE2); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_operator_void) { Test test; etl::delegate d(test); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_operator_void_create) { Test test; etl::delegate d = etl::delegate::create(test); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_operator_void_const) { const Test test; etl::delegate d(test); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_assignment_member_operator_void) { Test test; etl::delegate d; d = test; d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_void) { Test test; etl::delegate d = etl::delegate::create(test); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_void_const) { const Test test; etl::delegate d = etl::delegate::create(test); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int) { Test test; etl::delegate d = etl::delegate::create(test); d(VALUE1, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int_const) { const Test test; etl::delegate d = etl::delegate::create(test); d(VALUE1, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference) { Test test; etl::delegate d = etl::delegate::create(test); Data data; data.d = VALUE1; d(data, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference_const) { const Test test; etl::delegate d = etl::delegate::create(test); Data data; data.d = VALUE1; d(data, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_static) { etl::delegate d = etl::delegate::create(); Data data; data.d = VALUE1; d(data, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_void_compile_time) { etl::delegate d = etl::delegate::create(); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_void_const_compile_time) { etl::delegate d = etl::delegate::create(); d(); CHECK(function_called); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int_compile_time) { etl::delegate d = etl::delegate::create(); d(VALUE1, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_int_const_compile_time) { etl::delegate d = etl::delegate::create(); d(VALUE1, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference_compile_time) { etl::delegate d = etl::delegate::create(); Data data; data.d = VALUE1; d(data, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_member_reference_const_compile_time) { etl::delegate d = etl::delegate::create(); Data data; data.d = VALUE1; d(data, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_copy_construct) { Test test; etl::delegate d1 = etl::delegate::create(test); etl::delegate d2(d1); d2(VALUE1, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_assignment) { Test test; etl::delegate d1 = etl::delegate::create(test); etl::delegate d2; d2 = d1; d2(VALUE1, VALUE2); CHECK(function_called); CHECK(parameter_correct); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_delegate_equal) { Test test; etl::delegate d1 = etl::delegate::create(test); etl::delegate d2 = d1; CHECK(d1 == d2); } //************************************************************************* TEST_FIXTURE(SetupFixture, test_delegate_not_equal) { Test test; etl::delegate d1 = etl::delegate::create(test); etl::delegate d2 = etl::delegate::create(test);; CHECK(d1 != d2); } }; }