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