]> git.saurik.com Git - wxWidgets.git/blob - tests/longlong/longlongtest.cpp
No real changes, just fix a syntax error in unused file.
[wxWidgets.git] / tests / longlong / longlongtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/longlong/longlong.cpp
3 // Purpose: wxLongLong unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
5 // Created: 2004-04-01
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
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 "wx/longlong.h"
25 #include "wx/timer.h"
26
27 #if wxUSE_LONGLONG
28
29 // ----------------------------------------------------------------------------
30 // helpers for testing
31 // ----------------------------------------------------------------------------
32
33 // number of iterations in loops
34 #define ITEMS 1000
35
36 // make a 64 bit number from 4 16 bit ones
37 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
38
39 // get a random 64 bit number
40 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
41
42 static const long testLongs[] =
43 {
44 0,
45 1,
46 -1,
47 LONG_MAX,
48 LONG_MIN,
49 0x1234,
50 -0x1234
51 };
52
53 // ----------------------------------------------------------------------------
54 // test class
55 // ----------------------------------------------------------------------------
56
57 class LongLongTestCase : public CppUnit::TestCase
58 {
59 public:
60 LongLongTestCase();
61
62 private:
63 CPPUNIT_TEST_SUITE( LongLongTestCase );
64 CPPUNIT_TEST( Conversion );
65 CPPUNIT_TEST( Comparison );
66 CPPUNIT_TEST( Addition );
67 CPPUNIT_TEST( Multiplication );
68 CPPUNIT_TEST( Division );
69 CPPUNIT_TEST( BitOperations );
70 CPPUNIT_TEST( ToString );
71 CPPUNIT_TEST( LoHi );
72 CPPUNIT_TEST_SUITE_END();
73
74 void Conversion();
75 void Comparison();
76 void Addition();
77 void Multiplication();
78 void Division();
79 void BitOperations();
80 void ToString();
81 void LoHi();
82
83 DECLARE_NO_COPY_CLASS(LongLongTestCase)
84 };
85
86 // register in the unnamed registry so that these tests are run by default
87 CPPUNIT_TEST_SUITE_REGISTRATION( LongLongTestCase );
88
89 // also include in it's own registry so that these tests can be run alone
90 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LongLongTestCase, "LongLongTestCase" );
91
92 LongLongTestCase::LongLongTestCase()
93 {
94 srand((unsigned)time(NULL));
95 }
96
97 void LongLongTestCase::Conversion()
98 {
99 for ( size_t n = 0; n < ITEMS; n++ )
100 {
101 wxLongLong a = RAND_LL();
102
103 wxLongLong b(a.GetHi(), a.GetLo());
104 CPPUNIT_ASSERT( a == b );
105
106 #if wxUSE_LONGLONG_WX
107 wxLongLongWx c(a.GetHi(), a.GetLo());
108 CPPUNIT_ASSERT( a == c );
109 #endif
110
111 #if wxUSE_LONGLONG_NATIVE
112 wxLongLongNative d(a.GetHi(), a.GetLo());
113 CPPUNIT_ASSERT( a == d );
114 #endif
115 }
116 }
117
118 void LongLongTestCase::Comparison()
119 {
120 static const long ls[2] =
121 {
122 0x1234,
123 -0x1234,
124 };
125
126 wxLongLong lls[2];
127 lls[0] = ls[0];
128 lls[1] = ls[1];
129
130 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
131 {
132 for ( size_t m = 0; m < WXSIZEOF(lls); m++ )
133 {
134 CPPUNIT_ASSERT( (lls[m] < testLongs[n]) == (ls[m] < testLongs[n]) );
135 CPPUNIT_ASSERT( (lls[m] > testLongs[n]) == (ls[m] > testLongs[n]) );
136 CPPUNIT_ASSERT( (lls[m] <= testLongs[n]) == (ls[m] <= testLongs[n]) );
137 CPPUNIT_ASSERT( (lls[m] >= testLongs[n]) == (ls[m] >= testLongs[n]) );
138 CPPUNIT_ASSERT( (lls[m] != testLongs[n]) == (ls[m] != testLongs[n]) );
139 CPPUNIT_ASSERT( (lls[m] == testLongs[n]) == (ls[m] == testLongs[n]) );
140 }
141 }
142 }
143
144 void LongLongTestCase::Addition()
145 {
146 for ( size_t n = 0; n < ITEMS; n++ )
147 {
148 wxLongLong a = RAND_LL();
149 wxLongLong b = RAND_LL();
150 wxLongLong c = a + b;
151
152 #if wxUSE_LONGLONG_NATIVE
153 wxLongLongNative a1 = a;
154 wxLongLongNative b1 = b;
155 wxLongLongNative c1 = a1 + b1;
156 CPPUNIT_ASSERT( c == c1 );
157 #endif
158
159 #if wxUSE_LONGLONG_WX
160 wxLongLongWx a2 = a;
161 wxLongLongWx b2 = b;
162 wxLongLongWx c2 = a2 + b2;
163 CPPUNIT_ASSERT( c == c2 );
164 #endif
165 }
166 }
167
168 void LongLongTestCase::Multiplication()
169 {
170 for ( size_t n = 0; n < ITEMS; n++ )
171 {
172 wxLongLong a = RAND_LL();
173 wxLongLong b = RAND_LL();
174 wxLongLong c = a*b;
175
176 wxLongLong a1(a.GetHi(), a.GetLo());
177 wxLongLong b1(b.GetHi(), b.GetLo());
178 wxLongLong c1 = a1*b1;
179 CPPUNIT_ASSERT( c1 == c );
180
181 #if wxUSE_LONGLONG_WX
182 wxLongLongWx a2(a.GetHi(), a.GetLo());
183 wxLongLongWx b2(b.GetHi(), b.GetLo());
184 wxLongLongWx c2 = a2*b2;
185 CPPUNIT_ASSERT( c2 == c );
186 #endif
187
188 #if wxUSE_LONGLONG_NATIVE
189 wxLongLongNative a3(a.GetHi(), a.GetLo());
190 wxLongLongNative b3(b.GetHi(), b.GetLo());
191 wxLongLongNative c3 = a3*b3;
192 CPPUNIT_ASSERT( c3 == c );
193 #endif
194 }
195 }
196
197 void LongLongTestCase::Division()
198 {
199 for ( size_t n = 0; n < ITEMS; n++ )
200 {
201 // get a random wxLongLong (shifting by 12 the MSB ensures that the
202 // multiplication will not overflow)
203 wxLongLong a = MAKE_LL((rand() >> 12), rand(), rand(), rand());
204
205 // get a random (but non null) long (not wxLongLong for now) divider
206 long l;
207 do
208 {
209 l = rand();
210 }
211 while ( !l );
212
213 wxLongLong q = a / l;
214 wxLongLong r = a % l;
215
216 CPPUNIT_ASSERT( a == ( q * l + r ) );
217
218 #if wxUSE_LONGLONG_WX
219 wxLongLongWx a1(a.GetHi(), a.GetLo());
220 wxLongLongWx q1 = a1 / l;
221 wxLongLongWx r1 = a1 % l;
222 CPPUNIT_ASSERT( q == q1 );
223 CPPUNIT_ASSERT( r == r1 );
224 CPPUNIT_ASSERT( a1 == ( q1 * l + r1 ) );
225 #endif
226
227 #if wxUSE_LONGLONG_NATIVE
228 wxLongLongNative a2(a.GetHi(), a.GetLo());
229 wxLongLongNative q2 = a2 / l;
230 wxLongLongNative r2 = a2 % l;
231 CPPUNIT_ASSERT( q == q2 );
232 CPPUNIT_ASSERT( r == r2 );
233 CPPUNIT_ASSERT( a2 == ( q2 * l + r2 ) );
234 #endif
235 }
236 }
237
238 void LongLongTestCase::BitOperations()
239 {
240 for ( size_t m = 0; m < ITEMS; m++ )
241 {
242 wxLongLong a = RAND_LL();
243
244 for ( size_t n = 0; n < 33; n++ )
245 {
246 wxLongLong b(a.GetHi(), a.GetLo()), c, d = b, e;
247 d >>= n;
248 c = b >> n;
249 CPPUNIT_ASSERT( c == d );
250 d <<= n;
251 e = c << n;
252 CPPUNIT_ASSERT( d == e );
253
254 #if wxUSE_LONGLONG_WX
255 wxLongLongWx b1(a.GetHi(), a.GetLo()), c1, d1 = b1, e1;
256 d1 >>= n;
257 c1 = b1 >> n;
258 CPPUNIT_ASSERT( c1 == d1 );
259 d1 <<= n;
260 e1 = c1 << n;
261 CPPUNIT_ASSERT( d1 == e1 );
262 #endif
263
264 #if wxUSE_LONGLONG_NATIVE
265 wxLongLongNative b2(a.GetHi(), a.GetLo()), c2, d2 = b2, e2;
266 d2 >>= n;
267 c2 = b2 >> n;
268 CPPUNIT_ASSERT( c2 == d2 );
269 d2 <<= n;
270 e2 = c2 << n;
271 CPPUNIT_ASSERT( d2 == e2 );
272 #endif
273 }
274 }
275 }
276
277 void LongLongTestCase::ToString()
278 {
279 wxString s1, s2;
280
281 for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
282 {
283 wxLongLong a = testLongs[n];
284 s1 = wxString::Format(wxT("%ld"), testLongs[n]);
285 s2 = a.ToString();
286 CPPUNIT_ASSERT( s1 == s2 );
287
288 s2 = wxEmptyString;
289 s2 << a;
290 CPPUNIT_ASSERT( s1 == s2 );
291
292 #if wxUSE_LONGLONG_WX
293 wxLongLongWx a1 = testLongs[n];
294 s2 = a1.ToString();
295 CPPUNIT_ASSERT( s1 == s2 );
296 #endif
297
298 #if wxUSE_LONGLONG_NATIVE
299 wxLongLongNative a2 = testLongs[n];
300 s2 = a2.ToString();
301 CPPUNIT_ASSERT( s1 == s2 );
302 #endif
303 }
304
305 wxLongLong a(0x12345678, 0x87654321);
306 CPPUNIT_ASSERT( a.ToString() == wxT("1311768467139281697") );
307 a.Negate();
308 CPPUNIT_ASSERT( a.ToString() == wxT("-1311768467139281697") );
309
310 wxLongLong llMin(-2147483647L - 1L, 0);
311 CPPUNIT_ASSERT( llMin.ToString() == wxT("-9223372036854775808") );
312
313 #if wxUSE_LONGLONG_WX
314 wxLongLongWx a1(a.GetHi(), a.GetLo());
315 CPPUNIT_ASSERT( a1.ToString() == wxT("-1311768467139281697") );
316 a1.Negate();
317 CPPUNIT_ASSERT( a1.ToString() == wxT("1311768467139281697") );
318 #endif
319
320 #if wxUSE_LONGLONG_NATIVE
321 wxLongLongNative a2(a.GetHi(), a.GetLo());
322 CPPUNIT_ASSERT( a2.ToString() == wxT("-1311768467139281697") );
323 a2.Negate();
324 CPPUNIT_ASSERT( a2.ToString() == wxT("1311768467139281697") );
325 #endif
326
327 }
328
329 void LongLongTestCase::LoHi()
330 {
331 wxLongLong ll(123, 456);
332 CPPUNIT_ASSERT_EQUAL( 456u, ll.GetLo() );
333 CPPUNIT_ASSERT_EQUAL( 123, ll.GetHi() );
334
335 wxULongLong ull(987, 654);
336 CPPUNIT_ASSERT_EQUAL( 654u, ull.GetLo() );
337 CPPUNIT_ASSERT_EQUAL( 987u, ull.GetHi() );
338 }
339
340 #endif // wxUSE_LONGLONG