diff options
Diffstat (limited to 'tests/auto/testlib/selftests/sleep')
-rw-r--r-- | tests/auto/testlib/selftests/sleep/CMakeLists.txt | 13 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/sleep/tst_sleep.cpp | 60 |
2 files changed, 31 insertions, 42 deletions
diff --git a/tests/auto/testlib/selftests/sleep/CMakeLists.txt b/tests/auto/testlib/selftests/sleep/CMakeLists.txt index 418c61bab9..2e789b59d7 100644 --- a/tests/auto/testlib/selftests/sleep/CMakeLists.txt +++ b/tests/auto/testlib/selftests/sleep/CMakeLists.txt @@ -1,21 +1,22 @@ -# Generated from sleep.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## sleep Binary: ##################################################################### qt_internal_add_executable(sleep - NO_INSTALL # special case - OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} # special case + NO_INSTALL + EXCEPTIONS + OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} SOURCES tst_sleep.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Test + Qt::CorePrivate ) ## Scopes: ##################################################################### -# special case begin qt_internal_apply_testlib_coverage_options(sleep) -# special case end diff --git a/tests/auto/testlib/selftests/sleep/tst_sleep.cpp b/tests/auto/testlib/selftests/sleep/tst_sleep.cpp index 20e80950c2..d9cd6959e5 100644 --- a/tests/auto/testlib/selftests/sleep/tst_sleep.cpp +++ b/tests/auto/testlib/selftests/sleep/tst_sleep.cpp @@ -1,36 +1,20 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// 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 @@ -42,17 +26,21 @@ private slots: 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); - QVERIFY(t.elapsed() > 90); + QCOMPARE_GT(t.durationElapsed(), 100ms - MarginForError); - QTest::qSleep(1000); - QVERIFY(t.elapsed() > 1000); + QTest::qSleep(1s); + QCOMPARE_GT(t.durationElapsed(), 1s - MarginForError); - QTest::qSleep(1000 * 10); // 10 seconds - QVERIFY(t.elapsed() > 1000 * 10); + QTest::qSleep(10s); + QCOMPARE_GT(t.durationElapsed(), 10s - MarginForError); } void tst_Sleep::wait() @@ -61,16 +49,16 @@ void tst_Sleep::wait() t.start(); QTest::qWait(1); - QVERIFY(t.elapsed() >= 1); + QCOMPARE_GE(t.durationElapsed(), 1ms); QTest::qWait(10); - QVERIFY(t.elapsed() >= 11); + QCOMPARE_GE(t.durationElapsed(), 11ms); QTest::qWait(100); - QVERIFY(t.elapsed() >= 111); + QCOMPARE_GE(t.durationElapsed(), 111ms); QTest::qWait(1000); - QVERIFY(t.elapsed() >= 1111); + QCOMPARE_GE(t.durationElapsed(), 1111ms); } QTEST_MAIN(tst_Sleep) |