1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2012 Hewlett-Packard Development Company, L.P. |
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 <QTest> |
30 | #include <private/qbytedata_p.h> |
31 | // for QIODEVICE_BUFFERSIZE macro (== 16384): |
32 | #include <private/qiodevice_p.h> |
33 | |
34 | class tst_QByteDataBuffer : public QObject |
35 | { |
36 | Q_OBJECT |
37 | private Q_SLOTS: |
38 | void canReadLine(); |
39 | void positionHandling(); |
40 | void appendBuffer(); |
41 | void readCompleteBuffer_data(); |
42 | void readCompleteBuffer(); |
43 | void readPartialBuffer_data(); |
44 | void readPartialBuffer(); |
45 | private: |
46 | void readBuffer(int size, int readSize); |
47 | }; |
48 | |
49 | void tst_QByteDataBuffer::canReadLine() |
50 | { |
51 | QByteDataBuffer buf; |
52 | buf.append(bd: QByteArray("a" )); |
53 | buf.append(bd: QByteArray("\nb" )); |
54 | QVERIFY(buf.canReadLine()); |
55 | QVERIFY(buf.getChar() == 'a'); |
56 | QVERIFY(buf.canReadLine()); |
57 | QVERIFY(buf.getChar() == '\n'); |
58 | QVERIFY(!buf.canReadLine()); |
59 | } |
60 | |
61 | void tst_QByteDataBuffer::positionHandling() |
62 | { |
63 | QByteDataBuffer buf; |
64 | buf.append(bd: QByteArray("abc" )); |
65 | buf.append(bd: QByteArray("def" )); |
66 | |
67 | QCOMPARE(buf.byteAmount(), (qlonglong)6); |
68 | QCOMPARE(buf.sizeNextBlock(), (qlonglong)3); |
69 | |
70 | QCOMPARE(buf.getChar(), 'a'); |
71 | QCOMPARE(buf.byteAmount(), (qlonglong)5); |
72 | QCOMPARE(buf.sizeNextBlock(), (qlonglong)2); |
73 | |
74 | QVERIFY(!strcmp(buf[0].constData(), "bc" )); |
75 | QCOMPARE(buf.getChar(), 'b'); |
76 | QCOMPARE(buf.byteAmount(), (qlonglong)4); |
77 | QCOMPARE(buf.sizeNextBlock(), (qlonglong)1); |
78 | |
79 | QByteArray tmp("ab" ); |
80 | buf.prepend(bd: tmp); |
81 | QCOMPARE(buf.byteAmount(), (qlonglong)6); |
82 | QVERIFY(!strcmp(buf.readAll().constData(), "abcdef" )); |
83 | QCOMPARE(buf.byteAmount(), (qlonglong)0); |
84 | |
85 | QByteDataBuffer buf2; |
86 | buf2.append(bd: QByteArray("abc" )); |
87 | buf2.getChar(); |
88 | QCOMPARE(buf2.read(), QByteArray("bc" )); |
89 | } |
90 | |
91 | void tst_QByteDataBuffer::appendBuffer() |
92 | { |
93 | QByteDataBuffer buf; |
94 | buf.append(bd: QByteArray("\1\2\3" )); |
95 | buf.getChar(); |
96 | |
97 | QByteDataBuffer tmp; |
98 | tmp.append(other: buf); |
99 | QCOMPARE(tmp.readAll(), buf.readAll()); |
100 | } |
101 | |
102 | static QByteArray makeByteArray(int size) |
103 | { |
104 | QByteArray array; |
105 | array.resize(size); |
106 | char *data = array.data(); |
107 | for (int i = 0; i < size; ++i) |
108 | data[i] = i % 256; |
109 | return array; |
110 | } |
111 | |
112 | |
113 | void tst_QByteDataBuffer::readBuffer(int size, int readSize) |
114 | { |
115 | QByteArray data = makeByteArray(size); |
116 | |
117 | QByteDataBuffer buf; |
118 | buf.append(bd: data); |
119 | |
120 | QByteArray tmp; |
121 | tmp.resize(size); |
122 | |
123 | QBENCHMARK_ONCE { |
124 | for (int i = 0; i < (size - 1) / readSize + 1; ++i) |
125 | buf.read(dst: tmp.data() + i * readSize, amount: readSize); |
126 | } |
127 | |
128 | QCOMPARE(data.size(), tmp.size()); |
129 | QCOMPARE(data, tmp); |
130 | } |
131 | |
132 | void tst_QByteDataBuffer::readCompleteBuffer_data() |
133 | { |
134 | QTest::addColumn<int>(name: "size" ); |
135 | QTest::newRow(dataTag: "10B" ) << (int)10; |
136 | QTest::newRow(dataTag: "1MB" ) << (int)1e6; |
137 | QTest::newRow(dataTag: "5MB" ) << (int)5e6; |
138 | QTest::newRow(dataTag: "10MB" ) << (int)10e6; |
139 | } |
140 | |
141 | void tst_QByteDataBuffer::readCompleteBuffer() |
142 | { |
143 | QFETCH(int, size); |
144 | readBuffer(size, readSize: size); |
145 | } |
146 | |
147 | void tst_QByteDataBuffer::readPartialBuffer_data() |
148 | { |
149 | readCompleteBuffer_data(); |
150 | } |
151 | |
152 | void tst_QByteDataBuffer::readPartialBuffer() |
153 | { |
154 | QFETCH(int, size); |
155 | // QIODevice::readAll() reads in QIODEVICE_BUFFERSIZE size |
156 | // increments. |
157 | readBuffer(size, QIODEVICE_BUFFERSIZE); |
158 | } |
159 | |
160 | QTEST_MAIN(tst_QByteDataBuffer) |
161 | #include "tst_qbytedatabuffer.moc" |
162 | |