| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2018 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 | #include <QtTest/QtTest> | 
| 30 |  | 
| 31 | #include <private/qmakearray_p.h> | 
| 32 |  | 
| 33 |  | 
| 34 | class tst_QMakeArray : public QObject | 
| 35 | { | 
| 36 |     Q_OBJECT | 
| 37 |  | 
| 38 | private slots: | 
| 39 |     void quicksort(); | 
| 40 | }; | 
| 41 |  | 
| 42 | struct Pair { | 
| 43 |     unsigned int key; | 
| 44 |     unsigned int val; | 
| 45 |  | 
| 46 |     constexpr bool operator <=(const Pair &that) const noexcept | 
| 47 |     { | 
| 48 |         return key <= that.key; | 
| 49 |     } | 
| 50 |  | 
| 51 |     constexpr bool operator<(const Pair &that) const noexcept | 
| 52 |     { | 
| 53 |         return key < that.key; | 
| 54 |     } | 
| 55 |  | 
| 56 |     constexpr bool operator==(const Pair &that) const noexcept | 
| 57 |     { | 
| 58 |         return key == that.key; | 
| 59 |     } | 
| 60 | }; | 
| 61 |  | 
| 62 | template<std::size_t Key, std::size_t Val> | 
| 63 | struct PairQuickSortElem | 
| 64 | { | 
| 65 |     using Type = Pair; | 
| 66 |     static constexpr Type data() noexcept { return Type{.key: Key, .val: Val}; } | 
| 67 | }; | 
| 68 |  | 
| 69 | void tst_QMakeArray::quicksort() | 
| 70 | { | 
| 71 |     constexpr const auto sorted_array = qMakeArray( | 
| 72 |         QSortedData< | 
| 73 |             PairQuickSortElem<10, 0>, | 
| 74 |             PairQuickSortElem<5, 0>, | 
| 75 |             PairQuickSortElem<7, 0>, | 
| 76 |             PairQuickSortElem<1, 0>, | 
| 77 |             PairQuickSortElem<8, 0>, | 
| 78 |             PairQuickSortElem<6, 0>, | 
| 79 |             PairQuickSortElem<4, 0>, | 
| 80 |             PairQuickSortElem<3, 0>, | 
| 81 |             PairQuickSortElem<1, 0>, | 
| 82 |             PairQuickSortElem<2, 0>, | 
| 83 |             PairQuickSortElem<10, 0>, | 
| 84 |             PairQuickSortElem<5, 0> | 
| 85 |         >::Data{}); | 
| 86 |     static_assert(sorted_array.size() == 12, "sorted_array.size() != 12" ); | 
| 87 |     QCOMPARE(sorted_array[0].key, 1u); | 
| 88 |     QCOMPARE(sorted_array[1].key, 1u); | 
| 89 |     QCOMPARE(sorted_array[2].key, 2u); | 
| 90 |     QCOMPARE(sorted_array[3].key, 3u); | 
| 91 |     QCOMPARE(sorted_array[4].key, 4u); | 
| 92 |     QCOMPARE(sorted_array[5].key, 5u); | 
| 93 |     QCOMPARE(sorted_array[6].key, 5u); | 
| 94 |     QCOMPARE(sorted_array[7].key, 6u); | 
| 95 |     QCOMPARE(sorted_array[8].key, 7u); | 
| 96 |     QCOMPARE(sorted_array[9].key, 8u); | 
| 97 |     QCOMPARE(sorted_array[10].key, 10u); | 
| 98 |     QCOMPARE(sorted_array[11].key, 10u); | 
| 99 | } | 
| 100 |  | 
| 101 |  | 
| 102 | QTEST_APPLESS_MAIN(tst_QMakeArray) | 
| 103 | #include "tst_qmakearray.moc" | 
| 104 |  |