|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2018 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "mbed.h" |
| 17 | +#include "greentea-client/test_env.h" |
| 18 | +#include "utest/utest.h" |
| 19 | +#include "unity/unity.h" |
| 20 | + |
| 21 | +#include "platform/SharedPtr.h" |
| 22 | + |
| 23 | +using utest::v1::Case; |
| 24 | + |
| 25 | +struct TestStruct { |
| 26 | + TestStruct() |
| 27 | + { |
| 28 | + s_count++; |
| 29 | + value = 42; |
| 30 | + } |
| 31 | + |
| 32 | + ~TestStruct() |
| 33 | + { |
| 34 | + s_count--; |
| 35 | + value = 0; |
| 36 | + } |
| 37 | + |
| 38 | + int value; |
| 39 | + static int s_count; |
| 40 | +}; |
| 41 | + |
| 42 | +int TestStruct::s_count = 0; |
| 43 | + |
| 44 | +/** |
| 45 | + * Test that a shared pointer correctly manages the lifetime of the underlying raw pointer |
| 46 | + */ |
| 47 | +void test_single_sharedptr_lifetime() |
| 48 | +{ |
| 49 | + // Sanity-check value of counter |
| 50 | + TEST_ASSERT_EQUAL(0, TestStruct::s_count); |
| 51 | + |
| 52 | + // Create and destroy shared pointer in given scope |
| 53 | + { |
| 54 | + SharedPtr<TestStruct> s_ptr(new TestStruct); |
| 55 | + TEST_ASSERT_EQUAL(1, TestStruct::s_count); |
| 56 | + } |
| 57 | + |
| 58 | + // Destroy shared pointer |
| 59 | + TEST_ASSERT_EQUAL(0, TestStruct::s_count); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Test that multiple instances of shared pointers correctly manage the reference count |
| 64 | + * to release the object at the correct point |
| 65 | + */ |
| 66 | +void test_instance_sharing() |
| 67 | +{ |
| 68 | + SharedPtr<TestStruct> s_ptr1(NULL); |
| 69 | + |
| 70 | + // Sanity-check value of counter |
| 71 | + TEST_ASSERT_EQUAL(0, TestStruct::s_count); |
| 72 | + |
| 73 | + // Create and destroy shared pointer in given scope |
| 74 | + { |
| 75 | + SharedPtr<TestStruct> s_ptr2(new TestStruct); |
| 76 | + TEST_ASSERT_EQUAL(1, TestStruct::s_count); |
| 77 | + s_ptr1 = s_ptr2; |
| 78 | + TEST_ASSERT_EQUAL(1, TestStruct::s_count); |
| 79 | + } |
| 80 | + |
| 81 | + TEST_ASSERT_EQUAL(1, TestStruct::s_count); |
| 82 | + |
| 83 | + s_ptr1 = NULL; |
| 84 | + |
| 85 | + // Destroy shared pointer |
| 86 | + TEST_ASSERT_EQUAL(0, TestStruct::s_count); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Test whether comparison operators operate as expected, both between |
| 91 | + * shared pointers managing the same object, |
| 92 | + * and a shared pointer and underlying raw pointer |
| 93 | + */ |
| 94 | +void test_equality_comparators() |
| 95 | +{ |
| 96 | + TestStruct *raw_ptr1 = new TestStruct; |
| 97 | + TestStruct *raw_ptr2 = new TestStruct; |
| 98 | + SharedPtr<TestStruct> s_ptr1_1(raw_ptr1); |
| 99 | + SharedPtr<TestStruct> s_ptr1_2 = s_ptr1_1; |
| 100 | + SharedPtr<TestStruct> s_ptr2(raw_ptr2); |
| 101 | + |
| 102 | + // Pointers that should be deemed equal |
| 103 | + TEST_ASSERT_TRUE(s_ptr1_1 == raw_ptr1); // Shared pointer / Raw pointer |
| 104 | + TEST_ASSERT_TRUE(s_ptr1_1 == s_ptr1_2); // Shared pointer / Shared pointer |
| 105 | + |
| 106 | + // Pointers that should be deemed different |
| 107 | + TEST_ASSERT_TRUE(s_ptr1_1 != raw_ptr2); // Shared pointer / Raw pointer |
| 108 | + TEST_ASSERT_TRUE(s_ptr1_1 != s_ptr2); // Shared pointer / Shared pointer |
| 109 | +} |
| 110 | + |
| 111 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 112 | +{ |
| 113 | + GREENTEA_SETUP(10, "default_auto"); |
| 114 | + return utest::v1::verbose_test_setup_handler(number_of_cases); |
| 115 | +} |
| 116 | + |
| 117 | +Case cases[] = { |
| 118 | + Case("Test single shared pointer instance", test_single_sharedptr_lifetime), |
| 119 | + Case("Test instance sharing across multiple shared pointers", test_instance_sharing), |
| 120 | + Case("Test equality comparators", test_equality_comparators) |
| 121 | +}; |
| 122 | + |
| 123 | +utest::v1::Specification specification(test_setup, cases); |
| 124 | + |
| 125 | +int main() |
| 126 | +{ |
| 127 | + return !utest::v1::Harness::run(specification); |
| 128 | +} |
0 commit comments