]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/streams/datastreamtest.cpp | |
3 | // Purpose: wxDataXXXStream Unit Test | |
4 | // Author: Ryan Norton | |
5 | // Created: 2004-08-14 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Ryan Norton | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "testprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/wx.h" | |
22 | #endif // WX_PRECOMP | |
23 | ||
24 | #include <vector> | |
25 | ||
26 | #include "wx/datstrm.h" | |
27 | #include "wx/wfstream.h" | |
28 | #include "wx/math.h" | |
29 | ||
30 | // ---------------------------------------------------------------------------- | |
31 | // test class | |
32 | // ---------------------------------------------------------------------------- | |
33 | ||
34 | class DataStreamTestCase : public CppUnit::TestCase | |
35 | { | |
36 | public: | |
37 | DataStreamTestCase(); | |
38 | ||
39 | private: | |
40 | CPPUNIT_TEST_SUITE( DataStreamTestCase ); | |
41 | CPPUNIT_TEST( FloatRW ); | |
42 | CPPUNIT_TEST( DoubleRW ); | |
43 | #if wxUSE_LONGLONG | |
44 | CPPUNIT_TEST( LongLongRW ); | |
45 | #endif | |
46 | #if wxHAS_INT64 | |
47 | CPPUNIT_TEST( Int64RW ); | |
48 | #endif | |
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. | |
55 | #if wxUSE_APPLE_IEEE | |
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(); | |
65 | ||
66 | wxFloat64 TestFloatRW(wxFloat64 fValue); | |
67 | ||
68 | void FloatRW(); | |
69 | void DoubleRW(); | |
70 | #if wxUSE_LONGLONG | |
71 | void LongLongRW(); | |
72 | #endif | |
73 | #if wxHAS_INT64 | |
74 | void Int64RW(); | |
75 | #endif | |
76 | void NaNRW(); | |
77 | ||
78 | void PseudoTest_UseBigEndian() { ms_useBigEndianFormat = true; } | |
79 | void PseudoTest_UseLittleEndian() { ms_useBigEndianFormat = false; } | |
80 | #if wxUSE_APPLE_IEEE | |
81 | void PseudoTest_UseIEEE754() { ms_useIEEE754 = true; } | |
82 | #endif // wxUSE_APPLE_IEEE | |
83 | ||
84 | static bool ms_useBigEndianFormat; | |
85 | #if wxUSE_APPLE_IEEE | |
86 | static bool ms_useIEEE754; | |
87 | #endif // wxUSE_APPLE_IEEE | |
88 | ||
89 | DECLARE_NO_COPY_CLASS(DataStreamTestCase) | |
90 | }; | |
91 | ||
92 | // register in the unnamed registry so that these tests are run by default | |
93 | CPPUNIT_TEST_SUITE_REGISTRATION( DataStreamTestCase ); | |
94 | ||
95 | // also include in its own registry so that these tests can be run alone | |
96 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase, "DataStreamTestCase" ); | |
97 | ||
98 | bool DataStreamTestCase::ms_useBigEndianFormat = false; | |
99 | #if wxUSE_APPLE_IEEE | |
100 | bool DataStreamTestCase::ms_useIEEE754 = false; | |
101 | #endif // wxUSE_APPLE_IEEE | |
102 | ||
103 | DataStreamTestCase::DataStreamTestCase() | |
104 | { | |
105 | } | |
106 | ||
107 | wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue) | |
108 | { | |
109 | { | |
110 | wxFileOutputStream pFileOutput( wxT("mytext.dat") ); | |
111 | wxDataOutputStream pDataOutput( pFileOutput ); | |
112 | if ( ms_useBigEndianFormat ) | |
113 | pDataOutput.BigEndianOrdered(true); | |
114 | ||
115 | #if wxUSE_APPLE_IEEE | |
116 | if ( ms_useIEEE754 ) | |
117 | pDataOutput.UseBasicPrecisions(); | |
118 | #endif // wxUSE_APPLE_IEEE | |
119 | ||
120 | pDataOutput << fValue; | |
121 | } | |
122 | ||
123 | wxFileInputStream pFileInput( wxT("mytext.dat") ); | |
124 | wxDataInputStream pDataInput( pFileInput ); | |
125 | if ( ms_useBigEndianFormat ) | |
126 | pDataInput.BigEndianOrdered(true); | |
127 | ||
128 | #if wxUSE_APPLE_IEEE | |
129 | if ( ms_useIEEE754 ) | |
130 | pDataInput.UseBasicPrecisions(); | |
131 | #endif // wxUSE_APPLE_IEEE | |
132 | ||
133 | wxFloat64 fInFloat; | |
134 | ||
135 | pDataInput >> fInFloat; | |
136 | ||
137 | return fInFloat; | |
138 | } | |
139 | ||
140 | template <class T> | |
141 | class TestMultiRW { | |
142 | public: | |
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); | |
146 | ||
147 | private: | |
148 | bool m_ok; | |
149 | ||
150 | private: | |
151 | void ProcessData(const T *Values, | |
152 | typename ValueArray::size_type Size, | |
153 | FnWriter pfnWriter, | |
154 | FnReader pfnReader) | |
155 | { | |
156 | ValueArray InValues(Size); | |
157 | ||
158 | { | |
159 | wxFileOutputStream FileOutput( wxT("mytext.dat") ); | |
160 | wxDataOutputStream DataOutput( FileOutput ); | |
161 | ||
162 | (DataOutput.*pfnWriter)(Values, Size); | |
163 | } | |
164 | ||
165 | { | |
166 | wxFileInputStream FileInput( wxT("mytext.dat") ); | |
167 | wxDataInputStream DataInput( FileInput ); | |
168 | ||
169 | (DataInput.*pfnReader)(&*InValues.begin(), InValues.size()); | |
170 | } | |
171 | ||
172 | m_ok = true; | |
173 | for (typename ValueArray::size_type idx=0; idx!=Size; ++idx) { | |
174 | if (InValues[idx]!=Values[idx]) { | |
175 | m_ok = false; | |
176 | break; | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | ||
182 | public: | |
183 | TestMultiRW(const T *Values, | |
184 | size_t Size, | |
185 | FnWriter pfnWriter, | |
186 | FnReader pfnReader) | |
187 | { | |
188 | ProcessData(Values, (typename ValueArray::size_type) Size, pfnWriter, pfnReader); | |
189 | } | |
190 | TestMultiRW(const ValueArray &Values, | |
191 | FnWriter pfnWriter, | |
192 | FnReader pfnReader) | |
193 | { | |
194 | ProcessData(&*Values.begin(), Values.size(), pfnWriter, pfnReader); | |
195 | } | |
196 | ||
197 | bool IsOk() const | |
198 | { | |
199 | return m_ok; | |
200 | } | |
201 | }; | |
202 | ||
203 | template <class T> | |
204 | static | |
205 | T TestRW(const T &Value) | |
206 | { | |
207 | T InValue; | |
208 | ||
209 | { | |
210 | wxFileOutputStream FileOutput( wxT("mytext.dat") ); | |
211 | wxDataOutputStream DataOutput( FileOutput ); | |
212 | ||
213 | DataOutput << Value; | |
214 | } | |
215 | ||
216 | { | |
217 | wxFileInputStream FileInput( wxT("mytext.dat") ); | |
218 | wxDataInputStream DataInput( FileInput ); | |
219 | ||
220 | DataInput >> InValue; | |
221 | } | |
222 | ||
223 | return InValue; | |
224 | } | |
225 | ||
226 | void DataStreamTestCase::FloatRW() | |
227 | { | |
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 ); | |
232 | } | |
233 | ||
234 | void DataStreamTestCase::DoubleRW() | |
235 | { | |
236 | CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 ); | |
237 | CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 ); | |
238 | } | |
239 | ||
240 | #if wxUSE_LONGLONG | |
241 | void DataStreamTestCase::LongLongRW() | |
242 | { | |
243 | TestMultiRW<wxLongLong>::ValueArray ValuesLL; | |
244 | TestMultiRW<wxULongLong>::ValueArray ValuesULL; | |
245 | ||
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)); | |
251 | ||
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)); | |
256 | ||
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() ); | |
261 | } | |
262 | #endif | |
263 | ||
264 | #if wxHAS_INT64 | |
265 | void DataStreamTestCase::Int64RW() | |
266 | { | |
267 | TestMultiRW<wxInt64>::ValueArray ValuesI64; | |
268 | TestMultiRW<wxUint64>::ValueArray ValuesUI64; | |
269 | ||
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)); | |
275 | ||
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)); | |
280 | ||
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() ); | |
285 | } | |
286 | #endif | |
287 | ||
288 | void DataStreamTestCase::NaNRW() | |
289 | { | |
290 | //TODO? | |
291 | } | |
292 | ||
293 |