os/kernelhwsrv/kerneltest/e32test/math/t_i64_2.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/math/t_i64_2.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,484 @@
     1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\math\t_i64_2.cpp
    1.18 +// Overview:
    1.19 +// Test 64-bit integer functionality.
    1.20 +// API Information:
    1.21 +// TInt64, TUInt64.
    1.22 +// Details:
    1.23 +// - Construct TInt64 and TUInt64 and verify the results.
    1.24 +// - Test the unary and shift operators and check results are as expected.
    1.25 +// - Test the  + - * / and % operators, verify results are as expected.
    1.26 +// - Test the  + - * / and % operators with random numbers, verify results 
    1.27 +// are as expected.
    1.28 +// - Test the conversion of TInt64 to/from TReal. Verify that the results
    1.29 +// are as expected.
    1.30 +// - Test the conversion of TInt64 to/from text. Verify that the results
    1.31 +// are as expected.
    1.32 +// Platforms/Drives/Compatibility:
    1.33 +// All.
    1.34 +// Assumptions/Requirement/Pre-requisites:
    1.35 +// Failures and causes:
    1.36 +// Base Port information:
    1.37 +// 
    1.38 +//
    1.39 +
    1.40 +#include <e32test.h>
    1.41 +#include <e32math.h>
    1.42 +#include "largeint.h"
    1.43 +#include "../misc/prbs.h"
    1.44 +
    1.45 +typedef TLargeInt<2> I64;
    1.46 +typedef TLargeInt<4> I128;
    1.47 +
    1.48 +RTest test(_L("T_I64_2"));
    1.49 +TUint Seed[2];
    1.50 +
    1.51 +TUint64 Random64()
    1.52 +	{
    1.53 +	TUint h = Random(Seed);
    1.54 +	TUint l = Random(Seed);
    1.55 +	return MAKE_TUINT64(h,l);
    1.56 +	}
    1.57 +
    1.58 +#define BOOL(x)	((x)?1:0)
    1.59 +
    1.60 +#define FOREACH(p, table, esize)	\
    1.61 +	for(p=table; p<(const TUint32*)((const TUint8*)table+sizeof(table)); p+=esize/sizeof(TUint32))
    1.62 +
    1.63 +const TUint32 Table1[] =
    1.64 +	{
    1.65 +	0x00000000, 0x00000000,
    1.66 +	0x00000001, 0x00000000,
    1.67 +	0x0000cc01, 0x00000000,
    1.68 +	0x00db2701, 0x00000000,
    1.69 +	0xcc9ffcd1, 0x00000000,
    1.70 +	0x00000000, 0xffffffff,
    1.71 +	0xeeeeeeee, 0xffffffff,
    1.72 +	0x04030201, 0x00000055,
    1.73 +	0x04030201, 0x00006655,
    1.74 +	0x04030201, 0x00776655,
    1.75 +	0x04030201, 0x33776655,
    1.76 +	0xf9de6484, 0xb504f333
    1.77 +	};
    1.78 +
    1.79 +void Test1()
    1.80 +	{
    1.81 +	test.Next(_L("Unary operators and shifts"));
    1.82 +
    1.83 +	const TUint32* p;
    1.84 +	FOREACH(p,Table1,8)
    1.85 +		{
    1.86 +		I64 a(p);
    1.87 +		TInt64 b = MAKE_TINT64(p[1], p[0]);
    1.88 +		I64 c(b);
    1.89 +		test(a==c);
    1.90 +		TUint64 d = MAKE_TUINT64(p[1], p[0]);
    1.91 +		I64 e(d);
    1.92 +		test(a==e);
    1.93 +		I64 c2(~b);
    1.94 +		I64 c3(-b);
    1.95 +		a.Not();
    1.96 +		test(c2==a);
    1.97 +		a.Not();
    1.98 +		test(c==a);
    1.99 +		a.Neg();
   1.100 +		test(c3==a);
   1.101 +		a.Neg();
   1.102 +		test(c==a);
   1.103 +		}
   1.104 +	FOREACH(p,Table1,8)
   1.105 +		{
   1.106 +		I64 a(p);
   1.107 +		TInt64 s = MAKE_TINT64(p[1], p[0]);
   1.108 +		TUint64 u = MAKE_TUINT64(p[1], p[0]);
   1.109 +		TInt n;
   1.110 +		for (n=0; n<64; ++n)
   1.111 +			{
   1.112 +			I64 b(a), c(a), d(a);
   1.113 +			b.Lsl(n), c.Lsr(n), d.Asr(n);
   1.114 +			TInt64 s2 = s<<n;
   1.115 +			TInt64 s3 = s>>n;
   1.116 +			TUint64 u2 = u<<n;
   1.117 +			TUint64 u3 = u>>n;
   1.118 +//			test.Printf(_L("s2=%lx\ns3=%lx\n,u2=%lx\n,u3=%lx\n"),s2,s3,u2,u3);
   1.119 +			test(b == I64(s2));
   1.120 +			test(b == I64(u2));
   1.121 +			test(c == I64(u3));
   1.122 +			test(d == I64(s3));
   1.123 +			}
   1.124 +		}
   1.125 +	}
   1.126 +
   1.127 +const TUint32 Table2[] =
   1.128 +	{
   1.129 +	0x00000000, 0x00000000, 0x00000000, 0x00000000,
   1.130 +	0x00000001, 0x00000000, 0x00000000, 0x00000000,
   1.131 +	0x05f5e100, 0x00000000, 0x000000cb, 0x00000000,
   1.132 +	0xffffff9c, 0xffffffff, 0x00129cbb, 0x00000000,
   1.133 +	0xffffcd03, 0xffffffff, 0xffff9123, 0xffffffff,
   1.134 +	0xf9de6484, 0xb504f333, 0xf9de6484, 0xb504f333,
   1.135 +	0xf9de6484, 0xb504f333, 0x2168c235, 0xc90fdaa2,
   1.136 +	0xf9de6484, 0xb504f333, 0x000000cb, 0x00000000,
   1.137 +	0xf9de6484, 0xb504f333, 0x800000cb, 0x00000000,
   1.138 +	0xf9de6484, 0xb504f333, 0x000000cb, 0x00000001,
   1.139 +	0xf9de6484, 0xb504f333, 0xfffffed9, 0xffffffff,
   1.140 +	0xf9de6484, 0xb504f333, 0x197383db, 0xffffffff,
   1.141 +	0xf9de6484, 0xb504f333, 0x197383db, 0xffffffec,
   1.142 +	0x38aa3b29, 0x5c17f0bc, 0x000019c7, 0x00000000,
   1.143 +	0x38aa3b29, 0x5c17f0bc, 0x800019c7, 0x00000000,
   1.144 +	0x38aa3b29, 0x5c17f0bc, 0x000019c7, 0x00000003,
   1.145 +	0x38aa3b29, 0x5c17f0bc, 0x197383db, 0xffffffff,
   1.146 +	0x38aa3b29, 0x5c17f0bc, 0x197383db, 0xffffffec,
   1.147 +	0x00123456, 0x00000000, 0x8cb9fc1b, 0x00000000,
   1.148 +	0x00000123, 0x00000000, 0x8cb9fc1b, 0x0000cc9f,
   1.149 +	0xfffffe33, 0xffffffff, 0x8cb9fc1b, 0x0000cc9f
   1.150 +	};
   1.151 +
   1.152 +void Test2(const TUint32* p)
   1.153 +	{
   1.154 +	I64 a(p), b(p+2);
   1.155 +	TInt64 x = MAKE_TINT64(p[1], p[0]);
   1.156 +	TInt64 y = MAKE_TINT64(p[3], p[2]);
   1.157 +	TUint64 u = MAKE_TUINT64(p[1], p[0]);
   1.158 +	TUint64 v = MAKE_TUINT64(p[3], p[2]);
   1.159 +	{
   1.160 +	I64 c(a); c.Add(b); test(c==I64(x+y)); test(c==I64(u+v));
   1.161 +	test(c==I64(y+x)); test(c==I64(v+u));
   1.162 +	}
   1.163 +	{
   1.164 +	I64 c(a); c.Sub(b); test(c==I64(x-y)); test(c==I64(u-v));
   1.165 +	I64 d(b); d.Sub(a); test(d==I64(y-x)); test(d==I64(v-u));
   1.166 +	}
   1.167 +	{
   1.168 +	I64 c(a); c.Mul(b); test(c==I64(x*y)); test(c==I64(u*v));
   1.169 +	test(c==I64(y*x)); test(c==I64(v*u));
   1.170 +	}
   1.171 +	{
   1.172 +	I128 c = a.LongMultS(b);
   1.173 +	TUint32 t[4];
   1.174 +	Math::Mul64(x, y, *(TInt64*)(t+2), *(TUint64*)t);
   1.175 +	test(c==I128(t));
   1.176 +	Math::Mul64(y, x, *(TInt64*)(t+2), *(TUint64*)t);
   1.177 +	test(c==I128(t));
   1.178 +	}
   1.179 +	{
   1.180 +	I128 c = a.LongMultU(b);
   1.181 +	TUint32 t[4];
   1.182 +	Math::UMul64(u, v, *(TUint64*)(t+2), *(TUint64*)t);
   1.183 +	test(c==I128(t));
   1.184 +	Math::UMul64(v, u, *(TUint64*)(t+2), *(TUint64*)t);
   1.185 +	test(c==I128(t));
   1.186 +	}
   1.187 +	if (y!=0)
   1.188 +		{
   1.189 +		I64 r; I64 q(a); q.DivS(b,r);
   1.190 +		test(q==I64(x/y));
   1.191 +		test(r==I64(x%y));
   1.192 +		TInt64 r2;
   1.193 +		TInt64 q2 = Math::DivMod64(x, y, r2);
   1.194 +		test(q==I64(q2));
   1.195 +		test(r==I64(r2));
   1.196 +		}
   1.197 +	if (x!=0)
   1.198 +		{
   1.199 +		I64 r; I64 q(b); q.DivS(a,r);
   1.200 +		test(q==I64(y/x));
   1.201 +		test(r==I64(y%x));
   1.202 +		TInt64 r2;
   1.203 +		TInt64 q2 = Math::DivMod64(y, x, r2);
   1.204 +		test(q==I64(q2));
   1.205 +		test(r==I64(r2));
   1.206 +		}
   1.207 +	if (v!=0)
   1.208 +		{
   1.209 +		I64 r; I64 q(a); q.DivU(b,r);
   1.210 +		test(q==I64(u/v));
   1.211 +		test(r==I64(u%v));
   1.212 +		TUint64 r2;
   1.213 +		TUint64 q2 = Math::UDivMod64(u, v, r2);
   1.214 +		test(q==I64(q2));
   1.215 +		test(r==I64(r2));
   1.216 +		}
   1.217 +	if (u!=0)
   1.218 +		{
   1.219 +		I64 r; I64 q(b); q.DivU(a,r);
   1.220 +		test(q==I64(v/u));
   1.221 +		test(r==I64(v%u));
   1.222 +		TUint64 r2;
   1.223 +		TUint64 q2 = Math::UDivMod64(v, u, r2);
   1.224 +		test(q==I64(q2));
   1.225 +		test(r==I64(r2));
   1.226 +		}
   1.227 +	{
   1.228 +	TInt cmpu = a.CompareU(b);
   1.229 +	TInt cmps = a.CompareS(b);
   1.230 +	TInt equ = BOOL(u==v);
   1.231 +	TInt neu = BOOL(u!=v);
   1.232 +	TInt hi = BOOL(u>v);
   1.233 +	TInt hs = BOOL(u>=v);
   1.234 +	TInt lo = BOOL(u<v);
   1.235 +	TInt ls = BOOL(u<=v);
   1.236 +
   1.237 +	TInt eqs = BOOL(x==y);
   1.238 +	TInt nes = BOOL(x!=y);
   1.239 +	TInt gt = BOOL(x>y);
   1.240 +	TInt ge = BOOL(x>=y);
   1.241 +	TInt lt = BOOL(x<y);
   1.242 +	TInt le = BOOL(x<=y);
   1.243 +
   1.244 +	test(equ==eqs);
   1.245 +	test(neu==nes);
   1.246 +	test(equ!=neu);
   1.247 +	if (cmpu>0)
   1.248 +		test(!equ && hi && hs && !lo && !ls);
   1.249 +	else if (cmpu<0)
   1.250 +		test(!equ && !hi && !hs && lo && ls);
   1.251 +	else
   1.252 +		test(equ && !hi && hs && !lo && ls);
   1.253 +	if (cmps>0)
   1.254 +		test(!eqs && gt && ge && !lt && !le);
   1.255 +	else if (cmps<0)
   1.256 +		test(!eqs && !gt && !ge && lt && le);
   1.257 +	else
   1.258 +		test(eqs && !gt && ge && !lt && le);
   1.259 +	}
   1.260 +	}
   1.261 +
   1.262 +void Test2()
   1.263 +	{
   1.264 +	test.Next(_L("Test + - * / % (1)"));
   1.265 +	const TUint32* p;
   1.266 +	FOREACH(p,Table2,16)
   1.267 +		{
   1.268 +		Test2(p);
   1.269 +		}
   1.270 +	}
   1.271 +
   1.272 +void Test3()
   1.273 +	{
   1.274 +	test.Next(_L("Test + - * / % (2)"));
   1.275 +	TInt i;
   1.276 +	for (i=0; i<100; ++i)
   1.277 +		{
   1.278 +		TUint32 p[4];
   1.279 +		p[0] = Random(Seed);
   1.280 +		p[1] = Random(Seed);
   1.281 +		p[2] = Random(Seed);
   1.282 +		p[3] = Random(Seed);
   1.283 +		Test2(p);
   1.284 +		}
   1.285 +	}
   1.286 +
   1.287 +void Test4()
   1.288 +	{
   1.289 +	test.Next(_L("Test conversion to/from TReal"));
   1.290 +	TReal x;
   1.291 +	TReal limit=1048576.0*1048576.0*8192.0;
   1.292 +	TInt64 t22 = (TInt64)limit;
   1.293 +	test(t22 == TInt64(1)<<53);
   1.294 +	TInt64 t23 = (TInt64)(limit-1.0);
   1.295 +	test(t23 == (TInt64(1)<<53)-1);
   1.296 +
   1.297 +
   1.298 +	TInt i;
   1.299 +	TInt64 l;
   1.300 +	for (i=-99; i<100; i++)
   1.301 +		{
   1.302 +		x=1;
   1.303 +		l=1;
   1.304 +		TReal a(i);
   1.305 +		TInt64 b(i);
   1.306 +		while (Abs(x)<limit)
   1.307 +			{
   1.308 +			TInt64 ll = (TInt64)x;
   1.309 +//			test.Printf(_L("r64 %g -> i64 %lx (%lx)\n"), x, ll, l);
   1.310 +			test(ll==l);
   1.311 +			ll=0;
   1.312 +			ll = (TInt64)x;
   1.313 +			test(ll==l);
   1.314 +			x*=a;
   1.315 +			l*=b;
   1.316 +			if (i==1 || i==0 || (i==-1 && l==TInt64(1)))
   1.317 +				break;
   1.318 +			}
   1.319 +		}
   1.320 +
   1.321 +	TReal i64limit = 1024.0*limit;
   1.322 +	l=MAKE_TINT64(0x7fffffff,0xfffffc00);
   1.323 +	x=(TReal)l;
   1.324 +	test(x==i64limit-1024.0);
   1.325 +	l=MAKE_TINT64(0x80000000,0x00000000);
   1.326 +	x=(TReal)l;
   1.327 +	test(x==-i64limit);
   1.328 +	l=MAKE_TINT64(0x80000000,0x00000400);
   1.329 +	x=(TReal)l;
   1.330 +	test(x==1024.0-i64limit);
   1.331 +	l=MAKE_TINT64(0x00000001,0x00000000);
   1.332 +	x=(TReal)l;
   1.333 +	test(x==65536.0*65536.0);
   1.334 +	l=MAKE_TINT64(0xffffffff,0x00000000);
   1.335 +	x=(TReal)l;
   1.336 +	test(x==-65536.0*65536.0);
   1.337 +
   1.338 +	for (i=-99; i<100; i++)
   1.339 +		{
   1.340 +		x=1;
   1.341 +		l=1;
   1.342 +		TReal a(i);
   1.343 +		TInt64 b(i);
   1.344 +		while (Abs(x)<limit)
   1.345 +			{
   1.346 +			TReal y = (TReal)l;
   1.347 +			test(y==x);
   1.348 +			x*=a;
   1.349 +			l*=b;
   1.350 +			if (i==1 || i==0 || (i==-1 && l==TInt64(1)))
   1.351 +				break;
   1.352 +			}
   1.353 +		}
   1.354 + 
   1.355 +	}
   1.356 +
   1.357 +_LIT8(KTestHex8,"0 1 8 a 1b 2c7 10000000 100000000 1901cbfdc b504f333f9de6484 ffffffffffffffff");
   1.358 +_LIT16(KTestHex16,"0 1 8 a 1b 2c7 10000000 100000000 1901cbfdc b504f333f9de6484 ffffffffffffffff");
   1.359 +
   1.360 +const TUint32 TestHexTable[] =
   1.361 +	{
   1.362 +	0x00000000, 0x00000000,
   1.363 +	0x00000001, 0x00000000,
   1.364 +	0x00000008, 0x00000000,
   1.365 +	0x0000000a, 0x00000000,
   1.366 +	0x0000001b, 0x00000000,
   1.367 +	0x000002c7, 0x00000000,
   1.368 +	0x10000000, 0x00000000,
   1.369 +	0x00000000, 0x00000001,
   1.370 +	0x901cbfdc, 0x00000001,
   1.371 +	0xf9de6484, 0xb504f333,
   1.372 +	0xffffffff, 0xffffffff
   1.373 +	};
   1.374 +
   1.375 +_LIT8(KTestDec8,"0 1 8 100 6561 536870912 2147483648 4294967295 4294967296 549755813888 1000000000000000 9223372036854775807	\
   1.376 +					-9223372036854775808 -9223372036854775807 -9000000000000000000 -1099511627776 -4294967296 -1000 -1");
   1.377 +_LIT16(KTestDec16,"0 1 8 100 6561 536870912 2147483648 4294967295 4294967296 549755813888 1000000000000000 9223372036854775807	\
   1.378 +					-9223372036854775808 -9223372036854775807 -9000000000000000000 -1099511627776 -4294967296 -1000 -1");
   1.379 +
   1.380 +const TUint32 TestDecTable[] =
   1.381 +	{
   1.382 +	0x00000000, 0x00000000,
   1.383 +	0x00000001, 0x00000000,
   1.384 +	0x00000008, 0x00000000,
   1.385 +	0x00000064, 0x00000000,
   1.386 +	0x000019a1, 0x00000000,
   1.387 +	0x20000000, 0x00000000,
   1.388 +	0x80000000, 0x00000000,
   1.389 +	0xffffffff, 0x00000000,
   1.390 +	0x00000000, 0x00000001,
   1.391 +	0x00000000, 0x00000080,
   1.392 +	0xa4c68000, 0x00038d7e,
   1.393 +	0xffffffff, 0x7fffffff,
   1.394 +	0x00000000, 0x80000000,
   1.395 +	0x00000001, 0x80000000,
   1.396 +	0x1d7c0000, 0x831993af,
   1.397 +	0x00000000, 0xffffff00,
   1.398 +	0x00000000, 0xffffffff,
   1.399 +	0xfffffc18, 0xffffffff,
   1.400 +	0xffffffff, 0xffffffff
   1.401 +	};
   1.402 +
   1.403 +void Test5()
   1.404 +	{
   1.405 +	test.Next(_L("Test conversion to/from text"));
   1.406 +	TLex8 lex8;
   1.407 +	lex8.Assign(KTestHex8());
   1.408 +	TInt64 u;
   1.409 +	const TUint32* p = TestHexTable;
   1.410 +	for (; !lex8.Eos(); lex8.SkipSpace(), p+=2)
   1.411 +		{
   1.412 +		lex8.Mark();
   1.413 +		test(lex8.Val(u,EHex)==KErrNone);
   1.414 +		test(u == MAKE_TINT64(p[1], p[0]));
   1.415 +		TPtrC8 text = lex8.MarkedToken();
   1.416 +		TBuf8<64> b;
   1.417 +		b.Num(u,EHex);
   1.418 +		test(b==text);
   1.419 +		b.NumUC(u,EHex);
   1.420 +		TBuf8<64> uc = text;
   1.421 +		uc.UpperCase();
   1.422 +		test(b==uc);
   1.423 +		}
   1.424 +	lex8.Assign(KTestDec8());
   1.425 +	TInt64 s;
   1.426 +	p = TestDecTable;
   1.427 +	for (; !lex8.Eos(); lex8.SkipSpace(), p+=2)
   1.428 +		{
   1.429 +		lex8.Mark();
   1.430 +		test(lex8.Val(s)==KErrNone);
   1.431 +		test(s == MAKE_TINT64(p[1], p[0]));
   1.432 +		TPtrC8 text = lex8.MarkedToken();
   1.433 +		TBuf8<64> b;
   1.434 +		b.Num(s);
   1.435 +		test(b==text);
   1.436 +		}
   1.437 +
   1.438 +	TLex16 lex16;
   1.439 +	lex16.Assign(KTestHex16());
   1.440 +	p = TestHexTable;
   1.441 +	for (; !lex16.Eos(); lex16.SkipSpace(), p+=2)
   1.442 +		{
   1.443 +		lex16.Mark();
   1.444 +		test(lex16.Val(u,EHex)==KErrNone);
   1.445 +		test(u == MAKE_TINT64(p[1], p[0]));
   1.446 +		TPtrC16 text = lex16.MarkedToken();
   1.447 +		TBuf16<64> b;
   1.448 +		b.Num(u,EHex);
   1.449 +		test(b==text);
   1.450 +		b.NumUC(u,EHex);
   1.451 +		TBuf16<64> uc = text;
   1.452 +		uc.UpperCase();
   1.453 +		test(b==uc);
   1.454 +		}
   1.455 +	lex16.Assign(KTestDec16());
   1.456 +	p = TestDecTable;
   1.457 +	for (; !lex16.Eos(); lex16.SkipSpace(), p+=2)
   1.458 +		{
   1.459 +		lex16.Mark();
   1.460 +		test(lex16.Val(s)==KErrNone);
   1.461 +		test(s == MAKE_TINT64(p[1], p[0]));
   1.462 +		TPtrC16 text = lex16.MarkedToken();
   1.463 +		TBuf16<64> b;
   1.464 +		b.Num(s);
   1.465 +		test(b==text);
   1.466 +		}
   1.467 +	}
   1.468 +
   1.469 +GLDEF_C TInt E32Main()
   1.470 +    {
   1.471 +
   1.472 +	Seed[0] = 0xb8aa3b29;
   1.473 +	Seed[1] = 0;
   1.474 +
   1.475 +	test.Title();
   1.476 +	test.Start(_L("Testing 64 bit integers"));
   1.477 +
   1.478 +	Test1();
   1.479 +	Test2();
   1.480 +	Test3();
   1.481 +	Test4();
   1.482 +	Test5();
   1.483 +
   1.484 +	test.End();
   1.485 +	return(KErrNone);
   1.486 +    }
   1.487 +