From ec660dc8dca7fe74786c28669cb45fe61f89756c Mon Sep 17 00:00:00 2001 From: fastium Date: Sun, 20 Oct 2024 18:05:12 +0200 Subject: [PATCH 1/2] ADD unique_ptr tests (run on onlineGDB) --- TESTS/simple-test/unique-ptr/main.cpp | 189 ++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 TESTS/simple-test/unique-ptr/main.cpp diff --git a/TESTS/simple-test/unique-ptr/main.cpp b/TESTS/simple-test/unique-ptr/main.cpp new file mode 100644 index 0000000..76d5a1d --- /dev/null +++ b/TESTS/simple-test/unique-ptr/main.cpp @@ -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 + +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 p1 = std::make_unique(); + 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 p1 = std::make_unique(); + TEST_ASSERT_EQUAL(Test::kMagicNumber, p1->_value); + TEST_ASSERT_EQUAL(1, Test::_instanceCount); + + //transfer p1 to p2 + std::unique_ptr 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 p1 = std::make_unique(); + 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 p1 = std::make_unique(); + 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 p2 = std::make_unique(); + 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); } \ No newline at end of file From 6e8089d0168315082c07f779adeede5136f7979f Mon Sep 17 00:00:00 2001 From: Klagarge Date: Mon, 21 Oct 2024 16:04:35 +0200 Subject: [PATCH 2/2] UPD github workflow to include test-unique-ptr --- .github/workflows/build-test.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index b27e936..f61d2f2 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -14,7 +14,11 @@ jobs: matrix: target: [DISCO_H747I] profile: [develop, debug, release] - tests: [tests-simple-test-always-succeed, tests-simple-test-ptr-test] + tests: [ + tests-simple-test-always-succeed, + tests-simple-test-ptr-test, + tests-simple-unique-ptr + ] steps: