|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 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 | +using utest::v1::Case; |
| 22 | + |
| 23 | + |
| 24 | +class InstanceTest { |
| 25 | +public: |
| 26 | + InstanceTest() |
| 27 | + { |
| 28 | + _instance_counter++; |
| 29 | + } |
| 30 | + |
| 31 | + static int get_instance_counter() |
| 32 | + { |
| 33 | + return _instance_counter; |
| 34 | + } |
| 35 | + |
| 36 | +private: |
| 37 | + static int _instance_counter; |
| 38 | +}; |
| 39 | + |
| 40 | +int InstanceTest::_instance_counter = 0; |
| 41 | + |
| 42 | + |
| 43 | +SingletonPtr<InstanceTest> singleton; |
| 44 | + |
| 45 | + |
| 46 | +/** Test SingletonPtr lazy initialization |
| 47 | + * |
| 48 | + * Given a global singleton of type SingletonPtr<InstanceTest> |
| 49 | + * When before first singleton use |
| 50 | + * Then underneath object is yet not created |
| 51 | + */ |
| 52 | +void test_lazy_initialization() |
| 53 | +{ |
| 54 | + TEST_ASSERT_MESSAGE(InstanceTest::get_instance_counter() == 0, "Initialized before first use!!!"); |
| 55 | +} |
| 56 | + |
| 57 | +/** Test SingletonPtr single instance |
| 58 | + * |
| 59 | + * Given a singleton of type SingletonPtr<InstanceTest> |
| 60 | + * |
| 61 | + * When after first singleton use |
| 62 | + * Then underneath object was created exactly once |
| 63 | + * |
| 64 | + * When after second singleton use |
| 65 | + * Then underneath object was created exactly once |
| 66 | + * and both (ref1 and ref2) are references to same instance |
| 67 | + * |
| 68 | + */ |
| 69 | +void test_single_instance() |
| 70 | +{ |
| 71 | + InstanceTest *ref1 = singleton.get(); |
| 72 | + TEST_ASSERT_NOT_NULL(ref1); |
| 73 | + |
| 74 | + TEST_ASSERT_EQUAL_INT(1, InstanceTest::get_instance_counter()); |
| 75 | + |
| 76 | + InstanceTest *ref2 = singleton.get(); |
| 77 | + TEST_ASSERT_NOT_NULL(ref2); |
| 78 | + |
| 79 | + TEST_ASSERT_EQUAL_INT(1, InstanceTest::get_instance_counter()); |
| 80 | + |
| 81 | + // same instance |
| 82 | + TEST_ASSERT_EQUAL_PTR(ref1, ref2); |
| 83 | +} |
| 84 | + |
| 85 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 86 | +{ |
| 87 | + GREENTEA_SETUP(10, "default_auto"); |
| 88 | + return utest::v1::verbose_test_setup_handler(number_of_cases); |
| 89 | +} |
| 90 | + |
| 91 | +Case cases[] = { |
| 92 | + Case("Test lazy initialization", test_lazy_initialization), |
| 93 | + Case("Test single instance", test_single_instance), |
| 94 | +}; |
| 95 | + |
| 96 | +utest::v1::Specification specification(test_setup, cases); |
| 97 | + |
| 98 | +int main() |
| 99 | +{ |
| 100 | + return !utest::v1::Harness::run(specification); |
| 101 | +} |
0 commit comments