ADD test unique_ptr
- ADD unique_ptr tests (run on onlineGDB) - UPD github workflow to include test-unique-ptr
This commit is contained in:
		
							
								
								
									
										1
									
								
								.github/workflows/build-test.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.github/workflows/build-test.yml
									
									
									
									
										vendored
									
									
								
							| @@ -17,6 +17,7 @@ jobs: | ||||
|         tests: [ | ||||
|           tests-simple-test-always-succeed, | ||||
|           tests-simple-test-ptr-test, | ||||
|           tests-simple-unique-ptr, | ||||
|           tests-simple-test-raw-ptr | ||||
|         ] | ||||
|  | ||||
|   | ||||
							
								
								
									
										189
									
								
								TESTS/simple-test/unique-ptr/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										189
									
								
								TESTS/simple-test/unique-ptr/main.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,189 @@ | ||||
| // Copyright 2022 Haute école d'ingénierie et d'architecture de Fribourg | ||||
| // | ||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| // you may not use this file except in compliance with the License. | ||||
| // You may obtain a copy of the License at | ||||
| // | ||||
| //     http://www.apache.org/licenses/LICENSE-2.0 | ||||
| // | ||||
| // Unless required by applicable law or agreed to in writing, software | ||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| // See the License for the specific language governing permissions and | ||||
| // limitations under the License. | ||||
|  | ||||
| /**************************************************************************** | ||||
|  * @file main.cpp | ||||
|  * @author Sierro Yann | ||||
|  * | ||||
|  * @brief Test for std::unique_ptr template class | ||||
|  * | ||||
|  * @date 2024-10-20 | ||||
|  * @version 0.1.0 | ||||
|  ***************************************************************************/ | ||||
|  | ||||
| #include "greentea-client/test_env.h" | ||||
| #include "mbed.h" | ||||
| #include "unity/unity.h" | ||||
| #include "utest/utest.h" | ||||
| #include <memory> | ||||
|  | ||||
| using namespace utest::v1; | ||||
| struct Test { | ||||
|     Test() { | ||||
|         _instanceCount++; | ||||
|         _value = kMagicNumber; | ||||
|     } | ||||
|  | ||||
|     ~Test() { | ||||
|         _instanceCount--; | ||||
|         _value = 0; | ||||
|     } | ||||
|  | ||||
|     uint32_t _value; | ||||
|     static constexpr uint32_t kMagicNumber = 33; | ||||
|     static uint32_t _instanceCount; | ||||
| }; | ||||
| uint32_t Test::_instanceCount = 0; | ||||
|  | ||||
| /* | ||||
|  * Check normal lifetime on a unique_ptr | ||||
| */ | ||||
| void test_single_unique_ptr_lifetime() { | ||||
|     // Sanity-check value of counter | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
|  | ||||
|     // create and destroy a unique_ptr | ||||
|     { | ||||
|         std::unique_ptr<Test> p1 = std::make_unique<Test>(); | ||||
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount); | ||||
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value); | ||||
|  | ||||
|         const uint32_t number = 42; | ||||
|         p1->_value = number; | ||||
|         TEST_ASSERT_EQUAL(number, p1->_value); | ||||
|  | ||||
|         p1.reset(); | ||||
|         TEST_ASSERT(!p1); | ||||
|     } | ||||
|  | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
| } | ||||
|  | ||||
| /* | ||||
|  * Check transfer on a unique_ptr | ||||
| */ | ||||
| void test_unique_ptr_transfer() { | ||||
|     // Sanity-check value of counter | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
|  | ||||
|     { | ||||
|         //create p1 | ||||
|         std::unique_ptr<Test> p1 = std::make_unique<Test>(); | ||||
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value); | ||||
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount); | ||||
|  | ||||
|         //transfer p1 to p2 | ||||
|         std::unique_ptr<Test> p2 = std::move(p1); | ||||
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value); | ||||
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount); | ||||
|  | ||||
|         p2.reset(); | ||||
|  | ||||
|         TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
|         TEST_ASSERT(!p1); | ||||
|         TEST_ASSERT(!p2); | ||||
|     } | ||||
|  | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
| } | ||||
|  | ||||
| /* | ||||
|  * Check the release of a unique ptr | ||||
| */ | ||||
| void test_unique_ptr_release() { | ||||
|     // Sanity-check value of counter | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
|  | ||||
|     { | ||||
|         //create p1 | ||||
|         std::unique_ptr<Test> p1 = std::make_unique<Test>(); | ||||
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value); | ||||
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount); | ||||
|  | ||||
|         //transfer and release p1 to p2 | ||||
|         Test * p2 = p1.release(); | ||||
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value); | ||||
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount); | ||||
|  | ||||
|         delete p2; | ||||
|  | ||||
|         TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
|         TEST_ASSERT(!p1); | ||||
|         TEST_ASSERT(!p2); | ||||
|     } | ||||
|  | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
| } | ||||
|  | ||||
| /* | ||||
|  * Check the swap of 2 unique ptr | ||||
| */ | ||||
| void test_unique_ptr_swap() { | ||||
|     // Sanity-check value of counter | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
|  | ||||
|     { | ||||
|         const uint32_t number1 = 65; | ||||
|         const uint32_t number2 = 42; | ||||
|          | ||||
|         //create p1 | ||||
|         std::unique_ptr<Test> p1 = std::make_unique<Test>(); | ||||
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value); | ||||
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount); | ||||
|         p1->_value = number1; | ||||
|         TEST_ASSERT_EQUAL(number1, p1->_value); | ||||
|  | ||||
|         //create p2 | ||||
|         std::unique_ptr<Test> p2 = std::make_unique<Test>(); | ||||
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p2->_value); | ||||
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount); | ||||
|         p2->_value = number2; | ||||
|         TEST_ASSERT_EQUAL(number2, p2->_value); | ||||
|          | ||||
|         //swap | ||||
|         p1.swap(p2); | ||||
|  | ||||
|         TEST_ASSERT_EQUAL(number1, p2->_value); | ||||
|         TEST_ASSERT_EQUAL(number2, p1->_value); | ||||
|  | ||||
|         p1.reset(); | ||||
|         p2.reset(); | ||||
|  | ||||
|         TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
|         TEST_ASSERT(!p1); | ||||
|         TEST_ASSERT(!p2); | ||||
|     } | ||||
|  | ||||
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount); | ||||
| } | ||||
|  | ||||
| static utest::v1::status_t greentea_setup(const size_t number_of_cases) { | ||||
|     // Here, we specify the timeout (60s) and the host test (a built-in host test or the | ||||
|     // name of our Python file) | ||||
|     GREENTEA_SETUP(60, "default_auto"); | ||||
|     return greentea_test_setup_handler(number_of_cases); | ||||
| } | ||||
|  | ||||
| // List of test cases in this file | ||||
| static Case cases[] = { | ||||
|     Case("Test single unique pointer instance", test_single_unique_ptr_lifetime), | ||||
|     Case("Test transfer of unique pointer instance", test_unique_ptr_transfer), | ||||
|     Case("Test release of unique pointer instance", test_unique_ptr_release), | ||||
|     Case("Test swap of 2 unique ptr instance", test_unique_ptr_swap) | ||||
|      | ||||
|     }; | ||||
|  | ||||
| static Specification specification(greentea_setup, cases); | ||||
|  | ||||
| int main() { return !Harness::run(specification); } | ||||
		Reference in New Issue
	
	Block a user