]>
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"
27 // ----------------------------------------------------------------------------
28 // helpers for testing
29 // ----------------------------------------------------------------------------
31 // number of iterations in loops
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)
37 // get a random 64 bit number
38 #define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
40 static const long testLongs
[] =
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class LongLongTestCase
: public CppUnit::TestCase
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();
74 void Multiplication();
79 DECLARE_NO_COPY_CLASS(LongLongTestCase
)
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION( LongLongTestCase
);
85 // also include in it's own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LongLongTestCase
, "LongLongTestCase" );
88 LongLongTestCase::LongLongTestCase()
90 srand((unsigned)time(NULL
));
93 void LongLongTestCase::Conversion()
95 for ( size_t n
= 0; n
< ITEMS
; n
++ )
97 wxLongLong a
= RAND_LL();
99 wxLongLong
b(a
.GetHi(), a
.GetLo());
100 CPPUNIT_ASSERT( a
== b
);
102 #if wxUSE_LONGLONG_WX
103 wxLongLongWx
c(a
.GetHi(), a
.GetLo());
104 CPPUNIT_ASSERT( a
== c
);
107 #if wxUSE_LONGLONG_NATIVE
108 wxLongLongNative
d(a
.GetHi(), a
.GetLo());
109 CPPUNIT_ASSERT( a
== d
);
114 void LongLongTestCase::Comparison()
116 static const long ls
[2] =
126 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
128 for ( size_t m
= 0; m
< WXSIZEOF(lls
); m
++ )
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
]) );
140 void LongLongTestCase::Addition()
142 for ( size_t n
= 0; n
< ITEMS
; n
++ )
144 wxLongLong a
= RAND_LL();
145 wxLongLong b
= RAND_LL();
146 wxLongLong c
= a
+ b
;
148 #if wxUSE_LONGLONG_NATIVE
149 wxLongLongNative a1
= a
;
150 wxLongLongNative b1
= b
;
151 wxLongLongNative c1
= a1
+ b1
;
152 CPPUNIT_ASSERT( c
== c1
);
155 #if wxUSE_LONGLONG_WX
158 wxLongLongWx c2
= a2
+ b2
;
159 CPPUNIT_ASSERT( c
== c2
);
164 void LongLongTestCase::Multiplication()
166 for ( size_t n
= 0; n
< ITEMS
; n
++ )
168 wxLongLong a
= RAND_LL();
169 wxLongLong b
= RAND_LL();
172 wxLongLong
a1(a
.GetHi(), a
.GetLo());
173 wxLongLong
b1(b
.GetHi(), b
.GetLo());
174 wxLongLong c1
= a1
*b1
;
175 CPPUNIT_ASSERT( c1
== c
);
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
);
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
);
193 void LongLongTestCase::Division()
195 for ( size_t n
= 0; n
< ITEMS
; n
++ )
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());
201 // get a random (but non null) long (not wxLongLong for now) divider
209 wxLongLong q
= a
/ l
;
210 wxLongLong r
= a
% l
;
212 CPPUNIT_ASSERT( a
== ( q
* l
+ r
) );
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
) );
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
) );
234 void LongLongTestCase::BitOperations()
236 for ( size_t n
= 0; n
< ITEMS
; n
++ )
238 wxLongLong a
= RAND_LL();
240 for ( size_t n
= 0; n
< 33; n
++ )
242 wxLongLong
b(a
.GetHi(), a
.GetLo()), c
, d
= b
, e
;
245 CPPUNIT_ASSERT( c
== d
);
248 CPPUNIT_ASSERT( d
== e
);
250 #if wxUSE_LONGLONG_WX
251 wxLongLongWx
b1(a
.GetHi(), a
.GetLo()), c1
, d1
= b1
, e1
;
254 CPPUNIT_ASSERT( c1
== d1
);
257 CPPUNIT_ASSERT( d1
== e1
);
260 #if wxUSE_LONGLONG_NATIVE
261 wxLongLongNative
b2(a
.GetHi(), a
.GetLo()), c2
, d2
= b2
, e2
;
264 CPPUNIT_ASSERT( c2
== d2
);
267 CPPUNIT_ASSERT( d2
== e2
);
273 void LongLongTestCase::ToString()
277 for ( size_t n
= 0; n
< WXSIZEOF(testLongs
); n
++ )
279 wxLongLong a
= testLongs
[n
];
280 s1
= wxString::Format(_T("%ld"), testLongs
[n
]);
282 CPPUNIT_ASSERT( s1
== s2
);
286 CPPUNIT_ASSERT( s1
== s2
);
288 #if wxUSE_LONGLONG_WX
289 wxLongLongWx a1
= testLongs
[n
];
291 CPPUNIT_ASSERT( s1
== s2
);
294 #if wxUSE_LONGLONG_NATIVE
295 wxLongLongNative a2
= testLongs
[n
];
297 CPPUNIT_ASSERT( s1
== s2
);
301 wxLongLong
a(0x12345678, 0x87654321);
302 CPPUNIT_ASSERT( a
.ToString() == _T("1311768467139281697") );
304 CPPUNIT_ASSERT( a
.ToString() == _T("-1311768467139281697") );
306 #if wxUSE_LONGLONG_WX
307 wxLongLongWx
a1(a
.GetHi(), a
.GetLo());
308 CPPUNIT_ASSERT( a1
.ToString() == _T("-1311768467139281697") );
310 CPPUNIT_ASSERT( a1
.ToString() == _T("1311768467139281697") );
313 #if wxUSE_LONGLONG_NATIVE
314 wxLongLongNative
a2(a
.GetHi(), a
.GetLo());
315 CPPUNIT_ASSERT( a2
.ToString() == _T("-1311768467139281697") );
317 CPPUNIT_ASSERT( a2
.ToString() == _T("1311768467139281697") );