]>
git.saurik.com Git - wxWidgets.git/blob - tests/longlong/longlong.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 // ----------------------------------------------------------------------------
14 #include "wx/wxprec.h"
24 #include "wx/longlong.h"
27 #include "wx/cppunit.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
);
71 CPPUNIT_TEST_SUITE_END();
76 void Multiplication();
81 DECLARE_NO_COPY_CLASS(LongLongTestCase
)
84 // register in the unnamed registry so that these tests are run by default
85 CPPUNIT_TEST_SUITE_REGISTRATION( LongLongTestCase
);
87 // also include in it's own registry so that these tests can be run alone
88 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LongLongTestCase
, "LongLongTestCase" );
90 LongLongTestCase::LongLongTestCase()
92 srand((unsigned)time(NULL
));
95 void LongLongTestCase::Conversion()
97 for ( size_t n
= 0; n
< ITEMS
; n
++ )
99 wxLongLong a
= RAND_LL();
101 wxLongLong
b(a
.GetHi(), a
.GetLo());
102 CPPUNIT_ASSERT( a
== b
);
104 #if wxUSE_LONGLONG_WX
105 wxLongLongWx
c(a
.GetHi(), a
.GetLo());
106 CPPUNIT_ASSERT( a
== c
);
109 #if wxUSE_LONGLONG_NATIVE
110 wxLongLongNative
d(a
.GetHi(), a
.GetLo());
111 CPPUNIT_ASSERT( a
== d
);
116 void LongLongTestCase::Comparison()
118 static const long ls
[2] =
128 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
130 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
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 CPPUNIT_ASSERT( (lls
[m
] != testLongs
[n
]) == (ls
[m
] != testLongs
[n
]) );
137 CPPUNIT_ASSERT( (lls
[m
] == testLongs
[n
]) == (ls
[m
] == testLongs
[n
]) );
142 void LongLongTestCase::Addition()
144 for ( size_t n
= 0; n
< ITEMS
; n
++ )
146 wxLongLong a
= RAND_LL();
147 wxLongLong b
= RAND_LL();
148 wxLongLong c
= a
+ b
;
150 #if wxUSE_LONGLONG_NATIVE
151 wxLongLongNative a1
= a
;
152 wxLongLongNative b1
= b
;
153 wxLongLongNative c1
= a1
+ b1
;
154 CPPUNIT_ASSERT( c
== c1
);
157 #if wxUSE_LONGLONG_WX
160 wxLongLongWx c2
= a2
+ b2
;
161 CPPUNIT_ASSERT( c
== c2
);
166 void LongLongTestCase::Multiplication()
168 for ( size_t n
= 0; n
< ITEMS
; n
++ )
170 wxLongLong a
= RAND_LL();
171 wxLongLong b
= RAND_LL();
174 wxLongLong
a1(a
.GetHi(), a
.GetLo());
175 wxLongLong
b1(b
.GetHi(), b
.GetLo());
176 wxLongLong c1
= a1
*b1
;
177 CPPUNIT_ASSERT( c1
== c
);
179 #if wxUSE_LONGLONG_WX
180 wxLongLongWx
a2(a
.GetHi(), a
.GetLo());
181 wxLongLongWx
b2(b
.GetHi(), b
.GetLo());
182 wxLongLongWx c2
= a2
*b2
;
183 CPPUNIT_ASSERT( c2
== c
);
186 #if wxUSE_LONGLONG_NATIVE
187 wxLongLongNative
a3(a
.GetHi(), a
.GetLo());
188 wxLongLongNative
b3(b
.GetHi(), b
.GetLo());
189 wxLongLongNative c3
= a3
*b3
;
190 CPPUNIT_ASSERT( c3
== c
);
195 void LongLongTestCase::Division()
197 for ( size_t n
= 0; n
< ITEMS
; n
++ )
199 // get a random wxLongLong (shifting by 12 the MSB ensures that the
200 // multiplication will not overflow)
201 wxLongLong a
= MAKE_LL((rand() >> 12), rand(), rand(), rand());
203 // get a random (but non null) long (not wxLongLong for now) divider
211 wxLongLong q
= a
/ l
;
212 wxLongLong r
= a
% l
;
214 CPPUNIT_ASSERT( a
== ( q
* l
+ r
) );
216 #if wxUSE_LONGLONG_WX
217 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
218 wxLongLongWx q1
= a1
/ l
;
219 wxLongLongWx r1
= a1
% l
;
220 CPPUNIT_ASSERT( q
== q1
);
221 CPPUNIT_ASSERT( r
== r1
);
222 CPPUNIT_ASSERT( a1
== ( q1
* l
+ r1
) );
225 #if wxUSE_LONGLONG_NATIVE
226 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
227 wxLongLongNative q2
= a2
/ l
;
228 wxLongLongNative r2
= a2
% l
;
229 CPPUNIT_ASSERT( q
== q2
);
230 CPPUNIT_ASSERT( r
== r2
);
231 CPPUNIT_ASSERT( a2
== ( q2
* l
+ r2
) );
236 void LongLongTestCase::BitOperations()
238 for ( size_t n
= 0; n
< ITEMS
; n
++ )
240 wxLongLong a
= RAND_LL();
242 for ( size_t n
= 0; n
< 33; n
++ )
244 wxLongLong
b(a
.GetHi(), a
.GetLo()), c
, d
= b
, e
;
247 CPPUNIT_ASSERT( c
== d
);
250 CPPUNIT_ASSERT( d
== e
);
252 #if wxUSE_LONGLONG_WX
253 wxLongLongWx
b1(a
.GetHi(), a
.GetLo()), c1
, d1
= b1
, e1
;
256 CPPUNIT_ASSERT( c1
== d1
);
259 CPPUNIT_ASSERT( d1
== e1
);
262 #if wxUSE_LONGLONG_NATIVE
263 wxLongLongNative
b2(a
.GetHi(), a
.GetLo()), c2
, d2
= b2
, e2
;
266 CPPUNIT_ASSERT( c2
== d2
);
269 CPPUNIT_ASSERT( d2
== e2
);
275 void LongLongTestCase::ToString()
279 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
281 wxLongLong a
= testLongs
[n
];
282 s1
= wxString::Format(_T("%ld"), testLongs
[n
]);
284 CPPUNIT_ASSERT( s1
== s2
);
286 #if wxUSE_LONGLONG_WX
287 wxLongLongWx a1
= testLongs
[n
];
289 CPPUNIT_ASSERT( s1
== s2
);
292 #if wxUSE_LONGLONG_NATIVE
293 wxLongLongNative a2
= testLongs
[n
];
295 CPPUNIT_ASSERT( s1
== s2
);
299 wxLongLong
a(0x12345678, 0x87654321);
300 CPPUNIT_ASSERT( a
.ToString() == _T("1311768467139281697") );
302 CPPUNIT_ASSERT( a
.ToString() == _T("-1311768467139281697") );
304 #if wxUSE_LONGLONG_WX
305 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
306 CPPUNIT_ASSERT( a1
.ToString() == _T("-1311768467139281697") );
308 CPPUNIT_ASSERT( a1
.ToString() == _T("1311768467139281697") );
311 #if wxUSE_LONGLONG_NATIVE
312 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
313 CPPUNIT_ASSERT( a2
.ToString() == _T("-1311768467139281697") );
315 CPPUNIT_ASSERT( a2
.ToString() == _T("1311768467139281697") );