]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/datastreamtest.cpp
8f48c285cf2542554129eaf928127e24b05241c1
[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 // 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 CPPUNIT_TEST_SUITE_END();
54
55 wxFloat64 TestFloatRW(wxFloat64 fValue);
56
57 void FloatRW();
58 void DoubleRW();
59 #if wxUSE_LONGLONG
60 void LongLongRW();
61 #endif
62 #if wxHAS_INT64
63 void Int64RW();
64 #endif
65 void NaNRW();
66
67 void PseudoTest_UseBigEndian() { ms_useBigEndianFormat = true; }
68
69 static bool ms_useBigEndianFormat;
70
71 DECLARE_NO_COPY_CLASS(DataStreamTestCase)
72 };
73
74 // register in the unnamed registry so that these tests are run by default
75 CPPUNIT_TEST_SUITE_REGISTRATION( DataStreamTestCase );
76
77 // also include in its own registry so that these tests can be run alone
78 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataStreamTestCase, "DataStreamTestCase" );
79
80 bool DataStreamTestCase::ms_useBigEndianFormat = false;
81
82 DataStreamTestCase::DataStreamTestCase()
83 {
84 }
85
86 wxFloat64 DataStreamTestCase::TestFloatRW(wxFloat64 fValue)
87 {
88 {
89 wxFileOutputStream pFileOutput( wxT("mytext.dat") );
90 wxDataOutputStream pDataOutput( pFileOutput );
91 if ( ms_useBigEndianFormat )
92 pDataOutput.BigEndianOrdered(true);
93
94 pDataOutput << fValue;
95 }
96
97 wxFileInputStream pFileInput( wxT("mytext.dat") );
98 wxDataInputStream pDataInput( pFileInput );
99 if ( ms_useBigEndianFormat )
100 pDataInput.BigEndianOrdered(true);
101
102 wxFloat64 fInFloat;
103
104 pDataInput >> fInFloat;
105
106 return fInFloat;
107 }
108
109 template <class T>
110 class TestMultiRW {
111 public:
112 typedef std::vector<T> ValueArray;
113 typedef void (wxDataOutputStream::*FnWriter)(const T *buffer, size_t size);
114 typedef void (wxDataInputStream::*FnReader)(T *buffer, size_t size);
115
116 private:
117 bool m_ok;
118
119 private:
120 void ProcessData(const T *Values,
121 typename ValueArray::size_type Size,
122 FnWriter pfnWriter,
123 FnReader pfnReader)
124 {
125 ValueArray InValues(Size);
126
127 {
128 wxFileOutputStream FileOutput( wxT("mytext.dat") );
129 wxDataOutputStream DataOutput( FileOutput );
130
131 (DataOutput.*pfnWriter)(Values, Size);
132 }
133
134 {
135 wxFileInputStream FileInput( wxT("mytext.dat") );
136 wxDataInputStream DataInput( FileInput );
137
138 (DataInput.*pfnReader)(&*InValues.begin(), InValues.size());
139 }
140
141 m_ok = true;
142 for (typename ValueArray::size_type idx=0; idx!=Size; ++idx) {
143 if (InValues[idx]!=Values[idx]) {
144 m_ok = false;
145 break;
146 }
147 }
148 }
149
150
151 public:
152 TestMultiRW(const T *Values,
153 size_t Size,
154 FnWriter pfnWriter,
155 FnReader pfnReader)
156 {
157 ProcessData(Values, (typename ValueArray::size_type) Size, pfnWriter, pfnReader);
158 }
159 TestMultiRW(const ValueArray &Values,
160 FnWriter pfnWriter,
161 FnReader pfnReader)
162 {
163 ProcessData(&*Values.begin(), Values.size(), pfnWriter, pfnReader);
164 }
165
166 bool IsOk() const
167 {
168 return m_ok;
169 }
170 };
171
172 template <class T>
173 static
174 T TestRW(const T &Value)
175 {
176 T InValue;
177
178 {
179 wxFileOutputStream FileOutput( wxT("mytext.dat") );
180 wxDataOutputStream DataOutput( FileOutput );
181
182 DataOutput << Value;
183 }
184
185 {
186 wxFileInputStream FileInput( wxT("mytext.dat") );
187 wxDataInputStream DataInput( FileInput );
188
189 DataInput >> InValue;
190 }
191
192 return InValue;
193 }
194
195 void DataStreamTestCase::FloatRW()
196 {
197 CPPUNIT_ASSERT( TestFloatRW(5.5) == 5.5 );
198 CPPUNIT_ASSERT( TestFloatRW(5) == 5 );
199 CPPUNIT_ASSERT( TestFloatRW(5.55) == 5.55 );
200 CPPUNIT_ASSERT( TestFloatRW(55555.555555) == 55555.555555 );
201 }
202
203 void DataStreamTestCase::DoubleRW()
204 {
205 CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 );
206 CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 );
207 }
208
209 #if wxUSE_LONGLONG
210 void DataStreamTestCase::LongLongRW()
211 {
212 TestMultiRW<wxLongLong>::ValueArray ValuesLL;
213 TestMultiRW<wxULongLong>::ValueArray ValuesULL;
214
215 ValuesLL.push_back(wxLongLong(0l));
216 ValuesLL.push_back(wxLongLong(1l));
217 ValuesLL.push_back(wxLongLong(-1l));
218 ValuesLL.push_back(wxLongLong(0x12345678l));
219 ValuesLL.push_back(wxLongLong(0x12345678l, 0xabcdef01l));
220
221 ValuesULL.push_back(wxULongLong(0l));
222 ValuesULL.push_back(wxULongLong(1l));
223 ValuesULL.push_back(wxULongLong(0x12345678l));
224 ValuesULL.push_back(wxULongLong(0x12345678l, 0xabcdef01l));
225
226 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l)) == wxLongLong(0x12345678l) );
227 CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l, 0xabcdef01l)) == wxLongLong(0x12345678l, 0xabcdef01l) );
228 CPPUNIT_ASSERT( TestMultiRW<wxLongLong>(ValuesLL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() );
229 CPPUNIT_ASSERT( TestMultiRW<wxULongLong>(ValuesULL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() );
230 }
231 #endif
232
233 #if wxHAS_INT64
234 void DataStreamTestCase::Int64RW()
235 {
236 TestMultiRW<wxInt64>::ValueArray ValuesI64;
237 TestMultiRW<wxUint64>::ValueArray ValuesUI64;
238
239 ValuesI64.push_back(wxInt64(0l));
240 ValuesI64.push_back(wxInt64(1l));
241 ValuesI64.push_back(wxInt64(-1l));
242 ValuesI64.push_back(wxInt64(0x12345678l));
243 ValuesI64.push_back((wxInt64(0x12345678l) << 32) + wxInt64(0xabcdef01l));
244
245 ValuesUI64.push_back(wxUint64(0l));
246 ValuesUI64.push_back(wxUint64(1l));
247 ValuesUI64.push_back(wxUint64(0x12345678l));
248 ValuesUI64.push_back((wxUint64(0x12345678l) << 32) + wxUint64(0xabcdef01l));
249
250 CPPUNIT_ASSERT( TestRW(wxUint64(0x12345678l)) == wxUint64(0x12345678l) );
251 CPPUNIT_ASSERT( TestRW((wxUint64(0x12345678l) << 32) + wxUint64(0xabcdef01l)) == (wxUint64(0x12345678l) << 32) + wxUint64(0xabcdef01l) );
252 CPPUNIT_ASSERT( TestMultiRW<wxInt64>(ValuesI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() );
253 CPPUNIT_ASSERT( TestMultiRW<wxUint64>(ValuesUI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() );
254 }
255 #endif
256
257 void DataStreamTestCase::NaNRW()
258 {
259 //TODO?
260 }
261
262