Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestClock01.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package jau.test.sys;
26
27import java.time.Instant;
28import java.time.ZoneOffset;
29import java.time.temporal.ChronoUnit;
30
31import org.jau.io.PrintUtil;
32import org.jau.sys.Clock;
33import org.junit.Assert;
34import org.junit.FixMethodOrder;
35import org.junit.Test;
36import org.junit.runners.MethodSorters;
37
38import jau.pkg.PlatformRuntime;
39import jau.test.junit.util.JunitTracer;
40
41@FixMethodOrder(MethodSorters.NAME_ASCENDING)
42public class TestClock01 extends JunitTracer {
43
44 @Test
45 public void test00() {
46 PlatformRuntime.checkInitialized();
47 PrintUtil.fprintf_td(System.err, "test00\n");
48
49 final Instant m_t0 = Instant.ofEpochMilli(Clock.currentTimeMillis());
50 final Instant m_t1 = Clock.getMonotonicTime();
51 final Instant w_t2 = Instant.ofEpochSecond(Clock.wallClockSeconds());
52 final Instant w_t3 = Clock.getWallClockTime();
53
54 try {
55 Thread.sleep(100);
56 } catch (final InterruptedException e) { }
57
58 final Instant m_t0_b = Instant.ofEpochMilli(Clock.currentTimeMillis());
59 final Instant m_t1_b = Clock.getMonotonicTime();
60 final Instant w_t2_b = Instant.ofEpochSecond(Clock.wallClockSeconds());
61 final Instant w_t3_b = Clock.getWallClockTime();
62
63 final long m_t0_d = m_t0.until(m_t0_b, ChronoUnit.MILLIS);
64 final long m_t1_d = m_t1.until(m_t1_b, ChronoUnit.MILLIS);
65 final long w_t2_d = w_t2.until(w_t2_b, ChronoUnit.MILLIS);
66 final long w_t3_d = w_t3.until(w_t3_b, ChronoUnit.MILLIS);
67
68 PrintUtil.fprintf_td(System.err, "mono t0 %s, %d ms\n", m_t0.atZone(ZoneOffset.UTC), m_t0_d);
69 PrintUtil.fprintf_td(System.err, "mono t1 %s, %d ms\n", m_t1.atZone(ZoneOffset.UTC), m_t1_d);
70 PrintUtil.fprintf_td(System.err, "wall t2 %s, %d ms\n", w_t2.atZone(ZoneOffset.UTC), w_t2_d);
71 PrintUtil.fprintf_td(System.err, "wall t3 %s, %d ms\n", w_t3.atZone(ZoneOffset.UTC), w_t3_d);
72
73 final long td_min = 50;
74 final long td_max = 150;
75 Assert.assertTrue( td_min <= m_t0_d );
76 Assert.assertTrue( td_max >= m_t0_d );
77
78 Assert.assertTrue( td_min <= m_t1_d );
79 Assert.assertTrue( td_max >= m_t1_d );
80
81 Assert.assertTrue( 0 <= w_t2_d ); // sec granularity only
82 Assert.assertTrue( 1000 >= w_t2_d );
83
84 Assert.assertTrue( td_min <= w_t3_d );
85 Assert.assertTrue( td_max >= w_t3_d );
86 }
87
88 public static void main(final String args[]) {
89 final String tstname = TestClock01.class.getName();
90 org.junit.runner.JUnitCore.main(tstname);
91 }
92
93}
static void main(final String args[])