1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdbool.h>
6 
7 #include <unittest/unittest.h>
8 #include <zircon/syscalls.h>
9 #include <zircon/time.h>
10 #include <zircon/types.h>
11 
clock_monotonic_test(void)12 static bool clock_monotonic_test(void) {
13     BEGIN_TEST;
14 
15     const zx_time_t zero = 0;
16 
17     zx_time_t previous = zx_clock_get_monotonic();
18 
19     for (int idx = 0; idx < 100; ++idx) {
20         zx_time_t current = zx_clock_get_monotonic();
21         ASSERT_GT(current, zero, "monotonic time should be a positive number of nanoseconds");
22         ASSERT_GE(current, previous, "monotonic time should only advance");
23 
24         // This calls zx_nanosleep directly rather than using
25         // zx_deadline_after, which internally gets the monotonic
26         // clock.
27         zx_nanosleep(zx_time_add_duration(current, 1000u));
28 
29         previous = current;
30     }
31 
32     END_TEST;
33 }
34 
35 BEGIN_TEST_CASE(clock_tests)
RUN_TEST(clock_monotonic_test)36 RUN_TEST(clock_monotonic_test)
37 END_TEST_CASE(clock_tests)
38 
39 #ifndef BUILD_COMBINED_TESTS
40 int main(int argc, char** argv) {
41     return unittest_run_all_tests(argc, argv) ? 0 : -1;
42 }
43 #endif
44