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 * This is a unit test suite for <tt>chip::Time::TimeSource</tt>. Tests mainly
21 * the ability to compile and use the test implementation of the time source.
24 #ifndef __STDC_LIMIT_MACROS
25 #define __STDC_LIMIT_MACROS
28 #include <system/SystemConfig.h>
30 #include <nlunit-test.h>
31 #include <support/CodeUtils.h>
32 #include <support/ErrorStr.h>
33 #include <support/UnitTestRegistration.h>
34 #include <system/TimeSource.h>
38 void TestTimeSourceSetAndGet(nlTestSuite * inSuite, void * inContext)
41 chip::Time::TimeSource<chip::Time::Source::kTest> source;
43 NL_TEST_ASSERT(inSuite, source.GetCurrentMonotonicTimeMs() == 0);
45 source.SetCurrentMonotonicTimeMs(1234);
46 NL_TEST_ASSERT(inSuite, source.GetCurrentMonotonicTimeMs() == 1234);
49 void SystemTimeSourceGet(nlTestSuite * inSuite, void * inContext)
52 chip::Time::TimeSource<chip::Time::Source::kSystem> source;
54 uint64_t oldValue = source.GetCurrentMonotonicTimeMs();
56 // a basic monotonic test. This is likely to take less than 1ms, so the
57 // actual test value lies mostly in ensuring things compile.
58 for (int i = 0; i < 100; i++)
60 uint64_t newValue = source.GetCurrentMonotonicTimeMs();
61 NL_TEST_ASSERT(inSuite, newValue >= oldValue);
69 * Test Suite. It lists all the test functions.
72 static const nlTest sTests[] =
74 NL_TEST_DEF("TimeSource<Test>::SetAndGet", TestTimeSourceSetAndGet),
75 NL_TEST_DEF("TimeSource<System>::SetAndGet", SystemTimeSourceGet),
80 int TestTimeSource(void)
82 nlTestSuite theSuite = {
83 "chip-timesource", &sTests[0], nullptr /* setup */, nullptr /* teardown */
86 // Run test suit againt one context.
87 nlTestRunner(&theSuite, nullptr /* context */);
89 return (nlTestRunnerStats(&theSuite));
92 CHIP_REGISTER_TEST_SUITE(TestTimeSource)