| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | |
| 30 | #include <QtCore/QCoreApplication> |
| 31 | #include <QtTest/QtTest> |
| 32 | |
| 33 | class tst_StrCmp: public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | private slots: |
| 38 | void compareCharStars() const; |
| 39 | void compareByteArray() const; |
| 40 | void failByteArray() const; |
| 41 | void failByteArrayNull() const; |
| 42 | void failByteArrayEmpty() const; |
| 43 | void failByteArraySingleChars() const; |
| 44 | }; |
| 45 | |
| 46 | void tst_StrCmp::compareCharStars() const |
| 47 | { |
| 48 | QCOMPARE((const char *)"foo" , (const char *)"foo" ); |
| 49 | |
| 50 | const char *str1 = "foo" ; |
| 51 | QCOMPARE("foo" , str1); |
| 52 | QCOMPARE(str1, "foo" ); |
| 53 | QCOMPARE(str1, str1); |
| 54 | |
| 55 | char *str2 = const_cast<char *>("foo" ); |
| 56 | QCOMPARE("foo" , str2); |
| 57 | QCOMPARE(str2, "foo" ); |
| 58 | QCOMPARE(str2, str2); |
| 59 | QCOMPARE(str1, str2); |
| 60 | QCOMPARE(str2, str1); |
| 61 | |
| 62 | const char str3[] = "foo" ; |
| 63 | QCOMPARE((const char *)str3, "foo" ); |
| 64 | QCOMPARE("foo" , (const char *)str3); |
| 65 | QCOMPARE((const char *)str3, str1); |
| 66 | QCOMPARE((const char *)str3, str2); |
| 67 | QCOMPARE(str1, (const char *)str3); |
| 68 | QCOMPARE(str2, (const char *)str3); |
| 69 | } |
| 70 | |
| 71 | void tst_StrCmp::compareByteArray() const |
| 72 | { |
| 73 | QByteArray ba = "foo" ; |
| 74 | QEXPECT_FAIL("" , "Next test should fail" , Continue); |
| 75 | QCOMPARE(ba.constData(), "bar" ); |
| 76 | QCOMPARE(ba.constData(), "foo" ); |
| 77 | |
| 78 | char *bar = const_cast<char *>("bar" ); |
| 79 | char *foo = const_cast<char *>("foo" ); |
| 80 | |
| 81 | QEXPECT_FAIL("" , "Next test should fail" , Continue); |
| 82 | QCOMPARE(ba.data(), bar); |
| 83 | QCOMPARE(ba.data(), foo); |
| 84 | |
| 85 | const char *cbar = "bar" ; |
| 86 | const char *cfoo = "foo" ; |
| 87 | |
| 88 | QEXPECT_FAIL("" , "Next test should fail" , Continue); |
| 89 | QCOMPARE(ba.constData(), cbar); |
| 90 | QCOMPARE(ba.constData(), cfoo); |
| 91 | |
| 92 | /* Create QByteArrays of the size that makes the corresponding toString() crop output. */ |
| 93 | const QByteArray b(500, 'A'); |
| 94 | const QByteArray a(500, 'B'); |
| 95 | |
| 96 | QCOMPARE(a, b); |
| 97 | } |
| 98 | |
| 99 | void tst_StrCmp::failByteArray() const |
| 100 | { |
| 101 | /* Compare small, different byte arrays. */ |
| 102 | QCOMPARE(QByteArray("abc" ), QByteArray("cba" )); |
| 103 | } |
| 104 | |
| 105 | void tst_StrCmp::failByteArrayNull() const |
| 106 | { |
| 107 | /* Compare null byte array against with content. */ |
| 108 | QCOMPARE(QByteArray("foo" ), QByteArray()); |
| 109 | } |
| 110 | |
| 111 | void tst_StrCmp::failByteArrayEmpty() const |
| 112 | { |
| 113 | QCOMPARE(QByteArray("" ), QByteArray("foo" )); |
| 114 | } |
| 115 | |
| 116 | void tst_StrCmp::failByteArraySingleChars() const |
| 117 | { |
| 118 | /* Compare null byte array against with content. */ |
| 119 | //QCOMPARE(QString(250, 'a'), QString(250, 'b')); |
| 120 | QCOMPARE(QByteArray("6" ), QByteArray("7" )); |
| 121 | } |
| 122 | |
| 123 | QTEST_MAIN(tst_StrCmp) |
| 124 | |
| 125 | #include "tst_strcmp.moc" |
| 126 | |