diff options
Diffstat (limited to 'tests/auto/corelib/global/qglobal')
-rw-r--r-- | tests/auto/corelib/global/qglobal/CMakeLists.txt | 11 | ||||
-rw-r--r-- | tests/auto/corelib/global/qglobal/qglobal.c | 67 | ||||
-rw-r--r-- | tests/auto/corelib/global/qglobal/tst_qglobal.cpp | 285 |
3 files changed, 291 insertions, 72 deletions
diff --git a/tests/auto/corelib/global/qglobal/CMakeLists.txt b/tests/auto/corelib/global/qglobal/CMakeLists.txt index de9a94629a..5a0877663b 100644 --- a/tests/auto/corelib/global/qglobal/CMakeLists.txt +++ b/tests/auto/corelib/global/qglobal/CMakeLists.txt @@ -1,13 +1,22 @@ -# Generated from qglobal.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qglobal Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qglobal LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qglobal SOURCES qglobal.c tst_qglobal.cpp + NO_PCH_SOURCES + tst_qglobal.cpp # undef QT_NO_FOREACH ) ## Scopes: diff --git a/tests/auto/corelib/global/qglobal/qglobal.c b/tests/auto/corelib/global/qglobal/qglobal.c index b577a6f71f..2cfbd5e3be 100644 --- a/tests/auto/corelib/global/qglobal/qglobal.c +++ b/tests/auto/corelib/global/qglobal/qglobal.c @@ -1,41 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Intel Corporation. -** 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) 2017 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/qglobal.h> - -#if __has_include(<stdbool.h>) || __STDC_VERSION__ >= 199901L -# include <stdbool.h> -#else -# undef true -# define true 1 -# undef false -# define false 0 -#endif +#include <QtCore/qtversion.h> +#include <QtCore/qyieldcpu.h> +#include <QtCore/qtypes.h> #ifdef Q_COMPILER_THREAD_LOCAL # include <threads.h> @@ -46,6 +15,11 @@ * everything works. */ +#if defined(Q_OS_VXWORKS) && !defined(thread_local) +// threads.h forgot to define this (should be fixed for version 23.11) +# define thread_local _Thread_local +#endif + /* Types and Q_UNUSED */ void tst_GlobalTypes() { @@ -77,15 +51,28 @@ void tst_GlobalTypes() qintptr qip; quintptr qup; Q_UNUSED(qs); Q_UNUSED(qp); Q_UNUSED(qip); Q_UNUSED(qup); + +#ifdef QT_SUPPORTS_INT128 + qint128 s128; + quint128 u128; + Q_UNUSED(s128); Q_UNUSED(u128); +#endif /* QT_SUPPORTS_INT128 */ } +#if QT_SUPPORTS_INT128 +qint128 tst_qint128_min() { return Q_INT128_MIN + 0; } +qint128 tst_qint128_max() { return 0 + Q_INT128_MAX; } +quint128 tst_quint128_max() { return Q_UINT128_MAX - 1 + 1; } +#endif + /* Qt version */ int tst_QtVersion() { return QT_VERSION; } -const char *tst_qVersion() Q_DECL_NOEXCEPT +const char *tst_qVersion() Q_DECL_NOEXCEPT; +const char *tst_qVersion() { #if !defined(QT_NAMESPACE) return qVersion(); @@ -94,6 +81,12 @@ const char *tst_qVersion() Q_DECL_NOEXCEPT #endif } +void tst_qYieldCpu(void) Q_DECL_NOEXCEPT; +void tst_qYieldCpu(void) +{ + qYieldCpu(); +} + /* Static assertion */ Q_STATIC_ASSERT(true); Q_STATIC_ASSERT(1); diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 7323cfe545..1a19048bbe 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -1,40 +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 +#undef QT_NO_FOREACH // this file tests Q_FOREACH! + #include <QTest> #include <QPair> #include <QSysInfo> #include <QLatin1String> #include <QString> +#include <QtVersion> #include <cmath> +#include <limits> +#include <type_traits> class tst_QGlobal: public QObject { @@ -52,6 +32,7 @@ private slots: void qCoreAppStartupFunction(); void qCoreAppStartupFunctionRestart(); void integerForSize(); + void int128Literals(); void buildAbiEndianness(); void testqOverload(); void testqMinMax(); @@ -61,12 +42,19 @@ private slots: void qRoundDoubles(); void PRImacros(); void testqToUnderlying(); + void nodiscard(); }; extern "C" { // functions in qglobal.c void tst_GlobalTypes(); int tst_QtVersion(); const char *tst_qVersion(); +#if QT_SUPPORTS_INT128 +qint128 tst_qint128_min(); +qint128 tst_qint128_max(); +quint128 tst_quint128_max(); +#endif + } void tst_QGlobal::cMode() @@ -109,7 +97,7 @@ void tst_QGlobal::for_each() foreach(int i, list) { QCOMPARE(i, counter++); } - QCOMPARE(counter, list.count()); + QCOMPARE(counter, list.size()); // do it again, to make sure we don't have any for-scoping // problems with older compilers @@ -117,21 +105,21 @@ void tst_QGlobal::for_each() foreach(int i, list) { QCOMPARE(i, counter++); } - QCOMPARE(counter, list.count()); + QCOMPARE(counter, list.size()); // check whether we can pass a constructor as container argument counter = 0; foreach (int i, QList<int>(list)) { QCOMPARE(i, counter++); } - QCOMPARE(counter, list.count()); + QCOMPARE(counter, list.size()); // check whether we can use a lambda counter = 0; foreach (int i, [&](){ return list; }()) { QCOMPARE(i, counter++); } - QCOMPARE(counter, list.count()); + QCOMPARE(counter, list.size()); // Should also work with an existing variable int local = 0; @@ -139,7 +127,7 @@ void tst_QGlobal::for_each() foreach (local, list) { QCOMPARE(local, counter++); } - QCOMPARE(counter, list.count()); + QCOMPARE(counter, list.size()); QCOMPARE(local, counter - 1); // Test the macro does not mess if/else conditions @@ -149,7 +137,7 @@ void tst_QGlobal::for_each() QCOMPARE(i, counter++); else QFAIL("If/Else mismatch"); - QCOMPARE(counter, list.count()); + QCOMPARE(counter, list.size()); counter = 0; if (false) @@ -160,7 +148,7 @@ void tst_QGlobal::for_each() foreach (int i, list) if (false) { } else QCOMPARE(i, counter++); - QCOMPARE(counter, list.count()); + QCOMPARE(counter, list.size()); // break and continue counter = 0; @@ -445,11 +433,166 @@ void tst_QGlobal::integerForSize() static_assert(sizeof(QIntegerForSize<2>::Signed) == 2); static_assert(sizeof(QIntegerForSize<4>::Signed) == 4); static_assert(sizeof(QIntegerForSize<8>::Signed) == 8); +#ifdef QT_SUPPORTS_INT128 + static_assert(sizeof(QIntegerForSize<16>::Signed) == 16); +#endif static_assert(sizeof(QIntegerForSize<1>::Unsigned) == 1); static_assert(sizeof(QIntegerForSize<2>::Unsigned) == 2); static_assert(sizeof(QIntegerForSize<4>::Unsigned) == 4); static_assert(sizeof(QIntegerForSize<8>::Unsigned) == 8); +#ifdef QT_SUPPORTS_INT128 + static_assert(sizeof(QIntegerForSize<16>::Unsigned) == 16); +#endif +} + +void tst_QGlobal::int128Literals() +{ +#ifdef QT_SUPPORTS_INT128 +#define COMPARE_EQ(lhs, rhs, Expected128) do { \ + constexpr auto lhs_ = lhs; \ + static_assert(std::is_same_v<std::remove_cv_t<decltype(lhs_)>, Expected128>); \ + QCOMPARE_EQ(lhs_, rhs); \ + } while (0) + COMPARE_EQ(Q_INT128_MIN, std::numeric_limits<qint128>::min(), qint128); + COMPARE_EQ(Q_INT128_MAX, std::numeric_limits<qint128>::max(), qint128); + COMPARE_EQ(Q_UINT128_MAX, std::numeric_limits<quint128>::max(), quint128); + QCOMPARE_EQ(tst_qint128_min(), Q_INT128_MIN); + QCOMPARE_EQ(tst_qint128_max(), Q_INT128_MAX); + QCOMPARE_EQ(tst_quint128_max(), Q_UINT128_MAX); + { + #define CHECK_S(x) COMPARE_EQ(Q_INT128_C(x), Q_INT64_C(x), qint128) + #define CHECK_U(x) COMPARE_EQ(Q_UINT128_C(x), Q_UINT64_C(x), quint128); + #define CHECK(x) do { CHECK_S(x); CHECK_U(x); } while (0) + // basics: + CHECK(0); + CHECK(1); + CHECK_S(-1); + QCOMPARE_EQ(Q_INT64_C(9223372036854775807), std::numeric_limits<qint64>::max()); + CHECK(9223372036854775807); // LLONG_MAX + // Q_INT64_C(-9223372036854775808) gives -Wimplicitly-unsigned-literal on GCC, so use numeric_limits: + { + constexpr auto i = Q_INT128_C(-9223372036854775808); // LLONG_MIN + static_assert(std::is_same_v<decltype(i), const qint128>); + QCOMPARE_EQ(i, std::numeric_limits<qint64>::min()); + } + // actual 128-bit numbers + { + constexpr auto i = Q_INT128_C( 9223372036854775808); // LLONG_MAX + 1 + constexpr auto u = Q_UINT128_C(9223372036854775808); // LLONG_MAX + 1 + static_assert(std::is_same_v<decltype(i), const qint128>); + static_assert(std::is_same_v<decltype(u), const quint128>); + QCOMPARE_EQ(i, qint128{ std::numeric_limits<qint64>::max()} + 1); + QCOMPARE_EQ(u, quint128{std::numeric_limits<qint64>::max()} + 1); + } + { + constexpr auto i = Q_INT128_C(-9223372036854775809); // LLONG_MIN - 1 + static_assert(std::is_same_v<decltype(i), const qint128>); + QCOMPARE_EQ(i, qint128{std::numeric_limits<qint64>::min()} - 1); + } + { + constexpr auto i = Q_INT128_C( 18446744073709551616); // ULLONG_MAX + 1 + constexpr auto u = Q_UINT128_C(18446744073709551616); + constexpr auto expected = qint128{1} << 64; + static_assert(std::is_same_v<decltype(i), const qint128>); + static_assert(std::is_same_v<decltype(expected), const qint128>); + static_assert(std::is_same_v<decltype(u), const quint128>); + QCOMPARE_EQ(i, expected); + QCOMPARE_EQ(u, quint128{expected}); + } + { + // compilers don't let one write signed _MIN literals, so use MIN + 1: + // Q_INT128_C(-170141183460469231731687303715884105728) gives + // ERROR: ~~~ outside range of representable values of type qint128 + // This is because the unary minus is technically speaking not part of + // the literal, but called on the result of the literal. + constexpr auto i = Q_INT128_C(-170141183460469231731687303715884105727); // 128-bit MIN + 1 + static_assert(std::is_same_v<decltype(i), const qint128>); + QCOMPARE_EQ(i, std::numeric_limits<qint128>::min() + 1); + } + { + constexpr auto i = Q_INT128_C( 170141183460469231731687303715884105727); // MAX + constexpr auto u = Q_UINT128_C(340282366920938463463374607431768211455); // UMAX + static_assert(std::is_same_v<decltype(i), const qint128>); + static_assert(std::is_same_v<decltype(u), const quint128>); + QCOMPARE_EQ(i, std::numeric_limits<qint128>::max()); + QCOMPARE_EQ(u, std::numeric_limits<quint128>::max()); + QCOMPARE_EQ(u, Q_UINT128_C(-1)); + } + + // binary literals: + CHECK(0b0); + CHECK(0b1); + CHECK_S(-0b1); + CHECK(0b01); + CHECK(0b10); + CHECK(0b1'1); // with digit separator + CHECK(0b0111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111); + //bytes |---1---| |---2---| |---3---| |---4---| |---5---| |---6---| |---7---| |---8---| + { + // bytes: |---1---| |---2---| |---3---| |---4---| |---5---| |---6---| |---7---| |---8---| |---9---| |--10---| |--11---| |--12---| |--13---| |--14---| |--15---| |--16---| + constexpr auto i = Q_INT128_C( 0b0111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111); + constexpr auto u = Q_UINT128_C(0b1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111'1111); + static_assert(std::is_same_v<decltype(i), const qint128>); + static_assert(std::is_same_v<decltype(u), const quint128>); + QCOMPARE_EQ(i, std::numeric_limits<qint128>::max()); + QCOMPARE_EQ(u, std::numeric_limits<quint128>::max()); + QCOMPARE_EQ(u, Q_UINT128_C(-0b1)); + } + + // octal literals: + CHECK(00); + CHECK(01); + CHECK(02); + CHECK(03); + CHECK(04); + CHECK(05); + CHECK(06); + CHECK(07); + CHECK_S(-01); + CHECK(010); + CHECK_S(-01'0); // with digit separator + CHECK(07'7777'7777'7777'7777'7777); // LLONG_MAX + { + // bits: 120| 108| 96| 84| 72| 60| 48| 36| 24| 12| 0| + constexpr auto i = Q_INT128_C( 0177'7777'7777'7777'7777'7777'7777'7777'7777'7777'7777); + constexpr auto u = Q_UINT128_C(0377'7777'7777'7777'7777'7777'7777'7777'7777'7777'7777); + static_assert(std::is_same_v<decltype(i), const qint128>); + static_assert(std::is_same_v<decltype(u), const quint128>); + QCOMPARE_EQ(i, std::numeric_limits<qint128>::max()); + QCOMPARE_EQ(u, std::numeric_limits<quint128>::max()); + QCOMPARE_EQ(u, Q_UINT128_C(-01)); + } + + // hex literals: + CHECK(0x0); + CHECK(0x1); + CHECK(0x9); + CHECK(0xA); + CHECK(0xB); + CHECK(0xC); + CHECK(0xD); + CHECK(0xE); + CHECK(0x0F); + CHECK(0x10); + CHECK_S(-0x1); + CHECK_S(-0x1'0); // with digit separator + CHECK(0x7FFF'FFFF'FFFF'FFFF); + { + constexpr auto i = Q_INT128_C( 0x7FFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF); + constexpr auto u = Q_UINT128_C(0xFFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF'FFFF); + static_assert(std::is_same_v<decltype(i), const qint128>); + static_assert(std::is_same_v<decltype(u), const quint128>); + QCOMPARE_EQ(i, std::numeric_limits<qint128>::max()); + QCOMPARE_EQ(u, std::numeric_limits<quint128>::max()); + QCOMPARE_EQ(Q_UINT128_C(-1), u); + } + #undef CHECK + } +#undef COMPARE_EQ +#else + QSKIP("This test requires 128-bit integer support enabled in the compiler."); +#endif } typedef QPair<const char *, const char *> stringpair; @@ -715,5 +858,79 @@ void tst_QGlobal::testqToUnderlying() QCOMPARE(qToUnderlying(EE2), 456UL); } +void tst_QGlobal::nodiscard() +{ + // Syntax-only test, just to make sure that the Q_NODISCARD_* compile + // on all platforms. + // Other code is just to silence all various compiler warnings about + // unused private members or methods. + class Test { + public: + Q_NODISCARD_CTOR_X("Why construct a Test instead of just passing the int through?") + explicit Test(int val) : m_val(val) {} + Q_NODISCARD_CTOR explicit Test(float val) : m_val(int(val)) {} + + Q_NODISCARD_X("Why call get() if you don't use the returned value, hu?") // NOT idiomatic use! + int get() const { return m_val; } + + private: + int m_val; + }; + + Test t{42}; + QCOMPARE(t.get(), 42); + Test t2{42.0f}; + QCOMPARE(t2.get(), 42); +} + +QT_BEGIN_NAMESPACE + +// Compile-time typeinfo tests +struct Complex1 +{ + ~Complex1(); +}; +static_assert(QTypeInfo<Complex1>::isComplex); +static_assert(!QTypeInfo<Complex1>::isRelocatable); + +struct Complex2 +{ + Complex2(Complex2 &&); +}; +static_assert(QTypeInfo<Complex2>::isComplex); +static_assert(!QTypeInfo<Complex2>::isRelocatable); + +struct Complex3 +{ + Complex3(int); +}; +static_assert(QTypeInfo<Complex3>::isComplex); +static_assert(QTypeInfo<Complex3>::isRelocatable); + +struct Relocatable1 +{ + ~Relocatable1(); +}; +Q_DECLARE_TYPEINFO(Relocatable1, Q_RELOCATABLE_TYPE); +static_assert(QTypeInfo<Relocatable1>::isComplex); +static_assert(QTypeInfo<Relocatable1>::isRelocatable); + +struct Relocatable2 +{ + Relocatable2(int); +}; +Q_DECLARE_TYPEINFO(Relocatable2, Q_RELOCATABLE_TYPE); +static_assert(QTypeInfo<Relocatable2>::isComplex); +static_assert(QTypeInfo<Relocatable2>::isRelocatable); + +struct Trivial1 +{ + int x[42]; +}; +static_assert(!QTypeInfo<Trivial1>::isComplex); +static_assert(QTypeInfo<Trivial1>::isRelocatable); + +QT_END_NAMESPACE + QTEST_APPLESS_MAIN(tst_QGlobal) #include "tst_qglobal.moc" |