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