diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..3e33698 --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +--- +BasedOnStyle: Google +IndentWidth: 4 +--- +Language: Cpp +ColumnLimit: 90 +AlignConsecutiveAssignments: true +DerivePointerAlignment: false +PointerAlignment: Left +BinPackArguments: false +BinPackParameters: false +IndentAccessModifiers: false \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..e9dd352 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,58 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: 'type: bug' +assignees: '' + +--- + + + +### Description of defect + + + + +#### Target(s) affected by this defect ? + + +#### Toolchain(s) (name and version) displaying this defect ? + + +#### What version of Mbed-os are you using (tag or sha) ? + + + +#### What version(s) of tools are you using. List all that apply (E.g. mbed-cli) + + +#### How is this defect reproduced ? diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml new file mode 100644 index 0000000..dd89ccd --- /dev/null +++ b/.github/workflows/build-test.yml @@ -0,0 +1,36 @@ +name: Build test application +on: + pull_request: + push: + +jobs: + build-cli-v1: + container: + image: ghcr.io/armmbed/mbed-os-env:master-2022.05.21t04.23.55 + + runs-on: ubuntu-20.04 + + strategy: + matrix: + target: [DISCO_H747I] + profile: [develop, debug, release] + tests: [ + tests-simple-test-always-succeed, + tests-simple-test-ptr-test, + tests-simple-unique-ptr, + tests-simple-test-raw-ptr + ] + + + steps: + - + name: checkout + uses: actions/checkout@v2 + + - + name: build-test + run: | + set -e + mbed deploy + mbed test -t GCC_ARM -m ${{ matrix.target }} --profile ${{ matrix.profile }} --compile -n ${{ matrix.tests }} + mbed compile -t GCC_ARM -m ${{ matrix.target }} --profile ${{ matrix.profile }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e90278 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.build +.mbed +projectfiles +*.py* +BUILD +mbed-os diff --git a/.mbedignore b/.mbedignore new file mode 100644 index 0000000..ef3cf8a --- /dev/null +++ b/.mbedignore @@ -0,0 +1,17 @@ +mbed-os/drivers/device_key/* +mbed-os/drivers/source/usb/USBMSD.cpp +mbed-os/drivers/source/SFDP.cpp +mbed-os/connectivity/cellular/* +mbed-os/connectivity/drivers/* +mbed-os/connectivity/FEATURE_BLE/* +mbed-os/connectivity/libraries/* +mbed-os/connectivity/lorawan/* +mbed-os/connectivity/lwipstack/* +mbed-os/connectivity/nanostack/* +mbed-os/connectivity/netsocket/* +mbed-os/connectivity/nfc/* +mbed-os/features/FEATURE_BOOTLOADER/* +mbed-os/features/frameworks/mbed-client-cli/* +mbed-os/features/frameworks/COMPONENT_FPGA_CI_TEST_SHIELD/* +mbed-os/platform/randlib/* +mbed-os/storage/kvstore/* \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..cace89e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,24 @@ +files: ^main.cpp +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: check-yaml + args: [--allow-multiple-documents] + - id: end-of-file-fixer + - id: trailing-whitespace +- repo: https://github.com/pre-commit/mirrors-clang-format + rev: 'v14.0.6' + hooks: + - id: clang-format +- repo: https://github.com/cpplint/cpplint + rev: '1.6.1' + hooks: + - id: cpplint +- repo: local + hooks: + - id: cppcheck + name: cppcheck + require_serial: true + entry: cppcheck --enable=all --suppress=missingInclude:* --inline-suppr -i mbed-os --std=c++14 --error-exitcode=1 + language: system \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..db75bad --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,33 @@ +# Copyright (c) 2020 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR) + +set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "") +set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "") +set(APP_TARGET mbed-os-example-blinky) + +include(${MBED_PATH}/tools/cmake/app.cmake) + +project(${APP_TARGET}) + +add_subdirectory(${MBED_PATH}) + +add_executable(${APP_TARGET}) + +target_sources(${APP_TARGET} + PRIVATE + main.cpp +) + +target_link_libraries(${APP_TARGET} + PRIVATE + mbed-os +) + +mbed_set_post_build(${APP_TARGET}) + +option(VERBOSE_BUILD "Have a verbose build process") +if(VERBOSE_BUILD) + set(CMAKE_VERBOSE_MAKEFILE ON) +endif() diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..f433ee4 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,5 @@ +# Contributing to Mbed OS + +Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor. + +To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html). diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..59cd3f8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,165 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. diff --git a/TESTS/simple-test/always-succeed/main.cpp b/TESTS/simple-test/always-succeed/main.cpp new file mode 100644 index 0000000..fb22a6f --- /dev/null +++ b/TESTS/simple-test/always-succeed/main.cpp @@ -0,0 +1,54 @@ +// 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 + * + * @brief Simple example of test program that always succeeds + * + * @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; + +// test handler function +static control_t always_succeed(const size_t call_count) { + // this is the always succeed test + TEST_ASSERT_EQUAL(4, 2 * 2); + + // execute the test only once and move to the next one, without waiting + return CaseNext; +} + +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("always succeed test", always_succeed)}; + +static Specification specification(greentea_setup, cases); + +int main() { return !Harness::run(specification); } \ No newline at end of file diff --git a/TESTS/simple-test/test-ptr/main.cpp b/TESTS/simple-test/test-ptr/main.cpp new file mode 100644 index 0000000..142846d --- /dev/null +++ b/TESTS/simple-test/test-ptr/main.cpp @@ -0,0 +1,111 @@ +// 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 + * + * @brief Simple example of test program for raw and shared pointers + * + * @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_sharedptr_lifetime() { + // Sanity-check value of counter + TEST_ASSERT_EQUAL(0, Test::_instanceCount); + + // Create and destroy shared pointer in given scope + { + std::shared_ptr 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 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 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); +} + +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 shared pointer instance", test_single_sharedptr_lifetime), + Case("Test instance sharing across multiple shared pointers", test_instance_sharing)}; + +static Specification specification(greentea_setup, cases); + +int main() { return !Harness::run(specification); } \ No newline at end of file diff --git a/TESTS/simple-test/test-raw-ptr/main.cpp b/TESTS/simple-test/test-raw-ptr/main.cpp new file mode 100644 index 0000000..a689d47 --- /dev/null +++ b/TESTS/simple-test/test-raw-ptr/main.cpp @@ -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); } 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 diff --git a/cpplint.cfg b/cpplint.cfg new file mode 100644 index 0000000..55a5a67 --- /dev/null +++ b/cpplint.cfg @@ -0,0 +1,2 @@ +linelength=90 +filter=-build/include_subdir,-whitespace/indent,-build/namespaces,-build/c++11 \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..9bb6191 --- /dev/null +++ b/main.cpp @@ -0,0 +1,24 @@ +/* mbed Microcontroller Library + * Copyright (c) 2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ + +#if !MBED_TEST_MODE + +#include "mbed.h" // NOLINT + +// Blinking rate in milliseconds +#define BLINKING_RATE 500ms + +int main() { + // Initialise the digital pin LED1 as an output + + DigitalOut led(LED1); + + while (true) { + led = !led; + ThisThread::sleep_for(BLINKING_RATE); + } +} + +#endif // MBED_TEST_MODE diff --git a/mbed-os.lib b/mbed-os.lib new file mode 100644 index 0000000..144619b --- /dev/null +++ b/mbed-os.lib @@ -0,0 +1 @@ +https://github.com/ARMmbed/mbed-os/#17dc3dc2e6e2817a8bd3df62f38583319f0e4fed diff --git a/mbed_app.json b/mbed_app.json new file mode 100644 index 0000000..2f706a5 --- /dev/null +++ b/mbed_app.json @@ -0,0 +1,26 @@ +{ + "macros": [ + "MBED_CONF_MBED_TRACE_FEA_IPV6=0" + ], + "config": { + "main-stack-size": { + "value": 4096 + } + }, + "target_overrides": { + "*": { + "mbed-trace.enable": false, + "platform.stdio-convert-newlines": true, + "platform.stdio-baud-rate": 115200, + "platform.default-serial-baud-rate": 115200, + "platform.stdio-buffered-serial": true, + "target.printf_lib":"minimal-printf", + "platform.minimal-printf-enable-floating-point": true, + "platform.minimal-printf-set-floating-point-max-decimals": 2 + }, + "DISCO_H747I": { + "mbed-trace.enable": true, + "mbed-trace.max-level": "TRACE_LEVEL_DEBUG" + } + } + } \ No newline at end of file