1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
#include <QtTest/QtTest>
44
#include <QtCore/qtypetraits.h>
46
class tst_QGlobal: public QObject
57
void qConstructorFunction();
62
void tst_QGlobal::qIsNull()
67
QVERIFY(::qIsNull(d));
68
QVERIFY(::qIsNull(f));
73
QVERIFY(!::qIsNull(d));
74
QVERIFY(!::qIsNull(f));
79
QVERIFY(::qIsNull(d));
80
QVERIFY(::qIsNull(f));
83
void tst_QGlobal::for_each()
86
list << 0 << 1 << 2 << 3 << 4 << 5;
89
foreach(int i, list) {
90
QCOMPARE(i, counter++);
92
QCOMPARE(counter, list.count());
94
// do it again, to make sure we don't have any for-scoping
95
// problems with older compilers
97
foreach(int i, list) {
98
QCOMPARE(i, counter++);
100
QCOMPARE(counter, list.count());
103
void tst_QGlobal::qassert()
115
Q_ASSERT_X(false, "tst_QGlobal", "qassert");
130
Q_ASSERT_X(false, "tst_QGlobal", "qassert");
136
void tst_QGlobal::qtry()
147
#ifdef QT_NO_EXCEPTIONS
153
// check propper if/else scoping
200
void tst_QGlobal::checkptr()
203
QCOMPARE(q_check_ptr(&i), &i);
205
const char *c = "hello";
206
QCOMPARE(q_check_ptr(c), c);
209
// Check Q_STATIC_ASSERT, It should compile
210
// note that, we are not able to test Q_STATIC_ASSERT(false), to do it manually someone has
211
// to replace expressions (in the asserts) one by one to false, and check if it breaks build.
217
Q_STATIC_ASSERT(true);
218
Q_STATIC_ASSERT(!false);
219
Q_STATIC_ASSERT_X(true,"");
220
Q_STATIC_ASSERT_X(!false,"");
224
Q_STATIC_ASSERT(true);
225
Q_STATIC_ASSERT(!false);
226
Q_STATIC_ASSERT_X(true,"");
227
Q_STATIC_ASSERT_X(!false,"");
229
Q_STATIC_ASSERT(true);
230
Q_STATIC_ASSERT(!false);
231
Q_STATIC_ASSERT_X(true,"");
232
Q_STATIC_ASSERT_X(!false,"");
239
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
240
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
241
Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0,"");
242
Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0,"");
245
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
246
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
247
Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0, "");
248
Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0, "");
254
Q_STATIC_ASSERT(sizeof(T));
255
Q_STATIC_ASSERT_X(sizeof(T), "");
258
template<typename T1, typename T2>
261
static const bool True = true;
262
typedef typename T1::T DependentType;
263
Q_STATIC_ASSERT(True);
264
Q_STATIC_ASSERT(!!True);
265
Q_STATIC_ASSERT(sizeof(DependentType));
266
Q_STATIC_ASSERT(!!sizeof(DependentType));
267
Q_STATIC_ASSERT_X(True, "");
268
Q_STATIC_ASSERT_X(!!True, "");
269
Q_STATIC_ASSERT_X(sizeof(DependentType), "");
270
Q_STATIC_ASSERT_X(!!sizeof(DependentType), "");
275
static const bool Value = Template<TypeDef, int>::True;
276
Q_STATIC_ASSERT(Value);
277
Q_STATIC_ASSERT(!!Value);
278
Q_STATIC_ASSERT_X(Value, "");
279
Q_STATIC_ASSERT_X(!!Value, "");
282
void tst_QGlobal::qstaticassert()
284
// Force compilation of these classes
291
QVERIFY(true); // if the test compiles it has passed.
294
static int qConstructorFunctionValue;
295
static void qConstructorFunctionCtor()
297
qConstructorFunctionValue = 123;
299
Q_CONSTRUCTOR_FUNCTION(qConstructorFunctionCtor);
301
void tst_QGlobal::qConstructorFunction()
303
QCOMPARE(qConstructorFunctionValue, 123);
310
enum isEnum_B_Byte { isEnum_B_Byte_x = 63 };
311
enum isEnum_B_Short { isEnum_B_Short_x = 1024 };
312
enum isEnum_B_Int { isEnum_B_Int_x = 1 << 20 };
318
operator int() const;
323
operator int() const;
331
#if defined (Q_COMPILER_CLASS_ENUM)
332
enum class isEnum_G : qint64 {};
335
void tst_QGlobal::isEnum()
337
#if defined (Q_CC_MSVC)
338
#define IS_ENUM_TRUE(x) (Q_IS_ENUM(x) == true)
339
#define IS_ENUM_FALSE(x) (Q_IS_ENUM(x) == false)
341
#define IS_ENUM_TRUE(x) (Q_IS_ENUM(x) == true && QtPrivate::is_enum<x>::value == true)
342
#define IS_ENUM_FALSE(x) (Q_IS_ENUM(x) == false && QtPrivate::is_enum<x>::value == false)
345
QVERIFY(IS_ENUM_TRUE(isEnum_B_Byte));
346
QVERIFY(IS_ENUM_TRUE(const isEnum_B_Byte));
347
QVERIFY(IS_ENUM_TRUE(volatile isEnum_B_Byte));
348
QVERIFY(IS_ENUM_TRUE(const volatile isEnum_B_Byte));
350
QVERIFY(IS_ENUM_TRUE(isEnum_B_Short));
351
QVERIFY(IS_ENUM_TRUE(const isEnum_B_Short));
352
QVERIFY(IS_ENUM_TRUE(volatile isEnum_B_Short));
353
QVERIFY(IS_ENUM_TRUE(const volatile isEnum_B_Short));
355
QVERIFY(IS_ENUM_TRUE(isEnum_B_Int));
356
QVERIFY(IS_ENUM_TRUE(const isEnum_B_Int));
357
QVERIFY(IS_ENUM_TRUE(volatile isEnum_B_Int));
358
QVERIFY(IS_ENUM_TRUE(const volatile isEnum_B_Int));
360
QVERIFY(IS_ENUM_TRUE(isEnum_F::AnEnum));
361
QVERIFY(IS_ENUM_TRUE(const isEnum_F::AnEnum));
362
QVERIFY(IS_ENUM_TRUE(volatile isEnum_F::AnEnum));
363
QVERIFY(IS_ENUM_TRUE(const volatile isEnum_F::AnEnum));
365
QVERIFY(IS_ENUM_FALSE(void));
366
QVERIFY(IS_ENUM_FALSE(isEnum_B_Byte &));
367
QVERIFY(IS_ENUM_FALSE(isEnum_B_Byte[1]));
368
QVERIFY(IS_ENUM_FALSE(const isEnum_B_Byte[1]));
369
QVERIFY(IS_ENUM_FALSE(isEnum_B_Byte[]));
370
QVERIFY(IS_ENUM_FALSE(int));
371
QVERIFY(IS_ENUM_FALSE(float));
372
QVERIFY(IS_ENUM_FALSE(isEnum_A));
373
QVERIFY(IS_ENUM_FALSE(isEnum_A *));
374
QVERIFY(IS_ENUM_FALSE(const isEnum_A));
375
QVERIFY(IS_ENUM_FALSE(isEnum_C));
376
QVERIFY(IS_ENUM_FALSE(isEnum_D));
377
QVERIFY(IS_ENUM_FALSE(isEnum_E));
378
QVERIFY(IS_ENUM_FALSE(void()));
379
QVERIFY(IS_ENUM_FALSE(void(*)()));
380
QVERIFY(IS_ENUM_FALSE(int isEnum_A::*));
381
QVERIFY(IS_ENUM_FALSE(void (isEnum_A::*)()));
383
QVERIFY(IS_ENUM_FALSE(size_t));
384
QVERIFY(IS_ENUM_FALSE(bool));
385
QVERIFY(IS_ENUM_FALSE(wchar_t));
387
QVERIFY(IS_ENUM_FALSE(char));
388
QVERIFY(IS_ENUM_FALSE(unsigned char));
389
QVERIFY(IS_ENUM_FALSE(short));
390
QVERIFY(IS_ENUM_FALSE(unsigned short));
391
QVERIFY(IS_ENUM_FALSE(int));
392
QVERIFY(IS_ENUM_FALSE(unsigned int));
393
QVERIFY(IS_ENUM_FALSE(long));
394
QVERIFY(IS_ENUM_FALSE(unsigned long));
396
QVERIFY(IS_ENUM_FALSE(qint8));
397
QVERIFY(IS_ENUM_FALSE(quint8));
398
QVERIFY(IS_ENUM_FALSE(qint16));
399
QVERIFY(IS_ENUM_FALSE(quint16));
400
QVERIFY(IS_ENUM_FALSE(qint32));
401
QVERIFY(IS_ENUM_FALSE(quint32));
402
QVERIFY(IS_ENUM_FALSE(qint64));
403
QVERIFY(IS_ENUM_FALSE(quint64));
405
QVERIFY(IS_ENUM_FALSE(void *));
406
QVERIFY(IS_ENUM_FALSE(int *));
408
#if defined (Q_COMPILER_UNICODE_STRINGS)
409
QVERIFY(IS_ENUM_FALSE(char16_t));
410
QVERIFY(IS_ENUM_FALSE(char32_t));
413
#if defined (Q_COMPILER_CLASS_ENUM)
414
// Strongly type class enums are not handled by the
415
// fallback type traits implementation. Any compiler
416
// supported by Qt that supports C++0x class enums
417
// should also support the __is_enum intrinsic.
418
QVERIFY(Q_IS_ENUM(isEnum_G) == true);
426
template <class T> struct AlignmentInStruct { T dummy; };
428
typedef int (*fun) ();
429
typedef int (Empty::*memFun) ();
431
#define TEST_AlignOf(type, alignment) \
433
TEST_AlignOf_impl(type, alignment); \
435
TEST_AlignOf_impl(type &, alignment); \
436
TEST_AlignOf_RValueRef(type &&, alignment); \
438
TEST_AlignOf_impl(type [5], alignment); \
439
TEST_AlignOf_impl(type (&) [5], alignment); \
441
TEST_AlignOf_impl(AlignmentInStruct<type>, alignment); \
443
/* Some internal sanity validation, just for fun */ \
444
TEST_AlignOf_impl(AlignmentInStruct<type [5]>, alignment); \
445
TEST_AlignOf_impl(AlignmentInStruct<type &>, Q_ALIGNOF(void *)); \
446
TEST_AlignOf_impl(AlignmentInStruct<type (&) [5]>, \
447
Q_ALIGNOF(void *)); \
448
TEST_AlignOf_RValueRef(AlignmentInStruct<type &&>, \
449
Q_ALIGNOF(void *)); \
453
#ifdef Q_COMPILER_RVALUE_REFS
454
#define TEST_AlignOf_RValueRef(type, alignment) \
455
TEST_AlignOf_impl(type, alignment)
457
#define TEST_AlignOf_RValueRef(type, alignment) do {} while (false)
460
#define TEST_AlignOf_impl(type, alignment) \
462
QCOMPARE(Q_ALIGNOF(type), size_t(alignment)); \
463
/* Compare to native operator for compilers that support it,
464
otherwise... erm... check consistency! :-) */ \
465
QCOMPARE(QT_EMULATED_ALIGNOF(type), Q_ALIGNOF(type)); \
469
void tst_QGlobal::qAlignOf()
471
// Built-in types, except 64-bit integers and double
472
TEST_AlignOf(char, 1);
473
TEST_AlignOf(signed char, 1);
474
TEST_AlignOf(unsigned char, 1);
475
TEST_AlignOf(qint8, 1);
476
TEST_AlignOf(quint8, 1);
477
TEST_AlignOf(qint16, 2);
478
TEST_AlignOf(quint16, 2);
479
TEST_AlignOf(qint32, 4);
480
TEST_AlignOf(quint32, 4);
481
TEST_AlignOf(void *, sizeof(void *));
483
// Depends on platform and compiler, disabling test for now
484
// TEST_AlignOf(long double, 16);
487
TEST_AlignOf(Empty, 1);
490
TEST_AlignOf(fun, Q_ALIGNOF(void *));
491
TEST_AlignOf(memFun, Q_ALIGNOF(void *));
494
// 64-bit integers and double
495
TEST_AlignOf_impl(qint64, 8);
496
TEST_AlignOf_impl(quint64, 8);
497
TEST_AlignOf_impl(double, 8);
499
TEST_AlignOf_impl(qint64 &, 8);
500
TEST_AlignOf_impl(quint64 &, 8);
501
TEST_AlignOf_impl(double &, 8);
503
TEST_AlignOf_RValueRef(qint64 &&, 8);
504
TEST_AlignOf_RValueRef(quint64 &&, 8);
505
TEST_AlignOf_RValueRef(double &&, 8);
507
// 32-bit x86 ABI idiosyncrasies
508
#if defined(Q_PROCESSOR_X86_32) && !defined(Q_OS_WIN)
509
TEST_AlignOf_impl(AlignmentInStruct<qint64>, 4);
511
TEST_AlignOf_impl(AlignmentInStruct<qint64>, 8);
514
TEST_AlignOf_impl(AlignmentInStruct<quint64>, Q_ALIGNOF(AlignmentInStruct<qint64>));
515
TEST_AlignOf_impl(AlignmentInStruct<double>, Q_ALIGNOF(AlignmentInStruct<qint64>));
517
// 32-bit x86 ABI, Clang disagrees with gcc
518
#if !defined(Q_PROCESSOR_X86_32) || !defined(Q_CC_CLANG)
519
TEST_AlignOf_impl(qint64 [5], Q_ALIGNOF(qint64));
521
TEST_AlignOf_impl(qint64 [5], Q_ALIGNOF(AlignmentInStruct<qint64>));
524
TEST_AlignOf_impl(qint64 (&) [5], Q_ALIGNOF(qint64 [5]));
525
TEST_AlignOf_impl(quint64 [5], Q_ALIGNOF(quint64 [5]));
526
TEST_AlignOf_impl(quint64 (&) [5], Q_ALIGNOF(quint64 [5]));
527
TEST_AlignOf_impl(double [5], Q_ALIGNOF(double [5]));
528
TEST_AlignOf_impl(double (&) [5], Q_ALIGNOF(double [5]));
532
#undef TEST_AlignOf_RValueRef
533
#undef TEST_AlignOf_impl
535
QTEST_MAIN(tst_QGlobal)
536
#include "tst_qglobal.moc"