1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtCore/QString>
30#include <QtCore/QTime>
31#include <QtCore/QElapsedTimer>
32#include <QtTest/QtTest>
33
34static const int minResolution = 100; // the minimum resolution for the tests
35
36QDebug operator<<(QDebug s, const QElapsedTimer &t)
37{
38 s.nospace() << "(" << t.msecsSinceReference() << ")";
39 return s.space();
40}
41
42class tst_QElapsedTimer : public QObject
43{
44 Q_OBJECT
45
46private Q_SLOTS:
47 void statics();
48 void validity();
49 void basics();
50 void elapsed();
51 void msecsTo();
52};
53
54void tst_QElapsedTimer::statics()
55{
56 qDebug() << "Clock type is" << QElapsedTimer::clockType();
57 qDebug() << "Said clock is" << (QElapsedTimer::isMonotonic() ? "monotonic" : "not monotonic");
58 QElapsedTimer t;
59 t.start();
60 qDebug() << "Current time is" << t.msecsSinceReference();
61}
62
63void tst_QElapsedTimer::validity()
64{
65 QElapsedTimer t;
66
67 QVERIFY(!t.isValid()); // non-POD now, it should always start invalid
68
69 t.start();
70 QVERIFY(t.isValid());
71
72 t.invalidate();
73 QVERIFY(!t.isValid());
74}
75
76void tst_QElapsedTimer::basics()
77{
78 QElapsedTimer t1;
79 t1.start();
80
81 QVERIFY(t1.msecsSinceReference() != 0);
82
83 QCOMPARE(t1, t1);
84 QVERIFY(!(t1 != t1));
85 QVERIFY(!(t1 < t1));
86 QCOMPARE(t1.msecsTo(t1), qint64(0));
87 QCOMPARE(t1.secsTo(t1), qint64(0));
88
89 quint64 value1 = t1.msecsSinceReference();
90 qDebug() << "value1:" << value1 << "t1:" << t1;
91 qint64 nsecs = t1.nsecsElapsed();
92 qint64 elapsed = t1.restart();
93 QVERIFY(elapsed < minResolution);
94 QVERIFY(nsecs / 1000000 < minResolution);
95
96 quint64 value2 = t1.msecsSinceReference();
97 qDebug() << "value2:" << value2 << "t1:" << t1
98 << "elapsed:" << elapsed << "nsecs:" << nsecs;
99 // in theory, elapsed == value2 - value1
100
101 // However, since QElapsedTimer keeps internally the full resolution,
102 // we have here a rounding error due to integer division
103 QVERIFY(qAbs(elapsed - qint64(value2 - value1)) <= 1);
104}
105
106void tst_QElapsedTimer::elapsed()
107{
108 QElapsedTimer t1;
109 t1.start();
110
111 QTest::qSleep(ms: 2*minResolution);
112
113 auto nsecs = t1.nsecsElapsed();
114 auto msecs = t1.elapsed();
115 QVERIFY(nsecs > 0);
116 QVERIFY(msecs > 0);
117 // the number of elapsed nanoseconds and milliseconds should match
118 QVERIFY(nsecs - msecs * 1000000 < 1000000);
119
120 if (msecs > 8 * minResolution)
121 QSKIP("Sampling timer took too long, aborting test");
122
123 QVERIFY(t1.hasExpired(minResolution));
124 QVERIFY(!t1.hasExpired(8*minResolution));
125 QVERIFY(!t1.hasExpired(-1));
126
127 qint64 elapsed = t1.restart();
128 QVERIFY(elapsed >= msecs);
129 QVERIFY(elapsed < msecs + 3*minResolution);
130}
131
132void tst_QElapsedTimer::msecsTo()
133{
134 QElapsedTimer t1;
135 t1.start();
136 QTest::qSleep(ms: minResolution);
137 QElapsedTimer t2;
138 t2.start();
139
140 QVERIFY(t1 != t2);
141 QVERIFY(!(t1 == t2));
142 QVERIFY(t1 < t2);
143
144 auto diff = t1.msecsTo(other: t2);
145 QVERIFY2(diff > 0, QString("difference t1 and t2 is %1").arg(diff).toLatin1());
146 diff = t2.msecsTo(other: t1);
147 QVERIFY2(diff < 0, QString("difference t2 and t1 is %1").arg(diff).toLatin1());
148}
149
150QTEST_MAIN(tst_QElapsedTimer);
151
152#include "tst_qelapsedtimer.moc"
153

source code of qtbase/tests/auto/corelib/kernel/qelapsedtimer/tst_qelapsedtimer.cpp