1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/datastreamtest.cpp
3 // Purpose: wxDataXXXStream Unit Test
6 // Copyright: (c) 2004 Ryan Norton
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
25 #include "wx/datstrm.h"
26 #include "wx/wfstream.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class DataStreamTestCase
: public CppUnit::TestCase
39 CPPUNIT_TEST_SUITE( DataStreamTestCase
);
40 CPPUNIT_TEST( FloatRW
);
41 CPPUNIT_TEST( DoubleRW
);
43 CPPUNIT_TEST( LongLongRW
);
46 CPPUNIT_TEST( Int64RW
);
48 CPPUNIT_TEST( NaNRW
);
49 CPPUNIT_TEST( PseudoTest_UseBigEndian
);
50 CPPUNIT_TEST( FloatRW
);
51 CPPUNIT_TEST( DoubleRW
);
52 // Only test standard IEEE 754 formats if we're using IEEE extended
53 // format by default, otherwise the tests above already covered them.
55 CPPUNIT_TEST( PseudoTest_UseIEEE754
);
56 CPPUNIT_TEST( FloatRW
);
57 CPPUNIT_TEST( DoubleRW
);
58 // Also retest little endian version with standard formats.
59 CPPUNIT_TEST( PseudoTest_UseLittleEndian
);
60 CPPUNIT_TEST( FloatRW
);
61 CPPUNIT_TEST( DoubleRW
);
62 #endif // wxUSE_APPLE_IEEE
63 CPPUNIT_TEST_SUITE_END();
65 wxFloat64
TestFloatRW(wxFloat64 fValue
);
77 void PseudoTest_UseBigEndian() { ms_useBigEndianFormat
= true; }
78 void PseudoTest_UseLittleEndian() { ms_useBigEndianFormat
= false; }
80 void PseudoTest_UseIEEE754() { ms_useIEEE754
= true; }
81 #endif // wxUSE_APPLE_IEEE
83 static bool ms_useBigEndianFormat
;
85 static bool ms_useIEEE754
;
86 #endif // wxUSE_APPLE_IEEE
88 DECLARE_NO_COPY_CLASS(DataStreamTestCase
)
91 // register in the unnamed registry so that these tests are run by default
92 CPPUNIT_TEST_SUITE_REGISTRATION( DataStreamTestCase
);
94 // also include in its own registry so that these tests can be run alone
95 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase
, "DataStreamTestCase" );
97 bool DataStreamTestCase::ms_useBigEndianFormat
= false;
99 bool DataStreamTestCase::ms_useIEEE754
= false;
100 #endif // wxUSE_APPLE_IEEE
102 DataStreamTestCase::DataStreamTestCase()
106 wxFloat64
DataStreamTestCase::TestFloatRW(wxFloat64 fValue
)
109 wxFileOutputStream
pFileOutput( wxT("mytext.dat") );
110 wxDataOutputStream
pDataOutput( pFileOutput
);
111 if ( ms_useBigEndianFormat
)
112 pDataOutput
.BigEndianOrdered(true);
116 pDataOutput
.UseBasicPrecisions();
117 #endif // wxUSE_APPLE_IEEE
119 pDataOutput
<< fValue
;
122 wxFileInputStream
pFileInput( wxT("mytext.dat") );
123 wxDataInputStream
pDataInput( pFileInput
);
124 if ( ms_useBigEndianFormat
)
125 pDataInput
.BigEndianOrdered(true);
129 pDataInput
.UseBasicPrecisions();
130 #endif // wxUSE_APPLE_IEEE
134 pDataInput
>> fInFloat
;
142 typedef std::vector
<T
> ValueArray
;
143 typedef void (wxDataOutputStream::*FnWriter
)(const T
*buffer
, size_t size
);
144 typedef void (wxDataInputStream::*FnReader
)(T
*buffer
, size_t size
);
150 void ProcessData(const T
*Values
,
151 typename
ValueArray::size_type Size
,
155 ValueArray
InValues(Size
);
158 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
159 wxDataOutputStream
DataOutput( FileOutput
);
161 (DataOutput
.*pfnWriter
)(Values
, Size
);
165 wxFileInputStream
FileInput( wxT("mytext.dat") );
166 wxDataInputStream
DataInput( FileInput
);
168 (DataInput
.*pfnReader
)(&*InValues
.begin(), InValues
.size());
172 for (typename
ValueArray::size_type idx
=0; idx
!=Size
; ++idx
) {
173 if (InValues
[idx
]!=Values
[idx
]) {
182 TestMultiRW(const T
*Values
,
187 ProcessData(Values
, (typename
ValueArray::size_type
) Size
, pfnWriter
, pfnReader
);
189 TestMultiRW(const ValueArray
&Values
,
193 ProcessData(&*Values
.begin(), Values
.size(), pfnWriter
, pfnReader
);
204 T
TestRW(const T
&Value
)
209 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
210 wxDataOutputStream
DataOutput( FileOutput
);
216 wxFileInputStream
FileInput( wxT("mytext.dat") );
217 wxDataInputStream
DataInput( FileInput
);
219 DataInput
>> InValue
;
225 void DataStreamTestCase::FloatRW()
227 CPPUNIT_ASSERT( TestFloatRW(5.5) == 5.5 );
228 CPPUNIT_ASSERT( TestFloatRW(5) == 5 );
229 CPPUNIT_ASSERT( TestFloatRW(5.55) == 5.55 );
230 CPPUNIT_ASSERT( TestFloatRW(55555.555555) == 55555.555555 );
233 void DataStreamTestCase::DoubleRW()
235 CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 );
236 CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 );
240 void DataStreamTestCase::LongLongRW()
242 TestMultiRW
<wxLongLong
>::ValueArray ValuesLL
;
243 TestMultiRW
<wxULongLong
>::ValueArray ValuesULL
;
245 ValuesLL
.push_back(wxLongLong(0l));
246 ValuesLL
.push_back(wxLongLong(1l));
247 ValuesLL
.push_back(wxLongLong(-1l));
248 ValuesLL
.push_back(wxLongLong(0x12345678l
));
249 ValuesLL
.push_back(wxLongLong(0x12345678l
, 0xabcdef01l
));
251 ValuesULL
.push_back(wxULongLong(0l));
252 ValuesULL
.push_back(wxULongLong(1l));
253 ValuesULL
.push_back(wxULongLong(0x12345678l
));
254 ValuesULL
.push_back(wxULongLong(0x12345678l
, 0xabcdef01l
));
256 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
)) == wxLongLong(0x12345678l
) );
257 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
, 0xabcdef01l
)) == wxLongLong(0x12345678l
, 0xabcdef01l
) );
258 CPPUNIT_ASSERT( TestMultiRW
<wxLongLong
>(ValuesLL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).IsOk() );
259 CPPUNIT_ASSERT( TestMultiRW
<wxULongLong
>(ValuesULL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).IsOk() );
264 void DataStreamTestCase::Int64RW()
266 TestMultiRW
<wxInt64
>::ValueArray ValuesI64
;
267 TestMultiRW
<wxUint64
>::ValueArray ValuesUI64
;
269 ValuesI64
.push_back(wxInt64(0l));
270 ValuesI64
.push_back(wxInt64(1l));
271 ValuesI64
.push_back(wxInt64(-1l));
272 ValuesI64
.push_back(wxInt64(0x12345678l
));
273 ValuesI64
.push_back((wxInt64(0x12345678l
) << 32) + wxInt64(0xabcdef01l
));
275 ValuesUI64
.push_back(wxUint64(0l));
276 ValuesUI64
.push_back(wxUint64(1l));
277 ValuesUI64
.push_back(wxUint64(0x12345678l
));
278 ValuesUI64
.push_back((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
));
280 CPPUNIT_ASSERT( TestRW(wxUint64(0x12345678l
)) == wxUint64(0x12345678l
) );
281 CPPUNIT_ASSERT( TestRW((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
)) == (wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
) );
282 CPPUNIT_ASSERT( TestMultiRW
<wxInt64
>(ValuesI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).IsOk() );
283 CPPUNIT_ASSERT( TestMultiRW
<wxUint64
>(ValuesUI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).IsOk() );
287 void DataStreamTestCase::NaNRW()