File indexing completed on 2024-05-12 05:42:41
0001 #include <cutehmi/Singleton.hpp> 0002 0003 #include <cutehmi/test/random.hpp> 0004 0005 #include <QtTest/QtTest> 0006 0007 namespace cutehmi { 0008 0009 class test_Singleton: 0010 public QObject 0011 { 0012 Q_OBJECT 0013 0014 private slots: 0015 void initTestCase(); 0016 0017 void InstanceA(); 0018 0019 void DestroyA(); 0020 0021 void DestroyB(); 0022 0023 void destroySingletonInstances(); 0024 0025 public: 0026 static int InstancesCtr; 0027 static QString destructionOrder; 0028 }; 0029 0030 int test_Singleton::InstancesCtr; 0031 QString test_Singleton::destructionOrder; 0032 0033 0034 class SingletonMockA: 0035 public Singleton<SingletonMockA> 0036 { 0037 public: 0038 SingletonMockA(); 0039 0040 ~SingletonMockA() override; 0041 }; 0042 0043 class SingletonMockB: 0044 public Singleton<SingletonMockB> 0045 { 0046 public: 0047 SingletonMockB(); 0048 0049 ~SingletonMockB() override; 0050 0051 bool test() const; 0052 }; 0053 0054 0055 class SingletonMockC: 0056 public Singleton<SingletonMockC> 0057 { 0058 public: 0059 SingletonMockC(); 0060 0061 ~SingletonMockC() override; 0062 }; 0063 0064 class SingletonMockD: 0065 public Singleton<SingletonMockD> 0066 { 0067 public: 0068 SingletonMockD(); 0069 0070 ~SingletonMockD() override; 0071 }; 0072 0073 0074 void test_Singleton::initTestCase() 0075 { 0076 InstancesCtr = 0; 0077 } 0078 0079 void test_Singleton::InstanceA() 0080 { 0081 QVERIFY(InstancesCtr == 0); 0082 0083 // Test if Instance() is returning a reference to the same object. 0084 QCOMPARE(& SingletonMockA::Instance(), & SingletonMockA::Instance()); 0085 0086 // Check if constructor has been called. 0087 QCOMPARE(InstancesCtr, 1); 0088 } 0089 0090 void test_Singleton::DestroyA() 0091 { 0092 QVERIFY(InstancesCtr == 1); 0093 0094 SingletonMockA::Destroy(); 0095 0096 QCOMPARE(InstancesCtr, 0); 0097 } 0098 0099 void test_Singleton::DestroyB() 0100 { 0101 QVERIFY(InstancesCtr == 0); 0102 0103 SingletonMockB::Destroy(); 0104 0105 QCOMPARE(InstancesCtr, 0); 0106 } 0107 0108 void test_Singleton::destroySingletonInstances() 0109 { 0110 QVERIFY(InstancesCtr == 0); 0111 QVERIFY(destructionOrder.isEmpty()); 0112 0113 SingletonMockC::Instance(); 0114 SingletonMockD::Instance(); 0115 0116 QCOMPARE(InstancesCtr, 2); 0117 0118 ::cutehmi::destroySingletonInstances(); 0119 0120 QCOMPARE(InstancesCtr, 0); 0121 QCOMPARE(destructionOrder, "DC"); 0122 } 0123 0124 0125 SingletonMockA::SingletonMockA() 0126 { 0127 test_Singleton::InstancesCtr++; 0128 } 0129 0130 SingletonMockA::~SingletonMockA() 0131 { 0132 test_Singleton::InstancesCtr--; 0133 } 0134 0135 0136 SingletonMockB::SingletonMockB() 0137 { 0138 test_Singleton::InstancesCtr++; 0139 } 0140 0141 SingletonMockB::~SingletonMockB() 0142 { 0143 QVERIFY(Instance().test()); // Check if CuteHMI-2.workaround is working. 0144 test_Singleton::InstancesCtr--; 0145 } 0146 0147 bool SingletonMockB::test() const 0148 { 0149 return true; 0150 } 0151 0152 0153 SingletonMockC::SingletonMockC() 0154 { 0155 test_Singleton::InstancesCtr++; 0156 } 0157 0158 SingletonMockC::~SingletonMockC() 0159 { 0160 test_Singleton::destructionOrder.append('C'); 0161 test_Singleton::InstancesCtr--; 0162 } 0163 0164 0165 SingletonMockD::SingletonMockD() 0166 { 0167 test_Singleton::InstancesCtr++; 0168 } 0169 0170 SingletonMockD::~SingletonMockD() 0171 { 0172 test_Singleton::destructionOrder.append('D'); 0173 test_Singleton::InstancesCtr--; 0174 } 0175 0176 } 0177 0178 QTEST_MAIN(cutehmi::test_Singleton) 0179 #include "test_Singleton.moc" 0180 0181 //(c)C: Copyright © 2019-2020, Michał Policht <michal@policht.pl>. All rights reserved. 0182 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT 0183 //(c)C: This file is a part of CuteHMI. 0184 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 0185 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 0186 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>. 0187 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below. 0188 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 0189 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 0190 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.