First public contribution.
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\math\t_i64_2.cpp
16 // Test 64-bit integer functionality.
20 // - Construct TInt64 and TUInt64 and verify the results.
21 // - Test the unary and shift operators and check results are as expected.
22 // - Test the + - * / and % operators, verify results are as expected.
23 // - Test the + - * / and % operators with random numbers, verify results
25 // - Test the conversion of TInt64 to/from TReal. Verify that the results
27 // - Test the conversion of TInt64 to/from text. Verify that the results
29 // Platforms/Drives/Compatibility:
31 // Assumptions/Requirement/Pre-requisites:
32 // Failures and causes:
33 // Base Port information:
40 #include "../misc/prbs.h"
42 typedef TLargeInt<2> I64;
43 typedef TLargeInt<4> I128;
45 RTest test(_L("T_I64_2"));
50 TUint h = Random(Seed);
51 TUint l = Random(Seed);
52 return MAKE_TUINT64(h,l);
55 #define BOOL(x) ((x)?1:0)
57 #define FOREACH(p, table, esize) \
58 for(p=table; p<(const TUint32*)((const TUint8*)table+sizeof(table)); p+=esize/sizeof(TUint32))
60 const TUint32 Table1[] =
62 0x00000000, 0x00000000,
63 0x00000001, 0x00000000,
64 0x0000cc01, 0x00000000,
65 0x00db2701, 0x00000000,
66 0xcc9ffcd1, 0x00000000,
67 0x00000000, 0xffffffff,
68 0xeeeeeeee, 0xffffffff,
69 0x04030201, 0x00000055,
70 0x04030201, 0x00006655,
71 0x04030201, 0x00776655,
72 0x04030201, 0x33776655,
73 0xf9de6484, 0xb504f333
78 test.Next(_L("Unary operators and shifts"));
84 TInt64 b = MAKE_TINT64(p[1], p[0]);
87 TUint64 d = MAKE_TUINT64(p[1], p[0]);
104 TInt64 s = MAKE_TINT64(p[1], p[0]);
105 TUint64 u = MAKE_TUINT64(p[1], p[0]);
109 I64 b(a), c(a), d(a);
110 b.Lsl(n), c.Lsr(n), d.Asr(n);
115 // test.Printf(_L("s2=%lx\ns3=%lx\n,u2=%lx\n,u3=%lx\n"),s2,s3,u2,u3);
124 const TUint32 Table2[] =
126 0x00000000, 0x00000000, 0x00000000, 0x00000000,
127 0x00000001, 0x00000000, 0x00000000, 0x00000000,
128 0x05f5e100, 0x00000000, 0x000000cb, 0x00000000,
129 0xffffff9c, 0xffffffff, 0x00129cbb, 0x00000000,
130 0xffffcd03, 0xffffffff, 0xffff9123, 0xffffffff,
131 0xf9de6484, 0xb504f333, 0xf9de6484, 0xb504f333,
132 0xf9de6484, 0xb504f333, 0x2168c235, 0xc90fdaa2,
133 0xf9de6484, 0xb504f333, 0x000000cb, 0x00000000,
134 0xf9de6484, 0xb504f333, 0x800000cb, 0x00000000,
135 0xf9de6484, 0xb504f333, 0x000000cb, 0x00000001,
136 0xf9de6484, 0xb504f333, 0xfffffed9, 0xffffffff,
137 0xf9de6484, 0xb504f333, 0x197383db, 0xffffffff,
138 0xf9de6484, 0xb504f333, 0x197383db, 0xffffffec,
139 0x38aa3b29, 0x5c17f0bc, 0x000019c7, 0x00000000,
140 0x38aa3b29, 0x5c17f0bc, 0x800019c7, 0x00000000,
141 0x38aa3b29, 0x5c17f0bc, 0x000019c7, 0x00000003,
142 0x38aa3b29, 0x5c17f0bc, 0x197383db, 0xffffffff,
143 0x38aa3b29, 0x5c17f0bc, 0x197383db, 0xffffffec,
144 0x00123456, 0x00000000, 0x8cb9fc1b, 0x00000000,
145 0x00000123, 0x00000000, 0x8cb9fc1b, 0x0000cc9f,
146 0xfffffe33, 0xffffffff, 0x8cb9fc1b, 0x0000cc9f
149 void Test2(const TUint32* p)
152 TInt64 x = MAKE_TINT64(p[1], p[0]);
153 TInt64 y = MAKE_TINT64(p[3], p[2]);
154 TUint64 u = MAKE_TUINT64(p[1], p[0]);
155 TUint64 v = MAKE_TUINT64(p[3], p[2]);
157 I64 c(a); c.Add(b); test(c==I64(x+y)); test(c==I64(u+v));
158 test(c==I64(y+x)); test(c==I64(v+u));
161 I64 c(a); c.Sub(b); test(c==I64(x-y)); test(c==I64(u-v));
162 I64 d(b); d.Sub(a); test(d==I64(y-x)); test(d==I64(v-u));
165 I64 c(a); c.Mul(b); test(c==I64(x*y)); test(c==I64(u*v));
166 test(c==I64(y*x)); test(c==I64(v*u));
169 I128 c = a.LongMultS(b);
171 Math::Mul64(x, y, *(TInt64*)(t+2), *(TUint64*)t);
173 Math::Mul64(y, x, *(TInt64*)(t+2), *(TUint64*)t);
177 I128 c = a.LongMultU(b);
179 Math::UMul64(u, v, *(TUint64*)(t+2), *(TUint64*)t);
181 Math::UMul64(v, u, *(TUint64*)(t+2), *(TUint64*)t);
186 I64 r; I64 q(a); q.DivS(b,r);
190 TInt64 q2 = Math::DivMod64(x, y, r2);
196 I64 r; I64 q(b); q.DivS(a,r);
200 TInt64 q2 = Math::DivMod64(y, x, r2);
206 I64 r; I64 q(a); q.DivU(b,r);
210 TUint64 q2 = Math::UDivMod64(u, v, r2);
216 I64 r; I64 q(b); q.DivU(a,r);
220 TUint64 q2 = Math::UDivMod64(v, u, r2);
225 TInt cmpu = a.CompareU(b);
226 TInt cmps = a.CompareS(b);
227 TInt equ = BOOL(u==v);
228 TInt neu = BOOL(u!=v);
230 TInt hs = BOOL(u>=v);
232 TInt ls = BOOL(u<=v);
234 TInt eqs = BOOL(x==y);
235 TInt nes = BOOL(x!=y);
237 TInt ge = BOOL(x>=y);
239 TInt le = BOOL(x<=y);
245 test(!equ && hi && hs && !lo && !ls);
247 test(!equ && !hi && !hs && lo && ls);
249 test(equ && !hi && hs && !lo && ls);
251 test(!eqs && gt && ge && !lt && !le);
253 test(!eqs && !gt && !ge && lt && le);
255 test(eqs && !gt && ge && !lt && le);
261 test.Next(_L("Test + - * / % (1)"));
271 test.Next(_L("Test + - * / % (2)"));
273 for (i=0; i<100; ++i)
286 test.Next(_L("Test conversion to/from TReal"));
288 TReal limit=1048576.0*1048576.0*8192.0;
289 TInt64 t22 = (TInt64)limit;
290 test(t22 == TInt64(1)<<53);
291 TInt64 t23 = (TInt64)(limit-1.0);
292 test(t23 == (TInt64(1)<<53)-1);
297 for (i=-99; i<100; i++)
305 TInt64 ll = (TInt64)x;
306 // test.Printf(_L("r64 %g -> i64 %lx (%lx)\n"), x, ll, l);
313 if (i==1 || i==0 || (i==-1 && l==TInt64(1)))
318 TReal i64limit = 1024.0*limit;
319 l=MAKE_TINT64(0x7fffffff,0xfffffc00);
321 test(x==i64limit-1024.0);
322 l=MAKE_TINT64(0x80000000,0x00000000);
325 l=MAKE_TINT64(0x80000000,0x00000400);
327 test(x==1024.0-i64limit);
328 l=MAKE_TINT64(0x00000001,0x00000000);
330 test(x==65536.0*65536.0);
331 l=MAKE_TINT64(0xffffffff,0x00000000);
333 test(x==-65536.0*65536.0);
335 for (i=-99; i<100; i++)
347 if (i==1 || i==0 || (i==-1 && l==TInt64(1)))
354 _LIT8(KTestHex8,"0 1 8 a 1b 2c7 10000000 100000000 1901cbfdc b504f333f9de6484 ffffffffffffffff");
355 _LIT16(KTestHex16,"0 1 8 a 1b 2c7 10000000 100000000 1901cbfdc b504f333f9de6484 ffffffffffffffff");
357 const TUint32 TestHexTable[] =
359 0x00000000, 0x00000000,
360 0x00000001, 0x00000000,
361 0x00000008, 0x00000000,
362 0x0000000a, 0x00000000,
363 0x0000001b, 0x00000000,
364 0x000002c7, 0x00000000,
365 0x10000000, 0x00000000,
366 0x00000000, 0x00000001,
367 0x901cbfdc, 0x00000001,
368 0xf9de6484, 0xb504f333,
369 0xffffffff, 0xffffffff
372 _LIT8(KTestDec8,"0 1 8 100 6561 536870912 2147483648 4294967295 4294967296 549755813888 1000000000000000 9223372036854775807 \
373 -9223372036854775808 -9223372036854775807 -9000000000000000000 -1099511627776 -4294967296 -1000 -1");
374 _LIT16(KTestDec16,"0 1 8 100 6561 536870912 2147483648 4294967295 4294967296 549755813888 1000000000000000 9223372036854775807 \
375 -9223372036854775808 -9223372036854775807 -9000000000000000000 -1099511627776 -4294967296 -1000 -1");
377 const TUint32 TestDecTable[] =
379 0x00000000, 0x00000000,
380 0x00000001, 0x00000000,
381 0x00000008, 0x00000000,
382 0x00000064, 0x00000000,
383 0x000019a1, 0x00000000,
384 0x20000000, 0x00000000,
385 0x80000000, 0x00000000,
386 0xffffffff, 0x00000000,
387 0x00000000, 0x00000001,
388 0x00000000, 0x00000080,
389 0xa4c68000, 0x00038d7e,
390 0xffffffff, 0x7fffffff,
391 0x00000000, 0x80000000,
392 0x00000001, 0x80000000,
393 0x1d7c0000, 0x831993af,
394 0x00000000, 0xffffff00,
395 0x00000000, 0xffffffff,
396 0xfffffc18, 0xffffffff,
397 0xffffffff, 0xffffffff
402 test.Next(_L("Test conversion to/from text"));
404 lex8.Assign(KTestHex8());
406 const TUint32* p = TestHexTable;
407 for (; !lex8.Eos(); lex8.SkipSpace(), p+=2)
410 test(lex8.Val(u,EHex)==KErrNone);
411 test(u == MAKE_TINT64(p[1], p[0]));
412 TPtrC8 text = lex8.MarkedToken();
421 lex8.Assign(KTestDec8());
424 for (; !lex8.Eos(); lex8.SkipSpace(), p+=2)
427 test(lex8.Val(s)==KErrNone);
428 test(s == MAKE_TINT64(p[1], p[0]));
429 TPtrC8 text = lex8.MarkedToken();
436 lex16.Assign(KTestHex16());
438 for (; !lex16.Eos(); lex16.SkipSpace(), p+=2)
441 test(lex16.Val(u,EHex)==KErrNone);
442 test(u == MAKE_TINT64(p[1], p[0]));
443 TPtrC16 text = lex16.MarkedToken();
448 TBuf16<64> uc = text;
452 lex16.Assign(KTestDec16());
454 for (; !lex16.Eos(); lex16.SkipSpace(), p+=2)
457 test(lex16.Val(s)==KErrNone);
458 test(s == MAKE_TINT64(p[1], p[0]));
459 TPtrC16 text = lex16.MarkedToken();
466 GLDEF_C TInt E32Main()
469 Seed[0] = 0xb8aa3b29;
473 test.Start(_L("Testing 64 bit integers"));