0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtCore/QtCore>
|
|
44 |
#include <QtTest/QtTest>
|
|
45 |
#include <Qt3Support/Q3TextStream>
|
|
46 |
|
|
47 |
Q_DECLARE_METATYPE(Q3CString)
|
|
48 |
|
|
49 |
//TESTED_CLASS=
|
|
50 |
//TESTED_FILES=
|
|
51 |
|
|
52 |
class tst_Q3TextStream : public QObject
|
|
53 |
{
|
|
54 |
Q_OBJECT
|
|
55 |
|
|
56 |
public:
|
|
57 |
tst_Q3TextStream();
|
|
58 |
virtual ~tst_Q3TextStream();
|
|
59 |
|
|
60 |
public slots:
|
|
61 |
void init();
|
|
62 |
void cleanup();
|
|
63 |
|
|
64 |
private slots:
|
|
65 |
void operator_shiftleft_data();
|
|
66 |
void operator_shiftleft();
|
|
67 |
void operator_shiftright_data();
|
|
68 |
void operator_shiftright();
|
|
69 |
|
|
70 |
void operator_shift_QChar_data();
|
|
71 |
void operator_shift_QChar();
|
|
72 |
void operator_shift_char_data();
|
|
73 |
void operator_shift_char();
|
|
74 |
void operator_shift_short_data();
|
|
75 |
void operator_shift_short();
|
|
76 |
void operator_shift_ushort_data();
|
|
77 |
void operator_shift_ushort();
|
|
78 |
void operator_shift_int_data();
|
|
79 |
void operator_shift_int();
|
|
80 |
void operator_shift_uint_data();
|
|
81 |
void operator_shift_uint();
|
|
82 |
void operator_shift_long_data();
|
|
83 |
void operator_shift_long();
|
|
84 |
void operator_shift_ulong_data();
|
|
85 |
void operator_shift_ulong();
|
|
86 |
void operator_shift_float_data();
|
|
87 |
void operator_shift_float();
|
|
88 |
void operator_shift_double_data();
|
|
89 |
void operator_shift_double();
|
|
90 |
void operator_shift_QString_data();
|
|
91 |
void operator_shift_QString();
|
|
92 |
void operator_shift_Q3CString_data();
|
|
93 |
void operator_shift_Q3CString();
|
|
94 |
|
|
95 |
void QTextCodecCodecForIndex() const;
|
|
96 |
|
|
97 |
void atEnd();
|
|
98 |
void eof();
|
|
99 |
void precision();
|
|
100 |
|
|
101 |
void task28319();
|
|
102 |
|
|
103 |
private:
|
|
104 |
void createWriteStream( Q3TextStream *&os );
|
|
105 |
void closeWriteStream( Q3TextStream *os );
|
|
106 |
void createReadStream( Q3TextStream *&is );
|
|
107 |
void closeReadStream( Q3TextStream *is );
|
|
108 |
|
|
109 |
void read_QChar( Q3TextStream *s );
|
|
110 |
void write_QChar( Q3TextStream *s );
|
|
111 |
void read_char( Q3TextStream *s );
|
|
112 |
void write_char( Q3TextStream *s );
|
|
113 |
void read_short( Q3TextStream *s );
|
|
114 |
void write_short( Q3TextStream *s );
|
|
115 |
void read_ushort( Q3TextStream *s );
|
|
116 |
void write_ushort( Q3TextStream *s );
|
|
117 |
void read_int( Q3TextStream *s );
|
|
118 |
void write_int( Q3TextStream *s );
|
|
119 |
void read_uint( Q3TextStream *s );
|
|
120 |
void write_uint( Q3TextStream *s );
|
|
121 |
void read_long( Q3TextStream *s );
|
|
122 |
void write_long( Q3TextStream *s );
|
|
123 |
void read_ulong( Q3TextStream *s );
|
|
124 |
void write_ulong( Q3TextStream *s );
|
|
125 |
void read_float( Q3TextStream *s );
|
|
126 |
void write_float( Q3TextStream *s );
|
|
127 |
void read_double( Q3TextStream *s );
|
|
128 |
void write_double( Q3TextStream *s );
|
|
129 |
void read_QString( Q3TextStream *s );
|
|
130 |
void write_QString( Q3TextStream *s );
|
|
131 |
void read_Q3CString( Q3TextStream *s );
|
|
132 |
void write_Q3CString( Q3TextStream *s );
|
|
133 |
|
|
134 |
void operatorShift_data();
|
|
135 |
|
|
136 |
void do_shiftleft( Q3TextStream *ts );
|
|
137 |
Q3TextStream::Encoding toEncoding( const QString& );
|
|
138 |
QString decodeString( QByteArray array, const QString& encoding );
|
|
139 |
|
|
140 |
Q3TextStream *os;
|
|
141 |
Q3TextStream *is;
|
|
142 |
Q3TextStream *ts;
|
|
143 |
QFile *inFile;
|
|
144 |
QFile *outFile;
|
|
145 |
QByteArray *inArray;
|
|
146 |
QBuffer *inBuffer;
|
|
147 |
QString *inString;
|
|
148 |
bool file_is_empty;
|
|
149 |
};
|
|
150 |
|
|
151 |
tst_Q3TextStream::tst_Q3TextStream()
|
|
152 |
{
|
|
153 |
ts = 0;
|
|
154 |
os = 0;
|
|
155 |
is = 0;
|
|
156 |
outFile = 0;
|
|
157 |
inFile = 0;
|
|
158 |
inArray = 0;
|
|
159 |
inBuffer = 0;
|
|
160 |
inString = 0;
|
|
161 |
file_is_empty = FALSE;
|
|
162 |
}
|
|
163 |
|
|
164 |
tst_Q3TextStream::~tst_Q3TextStream()
|
|
165 |
{
|
|
166 |
}
|
|
167 |
|
|
168 |
void tst_Q3TextStream::init()
|
|
169 |
{
|
|
170 |
ts = 0;
|
|
171 |
os = 0;
|
|
172 |
is = 0;
|
|
173 |
inFile = 0;
|
|
174 |
outFile = 0;
|
|
175 |
inArray = 0;
|
|
176 |
inBuffer = 0;
|
|
177 |
inString = 0;
|
|
178 |
file_is_empty = FALSE;
|
|
179 |
}
|
|
180 |
|
|
181 |
void tst_Q3TextStream::cleanup()
|
|
182 |
{
|
|
183 |
delete ts;
|
|
184 |
ts = 0;
|
|
185 |
delete os;
|
|
186 |
os = 0;
|
|
187 |
delete is;
|
|
188 |
is = 0;
|
|
189 |
delete inFile;
|
|
190 |
inFile = 0;
|
|
191 |
delete outFile;
|
|
192 |
outFile = 0;
|
|
193 |
delete inArray;
|
|
194 |
inArray = 0;
|
|
195 |
delete inBuffer;
|
|
196 |
inBuffer = 0;
|
|
197 |
delete inString;
|
|
198 |
inString = 0;
|
|
199 |
}
|
|
200 |
|
|
201 |
void tst_Q3TextStream::operator_shiftright_data()
|
|
202 |
{
|
|
203 |
operatorShift_data();
|
|
204 |
}
|
|
205 |
|
|
206 |
void tst_Q3TextStream::operator_shiftleft_data()
|
|
207 |
{
|
|
208 |
operatorShift_data();
|
|
209 |
}
|
|
210 |
|
|
211 |
static const double doubleDummy = 567.89;
|
|
212 |
static const int intDummy = 1234;
|
|
213 |
static const QString stringDummy = "ABCD";
|
|
214 |
|
|
215 |
static const char * const devices[] = {
|
|
216 |
"file",
|
|
217 |
"bytearray",
|
|
218 |
"buffer",
|
|
219 |
"string",
|
|
220 |
0
|
|
221 |
};
|
|
222 |
|
|
223 |
static const char * const encodings[] = {
|
|
224 |
"Locale",
|
|
225 |
"Latin1",
|
|
226 |
"Unicode",
|
|
227 |
"UnicodeNetworkOrder",
|
|
228 |
"UnicodeReverse",
|
|
229 |
"RawUnicode",
|
|
230 |
"UnicodeUTF8",
|
|
231 |
0
|
|
232 |
};
|
|
233 |
|
|
234 |
void tst_Q3TextStream::operatorShift_data()
|
|
235 |
{
|
|
236 |
QTest::addColumn<QString>( "device" );
|
|
237 |
QTest::addColumn<QString>( "encoding" );
|
|
238 |
QTest::addColumn<QString>( "type" );
|
|
239 |
QTest::addColumn<double>( "doubleVal" );
|
|
240 |
QTest::addColumn<int>( "intVal" );
|
|
241 |
QTest::addColumn<QString>( "stringVal" );
|
|
242 |
QTest::addColumn<QByteArray>( "encoded" );
|
|
243 |
|
|
244 |
for ( int i=0; devices[i] != 0; i++ ) {
|
|
245 |
QString dev = devices[i];
|
|
246 |
|
|
247 |
/*
|
|
248 |
We first test each type at least once.
|
|
249 |
*/
|
|
250 |
QTest::newRow( dev + "0" ) << dev << QString("UnicodeUTF8") << QString("QChar")
|
|
251 |
<< doubleDummy << (int) 'Z' << stringDummy
|
|
252 |
<< QByteArray( Q3CString("Z") );
|
|
253 |
QTest::newRow( dev + "1" ) << dev << QString("UnicodeUTF8") << QString("char")
|
|
254 |
<< doubleDummy << (int) 'Z' << stringDummy
|
|
255 |
<< QByteArray( Q3CString("Z") );
|
|
256 |
QTest::newRow( dev + "2" ) << dev << QString("UnicodeUTF8") << QString("signed short")
|
|
257 |
<< doubleDummy << 12345 << stringDummy
|
|
258 |
<< QByteArray( Q3CString("12345") );
|
|
259 |
QTest::newRow( dev + "3" ) << dev << QString("UnicodeUTF8") << QString("unsigned short")
|
|
260 |
<< doubleDummy << 12345 << stringDummy
|
|
261 |
<< QByteArray( Q3CString("12345") );
|
|
262 |
QTest::newRow( dev + "4" ) << dev << QString("UnicodeUTF8") << QString("signed int")
|
|
263 |
<< doubleDummy << 12345 << stringDummy
|
|
264 |
<< QByteArray( Q3CString("12345") );
|
|
265 |
QTest::newRow( dev + "5" ) << dev << QString("UnicodeUTF8") << QString("unsigned int")
|
|
266 |
<< doubleDummy << 12345 << stringDummy
|
|
267 |
<< QByteArray( Q3CString("12345") );
|
|
268 |
QTest::newRow( dev + "6" ) << dev << QString("UnicodeUTF8") << QString("signed long")
|
|
269 |
<< doubleDummy << 12345 << stringDummy
|
|
270 |
<< QByteArray( Q3CString("12345") );
|
|
271 |
QTest::newRow( dev + "7" ) << dev << QString("UnicodeUTF8") << QString("unsigned long")
|
|
272 |
<< doubleDummy << 12345 << stringDummy
|
|
273 |
<< QByteArray( Q3CString("12345") );
|
|
274 |
QTest::newRow( dev + "8" ) << dev << QString("UnicodeUTF8") << QString("float")
|
|
275 |
<< (double)3.1415f << intDummy << stringDummy
|
|
276 |
<< QByteArray( Q3CString("3.1415") );
|
|
277 |
QTest::newRow( dev + "9" ) << dev << QString("UnicodeUTF8") << QString("double")
|
|
278 |
<< 3.1415 << intDummy << stringDummy
|
|
279 |
<< QByteArray( Q3CString("3.1415") );
|
|
280 |
QTest::newRow( dev + "10" ) << dev << QString("UnicodeUTF8") << QString("char *")
|
|
281 |
<< doubleDummy << intDummy << QString("I-am-a-string")
|
|
282 |
<< QByteArray( Q3CString("I-am-a-string") );
|
|
283 |
QTest::newRow( dev + "11" ) << dev << QString("UnicodeUTF8") << QString("QString")
|
|
284 |
<< doubleDummy << intDummy << QString("I-am-a-string")
|
|
285 |
<< QByteArray( Q3CString("I-am-a-string") );
|
|
286 |
QTest::newRow( dev + "12" ) << dev << QString("UnicodeUTF8") << QString("Q3CString")
|
|
287 |
<< doubleDummy << intDummy << QString("I-am-a-string")
|
|
288 |
<< QByteArray( Q3CString("I-am-a-string") );
|
|
289 |
|
|
290 |
/*
|
|
291 |
Then we test some special cases that have caused problems in the past.
|
|
292 |
*/
|
|
293 |
QTest::newRow( dev + "20" ) << dev << QString("UnicodeUTF8") << QString("QChar")
|
|
294 |
<< doubleDummy << 0xff8c << stringDummy
|
|
295 |
<< QByteArray( Q3CString("\xef\xbe\x8c") );
|
|
296 |
QTest::newRow( dev + "21" ) << dev << QString("UnicodeUTF8") << QString("QChar")
|
|
297 |
<< doubleDummy << 0x8cff << stringDummy
|
|
298 |
<< QByteArray( Q3CString("\xe8\xb3\xbf") );
|
|
299 |
}
|
|
300 |
}
|
|
301 |
|
|
302 |
void tst_Q3TextStream::do_shiftleft( Q3TextStream *ts )
|
|
303 |
{
|
|
304 |
QFETCH( QString, encoding );
|
|
305 |
QFETCH( QString, type );
|
|
306 |
QFETCH( double, doubleVal );
|
|
307 |
QFETCH( int, intVal );
|
|
308 |
QFETCH( QString, stringVal );
|
|
309 |
|
|
310 |
ts->setEncoding( toEncoding(encoding) );
|
|
311 |
|
|
312 |
if ( type == "QChar" ) {
|
|
313 |
if ( intVal >= 0 && intVal <= 0xffff )
|
|
314 |
*ts << QChar( intVal );
|
|
315 |
} else if ( type == "char" ) {
|
|
316 |
*ts << (char) intVal;
|
|
317 |
} else if ( type == "signed short" ) {
|
|
318 |
*ts << (signed short) intVal;
|
|
319 |
} else if ( type == "unsigned short" ) {
|
|
320 |
*ts << (unsigned short) intVal;
|
|
321 |
} else if ( type == "signed int" ) {
|
|
322 |
*ts << (signed int) intVal;
|
|
323 |
} else if ( type == "unsigned int" ) {
|
|
324 |
*ts << (unsigned int) intVal;
|
|
325 |
} else if ( type == "signed long" ) {
|
|
326 |
*ts << (signed long) intVal;
|
|
327 |
} else if ( type == "unsigned long" ) {
|
|
328 |
*ts << (unsigned long) intVal;
|
|
329 |
} else if ( type == "float" ) {
|
|
330 |
*ts << (float) doubleVal;
|
|
331 |
} else if ( type == "double" ) {
|
|
332 |
*ts << (double) doubleVal;
|
|
333 |
} else if ( type == "char *" ) {
|
|
334 |
*ts << stringVal.latin1();
|
|
335 |
} else if ( type == "QString" ) {
|
|
336 |
*ts << stringVal;
|
|
337 |
} else if ( type == "Q3CString" ) {
|
|
338 |
*ts << Q3CString( stringVal.latin1() );
|
|
339 |
} else {
|
|
340 |
qWarning( "Unknown type: %s" + type );
|
|
341 |
}
|
|
342 |
}
|
|
343 |
|
|
344 |
void tst_Q3TextStream::operator_shiftleft()
|
|
345 |
{
|
|
346 |
QFETCH( QString, device );
|
|
347 |
QFETCH( QString, encoding );
|
|
348 |
QFETCH( QByteArray, encoded );
|
|
349 |
|
|
350 |
if ( device == "file" ) {
|
|
351 |
QFile outFile( "qtextstream.out" );
|
|
352 |
QVERIFY( outFile.open( IO_WriteOnly ) );
|
|
353 |
Q3TextStream ts( &outFile );
|
|
354 |
|
|
355 |
do_shiftleft( &ts );
|
|
356 |
outFile.close();
|
|
357 |
|
|
358 |
QFile inFile( "qtextstream.out" );
|
|
359 |
QVERIFY( inFile.open( IO_ReadOnly ) );
|
|
360 |
QCOMPARE( inFile.readAll(), encoded );
|
|
361 |
} else if ( device == "bytearray" ) {
|
|
362 |
QByteArray outArray;
|
|
363 |
Q3TextStream ts( outArray, IO_WriteOnly );
|
|
364 |
|
|
365 |
do_shiftleft( &ts );
|
|
366 |
|
|
367 |
QCOMPARE( outArray, encoded );
|
|
368 |
} else if ( device == "buffer" ) {
|
|
369 |
QByteArray outArray;
|
|
370 |
QBuffer outBuffer;
|
|
371 |
outBuffer.setBuffer( &outArray );
|
|
372 |
QVERIFY( outBuffer.open(IO_WriteOnly) );
|
|
373 |
Q3TextStream ts( &outBuffer );
|
|
374 |
|
|
375 |
do_shiftleft( &ts );
|
|
376 |
|
|
377 |
QCOMPARE( outArray, encoded );
|
|
378 |
} else if ( device == "string" ) {
|
|
379 |
QString outString;
|
|
380 |
Q3TextStream ts( &outString, IO_WriteOnly );
|
|
381 |
|
|
382 |
do_shiftleft( &ts );
|
|
383 |
|
|
384 |
QString decodedString = decodeString( encoded, encoding );
|
|
385 |
QCOMPARE( outString, decodedString );
|
|
386 |
} else {
|
|
387 |
qWarning( "Unknown device type: " + device );
|
|
388 |
}
|
|
389 |
}
|
|
390 |
|
|
391 |
void tst_Q3TextStream::operator_shiftright()
|
|
392 |
{
|
|
393 |
QFETCH( QString, device );
|
|
394 |
QFETCH( QString, encoding );
|
|
395 |
QFETCH( QString, type );
|
|
396 |
QFETCH( double, doubleVal );
|
|
397 |
QFETCH( int, intVal );
|
|
398 |
QFETCH( QString, stringVal );
|
|
399 |
QFETCH( QByteArray, encoded );
|
|
400 |
|
|
401 |
if ( device == "file" ) {
|
|
402 |
QFile outFile( "qtextstream.out" );
|
|
403 |
QVERIFY( outFile.open( IO_WriteOnly ) );
|
|
404 |
QCOMPARE( (int) outFile.writeBlock(encoded), encoded.size() );
|
|
405 |
outFile.close();
|
|
406 |
|
|
407 |
inFile = new QFile( "qtextstream.out" );
|
|
408 |
QVERIFY( inFile->open( IO_ReadOnly ) );
|
|
409 |
ts = new Q3TextStream( inFile );
|
|
410 |
} else if ( device == "bytearray" ) {
|
|
411 |
ts = new Q3TextStream( encoded, IO_ReadOnly );
|
|
412 |
} else if ( device == "buffer" ) {
|
|
413 |
inBuffer = new QBuffer;
|
|
414 |
inBuffer->setData( encoded );
|
|
415 |
QVERIFY( inBuffer->open(IO_ReadOnly) );
|
|
416 |
ts = new Q3TextStream( inBuffer );
|
|
417 |
} else if ( device == "string" ) {
|
|
418 |
inString = new QString( decodeString(encoded, encoding) );
|
|
419 |
ts = new Q3TextStream( inString, IO_ReadOnly );
|
|
420 |
} else {
|
|
421 |
qWarning( "Unknown device type: " + device );
|
|
422 |
}
|
|
423 |
|
|
424 |
ts->setEncoding( toEncoding(encoding) );
|
|
425 |
|
|
426 |
if ( type == "QChar" ) {
|
|
427 |
QChar c;
|
|
428 |
if ( intVal >= 0 && intVal <= 0xffff )
|
|
429 |
*ts >> c;
|
|
430 |
QCOMPARE( c, QChar(intVal) );
|
|
431 |
} else if ( type == "char" ) {
|
|
432 |
char c;
|
|
433 |
*ts >> c;
|
|
434 |
QVERIFY( c == (char) intVal );
|
|
435 |
} else if ( type == "signed short" ) {
|
|
436 |
signed short h;
|
|
437 |
*ts >> h;
|
|
438 |
QVERIFY( h == (signed short) intVal );
|
|
439 |
} else if ( type == "unsigned short" ) {
|
|
440 |
unsigned short h;
|
|
441 |
*ts >> h;
|
|
442 |
QVERIFY( h == (unsigned short) intVal );
|
|
443 |
} else if ( type == "signed int" ) {
|
|
444 |
signed int i;
|
|
445 |
*ts >> i;
|
|
446 |
QVERIFY( i == (signed int) intVal );
|
|
447 |
} else if ( type == "unsigned int" ) {
|
|
448 |
unsigned int i;
|
|
449 |
*ts >> i;
|
|
450 |
QVERIFY( i == (unsigned int) intVal );
|
|
451 |
} else if ( type == "signed long" ) {
|
|
452 |
signed long ell;
|
|
453 |
*ts >> ell;
|
|
454 |
QVERIFY( ell == (signed long) intVal );
|
|
455 |
} else if ( type == "unsigned long" ) {
|
|
456 |
unsigned long ell;
|
|
457 |
*ts >> ell;
|
|
458 |
QVERIFY( ell == (unsigned long) intVal );
|
|
459 |
} else if ( type == "float" ) {
|
|
460 |
float f;
|
|
461 |
*ts >> f;
|
|
462 |
QVERIFY( f == (float) doubleVal );
|
|
463 |
} else if ( type == "double" ) {
|
|
464 |
double d;
|
|
465 |
*ts >> d;
|
|
466 |
QVERIFY( d == (double) doubleVal );
|
|
467 |
} else if ( type == "char *" ) {
|
|
468 |
char *cp = new char[2048];
|
|
469 |
*ts >> cp;
|
|
470 |
QVERIFY( qstrcmp(cp, stringVal.latin1()) == 0 );
|
|
471 |
delete[] cp;
|
|
472 |
} else if ( type == "QString" ) {
|
|
473 |
QString s;
|
|
474 |
*ts >> s;
|
|
475 |
QCOMPARE( s, stringVal );
|
|
476 |
} else if ( type == "Q3CString" ) {
|
|
477 |
Q3CString s;
|
|
478 |
*ts >> s;
|
|
479 |
QCOMPARE( QString::fromLatin1(s), stringVal );
|
|
480 |
} else {
|
|
481 |
qWarning( "Unknown type: %s" + type );
|
|
482 |
}
|
|
483 |
}
|
|
484 |
|
|
485 |
Q3TextStream::Encoding tst_Q3TextStream::toEncoding( const QString &str )
|
|
486 |
{
|
|
487 |
if ( str == "Locale" )
|
|
488 |
return Q3TextStream::Locale;
|
|
489 |
else if ( str == "Latin1" )
|
|
490 |
return Q3TextStream::Latin1;
|
|
491 |
else if ( str == "Unicode" )
|
|
492 |
return Q3TextStream::Unicode;
|
|
493 |
else if ( str == "UnicodeNetworkOrder" )
|
|
494 |
return Q3TextStream::UnicodeNetworkOrder;
|
|
495 |
else if ( str == "UnicodeReverse" )
|
|
496 |
return Q3TextStream::UnicodeReverse;
|
|
497 |
else if ( str == "RawUnicode" )
|
|
498 |
return Q3TextStream::RawUnicode;
|
|
499 |
else if ( str == "UnicodeUTF8" )
|
|
500 |
return Q3TextStream::UnicodeUTF8;
|
|
501 |
|
|
502 |
qWarning( "No such encoding " + str );
|
|
503 |
return Q3TextStream::Latin1;
|
|
504 |
}
|
|
505 |
|
|
506 |
QString tst_Q3TextStream::decodeString( QByteArray array, const QString& encoding )
|
|
507 |
{
|
|
508 |
switch ( toEncoding(encoding) ) {
|
|
509 |
case Q3TextStream::Locale:
|
|
510 |
return QString::fromLocal8Bit( array.data(), array.size() );
|
|
511 |
case Q3TextStream::Latin1:
|
|
512 |
return QString::fromLatin1( array.data(), array.size() );
|
|
513 |
case Q3TextStream::Unicode:
|
|
514 |
case Q3TextStream::UnicodeNetworkOrder:
|
|
515 |
case Q3TextStream::UnicodeReverse:
|
|
516 |
case Q3TextStream::RawUnicode:
|
|
517 |
qWarning( "Unicode not implemented ###" );
|
|
518 |
return QString();
|
|
519 |
case Q3TextStream::UnicodeUTF8:
|
|
520 |
return QString::fromUtf8( array.data(), array.size() );
|
|
521 |
default:
|
|
522 |
return QString();
|
|
523 |
}
|
|
524 |
}
|
|
525 |
|
|
526 |
// ************************************************
|
|
527 |
|
|
528 |
void tst_Q3TextStream::createWriteStream( Q3TextStream *&os )
|
|
529 |
{
|
|
530 |
QFETCH( QString, device );
|
|
531 |
|
|
532 |
if ( device == "file" ) {
|
|
533 |
outFile = new QFile( "qtextstream.out" );
|
|
534 |
QVERIFY( outFile->open( IO_WriteOnly ) );
|
|
535 |
os = new Q3TextStream( outFile );
|
|
536 |
} else if ( device == "bytearray" ) {
|
|
537 |
inArray = new QByteArray;
|
|
538 |
os = new Q3TextStream( *inArray, IO_WriteOnly );
|
|
539 |
} else if ( device == "buffer" ) {
|
|
540 |
inBuffer = new QBuffer;
|
|
541 |
QVERIFY( inBuffer->open(IO_WriteOnly) );
|
|
542 |
os = new Q3TextStream( inBuffer );
|
|
543 |
} else if ( device == "string" ) {
|
|
544 |
inString = new QString;
|
|
545 |
os = new Q3TextStream( inString, IO_WriteOnly );
|
|
546 |
} else {
|
|
547 |
qWarning( "Error creating write stream: Unknown device type '" + device + "'" );
|
|
548 |
}
|
|
549 |
|
|
550 |
QFETCH( QString, encoding );
|
|
551 |
os->setEncoding( toEncoding( encoding ));
|
|
552 |
}
|
|
553 |
|
|
554 |
void tst_Q3TextStream::closeWriteStream( Q3TextStream *os )
|
|
555 |
{
|
|
556 |
QFETCH( QString, device );
|
|
557 |
|
|
558 |
if ( os->device() )
|
|
559 |
os->device()->close();
|
|
560 |
}
|
|
561 |
|
|
562 |
void tst_Q3TextStream::createReadStream( Q3TextStream *&is )
|
|
563 |
{
|
|
564 |
QFETCH( QString, device );
|
|
565 |
|
|
566 |
if ( device == "file" ) {
|
|
567 |
inFile = new QFile( "qtextstream.out" );
|
|
568 |
QVERIFY( inFile->open( IO_ReadOnly ) );
|
|
569 |
is = new Q3TextStream( inFile );
|
|
570 |
} else if ( device == "bytearray" ) {
|
|
571 |
is = new Q3TextStream( *inArray, IO_ReadOnly );
|
|
572 |
} else if ( device == "buffer" ) {
|
|
573 |
QVERIFY( inBuffer->open(IO_ReadOnly) );
|
|
574 |
is = new Q3TextStream( inBuffer );
|
|
575 |
} else if ( device == "string" ) {
|
|
576 |
is = new Q3TextStream( inString, IO_ReadOnly );
|
|
577 |
} else {
|
|
578 |
qWarning( "Error creating read stream: Unknown device type '" + device + "'" );
|
|
579 |
}
|
|
580 |
|
|
581 |
QFETCH( QString, encoding );
|
|
582 |
is->setEncoding( toEncoding( encoding ));
|
|
583 |
|
|
584 |
if (!file_is_empty) {
|
|
585 |
QVERIFY( !is->atEnd() );
|
|
586 |
QVERIFY( !is->eof() );
|
|
587 |
}
|
|
588 |
}
|
|
589 |
|
|
590 |
void tst_Q3TextStream::closeReadStream( Q3TextStream *is )
|
|
591 |
{
|
|
592 |
QVERIFY( is->atEnd() );
|
|
593 |
QVERIFY( is->eof() );
|
|
594 |
|
|
595 |
if ( is->device() )
|
|
596 |
is->device()->close();
|
|
597 |
}
|
|
598 |
|
|
599 |
void tst_Q3TextStream::precision()
|
|
600 |
{
|
|
601 |
}
|
|
602 |
|
|
603 |
void tst_Q3TextStream::atEnd()
|
|
604 |
{
|
|
605 |
// atEnd is actually tested in all the operator_shift*functions
|
|
606 |
// but it's sufficient to mention one here.
|
|
607 |
}
|
|
608 |
|
|
609 |
void tst_Q3TextStream::eof()
|
|
610 |
{
|
|
611 |
// eof is obsolete, but as long as it's supported it should work.
|
|
612 |
// eof is actually tested in all the operator_shift*functions
|
|
613 |
// but it's sufficient to mention one here.
|
|
614 |
}
|
|
615 |
|
|
616 |
// **************** QChar ****************
|
|
617 |
|
|
618 |
void tst_Q3TextStream::operator_shift_QChar_data()
|
|
619 |
{
|
|
620 |
QTest::addColumn<QString>( "device" );
|
|
621 |
QTest::addColumn<QString>( "encoding" );
|
|
622 |
QTest::addColumn<ushort>( "qchar" );
|
|
623 |
|
|
624 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
625 |
QString device = devices[d];
|
|
626 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
627 |
QString encoding = encodings[e];
|
|
628 |
|
|
629 |
QString tag = device + "_" + encoding + "_";
|
|
630 |
QTest::newRow( tag + "0" ) << device << encoding << QChar( 'A' ).unicode();
|
|
631 |
QTest::newRow( tag + "1" ) << device << encoding << QChar( 'B' ).unicode();
|
|
632 |
QTest::newRow( tag + "2" ) << device << encoding << QChar( 'Z' ).unicode();
|
|
633 |
QTest::newRow( tag + "3" ) << device << encoding << QChar( 'z' ).unicode();
|
|
634 |
QTest::newRow( tag + "4" ) << device << encoding << QChar( '@' ).unicode();
|
|
635 |
}
|
|
636 |
}
|
|
637 |
}
|
|
638 |
|
|
639 |
void tst_Q3TextStream::operator_shift_QChar()
|
|
640 |
{
|
|
641 |
// Uncomment the qDebug and you'll see all the combinations we actually run :-))
|
|
642 |
// qDebug( data()->dataTag() );
|
|
643 |
|
|
644 |
createWriteStream( os );
|
|
645 |
write_QChar( os );
|
|
646 |
closeWriteStream( os );
|
|
647 |
|
|
648 |
createReadStream( is );
|
|
649 |
read_QChar( is );
|
|
650 |
closeReadStream( is );
|
|
651 |
}
|
|
652 |
|
|
653 |
void tst_Q3TextStream::read_QChar( Q3TextStream *s )
|
|
654 |
{
|
|
655 |
QFETCH( ushort, qchar );
|
|
656 |
QChar expected( qchar );
|
|
657 |
QChar actual;
|
|
658 |
*s >> actual;
|
|
659 |
QVERIFY( actual == expected );
|
|
660 |
}
|
|
661 |
|
|
662 |
void tst_Q3TextStream::write_QChar( Q3TextStream *s )
|
|
663 |
{
|
|
664 |
QFETCH( ushort, qchar );
|
|
665 |
QChar actual( qchar );
|
|
666 |
*s << actual;
|
|
667 |
}
|
|
668 |
|
|
669 |
// **************** char ****************
|
|
670 |
|
|
671 |
void tst_Q3TextStream::operator_shift_char_data()
|
|
672 |
{
|
|
673 |
QTest::addColumn<QString>( "device" );
|
|
674 |
QTest::addColumn<QString>( "encoding" );
|
|
675 |
QTest::addColumn<int>( "ch" );
|
|
676 |
|
|
677 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
678 |
QString device = devices[d];
|
|
679 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
680 |
QString encoding = encodings[e];
|
|
681 |
|
|
682 |
QString tag = device + "_" + encoding + "_";
|
|
683 |
QTest::newRow( tag + "0" ) << device << encoding << int('A');
|
|
684 |
QTest::newRow( tag + "1" ) << device << encoding << int('B');
|
|
685 |
QTest::newRow( tag + "2" ) << device << encoding << int('Z');
|
|
686 |
QTest::newRow( tag + "3" ) << device << encoding << int(14);
|
|
687 |
QTest::newRow( tag + "4" ) << device << encoding << int('0');
|
|
688 |
}
|
|
689 |
}
|
|
690 |
}
|
|
691 |
|
|
692 |
void tst_Q3TextStream::operator_shift_char()
|
|
693 |
{
|
|
694 |
createWriteStream( os );
|
|
695 |
write_char( os );
|
|
696 |
closeWriteStream( os );
|
|
697 |
|
|
698 |
createReadStream( is );
|
|
699 |
read_char( is );
|
|
700 |
closeReadStream( is );
|
|
701 |
}
|
|
702 |
|
|
703 |
void tst_Q3TextStream::read_char( Q3TextStream *s )
|
|
704 |
{
|
|
705 |
QFETCH( int, ch );
|
|
706 |
char c(ch);
|
|
707 |
char exp;
|
|
708 |
*s >> exp;
|
|
709 |
QCOMPARE( exp, c );
|
|
710 |
}
|
|
711 |
|
|
712 |
void tst_Q3TextStream::write_char( Q3TextStream *s )
|
|
713 |
{
|
|
714 |
QFETCH( int, ch );
|
|
715 |
char c(ch);
|
|
716 |
*s << c;
|
|
717 |
}
|
|
718 |
|
|
719 |
// **************** short ****************
|
|
720 |
|
|
721 |
void tst_Q3TextStream::operator_shift_short_data()
|
|
722 |
{
|
|
723 |
QTest::addColumn<QString>( "device" );
|
|
724 |
QTest::addColumn<QString>( "encoding" );
|
|
725 |
QTest::addColumn<short>( "ss" );
|
|
726 |
|
|
727 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
728 |
QString device = devices[d];
|
|
729 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
730 |
QString encoding = encodings[e];
|
|
731 |
|
|
732 |
QString tag = device + "_" + encoding + "_";
|
|
733 |
QTest::newRow( tag + "0" ) << device << encoding << short(0);
|
|
734 |
QTest::newRow( tag + "1" ) << device << encoding << short(-1);
|
|
735 |
QTest::newRow( tag + "2" ) << device << encoding << short(1);
|
|
736 |
QTest::newRow( tag + "3" ) << device << encoding << short(255);
|
|
737 |
QTest::newRow( tag + "4" ) << device << encoding << short(-254);
|
|
738 |
}
|
|
739 |
}
|
|
740 |
}
|
|
741 |
|
|
742 |
void tst_Q3TextStream::operator_shift_short()
|
|
743 |
{
|
|
744 |
createWriteStream( os );
|
|
745 |
write_short( os );
|
|
746 |
closeWriteStream( os );
|
|
747 |
|
|
748 |
createReadStream( is );
|
|
749 |
read_short( is );
|
|
750 |
closeReadStream( is );
|
|
751 |
}
|
|
752 |
|
|
753 |
void tst_Q3TextStream::read_short( Q3TextStream *s )
|
|
754 |
{
|
|
755 |
QFETCH( short, ss );
|
|
756 |
short exp;
|
|
757 |
QString A, B;
|
|
758 |
*s >> A >> exp >> B;
|
|
759 |
QCOMPARE( A, QString("A") );
|
|
760 |
QCOMPARE( B, QString("B") );
|
|
761 |
QCOMPARE( exp, ss );
|
|
762 |
s->skipWhiteSpace();
|
|
763 |
}
|
|
764 |
|
|
765 |
void tst_Q3TextStream::write_short( Q3TextStream *s )
|
|
766 |
{
|
|
767 |
QFETCH( short, ss );
|
|
768 |
*s << " A " << ss << " B ";
|
|
769 |
}
|
|
770 |
|
|
771 |
// **************** ushort ****************
|
|
772 |
|
|
773 |
void tst_Q3TextStream::operator_shift_ushort_data()
|
|
774 |
{
|
|
775 |
QTest::addColumn<QString>( "device" );
|
|
776 |
QTest::addColumn<QString>( "encoding" );
|
|
777 |
QTest::addColumn<ushort>( "us" );
|
|
778 |
|
|
779 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
780 |
QString device = devices[d];
|
|
781 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
782 |
QString encoding = encodings[e];
|
|
783 |
|
|
784 |
QString tag = device + "_" + encoding + "_";
|
|
785 |
QTest::newRow( tag + "0" ) << device << encoding << ushort(0);
|
|
786 |
QTest::newRow( tag + "1" ) << device << encoding << ushort(1);
|
|
787 |
QTest::newRow( tag + "2" ) << device << encoding << ushort(10);
|
|
788 |
QTest::newRow( tag + "3" ) << device << encoding << ushort(255);
|
|
789 |
QTest::newRow( tag + "4" ) << device << encoding << ushort(512);
|
|
790 |
}
|
|
791 |
}
|
|
792 |
}
|
|
793 |
|
|
794 |
void tst_Q3TextStream::operator_shift_ushort()
|
|
795 |
{
|
|
796 |
createWriteStream( os );
|
|
797 |
write_ushort( os );
|
|
798 |
closeWriteStream( os );
|
|
799 |
|
|
800 |
createReadStream( is );
|
|
801 |
read_ushort( is );
|
|
802 |
closeReadStream( is );
|
|
803 |
}
|
|
804 |
|
|
805 |
void tst_Q3TextStream::read_ushort( Q3TextStream *s )
|
|
806 |
{
|
|
807 |
QFETCH( ushort, us );
|
|
808 |
ushort exp;
|
|
809 |
QString A, B;
|
|
810 |
*s >> A >> exp >> B;
|
|
811 |
QCOMPARE( A, QString("A") );
|
|
812 |
QCOMPARE( B, QString("B") );
|
|
813 |
QCOMPARE( exp, us );
|
|
814 |
s->skipWhiteSpace();
|
|
815 |
}
|
|
816 |
|
|
817 |
void tst_Q3TextStream::write_ushort( Q3TextStream *s )
|
|
818 |
{
|
|
819 |
QFETCH( ushort, us );
|
|
820 |
*s << " A " << us << " B ";
|
|
821 |
}
|
|
822 |
|
|
823 |
// **************** int ****************
|
|
824 |
|
|
825 |
void tst_Q3TextStream::operator_shift_int_data()
|
|
826 |
{
|
|
827 |
QTest::addColumn<QString>( "device" );
|
|
828 |
QTest::addColumn<QString>( "encoding" );
|
|
829 |
QTest::addColumn<int>( "si" );
|
|
830 |
|
|
831 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
832 |
QString device = devices[d];
|
|
833 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
834 |
QString encoding = encodings[e];
|
|
835 |
|
|
836 |
QString tag = device + "_" + encoding + "_";
|
|
837 |
QTest::newRow( tag + "0" ) << device << encoding << int(0);
|
|
838 |
QTest::newRow( tag + "1" ) << device << encoding << int(1);
|
|
839 |
QTest::newRow( tag + "2" ) << device << encoding << int(10);
|
|
840 |
QTest::newRow( tag + "3" ) << device << encoding << int(255);
|
|
841 |
QTest::newRow( tag + "4" ) << device << encoding << int(512);
|
|
842 |
QTest::newRow( tag + "5" ) << device << encoding << int(-1);
|
|
843 |
QTest::newRow( tag + "6" ) << device << encoding << int(-10);
|
|
844 |
QTest::newRow( tag + "7" ) << device << encoding << int(-255);
|
|
845 |
QTest::newRow( tag + "8" ) << device << encoding << int(-512);
|
|
846 |
}
|
|
847 |
}
|
|
848 |
}
|
|
849 |
|
|
850 |
void tst_Q3TextStream::operator_shift_int()
|
|
851 |
{
|
|
852 |
createWriteStream( os );
|
|
853 |
write_int( os );
|
|
854 |
closeWriteStream( os );
|
|
855 |
|
|
856 |
createReadStream( is );
|
|
857 |
read_int( is );
|
|
858 |
closeReadStream( is );
|
|
859 |
}
|
|
860 |
|
|
861 |
void tst_Q3TextStream::read_int( Q3TextStream *s )
|
|
862 |
{
|
|
863 |
QFETCH( int, si );
|
|
864 |
int exp;
|
|
865 |
QString A, B;
|
|
866 |
*s >> A >> exp >> B;
|
|
867 |
QCOMPARE( A, QString("A") );
|
|
868 |
QCOMPARE( B, QString("B") );
|
|
869 |
QCOMPARE( exp, si );
|
|
870 |
s->skipWhiteSpace();
|
|
871 |
}
|
|
872 |
|
|
873 |
void tst_Q3TextStream::write_int( Q3TextStream *s )
|
|
874 |
{
|
|
875 |
QFETCH( int, si );
|
|
876 |
*s << " A " << si << " B ";
|
|
877 |
}
|
|
878 |
|
|
879 |
// **************** uint ****************
|
|
880 |
|
|
881 |
void tst_Q3TextStream::operator_shift_uint_data()
|
|
882 |
{
|
|
883 |
QTest::addColumn<QString>( "device" );
|
|
884 |
QTest::addColumn<QString>( "encoding" );
|
|
885 |
QTest::addColumn<uint>( "ui" );
|
|
886 |
|
|
887 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
888 |
QString device = devices[d];
|
|
889 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
890 |
QString encoding = encodings[e];
|
|
891 |
|
|
892 |
QString tag = device + "_" + encoding + "_";
|
|
893 |
QTest::newRow( tag + "0" ) << device << encoding << uint(0);
|
|
894 |
QTest::newRow( tag + "1" ) << device << encoding << uint(1);
|
|
895 |
QTest::newRow( tag + "2" ) << device << encoding << uint(10);
|
|
896 |
QTest::newRow( tag + "3" ) << device << encoding << uint(255);
|
|
897 |
QTest::newRow( tag + "4" ) << device << encoding << uint(512);
|
|
898 |
}
|
|
899 |
}
|
|
900 |
}
|
|
901 |
|
|
902 |
void tst_Q3TextStream::operator_shift_uint()
|
|
903 |
{
|
|
904 |
createWriteStream( os );
|
|
905 |
write_uint( os );
|
|
906 |
closeWriteStream( os );
|
|
907 |
|
|
908 |
createReadStream( is );
|
|
909 |
read_uint( is );
|
|
910 |
closeReadStream( is );
|
|
911 |
}
|
|
912 |
|
|
913 |
void tst_Q3TextStream::read_uint( Q3TextStream *s )
|
|
914 |
{
|
|
915 |
QFETCH( uint, ui );
|
|
916 |
uint exp;
|
|
917 |
QString A, B;
|
|
918 |
*s >> A >> exp >> B;
|
|
919 |
QCOMPARE( A, QString("A") );
|
|
920 |
QCOMPARE( B, QString("B") );
|
|
921 |
QCOMPARE( exp, ui );
|
|
922 |
s->skipWhiteSpace();
|
|
923 |
}
|
|
924 |
|
|
925 |
void tst_Q3TextStream::write_uint( Q3TextStream *s )
|
|
926 |
{
|
|
927 |
QFETCH( uint, ui );
|
|
928 |
*s << " A " << ui << " B ";
|
|
929 |
}
|
|
930 |
|
|
931 |
// **************** long ****************
|
|
932 |
|
|
933 |
void tst_Q3TextStream::operator_shift_long_data()
|
|
934 |
{
|
|
935 |
QTest::addColumn<QString>( "device" );
|
|
936 |
QTest::addColumn<QString>( "encoding" );
|
|
937 |
QTest::addColumn<long>( "sl" );
|
|
938 |
|
|
939 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
940 |
QString device = devices[d];
|
|
941 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
942 |
QString encoding = encodings[e];
|
|
943 |
|
|
944 |
QString tag = device + "_" + encoding + "_";
|
|
945 |
QTest::newRow( tag + "0" ) << device << encoding << long(0);
|
|
946 |
QTest::newRow( tag + "1" ) << device << encoding << long(1);
|
|
947 |
QTest::newRow( tag + "2" ) << device << encoding << long(10);
|
|
948 |
QTest::newRow( tag + "3" ) << device << encoding << long(255);
|
|
949 |
QTest::newRow( tag + "4" ) << device << encoding << long(65535);
|
|
950 |
QTest::newRow( tag + "5" ) << device << encoding << long(-1);
|
|
951 |
QTest::newRow( tag + "6" ) << device << encoding << long(-10);
|
|
952 |
QTest::newRow( tag + "7" ) << device << encoding << long(-255);
|
|
953 |
QTest::newRow( tag + "8" ) << device << encoding << long(-65534);
|
|
954 |
}
|
|
955 |
}
|
|
956 |
}
|
|
957 |
|
|
958 |
void tst_Q3TextStream::operator_shift_long()
|
|
959 |
{
|
|
960 |
createWriteStream( os );
|
|
961 |
write_long( os );
|
|
962 |
closeWriteStream( os );
|
|
963 |
|
|
964 |
createReadStream( is );
|
|
965 |
read_long( is );
|
|
966 |
closeReadStream( is );
|
|
967 |
}
|
|
968 |
|
|
969 |
void tst_Q3TextStream::read_long( Q3TextStream *s )
|
|
970 |
{
|
|
971 |
QFETCH( long, sl );
|
|
972 |
long exp;
|
|
973 |
QString A, B;
|
|
974 |
*s >> A >> exp >> B;
|
|
975 |
QCOMPARE( A, QString("A") );
|
|
976 |
QCOMPARE( B, QString("B") );
|
|
977 |
QCOMPARE( exp, sl );
|
|
978 |
s->skipWhiteSpace();
|
|
979 |
}
|
|
980 |
|
|
981 |
void tst_Q3TextStream::write_long( Q3TextStream *s )
|
|
982 |
{
|
|
983 |
QFETCH( long, sl );
|
|
984 |
*s << " A " << sl << " B ";
|
|
985 |
}
|
|
986 |
|
|
987 |
// **************** long ****************
|
|
988 |
|
|
989 |
void tst_Q3TextStream::operator_shift_ulong_data()
|
|
990 |
{
|
|
991 |
QTest::addColumn<QString>( "device" );
|
|
992 |
QTest::addColumn<QString>( "encoding" );
|
|
993 |
QTest::addColumn<ulong>( "ul" );
|
|
994 |
|
|
995 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
996 |
QString device = devices[d];
|
|
997 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
998 |
QString encoding = encodings[e];
|
|
999 |
|
|
1000 |
QString tag = device + "_" + encoding + "_";
|
|
1001 |
QTest::newRow( tag + "0" ) << device << encoding << ulong(0);
|
|
1002 |
QTest::newRow( tag + "1" ) << device << encoding << ulong(1);
|
|
1003 |
QTest::newRow( tag + "2" ) << device << encoding << ulong(10);
|
|
1004 |
QTest::newRow( tag + "3" ) << device << encoding << ulong(255);
|
|
1005 |
QTest::newRow( tag + "4" ) << device << encoding << ulong(65535);
|
|
1006 |
}
|
|
1007 |
}
|
|
1008 |
}
|
|
1009 |
|
|
1010 |
void tst_Q3TextStream::operator_shift_ulong()
|
|
1011 |
{
|
|
1012 |
createWriteStream( os );
|
|
1013 |
write_ulong( os );
|
|
1014 |
closeWriteStream( os );
|
|
1015 |
|
|
1016 |
createReadStream( is );
|
|
1017 |
read_ulong( is );
|
|
1018 |
closeReadStream( is );
|
|
1019 |
}
|
|
1020 |
|
|
1021 |
void tst_Q3TextStream::read_ulong( Q3TextStream *s )
|
|
1022 |
{
|
|
1023 |
QFETCH( ulong, ul );
|
|
1024 |
ulong exp;
|
|
1025 |
QString A, B;
|
|
1026 |
*s >> A >> exp >> B;
|
|
1027 |
QCOMPARE( A, QString("A") );
|
|
1028 |
QCOMPARE( B, QString("B") );
|
|
1029 |
QCOMPARE( exp, ul );
|
|
1030 |
s->skipWhiteSpace();
|
|
1031 |
}
|
|
1032 |
|
|
1033 |
void tst_Q3TextStream::write_ulong( Q3TextStream *s )
|
|
1034 |
{
|
|
1035 |
QFETCH( ulong, ul );
|
|
1036 |
*s << " A " << ul << " B ";
|
|
1037 |
}
|
|
1038 |
|
|
1039 |
// **************** float ****************
|
|
1040 |
|
|
1041 |
void tst_Q3TextStream::operator_shift_float_data()
|
|
1042 |
{
|
|
1043 |
QTest::addColumn<QString>( "device" );
|
|
1044 |
QTest::addColumn<QString>( "encoding" );
|
|
1045 |
QTest::addColumn<float>( "f" );
|
|
1046 |
|
|
1047 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
1048 |
QString device = devices[d];
|
|
1049 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
1050 |
QString encoding = encodings[e];
|
|
1051 |
|
|
1052 |
QString tag = device + "_" + encoding + "_";
|
|
1053 |
QTest::newRow( tag + "0" ) << device << encoding << float(0.0);
|
|
1054 |
QTest::newRow( tag + "1" ) << device << encoding << float(0.0001);
|
|
1055 |
QTest::newRow( tag + "2" ) << device << encoding << float(-0.0001);
|
|
1056 |
QTest::newRow( tag + "3" ) << device << encoding << float(3.45678);
|
|
1057 |
QTest::newRow( tag + "4" ) << device << encoding << float(-3.45678);
|
|
1058 |
}
|
|
1059 |
}
|
|
1060 |
}
|
|
1061 |
|
|
1062 |
void tst_Q3TextStream::operator_shift_float()
|
|
1063 |
{
|
|
1064 |
createWriteStream( os );
|
|
1065 |
write_float( os );
|
|
1066 |
closeWriteStream( os );
|
|
1067 |
|
|
1068 |
createReadStream( is );
|
|
1069 |
read_float( is );
|
|
1070 |
closeReadStream( is );
|
|
1071 |
}
|
|
1072 |
|
|
1073 |
void tst_Q3TextStream::read_float( Q3TextStream *s )
|
|
1074 |
{
|
|
1075 |
QFETCH( float, f );
|
|
1076 |
float exp;
|
|
1077 |
QString A, B;
|
|
1078 |
*s >> A >> exp >> B;
|
|
1079 |
QCOMPARE( A, QString("A") );
|
|
1080 |
QCOMPARE( B, QString("B") );
|
|
1081 |
QCOMPARE( exp, f );
|
|
1082 |
s->skipWhiteSpace();
|
|
1083 |
}
|
|
1084 |
|
|
1085 |
void tst_Q3TextStream::write_float( Q3TextStream *s )
|
|
1086 |
{
|
|
1087 |
QFETCH( float, f );
|
|
1088 |
*s << " A " << f << " B ";
|
|
1089 |
}
|
|
1090 |
|
|
1091 |
// **************** double ****************
|
|
1092 |
|
|
1093 |
void tst_Q3TextStream::operator_shift_double_data()
|
|
1094 |
{
|
|
1095 |
QTest::addColumn<QString>( "device" );
|
|
1096 |
QTest::addColumn<QString>( "encoding" );
|
|
1097 |
QTest::addColumn<double>( "d" );
|
|
1098 |
|
|
1099 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
1100 |
QString device = devices[d];
|
|
1101 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
1102 |
QString encoding = encodings[e];
|
|
1103 |
|
|
1104 |
QString tag = device + "_" + encoding + "_";
|
|
1105 |
QTest::newRow( tag + "0" ) << device << encoding << double(0.0);
|
|
1106 |
QTest::newRow( tag + "1" ) << device << encoding << double(0.0001);
|
|
1107 |
QTest::newRow( tag + "2" ) << device << encoding << double(-0.0001);
|
|
1108 |
QTest::newRow( tag + "3" ) << device << encoding << double(3.45678);
|
|
1109 |
QTest::newRow( tag + "4" ) << device << encoding << double(-3.45678);
|
|
1110 |
QTest::newRow( tag + "5" ) << device << encoding << double(1.23456789);
|
|
1111 |
QTest::newRow( tag + "6" ) << device << encoding << double(-1.23456789);
|
|
1112 |
}
|
|
1113 |
}
|
|
1114 |
}
|
|
1115 |
|
|
1116 |
void tst_Q3TextStream::operator_shift_double()
|
|
1117 |
{
|
|
1118 |
createWriteStream( os );
|
|
1119 |
os->precision( 10 );
|
|
1120 |
write_double( os );
|
|
1121 |
closeWriteStream( os );
|
|
1122 |
|
|
1123 |
createReadStream( is );
|
|
1124 |
is->precision( 10 );
|
|
1125 |
read_double( is );
|
|
1126 |
closeReadStream( is );
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
void tst_Q3TextStream::read_double( Q3TextStream *s )
|
|
1130 |
{
|
|
1131 |
QFETCH( double, d );
|
|
1132 |
double exp;
|
|
1133 |
QString A, B;
|
|
1134 |
*s >> A >> exp >> B;
|
|
1135 |
QCOMPARE( A, QString("A") );
|
|
1136 |
QCOMPARE( B, QString("B") );
|
|
1137 |
QCOMPARE( exp, d );
|
|
1138 |
s->skipWhiteSpace();
|
|
1139 |
}
|
|
1140 |
|
|
1141 |
void tst_Q3TextStream::write_double( Q3TextStream *s )
|
|
1142 |
{
|
|
1143 |
QFETCH( double, d );
|
|
1144 |
*s << " A " << d << " B ";
|
|
1145 |
}
|
|
1146 |
|
|
1147 |
// **************** QString ****************
|
|
1148 |
|
|
1149 |
void tst_Q3TextStream::operator_shift_QString_data()
|
|
1150 |
{
|
|
1151 |
QTest::addColumn<QString>( "device" );
|
|
1152 |
QTest::addColumn<QString>( "encoding" );
|
|
1153 |
QTest::addColumn<QString>( "str" );
|
|
1154 |
QTest::addColumn<bool>( "multi_str" );
|
|
1155 |
QTest::addColumn<bool>( "zero_length" );
|
|
1156 |
|
|
1157 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
1158 |
QString device = devices[d];
|
|
1159 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
1160 |
QString encoding = encodings[e];
|
|
1161 |
|
|
1162 |
QString tag = device + "_" + encoding + "_";
|
|
1163 |
QTest::newRow( tag + "0" ) << device << encoding << QString("") << bool(FALSE) << bool(TRUE);
|
|
1164 |
QTest::newRow( tag + "1" ) << device << encoding << QString() << bool(FALSE) << bool(TRUE);
|
|
1165 |
QTest::newRow( tag + "2" ) << device << encoding << QString("foo") << bool(FALSE) << bool(FALSE);
|
|
1166 |
QTest::newRow( tag + "3" ) << device << encoding << QString("foo\nbar") << bool(TRUE) << bool(FALSE);
|
|
1167 |
QTest::newRow( tag + "4" ) << device << encoding << QString("cjacka ckha cka ckah ckac kahckadhcbkgdk vkzdfbvajef vkahv") << bool(TRUE) << bool(FALSE);
|
|
1168 |
}
|
|
1169 |
}
|
|
1170 |
}
|
|
1171 |
|
|
1172 |
void tst_Q3TextStream::operator_shift_QString()
|
|
1173 |
{
|
|
1174 |
QFETCH( bool, zero_length );
|
|
1175 |
file_is_empty = zero_length;
|
|
1176 |
|
|
1177 |
createWriteStream( os );
|
|
1178 |
write_QString( os );
|
|
1179 |
closeWriteStream( os );
|
|
1180 |
|
|
1181 |
createReadStream( is );
|
|
1182 |
read_QString( is );
|
|
1183 |
closeReadStream( is );
|
|
1184 |
}
|
|
1185 |
|
|
1186 |
void tst_Q3TextStream::read_QString( Q3TextStream *s )
|
|
1187 |
{
|
|
1188 |
QString exp;
|
|
1189 |
QFETCH( QString, str );
|
|
1190 |
if (str.isNull())
|
|
1191 |
str = "";
|
|
1192 |
|
|
1193 |
QFETCH( bool, multi_str );
|
|
1194 |
if (!multi_str) {
|
|
1195 |
*s >> exp;
|
|
1196 |
QCOMPARE( exp, str );
|
|
1197 |
} else {
|
|
1198 |
QStringList l;
|
|
1199 |
l = QStringList::split( " ", str );
|
|
1200 |
if (l.count() < 2)
|
|
1201 |
l = QStringList::split( "\n", str );
|
|
1202 |
for (uint i=0; i<uint(l.count()); i++) {
|
|
1203 |
*s >> exp;
|
|
1204 |
QCOMPARE( exp, l[i] );
|
|
1205 |
}
|
|
1206 |
}
|
|
1207 |
}
|
|
1208 |
|
|
1209 |
void tst_Q3TextStream::write_QString( Q3TextStream *s )
|
|
1210 |
{
|
|
1211 |
QFETCH( QString, str );
|
|
1212 |
*s << str;
|
|
1213 |
}
|
|
1214 |
|
|
1215 |
// **************** Q3CString ****************
|
|
1216 |
|
|
1217 |
void tst_Q3TextStream::operator_shift_Q3CString_data()
|
|
1218 |
{
|
|
1219 |
QTest::addColumn<QString>( "device" );
|
|
1220 |
QTest::addColumn<QString>( "encoding" );
|
|
1221 |
QTest::addColumn<Q3CString>( "cs" );
|
|
1222 |
QTest::addColumn<bool>( "multi_str" );
|
|
1223 |
QTest::addColumn<bool>( "zero_length" );
|
|
1224 |
|
|
1225 |
for ( int d=0; devices[d] != 0; d++ ) {
|
|
1226 |
QString device = devices[d];
|
|
1227 |
for ( int e=0; encodings[e] != 0; e++ ) {
|
|
1228 |
QString encoding = encodings[e];
|
|
1229 |
|
|
1230 |
QString tag = device + "_" + encoding + "_";
|
|
1231 |
QTest::newRow( tag + "0" ) << device << encoding << Q3CString("") << bool(FALSE) << bool(TRUE);
|
|
1232 |
QTest::newRow( tag + "1" ) << device << encoding << Q3CString(0) << bool(FALSE) << bool(TRUE);
|
|
1233 |
QTest::newRow( tag + "2" ) << device << encoding << Q3CString("foo") << bool(FALSE) << bool(FALSE);
|
|
1234 |
QTest::newRow( tag + "3" ) << device << encoding << Q3CString("foo\nbar") << bool(TRUE) << bool(FALSE);
|
|
1235 |
QTest::newRow( tag + "4" ) << device << encoding << Q3CString("cjacka ckha cka ckah ckac kahckadhcbkgdk vkzdfbvajef vkahv") << bool(TRUE) << bool(FALSE);
|
|
1236 |
}
|
|
1237 |
}
|
|
1238 |
}
|
|
1239 |
|
|
1240 |
void tst_Q3TextStream::operator_shift_Q3CString()
|
|
1241 |
{
|
|
1242 |
QFETCH( bool, zero_length );
|
|
1243 |
file_is_empty = zero_length;
|
|
1244 |
|
|
1245 |
createWriteStream( os );
|
|
1246 |
write_Q3CString( os );
|
|
1247 |
closeWriteStream( os );
|
|
1248 |
|
|
1249 |
createReadStream( is );
|
|
1250 |
read_Q3CString( is );
|
|
1251 |
closeReadStream( is );
|
|
1252 |
}
|
|
1253 |
|
|
1254 |
void tst_Q3TextStream::read_Q3CString( Q3TextStream *s )
|
|
1255 |
{
|
|
1256 |
/*
|
|
1257 |
QFETCH( Q3CString, cs );
|
|
1258 |
Q3CString exp;
|
|
1259 |
*s >> exp;
|
|
1260 |
QCOMPARE( exp, cs );
|
|
1261 |
*/
|
|
1262 |
Q3CString exp;
|
|
1263 |
QFETCH( Q3CString, cs );
|
|
1264 |
if (cs.isNull())
|
|
1265 |
cs = "";
|
|
1266 |
|
|
1267 |
QFETCH( bool, multi_str );
|
|
1268 |
if (!multi_str) {
|
|
1269 |
*s >> exp;
|
|
1270 |
QCOMPARE( exp, cs );
|
|
1271 |
} else {
|
|
1272 |
QStringList l;
|
|
1273 |
l = QStringList::split( " ", cs );
|
|
1274 |
if (l.count() < 2)
|
|
1275 |
l = QStringList::split( "\n", cs );
|
|
1276 |
for (uint i=0; i<uint(l.count()); i++) {
|
|
1277 |
*s >> exp;
|
|
1278 |
QCOMPARE( exp, Q3CString(l[i].toLatin1()) );
|
|
1279 |
}
|
|
1280 |
}
|
|
1281 |
}
|
|
1282 |
|
|
1283 |
void tst_Q3TextStream::write_Q3CString( Q3TextStream *s )
|
|
1284 |
{
|
|
1285 |
QFETCH( Q3CString, cs );
|
|
1286 |
*s << cs;
|
|
1287 |
}
|
|
1288 |
|
|
1289 |
void tst_Q3TextStream::task28319()
|
|
1290 |
{
|
|
1291 |
/*
|
|
1292 |
// Specific test for task 28319
|
|
1293 |
QFile f("28319.txt");
|
|
1294 |
if (f.open(IO_WriteOnly)) {
|
|
1295 |
// First we write some text
|
|
1296 |
Q3TextStream writeStream(&f);
|
|
1297 |
writeStream.setEncoding(Q3TextStream::Unicode);
|
|
1298 |
writeStream << "This is a";
|
|
1299 |
f.close();
|
|
1300 |
} else {
|
|
1301 |
QVERIFY(FALSE);
|
|
1302 |
}
|
|
1303 |
if (f.open(IO_WriteOnly | IO_Append)) {
|
|
1304 |
// Now we append some text
|
|
1305 |
Q3TextStream appendStream(&f);
|
|
1306 |
appendStream.setEncoding(Q3TextStream::Unicode);
|
|
1307 |
appendStream << " test";
|
|
1308 |
f.close();
|
|
1309 |
} else {
|
|
1310 |
QVERIFY(FALSE);
|
|
1311 |
}
|
|
1312 |
if (f.open(IO_ReadOnly)) {
|
|
1313 |
// Now we read in the text
|
|
1314 |
Q3TextStream readStream(&f);
|
|
1315 |
QString text = readStream.read();
|
|
1316 |
QCOMPARE(text, QString("This is a test"));
|
|
1317 |
} else {
|
|
1318 |
QVERIFY(FALSE);
|
|
1319 |
}
|
|
1320 |
*/
|
|
1321 |
}
|
|
1322 |
|
|
1323 |
/*!
|
|
1324 |
\since 4.5
|
|
1325 |
|
|
1326 |
This code worked in Qt 3 and should also in Qt 4. It should not crash due to
|
|
1327 |
out of bounds.
|
|
1328 |
|
|
1329 |
*/
|
|
1330 |
void tst_Q3TextStream::QTextCodecCodecForIndex() const
|
|
1331 |
{
|
|
1332 |
for(int i = 0; QTextCodec::codecForIndex(i); i++)
|
|
1333 |
;
|
|
1334 |
}
|
|
1335 |
|
|
1336 |
QTEST_MAIN(tst_Q3TextStream)
|
|
1337 |
#include "tst_q3textstream.moc"
|