tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtCore/QString>
       
    43 #include <QtCore/QTime>
       
    44 #include <QtCore/QElapsedTimer>
       
    45 #include <QtTest/QtTest>
       
    46 
       
    47 static const int minResolution = 50; // the minimum resolution for the tests
       
    48 
       
    49 QDebug operator<<(QDebug s, const QElapsedTimer &t)
       
    50 {
       
    51     union {
       
    52         QElapsedTimer t;
       
    53         struct { qint64 t1, t2; } i;
       
    54     } copy;
       
    55     copy.t = t;
       
    56     s.nospace() << "(" <<  copy.i.t1 << ", " << copy.i.t2 << ")";
       
    57     return s.space();
       
    58 }
       
    59 
       
    60 class tst_QElapsedTimer : public QObject
       
    61 {
       
    62     Q_OBJECT
       
    63 
       
    64 private Q_SLOTS:
       
    65     void statics();
       
    66     void validity();
       
    67     void basics();
       
    68     void elapsed();
       
    69 };
       
    70 
       
    71 void tst_QElapsedTimer::statics()
       
    72 {
       
    73     qDebug() << "Clock type is" << QElapsedTimer::clockType();
       
    74     qDebug() << "Said clock is" << (QElapsedTimer::isMonotonic() ? "monotonic" : "not monotonic");
       
    75     QElapsedTimer t;
       
    76     t.start();
       
    77     qDebug() << "Current time is" << t.msecsSinceReference();
       
    78 }
       
    79 
       
    80 void tst_QElapsedTimer::validity()
       
    81 {
       
    82     QElapsedTimer t;
       
    83 
       
    84     t.invalidate();
       
    85     QVERIFY(!t.isValid());
       
    86 
       
    87     t.start();
       
    88     QVERIFY(t.isValid());
       
    89 
       
    90     t.invalidate();
       
    91     QVERIFY(!t.isValid());
       
    92 }
       
    93 
       
    94 void tst_QElapsedTimer::basics()
       
    95 {
       
    96     QElapsedTimer t1;
       
    97     t1.start();
       
    98 
       
    99     QVERIFY(t1.msecsSinceReference() != 0);
       
   100 
       
   101     QCOMPARE(t1, t1);
       
   102     QVERIFY(!(t1 != t1));
       
   103     QVERIFY(!(t1 < t1));
       
   104     QCOMPARE(t1.msecsTo(t1), qint64(0));
       
   105     QCOMPARE(t1.secsTo(t1), qint64(0));
       
   106 //    QCOMPARE(t1 + 0, t1);
       
   107 //    QCOMPARE(t1 - 0, t1);
       
   108 
       
   109 #if 0
       
   110     QElapsedTimer t2 = t1;
       
   111     t2 += 1000;   // so we can use secsTo
       
   112 
       
   113     QVERIFY(t1 != t2);
       
   114     QVERIFY(!(t1 == t2));
       
   115     QVERIFY(t1 < t2);
       
   116     QVERIFY(!(t2 < t1));
       
   117     QCOMPARE(t1.msecsTo(t2), qint64(1000));
       
   118     QCOMPARE(t1.secsTo(t2), qint64(1));
       
   119 //    QCOMPARE(t2 - t1, qint64(1000));
       
   120 //    QCOMPARE(t1 - t2, qint64(-1000));
       
   121 #endif
       
   122 
       
   123     quint64 value1 = t1.msecsSinceReference();
       
   124     qDebug() << value1 << t1;
       
   125     qint64 elapsed = t1.restart();
       
   126     QVERIFY(elapsed < minResolution);
       
   127 
       
   128     quint64 value2 = t1.msecsSinceReference();
       
   129     qDebug() << value2 << t1 << elapsed;
       
   130     // in theory, elapsed == value2 - value1
       
   131 
       
   132     // However, since QElapsedTimer keeps internally the full resolution,
       
   133     // we have here a rounding error due to integer division
       
   134     QVERIFY(qAbs(elapsed - qint64(value2 - value1)) <= 1);
       
   135 }
       
   136 
       
   137 void tst_QElapsedTimer::elapsed()
       
   138 {
       
   139     QElapsedTimer t1;
       
   140     t1.start();
       
   141 
       
   142     QTest::qSleep(4*minResolution);
       
   143     QElapsedTimer t2;
       
   144     t2.start();
       
   145 
       
   146     QVERIFY(t1 != t2);
       
   147     QVERIFY(!(t1 == t2));
       
   148     QVERIFY(t1 < t2);
       
   149     QVERIFY(t1.msecsTo(t2) > 0);
       
   150     // don't check: t1.secsTo(t2)
       
   151 //    QVERIFY(t1 - t2 < 0);
       
   152 
       
   153     QVERIFY(t1.elapsed() > 0);
       
   154     QVERIFY(t1.hasExpired(minResolution));
       
   155     QVERIFY(!t1.hasExpired(8*minResolution));
       
   156     QVERIFY(!t2.hasExpired(minResolution));
       
   157 
       
   158     QVERIFY(!t1.hasExpired(-1));
       
   159     QVERIFY(!t2.hasExpired(-1));
       
   160 
       
   161     qint64 elapsed = t1.restart();
       
   162     QVERIFY(elapsed > 3*minResolution);
       
   163     QVERIFY(elapsed < 5*minResolution);
       
   164     qint64 diff = t2.msecsTo(t1);
       
   165     QVERIFY(diff < minResolution);
       
   166 }
       
   167 
       
   168 QTEST_MAIN(tst_QElapsedTimer);
       
   169 
       
   170 #include "tst_qelapsedtimer.moc"