]>
Commit | Line | Data |
---|---|---|
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 its 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 IsOk() const | |
157 | { | |
158 | return m_ok; | |
159 | } | |
160 | }; | |
161 | ||
162 | template <class T> | |
163 | static | |
164 | T TestRW(const T &Value) | |
165 | { | |
166 | T InValue; | |
167 | ||
168 | { | |
169 | wxFileOutputStream FileOutput( wxT("mytext.dat") ); | |
170 | wxDataOutputStream DataOutput( FileOutput ); | |
171 | ||
172 | DataOutput << Value; | |
173 | } | |
174 | ||
175 | { | |
176 | wxFileInputStream FileInput( wxT("mytext.dat") ); | |
177 | wxDataInputStream DataInput( FileInput ); | |
178 | ||
179 | DataInput >> InValue; | |
180 | } | |
181 | ||
182 | return InValue; | |
183 | } | |
184 | ||
185 | void DataStreamTestCase::FloatRW() | |
186 | { | |
187 | CPPUNIT_ASSERT( TestFloatRW(5.5) == 5.5 ); | |
188 | CPPUNIT_ASSERT( TestFloatRW(5) == 5 ); | |
189 | CPPUNIT_ASSERT( TestFloatRW(5.55) == 5.55 ); | |
190 | CPPUNIT_ASSERT( TestFloatRW(55555.555555) == 55555.555555 ); | |
191 | } | |
192 | ||
193 | void DataStreamTestCase::DoubleRW() | |
194 | { | |
195 | CPPUNIT_ASSERT( TestFloatRW(2132131.1232132) == 2132131.1232132 ); | |
196 | CPPUNIT_ASSERT( TestFloatRW(21321343431.1232143432) == 21321343431.1232143432 ); | |
197 | } | |
198 | ||
199 | #if wxUSE_LONGLONG | |
200 | void DataStreamTestCase::LongLongRW() | |
201 | { | |
202 | TestMultiRW<wxLongLong>::ValueArray ValuesLL; | |
203 | TestMultiRW<wxULongLong>::ValueArray ValuesULL; | |
204 | ||
205 | ValuesLL.push_back(wxLongLong(0l)); | |
206 | ValuesLL.push_back(wxLongLong(1l)); | |
207 | ValuesLL.push_back(wxLongLong(-1l)); | |
208 | ValuesLL.push_back(wxLongLong(0x12345678l)); | |
209 | ValuesLL.push_back(wxLongLong(0x12345678l, 0xabcdef01l)); | |
210 | ||
211 | ValuesULL.push_back(wxULongLong(0l)); | |
212 | ValuesULL.push_back(wxULongLong(1l)); | |
213 | ValuesULL.push_back(wxULongLong(0x12345678l)); | |
214 | ValuesULL.push_back(wxULongLong(0x12345678l, 0xabcdef01l)); | |
215 | ||
216 | CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l)) == wxLongLong(0x12345678l) ); | |
217 | CPPUNIT_ASSERT( TestRW(wxLongLong(0x12345678l, 0xabcdef01l)) == wxLongLong(0x12345678l, 0xabcdef01l) ); | |
218 | CPPUNIT_ASSERT( TestMultiRW<wxLongLong>(ValuesLL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() ); | |
219 | CPPUNIT_ASSERT( TestMultiRW<wxULongLong>(ValuesULL, &wxDataOutputStream::WriteLL, &wxDataInputStream::ReadLL).IsOk() ); | |
220 | } | |
221 | #endif | |
222 | ||
223 | #if wxHAS_INT64 | |
224 | void DataStreamTestCase::Int64RW() | |
225 | { | |
226 | TestMultiRW<wxInt64>::ValueArray ValuesI64; | |
227 | TestMultiRW<wxUint64>::ValueArray ValuesUI64; | |
228 | ||
229 | ValuesI64.push_back(wxInt64(0l)); | |
230 | ValuesI64.push_back(wxInt64(1l)); | |
231 | ValuesI64.push_back(wxInt64(-1l)); | |
232 | ValuesI64.push_back(wxInt64(0x12345678l)); | |
233 | ValuesI64.push_back((wxInt64(0x12345678l) << 32) + wxInt64(0xabcdef01l)); | |
234 | ||
235 | ValuesUI64.push_back(wxUint64(0l)); | |
236 | ValuesUI64.push_back(wxUint64(1l)); | |
237 | ValuesUI64.push_back(wxUint64(0x12345678l)); | |
238 | ValuesUI64.push_back((wxUint64(0x12345678l) << 32) + wxUint64(0xabcdef01l)); | |
239 | ||
240 | CPPUNIT_ASSERT( TestRW(wxUint64(0x12345678l)) == wxUint64(0x12345678l) ); | |
241 | CPPUNIT_ASSERT( TestRW((wxUint64(0x12345678l) << 32) + wxUint64(0xabcdef01l)) == (wxUint64(0x12345678l) << 32) + wxUint64(0xabcdef01l) ); | |
242 | CPPUNIT_ASSERT( TestMultiRW<wxInt64>(ValuesI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() ); | |
243 | CPPUNIT_ASSERT( TestMultiRW<wxUint64>(ValuesUI64, &wxDataOutputStream::Write64, &wxDataInputStream::Read64).IsOk() ); | |
244 | } | |
245 | #endif | |
246 | ||
247 | void DataStreamTestCase::NaNRW() | |
248 | { | |
249 | //TODO? | |
250 | } | |
251 | ||
252 |