]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/datastreamtest.cpp
8f48c285cf2542554129eaf928127e24b05241c1
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 CPPUNIT_TEST_SUITE_END();
55 wxFloat64
TestFloatRW(wxFloat64 fValue
);
67 void PseudoTest_UseBigEndian() { ms_useBigEndianFormat
= true; }
69 static bool ms_useBigEndianFormat
;
71 DECLARE_NO_COPY_CLASS(DataStreamTestCase
)
74 // register in the unnamed registry so that these tests are run by default
75 CPPUNIT_TEST_SUITE_REGISTRATION( DataStreamTestCase
);
77 // also include in its own registry so that these tests can be run alone
78 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase
, "DataStreamTestCase" );
80 bool DataStreamTestCase::ms_useBigEndianFormat
= false;
82 DataStreamTestCase::DataStreamTestCase()
86 wxFloat64
DataStreamTestCase::TestFloatRW(wxFloat64 fValue
)
89 wxFileOutputStream
pFileOutput( wxT("mytext.dat") );
90 wxDataOutputStream
pDataOutput( pFileOutput
);
91 if ( ms_useBigEndianFormat
)
92 pDataOutput
.BigEndianOrdered(true);
94 pDataOutput
<< fValue
;
97 wxFileInputStream
pFileInput( wxT("mytext.dat") );
98 wxDataInputStream
pDataInput( pFileInput
);
99 if ( ms_useBigEndianFormat
)
100 pDataInput
.BigEndianOrdered(true);
104 pDataInput
>> fInFloat
;
112 typedef std::vector
<T
> ValueArray
;
113 typedef void (wxDataOutputStream::*FnWriter
)(const T
*buffer
, size_t size
);
114 typedef void (wxDataInputStream::*FnReader
)(T
*buffer
, size_t size
);
120 void ProcessData(const T
*Values
,
121 typename
ValueArray::size_type Size
,
125 ValueArray
InValues(Size
);
128 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
129 wxDataOutputStream
DataOutput( FileOutput
);
131 (DataOutput
.*pfnWriter
)(Values
, Size
);
135 wxFileInputStream
FileInput( wxT("mytext.dat") );
136 wxDataInputStream
DataInput( FileInput
);
138 (DataInput
.*pfnReader
)(&*InValues
.begin(), InValues
.size());
142 for (typename
ValueArray::size_type idx
=0; idx
!=Size
; ++idx
) {
143 if (InValues
[idx
]!=Values
[idx
]) {
152 TestMultiRW(const T
*Values
,
157 ProcessData(Values
, (typename
ValueArray::size_type
) Size
, pfnWriter
, pfnReader
);
159 TestMultiRW(const ValueArray
&Values
,
163 ProcessData(&*Values
.begin(), Values
.size(), pfnWriter
, pfnReader
);
174 T
TestRW(const T
&Value
)
179 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
180 wxDataOutputStream
DataOutput( FileOutput
);
186 wxFileInputStream
FileInput( wxT("mytext.dat") );
187 wxDataInputStream
DataInput( FileInput
);
189 DataInput
>> InValue
;
195 void DataStreamTestCase::FloatRW()
197 CPPUNIT_ASSERT( TestFloatRW(5.5) == 5.5 );
198 CPPUNIT_ASSERT( TestFloatRW(5) == 5 );
199 CPPUNIT_ASSERT( TestFloatRW(5.55) == 5.55 );
200 CPPUNIT_ASSERT( TestFloatRW(55555.555555) == 55555.555555 );
203 void DataStreamTestCase::DoubleRW()
205 CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 );
206 CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 );
210 void DataStreamTestCase::LongLongRW()
212 TestMultiRW
<wxLongLong
>::ValueArray ValuesLL
;
213 TestMultiRW
<wxULongLong
>::ValueArray ValuesULL
;
215 ValuesLL
.push_back(wxLongLong(0l));
216 ValuesLL
.push_back(wxLongLong(1l));
217 ValuesLL
.push_back(wxLongLong(-1l));
218 ValuesLL
.push_back(wxLongLong(0x12345678l
));
219 ValuesLL
.push_back(wxLongLong(0x12345678l
, 0xabcdef01l
));
221 ValuesULL
.push_back(wxULongLong(0l));
222 ValuesULL
.push_back(wxULongLong(1l));
223 ValuesULL
.push_back(wxULongLong(0x12345678l
));
224 ValuesULL
.push_back(wxULongLong(0x12345678l
, 0xabcdef01l
));
226 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
)) == wxLongLong(0x12345678l
) );
227 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
, 0xabcdef01l
)) == wxLongLong(0x12345678l
, 0xabcdef01l
) );
228 CPPUNIT_ASSERT( TestMultiRW
<wxLongLong
>(ValuesLL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).IsOk() );
229 CPPUNIT_ASSERT( TestMultiRW
<wxULongLong
>(ValuesULL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).IsOk() );
234 void DataStreamTestCase::Int64RW()
236 TestMultiRW
<wxInt64
>::ValueArray ValuesI64
;
237 TestMultiRW
<wxUint64
>::ValueArray ValuesUI64
;
239 ValuesI64
.push_back(wxInt64(0l));
240 ValuesI64
.push_back(wxInt64(1l));
241 ValuesI64
.push_back(wxInt64(-1l));
242 ValuesI64
.push_back(wxInt64(0x12345678l
));
243 ValuesI64
.push_back((wxInt64(0x12345678l
) << 32) + wxInt64(0xabcdef01l
));
245 ValuesUI64
.push_back(wxUint64(0l));
246 ValuesUI64
.push_back(wxUint64(1l));
247 ValuesUI64
.push_back(wxUint64(0x12345678l
));
248 ValuesUI64
.push_back((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
));
250 CPPUNIT_ASSERT( TestRW(wxUint64(0x12345678l
)) == wxUint64(0x12345678l
) );
251 CPPUNIT_ASSERT( TestRW((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
)) == (wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
) );
252 CPPUNIT_ASSERT( TestMultiRW
<wxInt64
>(ValuesI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).IsOk() );
253 CPPUNIT_ASSERT( TestMultiRW
<wxUint64
>(ValuesUI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).IsOk() );
257 void DataStreamTestCase::NaNRW()