]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/datastreamtest.cpp
Define __VISUALC__ for ICC under Windows again.
[wxWidgets.git] / tests / streams / datastreamtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/datastreamtest.cpp
3 // Purpose: wxDataXXXStream Unit Test
4 // Author: Ryan Norton
5 // Created: 2004-08-14
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
23 #include <vector>
24
25 #include "wx/datstrm.h"
26 #include "wx/wfstream.h"
27 #include "wx/math.h"
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 );
42 #if wxUSE_LONGLONG
43 CPPUNIT_TEST( LongLongRW );
44 #endif
45 #if wxHAS_INT64
46 CPPUNIT_TEST( Int64RW );
47 #endif
48 CPPUNIT_TEST( NaNRW );
49 CPPUNIT_TEST( PseudoTest_UseBigEndian );
50 CPPUNIT_TEST( FloatRW );
51 CPPUNIT_TEST( DoubleRW );
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
63 CPPUNIT_TEST_SUITE_END();
64
65 wxFloat64 TestFloatRW(wxFloat64 fValue);
66
67 void FloatRW();
68 void DoubleRW();
69 #if wxUSE_LONGLONG
70 void LongLongRW();
71 #endif
72 #if wxHAS_INT64
73 void Int64RW();
74 #endif
75 void NaNRW();
76
77 void PseudoTest_UseBigEndian() { ms_useBigEndianFormat = true; }
78 void PseudoTest_UseLittleEndian() { ms_useBigEndianFormat = false; }
79 #if wxUSE_APPLE_IEEE
80 void PseudoTest_UseIEEE754() { ms_useIEEE754 = true; }
81 #endif // wxUSE_APPLE_IEEE
82
83 static bool ms_useBigEndianFormat;
84 #if wxUSE_APPLE_IEEE
85 static bool ms_useIEEE754;
86 #endif // wxUSE_APPLE_IEEE
87
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
94 // also include in its own registry so that these tests can be run alone
95 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase, "DataStreamTestCase" );
96
97 bool DataStreamTestCase::ms_useBigEndianFormat = false;
98 #if wxUSE_APPLE_IEEE
99 bool DataStreamTestCase::ms_useIEEE754 = false;
100 #endif // wxUSE_APPLE_IEEE
101
102 DataStreamTestCase::DataStreamTestCase()
103 {
104 }
105
106 wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue)
107 {
108 {
109 wxFileOutputStream pFileOutput( wxT("mytext.dat") );
110 wxDataOutputStream pDataOutput( pFileOutput );
111 if ( ms_useBigEndianFormat )
112 pDataOutput.BigEndianOrdered(true);
113
114 #if wxUSE_APPLE_IEEE
115 if ( ms_useIEEE754 )
116 pDataOutput.UseBasicPrecisions();
117 #endif // wxUSE_APPLE_IEEE
118
119 pDataOutput << fValue;
120 }
121
122 wxFileInputStream pFileInput( wxT("mytext.dat") );
123 wxDataInputStream pDataInput( pFileInput );
124 if ( ms_useBigEndianFormat )
125 pDataInput.BigEndianOrdered(true);
126
127 #if wxUSE_APPLE_IEEE
128 if ( ms_useIEEE754 )
129 pDataInput.UseBasicPrecisions();
130 #endif // wxUSE_APPLE_IEEE
131
132 wxFloat64 fInFloat;
133
134 pDataInput >> fInFloat;
135
136 return fInFloat;
137 }
138
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 {
158 wxFileOutputStream FileOutput( wxT("mytext.dat") );
159 wxDataOutputStream DataOutput( FileOutput );
160
161 (DataOutput.*pfnWriter)(Values, Size);
162 }
163
164 {
165 wxFileInputStream FileInput( wxT("mytext.dat") );
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
196 bool IsOk() const
197 {
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 {
209 wxFileOutputStream FileOutput( wxT("mytext.dat") );
210 wxDataOutputStream DataOutput( FileOutput );
211
212 DataOutput << Value;
213 }
214
215 {
216 wxFileInputStream FileInput( wxT("mytext.dat") );
217 wxDataInputStream DataInput( FileInput );
218
219 DataInput >> InValue;
220 }
221
222 return InValue;
223 }
224
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
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) );
258 CPPUNIT_ASSERT( TestMultiRW<wxLongLong>(ValuesLL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() );
259 CPPUNIT_ASSERT( TestMultiRW<wxULongLong>(ValuesULL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() );
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) );
282 CPPUNIT_ASSERT( TestMultiRW<wxInt64>(ValuesI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() );
283 CPPUNIT_ASSERT( TestMultiRW<wxUint64>(ValuesUI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() );
284 }
285 #endif
286
287 void DataStreamTestCase::NaNRW()
288 {
289 //TODO?
290 }
291
292