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