applied patch: wxLongLong CppUnit test (928257)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26637 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -70,7 +70,6 @@
|
||||
#define TEST_LIST
|
||||
#define TEST_LOCALE
|
||||
#define TEST_LOG
|
||||
#define TEST_LONGLONG
|
||||
#define TEST_MIME
|
||||
#define TEST_PATHLIST
|
||||
#define TEST_ODBC
|
||||
@@ -2250,345 +2249,6 @@ static void TestUserInfo()
|
||||
|
||||
#endif // TEST_INFO_FUNCTIONS
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// long long
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#ifdef TEST_LONGLONG
|
||||
|
||||
#include "wx/longlong.h"
|
||||
#include "wx/timer.h"
|
||||
|
||||
// make a 64 bit number from 4 16 bit ones
|
||||
#define MAKE_LL(x1, x2, x3, x4) wxLongLong((x1 << 16) | x2, (x3 << 16) | x3)
|
||||
|
||||
// get a random 64 bit number
|
||||
#define RAND_LL() MAKE_LL(rand(), rand(), rand(), rand())
|
||||
|
||||
static const long testLongs[] =
|
||||
{
|
||||
0,
|
||||
1,
|
||||
-1,
|
||||
LONG_MAX,
|
||||
LONG_MIN,
|
||||
0x1234,
|
||||
-0x1234
|
||||
};
|
||||
|
||||
#if wxUSE_LONGLONG_WX
|
||||
inline bool operator==(const wxLongLongWx& a, const wxLongLongNative& b)
|
||||
{ return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
|
||||
inline bool operator==(const wxLongLongNative& a, const wxLongLongWx& b)
|
||||
{ return a.GetHi() == b.GetHi() && a.GetLo() == b.GetLo(); }
|
||||
#endif // wxUSE_LONGLONG_WX
|
||||
|
||||
#if 0
|
||||
static void TestSpeed()
|
||||
{
|
||||
static const long max = 100000000;
|
||||
long n;
|
||||
|
||||
{
|
||||
wxStopWatch sw;
|
||||
|
||||
long l = 0;
|
||||
for ( n = 0; n < max; n++ )
|
||||
{
|
||||
l += n;
|
||||
}
|
||||
wxUnusedVar(l);
|
||||
|
||||
wxPrintf(_T("Summing longs took %ld milliseconds.\n"), sw.Time());
|
||||
}
|
||||
|
||||
#if wxUSE_LONGLONG_NATIVE
|
||||
{
|
||||
wxStopWatch sw;
|
||||
|
||||
wxLongLong_t l = 0;
|
||||
for ( n = 0; n < max; n++ )
|
||||
{
|
||||
l += n;
|
||||
}
|
||||
wxUnusedVar(l);
|
||||
|
||||
wxPrintf(_T("Summing wxLongLong_t took %ld milliseconds.\n"), sw.Time());
|
||||
}
|
||||
#endif // wxUSE_LONGLONG_NATIVE
|
||||
|
||||
{
|
||||
wxStopWatch sw;
|
||||
|
||||
wxLongLong l;
|
||||
for ( n = 0; n < max; n++ )
|
||||
{
|
||||
l += n;
|
||||
}
|
||||
|
||||
wxPrintf(_T("Summing wxLongLongs took %ld milliseconds.\n"), sw.Time());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void TestLongLongConversion()
|
||||
{
|
||||
wxPuts(_T("*** Testing wxLongLong conversions ***\n"));
|
||||
|
||||
size_t nTested = 0;
|
||||
for ( size_t n = 0; n < 100000; n++ )
|
||||
{
|
||||
#if wxUSE_LONGLONG_NATIVE
|
||||
wxLongLong a = RAND_LL();
|
||||
|
||||
wxLongLongNative b(a.GetHi(), a.GetLo());
|
||||
|
||||
if( a != b)
|
||||
wxPuts( _T("conversions failure") );
|
||||
#else
|
||||
wxPuts(_T("Can't do it without native long long type, test skipped."));
|
||||
|
||||
return;
|
||||
#endif // wxUSE_LONGLONG_NATIVE
|
||||
|
||||
if ( !(nTested % 1000) )
|
||||
{
|
||||
wxPutchar('.');
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
nTested++;
|
||||
}
|
||||
|
||||
wxPuts(_T(" done!"));
|
||||
}
|
||||
|
||||
static void TestMultiplication()
|
||||
{
|
||||
wxPuts(_T("*** Testing wxLongLong multiplication ***\n"));
|
||||
|
||||
size_t nTested = 0;
|
||||
for ( size_t n = 0; n < 100000; n++ )
|
||||
{
|
||||
#if wxUSE_LONGLONG_NATIVE
|
||||
wxLongLong a = RAND_LL();
|
||||
wxLongLong b = RAND_LL();
|
||||
|
||||
wxLongLongNative aa(a.GetHi(), a.GetLo());
|
||||
wxLongLongNative bb(b.GetHi(), b.GetLo());
|
||||
|
||||
if( a*b != aa*bb )
|
||||
wxPuts( _T("multiplication failure") );
|
||||
#else // !wxUSE_LONGLONG_NATIVE
|
||||
wxPuts(_T("Can't do it without native long long type, test skipped."));
|
||||
|
||||
return;
|
||||
#endif // wxUSE_LONGLONG_NATIVE
|
||||
|
||||
if ( !(nTested % 1000) )
|
||||
{
|
||||
wxPutchar('.');
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
nTested++;
|
||||
}
|
||||
|
||||
wxPuts(_T(" done!"));
|
||||
}
|
||||
|
||||
static void TestDivision()
|
||||
{
|
||||
wxPuts(_T("*** Testing wxLongLong division ***\n"));
|
||||
|
||||
wxLongLong q, r;
|
||||
size_t nTested = 0;
|
||||
for ( size_t n = 0; n < 100000; n++ )
|
||||
{
|
||||
// get a random wxLongLong (shifting by 12 the MSB ensures that the
|
||||
// multiplication will not overflow)
|
||||
wxLongLong ll = MAKE_LL((rand() >> 12), rand(), rand(), rand());
|
||||
|
||||
// get a random (but non null) long (not wxLongLong for now) to divide
|
||||
// it with
|
||||
long l;
|
||||
do
|
||||
{
|
||||
l = rand();
|
||||
}
|
||||
while ( !l );
|
||||
|
||||
q = ll / l;
|
||||
r = ll % l;
|
||||
|
||||
#if wxUSE_LONGLONG_NATIVE
|
||||
wxLongLongNative m(ll.GetHi(), ll.GetLo());
|
||||
|
||||
wxLongLongNative p = m / l, s = m % l;
|
||||
|
||||
if(q != p || r != s)
|
||||
wxPuts( _T("division failure") );
|
||||
#else // !wxUSE_LONGLONG_NATIVE
|
||||
// verify the result
|
||||
wxASSERT_MSG( ll == q*l + r, "division failure" );
|
||||
#endif // wxUSE_LONGLONG_NATIVE
|
||||
|
||||
if ( !(nTested % 1000) )
|
||||
{
|
||||
wxPutchar('.');
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
nTested++;
|
||||
}
|
||||
|
||||
wxPuts(_T(" done!"));
|
||||
}
|
||||
|
||||
static void TestAddition()
|
||||
{
|
||||
wxPuts(_T("*** Testing wxLongLong addition ***\n"));
|
||||
|
||||
wxLongLong a, b, c;
|
||||
size_t nTested = 0;
|
||||
for ( size_t n = 0; n < 100000; n++ )
|
||||
{
|
||||
a = RAND_LL();
|
||||
b = RAND_LL();
|
||||
c = a + b;
|
||||
|
||||
#if wxUSE_LONGLONG_NATIVE
|
||||
wxASSERT_MSG( c == wxLongLongNative(a.GetHi(), a.GetLo()) +
|
||||
wxLongLongNative(b.GetHi(), b.GetLo()),
|
||||
_T("addition failure") );
|
||||
#else // !wxUSE_LONGLONG_NATIVE
|
||||
wxASSERT_MSG( c - b == a, "addition failure" );
|
||||
#endif // wxUSE_LONGLONG_NATIVE
|
||||
|
||||
if ( !(nTested % 1000) )
|
||||
{
|
||||
wxPutchar('.');
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
nTested++;
|
||||
}
|
||||
|
||||
wxPuts(_T(" done!"));
|
||||
}
|
||||
|
||||
static void TestBitOperations()
|
||||
{
|
||||
wxPuts(_T("*** Testing wxLongLong bit operation ***\n"));
|
||||
|
||||
wxLongLong ll;
|
||||
size_t nTested = 0;
|
||||
for ( size_t n = 0; n < 100000; n++ )
|
||||
{
|
||||
ll = RAND_LL();
|
||||
|
||||
#if wxUSE_LONGLONG_NATIVE
|
||||
for ( size_t n = 0; n < 33; n++ )
|
||||
{
|
||||
}
|
||||
#else // !wxUSE_LONGLONG_NATIVE
|
||||
wxPuts(_T("Can't do it without native long long type, test skipped."));
|
||||
|
||||
return;
|
||||
#endif // wxUSE_LONGLONG_NATIVE
|
||||
|
||||
if ( !(nTested % 1000) )
|
||||
{
|
||||
wxPutchar('.');
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
nTested++;
|
||||
}
|
||||
|
||||
wxPuts(_T(" done!"));
|
||||
}
|
||||
|
||||
static void TestLongLongComparison()
|
||||
{
|
||||
#if wxUSE_LONGLONG_WX
|
||||
wxPuts(_T("*** Testing wxLongLong comparison ***\n"));
|
||||
|
||||
static const long ls[2] =
|
||||
{
|
||||
0x1234,
|
||||
-0x1234,
|
||||
};
|
||||
|
||||
wxLongLongWx lls[2];
|
||||
lls[0] = ls[0];
|
||||
lls[1] = ls[1];
|
||||
|
||||
for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
|
||||
{
|
||||
bool res;
|
||||
|
||||
for ( size_t m = 0; m < WXSIZEOF(lls); m++ )
|
||||
{
|
||||
res = lls[m] > testLongs[n];
|
||||
wxPrintf(_T("0x%lx > 0x%lx is %s (%s)\n"),
|
||||
ls[m], testLongs[n], res ? "true" : "false",
|
||||
res == (ls[m] > testLongs[n]) ? "ok" : "ERROR");
|
||||
|
||||
res = lls[m] < testLongs[n];
|
||||
wxPrintf(_T("0x%lx < 0x%lx is %s (%s)\n"),
|
||||
ls[m], testLongs[n], res ? "true" : "false",
|
||||
res == (ls[m] < testLongs[n]) ? "ok" : "ERROR");
|
||||
|
||||
res = lls[m] == testLongs[n];
|
||||
wxPrintf(_T("0x%lx == 0x%lx is %s (%s)\n"),
|
||||
ls[m], testLongs[n], res ? "true" : "false",
|
||||
res == (ls[m] == testLongs[n]) ? "ok" : "ERROR");
|
||||
}
|
||||
}
|
||||
#endif // wxUSE_LONGLONG_WX
|
||||
}
|
||||
|
||||
static void TestLongLongToString()
|
||||
{
|
||||
wxPuts(_T("*** Testing wxLongLong::ToString() ***\n"));
|
||||
|
||||
for ( size_t n = 0; n < WXSIZEOF(testLongs); n++ )
|
||||
{
|
||||
wxLongLong ll = testLongs[n];
|
||||
wxPrintf(_T("%ld == %s\n"), testLongs[n], ll.ToString().c_str());
|
||||
}
|
||||
|
||||
wxLongLong ll(0x12345678, 0x87654321);
|
||||
wxPrintf(_T("0x1234567887654321 = %s\n"), ll.ToString().c_str());
|
||||
|
||||
ll.Negate();
|
||||
wxPrintf(_T("-0x1234567887654321 = %s\n"), ll.ToString().c_str());
|
||||
}
|
||||
|
||||
static void TestLongLongPrintf()
|
||||
{
|
||||
wxPuts(_T("*** Testing wxLongLong printing ***\n"));
|
||||
|
||||
#ifdef wxLongLongFmtSpec
|
||||
#ifndef __MINGW32__
|
||||
wxLongLong ll = wxLL(0x1234567890abcdef);
|
||||
wxString s = wxString::Format(_T("%") wxLongLongFmtSpec _T("x"), ll);
|
||||
#else
|
||||
wxString s = _T("MinGW compiler does not allow wxLongLong in '...'");
|
||||
#endif
|
||||
wxPrintf(_T("0x1234567890abcdef -> %s (%s)\n"),
|
||||
s.c_str(), s == _T("1234567890abcdef") ? _T("ok") : _T("ERROR"));
|
||||
#else // !wxLongLongFmtSpec
|
||||
#error "wxLongLongFmtSpec not defined for this compiler/platform"
|
||||
#endif
|
||||
}
|
||||
|
||||
#undef MAKE_LL
|
||||
#undef RAND_LL
|
||||
|
||||
#endif // TEST_LONGLONG
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// path list
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -7172,26 +6832,6 @@ int main(int argc, char **argv)
|
||||
#endif
|
||||
#endif // TEST_FTP
|
||||
|
||||
#ifdef TEST_LONGLONG
|
||||
// seed pseudo random generator
|
||||
srand((unsigned)time(NULL));
|
||||
|
||||
#if 0
|
||||
TestSpeed();
|
||||
#endif
|
||||
|
||||
#if TEST_ALL
|
||||
TestMultiplication();
|
||||
TestDivision();
|
||||
TestAddition();
|
||||
TestLongLongConversion();
|
||||
TestBitOperations();
|
||||
TestLongLongComparison();
|
||||
TestLongLongToString();
|
||||
TestLongLongPrintf();
|
||||
#endif
|
||||
#endif // TEST_LONGLONG
|
||||
|
||||
#ifdef TEST_HASH
|
||||
TestHash();
|
||||
#endif // TEST_HASH
|
||||
|
Reference in New Issue
Block a user