1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/datastreamtest.cpp
3 // Purpose: wxDataXXXStream Unit Test
7 // Copyright: (c) 2004 Ryan Norton
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
26 #include "wx/datstrm.h"
27 #include "wx/wfstream.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class DataStreamTestCase
: public CppUnit::TestCase
40 CPPUNIT_TEST_SUITE( DataStreamTestCase
);
41 CPPUNIT_TEST( FloatRW
);
42 CPPUNIT_TEST( DoubleRW
);
44 CPPUNIT_TEST( LongLongRW
);
47 CPPUNIT_TEST( Int64RW
);
49 CPPUNIT_TEST( NaNRW
);
50 CPPUNIT_TEST( PseudoTest_UseBigEndian
);
51 CPPUNIT_TEST( FloatRW
);
52 CPPUNIT_TEST( DoubleRW
);
53 // Only test standard IEEE 754 formats if we're using IEEE extended
54 // format by default, otherwise the tests above already covered them.
56 CPPUNIT_TEST( PseudoTest_UseIEEE754
);
57 CPPUNIT_TEST( FloatRW
);
58 CPPUNIT_TEST( DoubleRW
);
59 // Also retest little endian version with standard formats.
60 CPPUNIT_TEST( PseudoTest_UseLittleEndian
);
61 CPPUNIT_TEST( FloatRW
);
62 CPPUNIT_TEST( DoubleRW
);
63 #endif // wxUSE_APPLE_IEEE
64 CPPUNIT_TEST_SUITE_END();
66 wxFloat64
TestFloatRW(wxFloat64 fValue
);
78 void PseudoTest_UseBigEndian() { ms_useBigEndianFormat
= true; }
79 void PseudoTest_UseLittleEndian() { ms_useBigEndianFormat
= false; }
81 void PseudoTest_UseIEEE754() { ms_useIEEE754
= true; }
82 #endif // wxUSE_APPLE_IEEE
84 static bool ms_useBigEndianFormat
;
86 static bool ms_useIEEE754
;
87 #endif // wxUSE_APPLE_IEEE
89 DECLARE_NO_COPY_CLASS(DataStreamTestCase
)
92 // register in the unnamed registry so that these tests are run by default
93 CPPUNIT_TEST_SUITE_REGISTRATION( DataStreamTestCase
);
95 // also include in its own registry so that these tests can be run alone
96 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase
, "DataStreamTestCase" );
98 bool DataStreamTestCase::ms_useBigEndianFormat
= false;
100 bool DataStreamTestCase::ms_useIEEE754
= false;
101 #endif // wxUSE_APPLE_IEEE
103 DataStreamTestCase::DataStreamTestCase()
107 wxFloat64
DataStreamTestCase::TestFloatRW(wxFloat64 fValue
)
110 wxFileOutputStream
pFileOutput( wxT("mytext.dat") );
111 wxDataOutputStream
pDataOutput( pFileOutput
);
112 if ( ms_useBigEndianFormat
)
113 pDataOutput
.BigEndianOrdered(true);
117 pDataOutput
.UseBasicPrecisions();
118 #endif // wxUSE_APPLE_IEEE
120 pDataOutput
<< fValue
;
123 wxFileInputStream
pFileInput( wxT("mytext.dat") );
124 wxDataInputStream
pDataInput( pFileInput
);
125 if ( ms_useBigEndianFormat
)
126 pDataInput
.BigEndianOrdered(true);
130 pDataInput
.UseBasicPrecisions();
131 #endif // wxUSE_APPLE_IEEE
135 pDataInput
>> fInFloat
;
143 typedef std::vector
<T
> ValueArray
;
144 typedef void (wxDataOutputStream::*FnWriter
)(const T
*buffer
, size_t size
);
145 typedef void (wxDataInputStream::*FnReader
)(T
*buffer
, size_t size
);
151 void ProcessData(const T
*Values
,
152 typename
ValueArray::size_type Size
,
156 ValueArray
InValues(Size
);
159 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
160 wxDataOutputStream
DataOutput( FileOutput
);
162 (DataOutput
.*pfnWriter
)(Values
, Size
);
166 wxFileInputStream
FileInput( wxT("mytext.dat") );
167 wxDataInputStream
DataInput( FileInput
);
169 (DataInput
.*pfnReader
)(&*InValues
.begin(), InValues
.size());
173 for (typename
ValueArray::size_type idx
=0; idx
!=Size
; ++idx
) {
174 if (InValues
[idx
]!=Values
[idx
]) {
183 TestMultiRW(const T
*Values
,
188 ProcessData(Values
, (typename
ValueArray::size_type
) Size
, pfnWriter
, pfnReader
);
190 TestMultiRW(const ValueArray
&Values
,
194 ProcessData(&*Values
.begin(), Values
.size(), pfnWriter
, pfnReader
);
205 T
TestRW(const T
&Value
)
210 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
211 wxDataOutputStream
DataOutput( FileOutput
);
217 wxFileInputStream
FileInput( wxT("mytext.dat") );
218 wxDataInputStream
DataInput( FileInput
);
220 DataInput
>> InValue
;
226 void DataStreamTestCase::FloatRW()
228 CPPUNIT_ASSERT( TestFloatRW(5.5) == 5.5 );
229 CPPUNIT_ASSERT( TestFloatRW(5) == 5 );
230 CPPUNIT_ASSERT( TestFloatRW(5.55) == 5.55 );
231 CPPUNIT_ASSERT( TestFloatRW(55555.555555) == 55555.555555 );
234 void DataStreamTestCase::DoubleRW()
236 CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 );
237 CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 );
241 void DataStreamTestCase::LongLongRW()
243 TestMultiRW
<wxLongLong
>::ValueArray ValuesLL
;
244 TestMultiRW
<wxULongLong
>::ValueArray ValuesULL
;
246 ValuesLL
.push_back(wxLongLong(0l));
247 ValuesLL
.push_back(wxLongLong(1l));
248 ValuesLL
.push_back(wxLongLong(-1l));
249 ValuesLL
.push_back(wxLongLong(0x12345678l
));
250 ValuesLL
.push_back(wxLongLong(0x12345678l
, 0xabcdef01l
));
252 ValuesULL
.push_back(wxULongLong(0l));
253 ValuesULL
.push_back(wxULongLong(1l));
254 ValuesULL
.push_back(wxULongLong(0x12345678l
));
255 ValuesULL
.push_back(wxULongLong(0x12345678l
, 0xabcdef01l
));
257 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
)) == wxLongLong(0x12345678l
) );
258 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
, 0xabcdef01l
)) == wxLongLong(0x12345678l
, 0xabcdef01l
) );
259 CPPUNIT_ASSERT( TestMultiRW
<wxLongLong
>(ValuesLL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).IsOk() );
260 CPPUNIT_ASSERT( TestMultiRW
<wxULongLong
>(ValuesULL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).IsOk() );
265 void DataStreamTestCase::Int64RW()
267 TestMultiRW
<wxInt64
>::ValueArray ValuesI64
;
268 TestMultiRW
<wxUint64
>::ValueArray ValuesUI64
;
270 ValuesI64
.push_back(wxInt64(0l));
271 ValuesI64
.push_back(wxInt64(1l));
272 ValuesI64
.push_back(wxInt64(-1l));
273 ValuesI64
.push_back(wxInt64(0x12345678l
));
274 ValuesI64
.push_back((wxInt64(0x12345678l
) << 32) + wxInt64(0xabcdef01l
));
276 ValuesUI64
.push_back(wxUint64(0l));
277 ValuesUI64
.push_back(wxUint64(1l));
278 ValuesUI64
.push_back(wxUint64(0x12345678l
));
279 ValuesUI64
.push_back((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
));
281 CPPUNIT_ASSERT( TestRW(wxUint64(0x12345678l
)) == wxUint64(0x12345678l
) );
282 CPPUNIT_ASSERT( TestRW((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
)) == (wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
) );
283 CPPUNIT_ASSERT( TestMultiRW
<wxInt64
>(ValuesI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).IsOk() );
284 CPPUNIT_ASSERT( TestMultiRW
<wxUint64
>(ValuesUI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).IsOk() );
288 void DataStreamTestCase::NaNRW()