1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 #include <zircon/syscalls.h>
7 #include <unittest/unittest.h>
8 #include <inttypes.h>
9 
10 // Calculation of elapsed time using ticks.
elapsed_time_using_ticks(void)11 static bool elapsed_time_using_ticks(void) {
12     BEGIN_TEST;
13 
14     zx_ticks_t per_second = zx_ticks_per_second();
15     ASSERT_GT(per_second, 0u, "Invalid ticks per second");
16     unittest_printf("Ticks per second: %" PRIu64 "\n", per_second);
17 
18     zx_ticks_t x = zx_ticks_get();
19     zx_ticks_t y = zx_ticks_get();
20     ASSERT_GE(y, x, "Ticks went backwards");
21 
22     double seconds = (y - x) / (double)per_second;
23     ASSERT_GE(seconds, 0u, "Time went backwards");
24 
25     END_TEST;
26 }
27 
28 BEGIN_TEST_CASE(ticks_tests)
RUN_TEST(elapsed_time_using_ticks)29 RUN_TEST(elapsed_time_using_ticks)
30 END_TEST_CASE(ticks_tests)
31 
32 #ifndef BUILD_COMBINED_TESTS
33 int main(int argc, char** argv) {
34     return unittest_run_all_tests(argc, argv) ? 0 : -1;
35 }
36 #endif
37