summaryrefslogtreecommitdiffstats
path: root/tests/auto/testlib/selftests/sleep/tst_sleep.cpp
blob: d9cd6959e5c85066ec3d7d238f79428218d96b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only


#include <QtCore/QCoreApplication>
#include <QtCore/QElapsedTimer>
#include <QTest>

#ifdef Q_OS_UNIX
#include <QtCore/private/qcore_unix_p.h>
#include <QtCore/qsystemdetection.h>

#include <time.h>
#endif

using namespace std::chrono_literals;

class tst_Sleep: public QObject
{
    Q_OBJECT

private slots:
    void sleep();
    void wait();
};

void tst_Sleep::sleep()
{
    // Subtracting 10ms as a margin for error
    static constexpr auto MarginForError = 10ms;

    QElapsedTimer t;
    t.start();

    // Test qSleep(int) overload, too
    QTest::qSleep(100);
    QCOMPARE_GT(t.durationElapsed(), 100ms - MarginForError);

    QTest::qSleep(1s);
    QCOMPARE_GT(t.durationElapsed(), 1s - MarginForError);

    QTest::qSleep(10s);
    QCOMPARE_GT(t.durationElapsed(), 10s - MarginForError);
}

void tst_Sleep::wait()
{
    QElapsedTimer t;
    t.start();

    QTest::qWait(1);
    QCOMPARE_GE(t.durationElapsed(), 1ms);

    QTest::qWait(10);
    QCOMPARE_GE(t.durationElapsed(), 11ms);

    QTest::qWait(100);
    QCOMPARE_GE(t.durationElapsed(), 111ms);

    QTest::qWait(1000);
    QCOMPARE_GE(t.durationElapsed(), 1111ms);
}

QTEST_MAIN(tst_Sleep)

#include "tst_sleep.moc"