]>
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
6 // Copyright: (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/longlong.h"
28 // ----------------------------------------------------------------------------
29 // helpers for testing
30 // ----------------------------------------------------------------------------
32 // number of iterations in loops
35 // make a 64 bit number from 4 16 bit ones
36 #define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
38 // get a random 64 bit number
39 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
41 static const long testLongs
[] =
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 class LongLongTestCase
: public CppUnit::TestCase
62 CPPUNIT_TEST_SUITE( LongLongTestCase
);
63 CPPUNIT_TEST( Conversion
);
64 CPPUNIT_TEST( Comparison
);
65 CPPUNIT_TEST( Addition
);
66 CPPUNIT_TEST( Multiplication
);
67 CPPUNIT_TEST( Division
);
68 CPPUNIT_TEST( BitOperations
);
69 CPPUNIT_TEST( ToString
);
71 CPPUNIT_TEST( Limits
);
72 CPPUNIT_TEST_SUITE_END();
77 void Multiplication();
84 DECLARE_NO_COPY_CLASS(LongLongTestCase
)
87 // register in the unnamed registry so that these tests are run by default
88 CPPUNIT_TEST_SUITE_REGISTRATION( LongLongTestCase
);
90 // also include in its own registry so that these tests can be run alone
91 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LongLongTestCase
, "LongLongTestCase" );
93 LongLongTestCase::LongLongTestCase()
95 srand((unsigned)time(NULL
));
98 void LongLongTestCase::Conversion()
100 for ( size_t n
= 0; n
< ITEMS
; n
++ )
102 wxLongLong a
= RAND_LL();
104 wxLongLong
b(a
.GetHi(), a
.GetLo());
105 CPPUNIT_ASSERT( a
== b
);
107 #if wxUSE_LONGLONG_WX
108 wxLongLongWx
c(a
.GetHi(), a
.GetLo());
109 CPPUNIT_ASSERT( a
== c
);
112 #if wxUSE_LONGLONG_NATIVE
113 wxLongLongNative
d(a
.GetHi(), a
.GetLo());
114 CPPUNIT_ASSERT( a
== d
);
119 void LongLongTestCase::Comparison()
121 static const long ls
[2] =
131 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
133 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
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 CPPUNIT_ASSERT( (lls
[m
] == testLongs
[n
]) == (ls
[m
] == testLongs
[n
]) );
145 void LongLongTestCase::Addition()
147 for ( size_t n
= 0; n
< ITEMS
; n
++ )
149 wxLongLong a
= RAND_LL();
150 wxLongLong b
= RAND_LL();
151 wxLongLong c
= a
+ b
;
153 #if wxUSE_LONGLONG_NATIVE
154 wxLongLongNative a1
= a
;
155 wxLongLongNative b1
= b
;
156 wxLongLongNative c1
= a1
+ b1
;
157 CPPUNIT_ASSERT( c
== c1
);
160 #if wxUSE_LONGLONG_WX
163 wxLongLongWx c2
= a2
+ b2
;
164 CPPUNIT_ASSERT( c
== c2
);
169 void LongLongTestCase::Multiplication()
171 for ( size_t n
= 0; n
< ITEMS
; n
++ )
173 wxLongLong a
= RAND_LL();
174 wxLongLong b
= RAND_LL();
177 wxLongLong
a1(a
.GetHi(), a
.GetLo());
178 wxLongLong
b1(b
.GetHi(), b
.GetLo());
179 wxLongLong c1
= a1
*b1
;
180 CPPUNIT_ASSERT( c1
== c
);
182 #if wxUSE_LONGLONG_WX
183 wxLongLongWx
a2(a
.GetHi(), a
.GetLo());
184 wxLongLongWx
b2(b
.GetHi(), b
.GetLo());
185 wxLongLongWx c2
= a2
*b2
;
186 CPPUNIT_ASSERT( c2
== c
);
189 #if wxUSE_LONGLONG_NATIVE
190 wxLongLongNative
a3(a
.GetHi(), a
.GetLo());
191 wxLongLongNative
b3(b
.GetHi(), b
.GetLo());
192 wxLongLongNative c3
= a3
*b3
;
193 CPPUNIT_ASSERT( c3
== c
);
198 void LongLongTestCase::Division()
200 for ( size_t n
= 0; n
< ITEMS
; n
++ )
202 // get a random wxLongLong (shifting by 12 the MSB ensures that the
203 // multiplication will not overflow)
204 wxLongLong a
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
206 // get a random (but non null) long (not wxLongLong for now) divider
214 wxLongLong q
= a
/ l
;
215 wxLongLong r
= a
% l
;
217 CPPUNIT_ASSERT( a
== ( q
* l
+ r
) );
219 #if wxUSE_LONGLONG_WX
220 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
221 wxLongLongWx q1
= a1
/ l
;
222 wxLongLongWx r1
= a1
% l
;
223 CPPUNIT_ASSERT( q
== q1
);
224 CPPUNIT_ASSERT( r
== r1
);
225 CPPUNIT_ASSERT( a1
== ( q1
* l
+ r1
) );
228 #if wxUSE_LONGLONG_NATIVE
229 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
230 wxLongLongNative q2
= a2
/ l
;
231 wxLongLongNative r2
= a2
% l
;
232 CPPUNIT_ASSERT( q
== q2
);
233 CPPUNIT_ASSERT( r
== r2
);
234 CPPUNIT_ASSERT( a2
== ( q2
* l
+ r2
) );
239 void LongLongTestCase::BitOperations()
241 for ( size_t m
= 0; m
< ITEMS
; m
++ )
243 wxLongLong a
= RAND_LL();
245 for ( size_t n
= 0; n
< 33; n
++ )
247 wxLongLong
b(a
.GetHi(), a
.GetLo()), c
, d
= b
, e
;
250 CPPUNIT_ASSERT( c
== d
);
253 CPPUNIT_ASSERT( d
== e
);
255 #if wxUSE_LONGLONG_WX
256 wxLongLongWx
b1(a
.GetHi(), a
.GetLo()), c1
, d1
= b1
, e1
;
259 CPPUNIT_ASSERT( c1
== d1
);
262 CPPUNIT_ASSERT( d1
== e1
);
265 #if wxUSE_LONGLONG_NATIVE
266 wxLongLongNative
b2(a
.GetHi(), a
.GetLo()), c2
, d2
= b2
, e2
;
269 CPPUNIT_ASSERT( c2
== d2
);
272 CPPUNIT_ASSERT( d2
== e2
);
278 void LongLongTestCase::ToString()
282 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
284 wxLongLong a
= testLongs
[n
];
285 s1
= wxString::Format(wxT("%ld"), testLongs
[n
]);
287 CPPUNIT_ASSERT( s1
== s2
);
291 CPPUNIT_ASSERT( s1
== s2
);
293 #if wxUSE_LONGLONG_WX
294 wxLongLongWx a1
= testLongs
[n
];
296 CPPUNIT_ASSERT( s1
== s2
);
299 #if wxUSE_LONGLONG_NATIVE
300 wxLongLongNative a2
= testLongs
[n
];
302 CPPUNIT_ASSERT( s1
== s2
);
306 wxLongLong
a(0x12345678, 0x87654321);
307 CPPUNIT_ASSERT( a
.ToString() == wxT("1311768467139281697") );
309 CPPUNIT_ASSERT( a
.ToString() == wxT("-1311768467139281697") );
311 wxLongLong
llMin(-2147483647L - 1L, 0);
312 CPPUNIT_ASSERT( llMin
.ToString() == wxT("-9223372036854775808") );
314 #if wxUSE_LONGLONG_WX
315 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
316 CPPUNIT_ASSERT( a1
.ToString() == wxT("-1311768467139281697") );
318 CPPUNIT_ASSERT( a1
.ToString() == wxT("1311768467139281697") );
321 #if wxUSE_LONGLONG_NATIVE
322 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
323 CPPUNIT_ASSERT( a2
.ToString() == wxT("-1311768467139281697") );
325 CPPUNIT_ASSERT( a2
.ToString() == wxT("1311768467139281697") );
330 void LongLongTestCase::LoHi()
332 wxLongLong
ll(123, 456);
333 CPPUNIT_ASSERT_EQUAL( 456u, ll
.GetLo() );
334 CPPUNIT_ASSERT_EQUAL( 123, ll
.GetHi() );
336 wxULongLong
ull(987, 654);
337 CPPUNIT_ASSERT_EQUAL( 654u, ull
.GetLo() );
338 CPPUNIT_ASSERT_EQUAL( 987u, ull
.GetHi() );
341 void LongLongTestCase::Limits()
343 // VC6 doesn't specialize numeric_limits<> for __int64 so skip this test
346 #if wxUSE_LONGLONG_NATIVE
347 CPPUNIT_ASSERT( std::numeric_limits
<wxLongLong
>::is_specialized
);
348 CPPUNIT_ASSERT( std::numeric_limits
<wxULongLong
>::is_specialized
);
350 wxULongLong maxval
= std::numeric_limits
<wxULongLong
>::max();
351 CPPUNIT_ASSERT( maxval
.ToDouble() > 0 );
352 #endif // wxUSE_LONGLONG_NATIVE
353 #endif // !__VISUALC6__
356 #endif // wxUSE_LONGLONG