BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "greentea-client/test_env.h"
00018 #include "utest/utest.h"
00019 #include "unity/unity.h"
00020 
00021 using utest::v1::Case;
00022 
00023 
00024 class InstanceTest {
00025 public:
00026     InstanceTest()
00027     {
00028         _instance_counter++;
00029     }
00030 
00031     static int get_instance_counter()
00032     {
00033         return _instance_counter;
00034     }
00035 
00036 private:
00037     static int _instance_counter;
00038 };
00039 
00040 int InstanceTest::_instance_counter = 0;
00041 
00042 
00043 SingletonPtr<InstanceTest> singleton;
00044 
00045 
00046 /** Test SingletonPtr lazy initialization
00047  *
00048  *  Given a global singleton of type SingletonPtr<InstanceTest>
00049  *  When before first singleton use
00050  *  Then underneath object is yet not created
00051  */
00052 void test_lazy_initialization()
00053 {
00054     TEST_ASSERT_MESSAGE(InstanceTest::get_instance_counter() == 0, "Initialized before first use!!!");
00055 }
00056 
00057 /** Test SingletonPtr single instance
00058  *
00059  *  Given a singleton of type SingletonPtr<InstanceTest>
00060  *
00061  *  When after first singleton use
00062  *  Then underneath object was created exactly once
00063  *
00064  *  When after second singleton use
00065  *  Then underneath object was created exactly once
00066  *       and both (ref1 and ref2) are references to same instance
00067  *
00068  */
00069 void test_single_instance()
00070 {
00071     InstanceTest *ref1 = singleton.get();
00072     TEST_ASSERT_NOT_NULL(ref1);
00073 
00074     TEST_ASSERT_EQUAL_INT(1, InstanceTest::get_instance_counter());
00075 
00076     InstanceTest *ref2 = singleton.get();
00077     TEST_ASSERT_NOT_NULL(ref2);
00078 
00079     TEST_ASSERT_EQUAL_INT(1, InstanceTest::get_instance_counter());
00080 
00081     // same instance
00082     TEST_ASSERT_EQUAL_PTR(ref1, ref2);
00083 }
00084 
00085 utest::v1::status_t test_setup(const size_t number_of_cases)
00086 {
00087     GREENTEA_SETUP(10, "default_auto");
00088     return utest::v1::verbose_test_setup_handler(number_of_cases);
00089 }
00090 
00091 Case cases[] = {
00092     Case("Test lazy initialization", test_lazy_initialization),
00093     Case("Test single instance", test_single_instance),
00094 };
00095 
00096 utest::v1::Specification specification(test_setup, cases);
00097 
00098 int main()
00099 {
00100     return !utest::v1::Harness::run(specification);
00101 }