]>
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( Limits
);
73 CPPUNIT_TEST_SUITE_END();
78 void Multiplication();
85 DECLARE_NO_COPY_CLASS(LongLongTestCase
)
88 // register in the unnamed registry so that these tests are run by default
89 CPPUNIT_TEST_SUITE_REGISTRATION( LongLongTestCase
);
91 // also include in its own registry so that these tests can be run alone
92 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LongLongTestCase
, "LongLongTestCase" );
94 LongLongTestCase::LongLongTestCase()
96 srand((unsigned)time(NULL
));
99 void LongLongTestCase::Conversion()
101 for ( size_t n
= 0; n
< ITEMS
; n
++ )
103 wxLongLong a
= RAND_LL();
105 wxLongLong
b(a
.GetHi(), a
.GetLo());
106 CPPUNIT_ASSERT( a
== b
);
108 #if wxUSE_LONGLONG_WX
109 wxLongLongWx
c(a
.GetHi(), a
.GetLo());
110 CPPUNIT_ASSERT( a
== c
);
113 #if wxUSE_LONGLONG_NATIVE
114 wxLongLongNative
d(a
.GetHi(), a
.GetLo());
115 CPPUNIT_ASSERT( a
== d
);
120 void LongLongTestCase::Comparison()
122 static const long ls
[2] =
132 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
134 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
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
]) );
141 CPPUNIT_ASSERT( (lls
[m
] == testLongs
[n
]) == (ls
[m
] == testLongs
[n
]) );
146 void LongLongTestCase::Addition()
148 for ( size_t n
= 0; n
< ITEMS
; n
++ )
150 wxLongLong a
= RAND_LL();
151 wxLongLong b
= RAND_LL();
152 wxLongLong c
= a
+ b
;
154 #if wxUSE_LONGLONG_NATIVE
155 wxLongLongNative a1
= a
;
156 wxLongLongNative b1
= b
;
157 wxLongLongNative c1
= a1
+ b1
;
158 CPPUNIT_ASSERT( c
== c1
);
161 #if wxUSE_LONGLONG_WX
164 wxLongLongWx c2
= a2
+ b2
;
165 CPPUNIT_ASSERT( c
== c2
);
170 void LongLongTestCase::Multiplication()
172 for ( size_t n
= 0; n
< ITEMS
; n
++ )
174 wxLongLong a
= RAND_LL();
175 wxLongLong b
= RAND_LL();
178 wxLongLong
a1(a
.GetHi(), a
.GetLo());
179 wxLongLong
b1(b
.GetHi(), b
.GetLo());
180 wxLongLong c1
= a1
*b1
;
181 CPPUNIT_ASSERT( c1
== c
);
183 #if wxUSE_LONGLONG_WX
184 wxLongLongWx
a2(a
.GetHi(), a
.GetLo());
185 wxLongLongWx
b2(b
.GetHi(), b
.GetLo());
186 wxLongLongWx c2
= a2
*b2
;
187 CPPUNIT_ASSERT( c2
== c
);
190 #if wxUSE_LONGLONG_NATIVE
191 wxLongLongNative
a3(a
.GetHi(), a
.GetLo());
192 wxLongLongNative
b3(b
.GetHi(), b
.GetLo());
193 wxLongLongNative c3
= a3
*b3
;
194 CPPUNIT_ASSERT( c3
== c
);
199 void LongLongTestCase::Division()
201 for ( size_t n
= 0; n
< ITEMS
; n
++ )
203 // get a random wxLongLong (shifting by 12 the MSB ensures that the
204 // multiplication will not overflow)
205 wxLongLong a
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
207 // get a random (but non null) long (not wxLongLong for now) divider
215 wxLongLong q
= a
/ l
;
216 wxLongLong r
= a
% l
;
218 CPPUNIT_ASSERT( a
== ( q
* l
+ r
) );
220 #if wxUSE_LONGLONG_WX
221 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
222 wxLongLongWx q1
= a1
/ l
;
223 wxLongLongWx r1
= a1
% l
;
224 CPPUNIT_ASSERT( q
== q1
);
225 CPPUNIT_ASSERT( r
== r1
);
226 CPPUNIT_ASSERT( a1
== ( q1
* l
+ r1
) );
229 #if wxUSE_LONGLONG_NATIVE
230 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
231 wxLongLongNative q2
= a2
/ l
;
232 wxLongLongNative r2
= a2
% l
;
233 CPPUNIT_ASSERT( q
== q2
);
234 CPPUNIT_ASSERT( r
== r2
);
235 CPPUNIT_ASSERT( a2
== ( q2
* l
+ r2
) );
240 void LongLongTestCase::BitOperations()
242 for ( size_t m
= 0; m
< ITEMS
; m
++ )
244 wxLongLong a
= RAND_LL();
246 for ( size_t n
= 0; n
< 33; n
++ )
248 wxLongLong
b(a
.GetHi(), a
.GetLo()), c
, d
= b
, e
;
251 CPPUNIT_ASSERT( c
== d
);
254 CPPUNIT_ASSERT( d
== e
);
256 #if wxUSE_LONGLONG_WX
257 wxLongLongWx
b1(a
.GetHi(), a
.GetLo()), c1
, d1
= b1
, e1
;
260 CPPUNIT_ASSERT( c1
== d1
);
263 CPPUNIT_ASSERT( d1
== e1
);
266 #if wxUSE_LONGLONG_NATIVE
267 wxLongLongNative
b2(a
.GetHi(), a
.GetLo()), c2
, d2
= b2
, e2
;
270 CPPUNIT_ASSERT( c2
== d2
);
273 CPPUNIT_ASSERT( d2
== e2
);
279 void LongLongTestCase::ToString()
283 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
285 wxLongLong a
= testLongs
[n
];
286 s1
= wxString::Format(wxT("%ld"), testLongs
[n
]);
288 CPPUNIT_ASSERT( s1
== s2
);
292 CPPUNIT_ASSERT( s1
== s2
);
294 #if wxUSE_LONGLONG_WX
295 wxLongLongWx a1
= testLongs
[n
];
297 CPPUNIT_ASSERT( s1
== s2
);
300 #if wxUSE_LONGLONG_NATIVE
301 wxLongLongNative a2
= testLongs
[n
];
303 CPPUNIT_ASSERT( s1
== s2
);
307 wxLongLong
a(0x12345678, 0x87654321);
308 CPPUNIT_ASSERT( a
.ToString() == wxT("1311768467139281697") );
310 CPPUNIT_ASSERT( a
.ToString() == wxT("-1311768467139281697") );
312 wxLongLong
llMin(-2147483647L - 1L, 0);
313 CPPUNIT_ASSERT( llMin
.ToString() == wxT("-9223372036854775808") );
315 #if wxUSE_LONGLONG_WX
316 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
317 CPPUNIT_ASSERT( a1
.ToString() == wxT("-1311768467139281697") );
319 CPPUNIT_ASSERT( a1
.ToString() == wxT("1311768467139281697") );
322 #if wxUSE_LONGLONG_NATIVE
323 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
324 CPPUNIT_ASSERT( a2
.ToString() == wxT("-1311768467139281697") );
326 CPPUNIT_ASSERT( a2
.ToString() == wxT("1311768467139281697") );
331 void LongLongTestCase::LoHi()
333 wxLongLong
ll(123, 456);
334 CPPUNIT_ASSERT_EQUAL( 456u, ll
.GetLo() );
335 CPPUNIT_ASSERT_EQUAL( 123, ll
.GetHi() );
337 wxULongLong
ull(987, 654);
338 CPPUNIT_ASSERT_EQUAL( 654u, ull
.GetLo() );
339 CPPUNIT_ASSERT_EQUAL( 987u, ull
.GetHi() );
342 void LongLongTestCase::Limits()
344 // VC6 doesn't specialize numeric_limits<> for __int64 so skip this test
347 #if wxUSE_LONGLONG_NATIVE
348 CPPUNIT_ASSERT( std::numeric_limits
<wxLongLong
>::is_specialized
);
349 CPPUNIT_ASSERT( std::numeric_limits
<wxULongLong
>::is_specialized
);
351 wxULongLong maxval
= std::numeric_limits
<wxULongLong
>::max();
352 CPPUNIT_ASSERT( maxval
.ToDouble() > 0 );
353 #endif // wxUSE_LONGLONG_NATIVE
354 #endif // !__VISUALC6__
357 #endif // wxUSE_LONGLONG