FIX raw + unique ptr tests
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.
This commit is contained in:
parent
df8edf3c34
commit
15d1183bac
4
.github/workflows/build-test.yml
vendored
4
.github/workflows/build-test.yml
vendored
@ -13,12 +13,10 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
target: [DISCO_H747I]
|
||||
profile: [develop, debug, release]
|
||||
profile: [debug, release]
|
||||
tests: [
|
||||
tests-simple-test-always-succeed,
|
||||
tests-simple-test-ptr-test,
|
||||
tests-simple-unique-ptr,
|
||||
tests-simple-test-raw-ptr
|
||||
]
|
||||
|
||||
|
||||
|
@ -15,11 +15,13 @@
|
||||
/****************************************************************************
|
||||
* @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 2022-09-01
|
||||
* @version 0.1.0
|
||||
* @date 2024-11-02
|
||||
* @version 0.2.0
|
||||
***************************************************************************/
|
||||
|
||||
#include "greentea-client/test_env.h"
|
||||
@ -94,6 +96,170 @@ void test_instance_sharing() {
|
||||
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)
|
||||
@ -103,8 +269,19 @@ static utest::v1::status_t greentea_setup(const size_t 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)};
|
||||
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);
|
||||
|
||||
|
@ -1,96 +0,0 @@
|
||||
// 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 of raw pointer
|
||||
*
|
||||
* @date 2022-09-01
|
||||
* @version 0.1.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_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);
|
||||
|
||||
t1.~Test();
|
||||
TEST_ASSERT_EQUAL(0, Test::_instanceCount);
|
||||
}
|
||||
|
||||
// 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[] = {
|
||||
Case("Test single raw pointer instance", test_single_raw_ptr_lifetime)};
|
||||
|
||||
static Specification specification(greentea_setup, cases);
|
||||
|
||||
int main() { return !Harness::run(specification); }
|
@ -1,189 +0,0 @@
|
||||
// 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); }
|
Loading…
x
Reference in New Issue
Block a user