]>
git.saurik.com Git - wxWidgets.git/blob - tests/longlong/longlongtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/longlong/longlong.cpp
3 // Purpose: wxLongLong unit test
4 // Author: Vadim Zeitlin, Wlodzimierz ABX Skiba
7 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/longlong.h"
29 // ----------------------------------------------------------------------------
30 // helpers for testing
31 // ----------------------------------------------------------------------------
33 // number of iterations in loops
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)
39 // get a random 64 bit number
40 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
42 static const long testLongs
[] =
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class LongLongTestCase
: public CppUnit::TestCase
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
);
72 CPPUNIT_TEST_SUITE_END();
77 void Multiplication();
83 DECLARE_NO_COPY_CLASS(LongLongTestCase
)
86 // register in the unnamed registry so that these tests are run by default
87 CPPUNIT_TEST_SUITE_REGISTRATION( LongLongTestCase
);
89 // also include in it's own registry so that these tests can be run alone
90 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LongLongTestCase
, "LongLongTestCase" );
92 LongLongTestCase::LongLongTestCase()
94 srand((unsigned)time(NULL
));
97 void LongLongTestCase::Conversion()
99 for ( size_t n
= 0; n
< ITEMS
; n
++ )
101 wxLongLong a
= RAND_LL();
103 wxLongLong
b(a
.GetHi(), a
.GetLo());
104 CPPUNIT_ASSERT( a
== b
);
106 #if wxUSE_LONGLONG_WX
107 wxLongLongWx
c(a
.GetHi(), a
.GetLo());
108 CPPUNIT_ASSERT( a
== c
);
111 #if wxUSE_LONGLONG_NATIVE
112 wxLongLongNative
d(a
.GetHi(), a
.GetLo());
113 CPPUNIT_ASSERT( a
== d
);
118 void LongLongTestCase::Comparison()
120 static const long ls
[2] =
130 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
132 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
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
]) );
144 void LongLongTestCase::Addition()
146 for ( size_t n
= 0; n
< ITEMS
; n
++ )
148 wxLongLong a
= RAND_LL();
149 wxLongLong b
= RAND_LL();
150 wxLongLong c
= a
+ b
;
152 #if wxUSE_LONGLONG_NATIVE
153 wxLongLongNative a1
= a
;
154 wxLongLongNative b1
= b
;
155 wxLongLongNative c1
= a1
+ b1
;
156 CPPUNIT_ASSERT( c
== c1
);
159 #if wxUSE_LONGLONG_WX
162 wxLongLongWx c2
= a2
+ b2
;
163 CPPUNIT_ASSERT( c
== c2
);
168 void LongLongTestCase::Multiplication()
170 for ( size_t n
= 0; n
< ITEMS
; n
++ )
172 wxLongLong a
= RAND_LL();
173 wxLongLong b
= RAND_LL();
176 wxLongLong
a1(a
.GetHi(), a
.GetLo());
177 wxLongLong
b1(b
.GetHi(), b
.GetLo());
178 wxLongLong c1
= a1
*b1
;
179 CPPUNIT_ASSERT( c1
== c
);
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
);
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
);
197 void LongLongTestCase::Division()
199 for ( size_t n
= 0; n
< ITEMS
; n
++ )
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());
205 // get a random (but non null) long (not wxLongLong for now) divider
213 wxLongLong q
= a
/ l
;
214 wxLongLong r
= a
% l
;
216 CPPUNIT_ASSERT( a
== ( q
* l
+ r
) );
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
) );
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
) );
238 void LongLongTestCase::BitOperations()
240 for ( size_t n
= 0; n
< ITEMS
; n
++ )
242 wxLongLong a
= RAND_LL();
244 for ( size_t n
= 0; n
< 33; n
++ )
246 wxLongLong
b(a
.GetHi(), a
.GetLo()), c
, d
= b
, e
;
249 CPPUNIT_ASSERT( c
== d
);
252 CPPUNIT_ASSERT( d
== e
);
254 #if wxUSE_LONGLONG_WX
255 wxLongLongWx
b1(a
.GetHi(), a
.GetLo()), c1
, d1
= b1
, e1
;
258 CPPUNIT_ASSERT( c1
== d1
);
261 CPPUNIT_ASSERT( d1
== e1
);
264 #if wxUSE_LONGLONG_NATIVE
265 wxLongLongNative
b2(a
.GetHi(), a
.GetLo()), c2
, d2
= b2
, e2
;
268 CPPUNIT_ASSERT( c2
== d2
);
271 CPPUNIT_ASSERT( d2
== e2
);
277 void LongLongTestCase::ToString()
281 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
283 wxLongLong a
= testLongs
[n
];
284 s1
= wxString::Format(_T("%ld"), testLongs
[n
]);
286 CPPUNIT_ASSERT( s1
== s2
);
290 CPPUNIT_ASSERT( s1
== s2
);
292 #if wxUSE_LONGLONG_WX
293 wxLongLongWx a1
= testLongs
[n
];
295 CPPUNIT_ASSERT( s1
== s2
);
298 #if wxUSE_LONGLONG_NATIVE
299 wxLongLongNative a2
= testLongs
[n
];
301 CPPUNIT_ASSERT( s1
== s2
);
305 wxLongLong
a(0x12345678, 0x87654321);
306 CPPUNIT_ASSERT( a
.ToString() == _T("1311768467139281697") );
308 CPPUNIT_ASSERT( a
.ToString() == _T("-1311768467139281697") );
310 wxLongLong
llMin(-2147483647L - 1L, 0);
311 CPPUNIT_ASSERT( llMin
.ToString() == _T("-9223372036854775808") );
313 #if wxUSE_LONGLONG_WX
314 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
315 CPPUNIT_ASSERT( a1
.ToString() == _T("-1311768467139281697") );
317 CPPUNIT_ASSERT( a1
.ToString() == _T("1311768467139281697") );
320 #if wxUSE_LONGLONG_NATIVE
321 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
322 CPPUNIT_ASSERT( a2
.ToString() == _T("-1311768467139281697") );
324 CPPUNIT_ASSERT( a2
.ToString() == _T("1311768467139281697") );
329 void LongLongTestCase::LoHi()
331 wxLongLong
ll(123, 456);
332 CPPUNIT_ASSERT_EQUAL( 456u, ll
.GetLo() );
333 CPPUNIT_ASSERT_EQUAL( 123, ll
.GetHi() );
335 wxULongLong
ull(987, 654);
336 CPPUNIT_ASSERT_EQUAL( 654u, ull
.GetLo() );
337 CPPUNIT_ASSERT_EQUAL( 987u, ull
.GetHi() );
340 #endif // wxUSE_LONGLONG