Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / tests / TestTimeSource.cpp
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 /**
19  * @file
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.
22  */
23
24 #ifndef __STDC_LIMIT_MACROS
25 #define __STDC_LIMIT_MACROS
26 #endif
27
28 #include <system/SystemConfig.h>
29
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>
35
36 namespace {
37
38 void TestTimeSourceSetAndGet(nlTestSuite * inSuite, void * inContext)
39 {
40
41     chip::Time::TimeSource<chip::Time::Source::kTest> source;
42
43     NL_TEST_ASSERT(inSuite, source.GetCurrentMonotonicTimeMs() == 0);
44
45     source.SetCurrentMonotonicTimeMs(1234);
46     NL_TEST_ASSERT(inSuite, source.GetCurrentMonotonicTimeMs() == 1234);
47 }
48
49 void SystemTimeSourceGet(nlTestSuite * inSuite, void * inContext)
50 {
51
52     chip::Time::TimeSource<chip::Time::Source::kSystem> source;
53
54     uint64_t oldValue = source.GetCurrentMonotonicTimeMs();
55
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++)
59     {
60         uint64_t newValue = source.GetCurrentMonotonicTimeMs();
61         NL_TEST_ASSERT(inSuite, newValue >= oldValue);
62         oldValue = newValue;
63     }
64 }
65
66 } // namespace
67
68 /**
69  *   Test Suite. It lists all the test functions.
70  */
71 // clang-format off
72 static const nlTest sTests[] =
73 {
74     NL_TEST_DEF("TimeSource<Test>::SetAndGet", TestTimeSourceSetAndGet),
75     NL_TEST_DEF("TimeSource<System>::SetAndGet", SystemTimeSourceGet),
76     NL_TEST_SENTINEL()
77 };
78 // clang-format on
79
80 int TestTimeSource(void)
81 {
82     nlTestSuite theSuite = {
83         "chip-timesource", &sTests[0], nullptr /* setup */, nullptr /* teardown */
84     };
85
86     // Run test suit againt one context.
87     nlTestRunner(&theSuite, nullptr /* context */);
88
89     return (nlTestRunnerStats(&theSuite));
90 }
91
92 CHIP_REGISTER_TEST_SUITE(TestTimeSource)