]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/datastreamtest.cpp
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_SUITE_END();
62 DECLARE_NO_COPY_CLASS(DataStreamTestCase
)
65 // register in the unnamed registry so that these tests are run by default
66 CPPUNIT_TEST_SUITE_REGISTRATION( DataStreamTestCase
);
68 // also include in its own registry so that these tests can be run alone
69 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase
, "DataStreamTestCase" );
71 DataStreamTestCase::DataStreamTestCase()
76 wxFloat64
TestFloatRW(wxFloat64 fValue
)
78 wxFileOutputStream
* pFileOutput
= new wxFileOutputStream( wxT("mytext.dat") );
79 wxDataOutputStream
* pDataOutput
= new wxDataOutputStream( *pFileOutput
);
81 *pDataOutput
<< fValue
;
86 wxFileInputStream
* pFileInput
= new wxFileInputStream( wxT("mytext.dat") );
87 wxDataInputStream
* pDataInput
= new wxDataInputStream( *pFileInput
);
91 *pDataInput
>> fInFloat
;
102 typedef std::vector
<T
> ValueArray
;
103 typedef void (wxDataOutputStream::*FnWriter
)(const T
*buffer
, size_t size
);
104 typedef void (wxDataInputStream::*FnReader
)(T
*buffer
, size_t size
);
110 void ProcessData(const T
*Values
,
111 typename
ValueArray::size_type Size
,
115 ValueArray
InValues(Size
);
118 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
119 wxDataOutputStream
DataOutput( FileOutput
);
121 (DataOutput
.*pfnWriter
)(Values
, Size
);
125 wxFileInputStream
FileInput( wxT("mytext.dat") );
126 wxDataInputStream
DataInput( FileInput
);
128 (DataInput
.*pfnReader
)(&*InValues
.begin(), InValues
.size());
132 for (typename
ValueArray::size_type idx
=0; idx
!=Size
; ++idx
) {
133 if (InValues
[idx
]!=Values
[idx
]) {
142 TestMultiRW(const T
*Values
,
147 ProcessData(Values
, (typename
ValueArray::size_type
) Size
, pfnWriter
, pfnReader
);
149 TestMultiRW(const ValueArray
&Values
,
153 ProcessData(&*Values
.begin(), Values
.size(), pfnWriter
, pfnReader
);
156 bool Ok(void) const {
163 T
TestRW(const T
&Value
)
168 wxFileOutputStream
FileOutput( wxT("mytext.dat") );
169 wxDataOutputStream
DataOutput( FileOutput
);
175 wxFileInputStream
FileInput( wxT("mytext.dat") );
176 wxDataInputStream
DataInput( FileInput
);
178 DataInput
>> InValue
;
184 void DataStreamTestCase::FloatRW()
186 CPPUNIT_ASSERT( TestFloatRW(5.5) == 5.5 );
187 CPPUNIT_ASSERT( TestFloatRW(5) == 5 );
188 CPPUNIT_ASSERT( TestFloatRW(5.55) == 5.55 );
189 CPPUNIT_ASSERT( TestFloatRW(55555.555555) == 55555.555555 );
192 void DataStreamTestCase::DoubleRW()
194 CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 );
195 CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 );
199 void DataStreamTestCase::LongLongRW()
201 TestMultiRW
<wxLongLong
>::ValueArray ValuesLL
;
202 TestMultiRW
<wxULongLong
>::ValueArray ValuesULL
;
204 ValuesLL
.push_back(wxLongLong(0l));
205 ValuesLL
.push_back(wxLongLong(1l));
206 ValuesLL
.push_back(wxLongLong(-1l));
207 ValuesLL
.push_back(wxLongLong(0x12345678l
));
208 ValuesLL
.push_back(wxLongLong(0x12345678l
, 0xabcdef01l
));
210 ValuesULL
.push_back(wxULongLong(0l));
211 ValuesULL
.push_back(wxULongLong(1l));
212 ValuesULL
.push_back(wxULongLong(0x12345678l
));
213 ValuesULL
.push_back(wxULongLong(0x12345678l
, 0xabcdef01l
));
215 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
)) == wxLongLong(0x12345678l
) );
216 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l
, 0xabcdef01l
)) == wxLongLong(0x12345678l
, 0xabcdef01l
) );
217 CPPUNIT_ASSERT( TestMultiRW
<wxLongLong
>(ValuesLL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).Ok() );
218 CPPUNIT_ASSERT( TestMultiRW
<wxULongLong
>(ValuesULL
, &wxDataOutputStream::WriteLL
, &wxDataInputStream::ReadLL
).Ok() );
223 void DataStreamTestCase::Int64RW()
225 TestMultiRW
<wxInt64
>::ValueArray ValuesI64
;
226 TestMultiRW
<wxUint64
>::ValueArray ValuesUI64
;
228 ValuesI64
.push_back(wxInt64(0l));
229 ValuesI64
.push_back(wxInt64(1l));
230 ValuesI64
.push_back(wxInt64(-1l));
231 ValuesI64
.push_back(wxInt64(0x12345678l
));
232 ValuesI64
.push_back((wxInt64(0x12345678l
) << 32) + wxInt64(0xabcdef01l
));
234 ValuesUI64
.push_back(wxUint64(0l));
235 ValuesUI64
.push_back(wxUint64(1l));
236 ValuesUI64
.push_back(wxUint64(0x12345678l
));
237 ValuesUI64
.push_back((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
));
239 CPPUNIT_ASSERT( TestRW(wxUint64(0x12345678l
)) == wxUint64(0x12345678l
) );
240 CPPUNIT_ASSERT( TestRW((wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
)) == (wxUint64(0x12345678l
) << 32) + wxUint64(0xabcdef01l
) );
241 CPPUNIT_ASSERT( TestMultiRW
<wxInt64
>(ValuesI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).Ok() );
242 CPPUNIT_ASSERT( TestMultiRW
<wxUint64
>(ValuesUI64
, &wxDataOutputStream::Write64
, &wxDataInputStream::Read64
).Ok() );
246 void DataStreamTestCase::NaNRW()