1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2 
3    This file is part of the Trojita Qt IMAP e-mail client,
4    http://trojita.flaska.net/
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2 of
9    the License or (at your option) version 3 or any later version
10    accepted by the membership of KDE e.V. (or its successor approved
11    by the membership of KDE e.V.), which shall act as a proxy
12    defined in Section 14 of version 3 of the license.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include <QDebug>
24 #include <QMetaType>
25 #include <QTest>
26 #include "test_RingBuffer.h"
27 #include "Common/RingBuffer.h"
28 
29 using namespace Common;
30 
31 Q_DECLARE_METATYPE(QVector<int>);
32 
testOne()33 void RingBufferTest::testOne()
34 {
35     QFETCH(int, size);
36     QFETCH(QVector<int>, sourceData);
37     QFETCH(QVector<int>, expectedData);
38     RingBuffer<int> rb(size);
39     Q_FOREACH(const int item, sourceData) {
40         rb.append(item);
41     }
42     QVector<int> output;
43 
44     for (RingBuffer<int>::const_iterator it = rb.begin(); it != rb.end(); ++it) {
45         output << *it;
46         if (output.size() >= size * 2) {
47             QFAIL("Iterated way too many times");
48             break;
49         }
50     }
51 
52     // Correct amount of data received?
53     QCOMPARE(output.size(), expectedData.size());
54 
55     // Correct data?
56     QCOMPARE(expectedData, output);
57 
58     // Did it overwrite a correct number of items?
59     QCOMPARE(static_cast<uint>(sourceData.size() - expectedData.size()), rb.skippedCount());
60 
61     // Try to nuke them
62     rb.clear();
63     // Is it really empty?
64     // Yes, QVERIFY instead of QCOMPARE -- they can't be printed
65     QVERIFY(rb.begin() == rb.end());
66 }
67 
testOne_data()68 void RingBufferTest::testOne_data()
69 {
70     QTest::addColumn<int>("size");
71     QTest::addColumn<QVector<int> >("sourceData");
72     QTest::addColumn<QVector<int> >("expectedData");
73 
74     QVector<int> data;
75     QTest::newRow("empty") << 5 << data << data;
76 
77     data.clear();
78     data << 333;
79     QTest::newRow("one-value") << 5 << data << data;
80 
81     data.clear();
82     data << 333 << 666;
83     QTest::newRow("two-values") << 5 << data << data;
84 
85     data.clear();
86     data << 333 << 666 << 7;
87     QTest::newRow("three-values") << 5 << data << data;
88 
89     data.clear();
90     data << 333 << 666 << 7 << 15;
91     QTest::newRow("four-values") << 5 << data << data;
92 
93     data.clear();
94     data << 333 << 666 << 7 << 15 << 9;
95     QTest::newRow("five-values") << 5 << data << data;
96 
97     data.clear();
98     data << 333 << 666 << 7 << 15 << 9 << 13;
99     QVector<int> expected;
100     expected << 666 << 7 << 15 << 9 << 13;
101     QTest::newRow("six-wrapped") << 5 << data << expected;
102 
103     data.clear();
104     data << 333 << 666 << 7 << 15 << 9 << 13 << 0;
105     expected.clear();
106     expected << 7 << 15 << 9 << 13 << 0;
107     QTest::newRow("seven-wrapped") << 5 << data << expected;
108 
109     data.clear();
110     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2;
111     expected.clear();
112     expected << 15 << 9 << 13 << 0 << 2;
113     QTest::newRow("eight-wrapped") << 5 << data << expected;
114 
115     data.clear();
116     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2 << 1;
117     expected.clear();
118     expected << 9 << 13 << 0 << 2 << 1;
119     QTest::newRow("nine-wrapped") << 5 << data << expected;
120 
121     data.clear();
122     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2 << 1 << 800500;
123     expected.clear();
124     expected << 13 << 0 << 2 << 1 << 800500;
125     QTest::newRow("ten-wrapped") << 5 << data << expected;
126 
127     data.clear();
128     data << 333 << 666 << 7 << 15 << 9 << 13 << 0 << 2 << 1 << 800500 << 11;
129     expected.clear();
130     expected << 0 << 2 << 1 << 800500 << 11;
131     QTest::newRow("eleven-wrapped") << 5 << data << expected;
132 }
133 
134 QTEST_GUILESS_MAIN( RingBufferTest )
135