]> git.saurik.com Git - wxWidgets.git/blame - tests/streams/datastreamtest.cpp
fixes potential crash under gatekeeper
[wxWidgets.git] / tests / streams / datastreamtest.cpp
CommitLineData
2f4bebe8
RN
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
216a72f3
VZ
24#include <vector>
25
2f4bebe8
RN
26#include "wx/datstrm.h"
27#include "wx/wfstream.h"
b713f891 28#include "wx/math.h"
2f4bebe8
RN
29
30// ----------------------------------------------------------------------------
31// test class
32// ----------------------------------------------------------------------------
33
34class DataStreamTestCase : public CppUnit::TestCase
35{
36public:
37 DataStreamTestCase();
38
39private:
40 CPPUNIT_TEST_SUITE( DataStreamTestCase );
41 CPPUNIT_TEST( FloatRW );
42 CPPUNIT_TEST( DoubleRW );
216a72f3
VZ
43#if wxUSE_LONGLONG
44 CPPUNIT_TEST( LongLongRW );
45#endif
46#if wxHAS_INT64
47 CPPUNIT_TEST( Int64RW );
48#endif
2f4bebe8 49 CPPUNIT_TEST( NaNRW );
1274add4
VZ
50 CPPUNIT_TEST( PseudoTest_UseBigEndian );
51 CPPUNIT_TEST( FloatRW );
52 CPPUNIT_TEST( DoubleRW );
789ab840
VZ
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
2f4bebe8
RN
64 CPPUNIT_TEST_SUITE_END();
65
1274add4
VZ
66 wxFloat64 TestFloatRW(wxFloat64 fValue);
67
2f4bebe8
RN
68 void FloatRW();
69 void DoubleRW();
216a72f3
VZ
70#if wxUSE_LONGLONG
71 void LongLongRW();
72#endif
73#if wxHAS_INT64
74 void Int64RW();
75#endif
2f4bebe8
RN
76 void NaNRW();
77
1274add4 78 void PseudoTest_UseBigEndian() { ms_useBigEndianFormat = true; }
789ab840
VZ
79 void PseudoTest_UseLittleEndian() { ms_useBigEndianFormat = false; }
80#if wxUSE_APPLE_IEEE
81 void PseudoTest_UseIEEE754() { ms_useIEEE754 = true; }
82#endif // wxUSE_APPLE_IEEE
1274add4
VZ
83
84 static bool ms_useBigEndianFormat;
789ab840
VZ
85#if wxUSE_APPLE_IEEE
86 static bool ms_useIEEE754;
87#endif // wxUSE_APPLE_IEEE
1274add4 88
2f4bebe8
RN
89 DECLARE_NO_COPY_CLASS(DataStreamTestCase)
90};
91
92// register in the unnamed registry so that these tests are run by default
93CPPUNIT_TEST_SUITE_REGISTRATION( DataStreamTestCase );
94
e3778b4d 95// also include in its own registry so that these tests can be run alone
2f4bebe8
RN
96CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase, "DataStreamTestCase" );
97
1274add4 98bool DataStreamTestCase::ms_useBigEndianFormat = false;
789ab840
VZ
99#if wxUSE_APPLE_IEEE
100bool DataStreamTestCase::ms_useIEEE754 = false;
101#endif // wxUSE_APPLE_IEEE
1274add4 102
2f4bebe8
RN
103DataStreamTestCase::DataStreamTestCase()
104{
105}
106
1274add4 107wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue)
2f4bebe8 108{
393b6027
VZ
109 {
110 wxFileOutputStream pFileOutput( wxT("mytext.dat") );
111 wxDataOutputStream pDataOutput( pFileOutput );
1274add4
VZ
112 if ( ms_useBigEndianFormat )
113 pDataOutput.BigEndianOrdered(true);
2f4bebe8 114
789ab840
VZ
115#if wxUSE_APPLE_IEEE
116 if ( ms_useIEEE754 )
117 pDataOutput.UseBasicPrecisions();
118#endif // wxUSE_APPLE_IEEE
119
393b6027
VZ
120 pDataOutput << fValue;
121 }
2f4bebe8 122
393b6027
VZ
123 wxFileInputStream pFileInput( wxT("mytext.dat") );
124 wxDataInputStream pDataInput( pFileInput );
1274add4
VZ
125 if ( ms_useBigEndianFormat )
126 pDataInput.BigEndianOrdered(true);
2f4bebe8 127
789ab840
VZ
128#if wxUSE_APPLE_IEEE
129 if ( ms_useIEEE754 )
130 pDataInput.UseBasicPrecisions();
131#endif // wxUSE_APPLE_IEEE
132
2f4bebe8
RN
133 wxFloat64 fInFloat;
134
393b6027 135 pDataInput >> fInFloat;
2f4bebe8
RN
136
137 return fInFloat;
138}
139
216a72f3
VZ
140template <class T>
141class TestMultiRW {
142public:
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
147private:
148 bool m_ok;
149
150private:
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 {
9a83f860 159 wxFileOutputStream FileOutput( wxT("mytext.dat") );
216a72f3
VZ
160 wxDataOutputStream DataOutput( FileOutput );
161
162 (DataOutput.*pfnWriter)(Values, Size);
163 }
164
165 {
9a83f860 166 wxFileInputStream FileInput( wxT("mytext.dat") );
216a72f3
VZ
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
182public:
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
a1b806b9
DS
197 bool IsOk() const
198 {
216a72f3
VZ
199 return m_ok;
200 }
201};
202
203template <class T>
204static
205T TestRW(const T &Value)
206{
207 T InValue;
208
209 {
9a83f860 210 wxFileOutputStream FileOutput( wxT("mytext.dat") );
216a72f3
VZ
211 wxDataOutputStream DataOutput( FileOutput );
212
213 DataOutput << Value;
214 }
215
216 {
9a83f860 217 wxFileInputStream FileInput( wxT("mytext.dat") );
216a72f3
VZ
218 wxDataInputStream DataInput( FileInput );
219
220 DataInput >> InValue;
221 }
222
223 return InValue;
224}
225
2f4bebe8
RN
226void 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
234void DataStreamTestCase::DoubleRW()
235{
236 CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 );
237 CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 );
238}
239
216a72f3
VZ
240#if wxUSE_LONGLONG
241void 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) );
a1b806b9
DS
259 CPPUNIT_ASSERT( TestMultiRW<wxLongLong>(ValuesLL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() );
260 CPPUNIT_ASSERT( TestMultiRW<wxULongLong>(ValuesULL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() );
216a72f3
VZ
261}
262#endif
263
264#if wxHAS_INT64
265void 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) );
a1b806b9
DS
283 CPPUNIT_ASSERT( TestMultiRW<wxInt64>(ValuesI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() );
284 CPPUNIT_ASSERT( TestMultiRW<wxUint64>(ValuesUI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() );
216a72f3
VZ
285}
286#endif
287
2f4bebe8
RN
288void DataStreamTestCase::NaNRW()
289{
290 //TODO?
291}
292
293