File indexing completed on 2024-06-16 05:02:00

0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include <QDebug>
0024 #include <QMetaType>
0025 #include <QTest>
0026 #include "test_RingBuffer.h"
0027 #include "Common/RingBuffer.h"
0028 
0029 using namespace Common;
0030 
0031 Q_DECLARE_METATYPE(QVector<int>);
0032 
0033 void RingBufferTest::testOne()
0034 {
0035     QFETCH(int, size);
0036     QFETCH(QVector<int>, sourceData);
0037     QFETCH(QVector<int>, expectedData);
0038     RingBuffer<int> rb(size);
0039     Q_FOREACH(const int item, sourceData) {
0040         rb.append(item);
0041     }
0042     QVector<int> output;
0043 
0044     for (RingBuffer<int>::const_iterator it = rb.begin(); it != rb.end(); ++it) {
0045         output << *it;
0046         if (output.size() >= size * 2) {
0047             QFAIL("Iterated way too many times");
0048             break;
0049         }
0050     }
0051 
0052     // Correct amount of data received?
0053     QCOMPARE(output.size(), expectedData.size());
0054 
0055     // Correct data?
0056     QCOMPARE(expectedData, output);
0057 
0058     // Did it overwrite a correct number of items?
0059     QCOMPARE(static_cast<uint>(sourceData.size() - expectedData.size()), rb.skippedCount());
0060 
0061     // Try to nuke them
0062     rb.clear();
0063     // Is it really empty?
0064     // Yes, QVERIFY instead of QCOMPARE -- they can't be printed
0065     QVERIFY(rb.begin() == rb.end());
0066 }
0067 
0068 void RingBufferTest::testOne_data()
0069 {
0070     QTest::addColumn<int>("size");
0071     QTest::addColumn<QVector<int> >("sourceData");
0072     QTest::addColumn<QVector<int> >("expectedData");
0073 
0074     QVector<int> data;
0075     QTest::newRow("empty") << 5 << data << data;
0076 
0077     data.clear();
0078     data << 333;
0079     QTest::newRow("one-value") << 5 << data << data;
0080 
0081     data.clear();
0082     data << 333 << 666;
0083     QTest::newRow("two-values") << 5 << data << data;
0084 
0085     data.clear();
0086     data << 333 << 666 << 7;
0087     QTest::newRow("three-values") << 5 << data << data;
0088 
0089     data.clear();
0090     data << 333 << 666 << 7 << 15;
0091     QTest::newRow("four-values") << 5 << data << data;
0092 
0093     data.clear();
0094     data << 333 << 666 << 7 << 15 << 9;
0095     QTest::newRow("five-values") << 5 << data << data;
0096 
0097     data.clear();
0098     data << 333 << 666 << 7 << 15 << 9 << 13;
0099     QVector<int> expected;
0100     expected << 666 << 7 << 15 << 9 << 13;
0101     QTest::newRow("six-wrapped") << 5 << data << expected;
0102 
0103     data.clear();
0104     data << 333 << 666 << 7 << 15 << 9 << 13 << 0;
0105     expected.clear();
0106     expected << 7 << 15 << 9 << 13 << 0;
0107     QTest::newRow("seven-wrapped") << 5 << data << expected;
0108 
0109     data.clear();
0110     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2;
0111     expected.clear();
0112     expected << 15 << 9 << 13 << 0 << 2;
0113     QTest::newRow("eight-wrapped") << 5 << data << expected;
0114 
0115     data.clear();
0116     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2 << 1;
0117     expected.clear();
0118     expected << 9 << 13 << 0 << 2 << 1;
0119     QTest::newRow("nine-wrapped") << 5 << data << expected;
0120 
0121     data.clear();
0122     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2 << 1 << 800500;
0123     expected.clear();
0124     expected << 13 << 0 << 2 << 1 << 800500;
0125     QTest::newRow("ten-wrapped") << 5 << data << expected;
0126 
0127     data.clear();
0128     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2 << 1 << 800500 << 11;
0129     expected.clear();
0130     expected << 0 << 2 << 1 << 800500 << 11;
0131     QTest::newRow("eleven-wrapped") << 5 << data << expected;
0132 }
0133 
0134 QTEST_GUILESS_MAIN( RingBufferTest )