3 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <support/UnitTestRegistration.h>
24 const size_t kTestSuitesMax = 64;
28 UnitTestTriggerFunction test_suites[kTestSuitesMax];
29 uint32_t num_test_suites;
32 static test_suites_t gs_test_suites;
35 inline static bool AlreadyExists(UnitTestTriggerFunction tests)
37 for (uint32_t i = 0; i < gs_test_suites.num_test_suites; ++i)
38 if (gs_test_suites.test_suites[i] == tests)
44 CHIP_ERROR RegisterUnitTests(UnitTestTriggerFunction tests)
46 if (gs_test_suites.num_test_suites >= kTestSuitesMax)
48 return CHIP_ERROR_NO_MEMORY;
52 // Not sure yet if it's a Zephyr bug or misconfiguration, but global constructors are called
53 // twice on native_posix platform - by libc and by Zephyr's main thread initialization code.
54 // This makes sure tests are not run twice for that reason.
55 if (AlreadyExists(tests))
59 gs_test_suites.test_suites[gs_test_suites.num_test_suites] = tests;
60 gs_test_suites.num_test_suites++;
64 int RunRegisteredUnitTests()
67 for (uint32_t i = 0; i < gs_test_suites.num_test_suites; i++)
69 status += gs_test_suites.test_suites[i]();