ADD test raw ptr

- ADD raw pointer test (run on onlineGDB)
- UPD change name and description of the test
- UPD github workflow to include test-raw-ptr
This commit is contained in:
Rémi Heredero 2024-10-21 15:53:15 +02:00 committed by GitHub
commit 7c1b4110d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 101 additions and 1 deletions

View File

@ -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-test-raw-ptr
]
steps:

View File

@ -0,0 +1,96 @@
// 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); }