Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2020 Project CHIP Authors 4 : * 5 : * Licensed under the Apache License, Version 2.0 (the "License"); 6 : * you may not use this file except in compliance with the License. 7 : * You may obtain a copy of the License at 8 : * 9 : * http://www.apache.org/licenses/LICENSE-2.0 10 : * 11 : * Unless required by applicable law or agreed to in writing, software 12 : * distributed under the License is distributed on an "AS IS" BASIS, 13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 : * See the License for the specific language governing permissions and 15 : * limitations under the License. 16 : */ 17 : 18 : #include <lib/support/UnitTestRegistration.h> 19 : #include <lib/support/logging/CHIPLogging.h> 20 : #include <stdlib.h> 21 : #include <string.h> 22 : 23 : namespace chip { 24 : 25 : const size_t kTestSuitesMax = 256; 26 : 27 : typedef struct 28 : { 29 : UnitTestTriggerFunction test_suites[kTestSuitesMax]; 30 : uint32_t num_test_suites; 31 : } test_suites_t; 32 : 33 : static test_suites_t gs_test_suites; 34 : 35 : #ifdef __ZEPHYR__ 36 : inline static bool AlreadyExists(UnitTestTriggerFunction tests) 37 : { 38 : for (uint32_t i = 0; i < gs_test_suites.num_test_suites; ++i) 39 : if (gs_test_suites.test_suites[i] == tests) 40 : return true; 41 : return false; 42 : } 43 : #endif 44 : 45 315 : CHIP_ERROR RegisterUnitTests(UnitTestTriggerFunction tests) 46 : { 47 315 : if (gs_test_suites.num_test_suites >= kTestSuitesMax) 48 : { 49 0 : ChipLogError(Support, "Test suits limit reached"); 50 0 : return CHIP_ERROR_NO_MEMORY; 51 : } 52 : 53 : #ifdef __ZEPHYR__ 54 : // Not sure yet if it's a Zephyr bug or misconfiguration, but global constructors are called 55 : // twice on native_posix platform - by libc and by Zephyr's main thread initialization code. 56 : // This makes sure tests are not run twice for that reason. 57 : if (AlreadyExists(tests)) 58 : return CHIP_NO_ERROR; 59 : #endif 60 : 61 315 : gs_test_suites.test_suites[gs_test_suites.num_test_suites] = tests; 62 315 : gs_test_suites.num_test_suites++; 63 315 : return CHIP_NO_ERROR; 64 : } 65 : 66 0 : int RunRegisteredUnitTests() 67 : { 68 0 : int status = 0; 69 0 : for (uint32_t i = 0; i < gs_test_suites.num_test_suites; i++) 70 : { 71 0 : status += gs_test_suites.test_suites[i](); 72 : } 73 0 : return status; 74 : } 75 : 76 : } // namespace chip