FIX issues on raw and unique pointer tests + Merge individual test files for unique and raw pointers and merged their test cases into a single test file for simplicity.
		
			
				
	
	
		
			288 lines
		
	
	
		
			8.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			288 lines
		
	
	
		
			8.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // 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 Serge Ayer <serge.ayer@hefr.ch>
 | |
|  * @author Yann Sierro <yannsierro.pro@gmail.com>
 | |
|  * @author Rémi Heredero <remi@heredero.ch>
 | |
|  *
 | |
|  * @brief Simple example of test program for raw and shared pointers
 | |
|  *
 | |
|  * @date 2024-11-02
 | |
|  * @version 0.2.0
 | |
|  ***************************************************************************/
 | |
| 
 | |
| #include "greentea-client/test_env.h"
 | |
| #include "mbed.h"
 | |
| #include "unity/unity.h"
 | |
| #include "utest/utest.h"
 | |
| 
 | |
| using namespace utest::v1;
 | |
| struct Test {
 | |
|     Test() {
 | |
|         _instanceCount++;
 | |
|         _value = kMagicNumber;
 | |
|     }
 | |
| 
 | |
|     ~Test() {
 | |
|         _instanceCount--;
 | |
|         _value = 0;
 | |
|     }
 | |
| 
 | |
|     int _value;
 | |
|     static constexpr uint32_t kMagicNumber = 33;
 | |
|     static uint32_t _instanceCount;
 | |
| };
 | |
| uint32_t Test::_instanceCount = 0;
 | |
| 
 | |
| /**
 | |
|  * Test that a shared pointer correctly manages the lifetime of the underlying raw pointer
 | |
|  */
 | |
| void test_single_sharedptr_lifetime() {
 | |
|     // Sanity-check value of counter
 | |
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount);
 | |
| 
 | |
|     // Create and destroy shared pointer in given scope
 | |
|     {
 | |
|         std::shared_ptr<Test> shared_ptr(new Test);
 | |
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount);
 | |
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, shared_ptr->_value);
 | |
|     }
 | |
| 
 | |
|     // Destroy shared pointer
 | |
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount);
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Test that multiple instances of shared pointers correctly manage the reference count
 | |
|  * to release the object at the correct point
 | |
|  */
 | |
| void test_instance_sharing() {
 | |
|     std::shared_ptr<Test> shared_ptr1(nullptr);
 | |
| 
 | |
|     // Sanity-check value of counter
 | |
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount);
 | |
| 
 | |
|     // Create and destroy shared pointer in given scope
 | |
|     {
 | |
|         std::shared_ptr<Test> shared_ptr2(new Test);
 | |
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount);
 | |
|         // share share_ptr2 with shared_ptr1
 | |
|         shared_ptr1 = shared_ptr2;
 | |
|         // still one instance only
 | |
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount);
 | |
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, shared_ptr1->_value);
 | |
|         TEST_ASSERT(shared_ptr1.get() == shared_ptr2.get());
 | |
|     }
 | |
| 
 | |
|     // shared_ptr1 still owns a raw pointer
 | |
|     TEST_ASSERT_EQUAL(1, Test::_instanceCount);
 | |
| 
 | |
|     shared_ptr1 = nullptr;
 | |
| 
 | |
|     // Shared pointer has been destroyed
 | |
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount);
 | |
| }
 | |
| 
 | |
| /**********************
 | |
| * UNIQUE PTR EXERCISE *
 | |
| **********************/
 | |
| 
 | |
| /*
 | |
|  * 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;
 | |
|         p2 = nullptr;
 | |
| 
 | |
|         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(2, 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);
 | |
| }
 | |
| 
 | |
| 
 | |
| 
 | |
| /*******************
 | |
| * RAW PTR EXERCISE *
 | |
| *******************/
 | |
| 
 | |
| /**
 | |
|  * Test that a shared pointer correctly manages the lifetime of the underlying raw pointer
 | |
|  */
 | |
| void test_single_raw_ptr_lifetime() {
 | |
|     // Sanity-check value of counter
 | |
|     TEST_ASSERT_EQUAL(0, Test::_instanceCount);
 | |
| 
 | |
|     // Create and destroy raw pointer in given scope
 | |
|     {
 | |
|         Test t1;
 | |
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount);
 | |
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, t1._value);
 | |
| 
 | |
|         Test * p1 = &t1;
 | |
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount);
 | |
|         TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value);
 | |
| 
 | |
|         const uint32_t number1 = 42;
 | |
|         p1->_value = number1;
 | |
|         TEST_ASSERT_EQUAL(number1, p1->_value);
 | |
|         TEST_ASSERT_EQUAL(number1, t1._value);
 | |
| 
 | |
|         p1 = nullptr;
 | |
|         TEST_ASSERT_EQUAL(1, Test::_instanceCount);
 | |
|         TEST_ASSERT(!p1);
 | |
|     }
 | |
| 
 | |
|     // Destroy shared pointer
 | |
|     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[] = {
 | |
|     // Shared test pointer
 | |
|     Case("Test single shared pointer instance", test_single_sharedptr_lifetime),
 | |
|     Case("Test instance sharing across multiple shared pointers", test_instance_sharing),
 | |
| 
 | |
|     // Unique test pointer
 | |
|     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),
 | |
| 
 | |
|     // Raw test pointer
 | |
|     Case("Test single raw pointer instance", test_single_raw_ptr_lifetime),
 | |
| };
 | |
| 
 | |
| static Specification specification(greentea_setup, cases);
 | |
| 
 | |
| int main() { return !Harness::run(specification); } |