backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / nsprpub / pr / tests / lltest.c
blob7e21e76f3d341ef15f86ae76a2149474461f3982
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 ** testll.c -- test suite for 64bit integer (longlong) operations
8 **
9 ** Summary: testll [-d] | [-h]
11 ** Where:
12 ** -d set debug mode on; displays individual test failures
13 ** -v verbose mode; displays progress in test, plus -d
14 ** -h gives usage message.
16 ** Description:
17 ** lltest.c tests the functions defined in NSPR 2.0's prlong.h.
18 **
19 ** Successive tests begin to depend on other LL functions working
20 ** correctly. So, ... Do not change the order of the tests as run
21 ** from main().
22 **
23 ** Caveats:
24 ** Do not even begin to think that this is an exhaustive test!
26 ** These tests try a little of everything, but not all boundary
27 ** conditions and limits are tested.
28 ** You want better coverage? ... Add it.
30 ** ---
31 ** Author: Lawrence Hardiman <larryh@netscape.com>.
32 ** ---
33 ** Revision History:
34 ** 01-Oct-1997. Original implementation.
38 #include "nspr.h"
39 #include "plgetopt.h"
41 /* --- Local Definitions --- */
42 #define ReportProgress(m) if (verboseMode) PR_fprintf(output, (m));
45 /* --- Global variables --- */
46 static PRIntn failedAlready = 0;
47 static PRFileDesc* output = NULL;
48 static PRBool debugMode = PR_FALSE;
49 static PRBool verboseMode = PR_FALSE;
52 ** Constants used in tests.
54 const PRInt64 bigZero = LL_INIT( 0, 0 );
55 const PRInt64 bigOne = LL_INIT( 0, 1 );
56 const PRInt64 bigTwo = LL_INIT( 0, 2 );
57 const PRInt64 bigSixTeen = LL_INIT( 0, 16 );
58 const PRInt64 bigThirtyTwo = LL_INIT( 0, 32 );
59 const PRInt64 bigMinusOne = LL_INIT( 0xffffffff, 0xffffffff );
60 const PRInt64 bigMinusTwo = LL_INIT( 0xffffffff, 0xfffffffe );
61 const PRInt64 bigNumber = LL_INIT( 0x7fffffff, 0xffffffff );
62 const PRInt64 bigMinusNumber = LL_INIT( 0x80000000, 0x00000001 );
63 const PRInt64 bigMaxInt32 = LL_INIT( 0x00000000, 0x7fffffff );
64 const PRInt64 big2To31 = LL_INIT( 0x00000000, 0x80000000 );
65 const PRUint64 bigZeroFox = LL_INIT( 0x00000000, 0xffffffff );
66 const PRUint64 bigFoxFox = LL_INIT( 0xffffffff, 0xffffffff );
67 const PRUint64 bigFoxZero = LL_INIT( 0xffffffff, 0x00000000 );
68 const PRUint64 bigEightZero = LL_INIT( 0x80000000, 0x00000000 );
69 const PRUint64 big64K = LL_INIT( 0x00000000, 0x00010000 );
70 const PRInt64 bigInt0 = LL_INIT( 0x01a00000, 0x00001000 );
71 const PRInt64 bigInt1 = LL_INIT( 0x01a00000, 0x00001100 );
72 const PRInt64 bigInt2 = LL_INIT( 0x01a00000, 0x00000100 );
73 const PRInt64 bigInt3 = LL_INIT( 0x01a00001, 0x00001000 );
74 const PRInt64 bigInt4 = LL_INIT( 0x01a00001, 0x00001100 );
75 const PRInt64 bigInt5 = LL_INIT( 0x01a00001, 0x00000100 );
76 const PRInt64 bigInt6 = LL_INIT( 0xb1a00000, 0x00001000 );
77 const PRInt64 bigInt7 = LL_INIT( 0xb1a00000, 0x00001100 );
78 const PRInt64 bigInt8 = LL_INIT( 0xb1a00000, 0x00000100 );
79 const PRInt64 bigInt9 = LL_INIT( 0xb1a00001, 0x00001000 );
80 const PRInt64 bigInt10 = LL_INIT( 0xb1a00001, 0x00001100 );
81 const PRInt64 bigInt11 = LL_INIT( 0xb1a00001, 0x00000100 );
82 const PRInt32 one = 1l;
83 const PRInt32 minusOne = -1l;
84 const PRInt32 sixteen = 16l;
85 const PRInt32 thirtyTwo = 32l;
86 const PRInt32 sixtyThree = 63l;
89 ** SetFailed() -- Report individual test failure
92 static void
93 SetFailed( char *what, char *how )
95 failedAlready = 1;
96 if ( debugMode )
97 PR_fprintf(output, "%s: failed: %s\n", what, how );
98 return;
101 static void
102 ResultFailed( char *what, char *how, PRInt64 expected, PRInt64 got)
104 if ( debugMode)
106 SetFailed( what, how );
107 PR_fprintf(output, "Expected: 0x%llx Got: 0x%llx\n", expected, got );
109 return;
114 ** TestAssignment() -- Test the assignment
116 static void TestAssignment( void )
118 PRInt64 zero = LL_Zero();
119 PRInt64 min = LL_MinInt();
120 PRInt64 max = LL_MaxInt();
121 if (!LL_EQ(zero, bigZero))
122 SetFailed("LL_EQ(zero, bigZero)", "!=");
123 if (!LL_CMP(max, >, min))
124 SetFailed("LL_CMP(max, >, min)", "!>");
128 ** TestComparisons() -- Test the longlong comparison operations
130 static void
131 TestComparisons( void )
133 ReportProgress("Testing Comparisons Operations\n");
135 /* test for zero */
136 if ( !LL_IS_ZERO( bigZero ))
137 SetFailed( "LL_IS_ZERO", "Zero is not zero" );
139 if ( LL_IS_ZERO( bigOne ))
140 SetFailed( "LL_IS_ZERO", "One tests as zero" );
142 if ( LL_IS_ZERO( bigMinusOne ))
143 SetFailed( "LL_IS_ZERO", "Minus One tests as zero" );
145 /* test equal */
146 if ( !LL_EQ( bigZero, bigZero ))
147 SetFailed( "LL_EQ", "zero EQ zero");
149 if ( !LL_EQ( bigOne, bigOne ))
150 SetFailed( "LL_EQ", "one EQ one" );
152 if ( !LL_EQ( bigNumber, bigNumber ))
153 SetFailed( "LL_EQ", "bigNumber EQ bigNumber" );
155 if ( !LL_EQ( bigMinusOne, bigMinusOne ))
156 SetFailed( "LL_EQ", "minus one EQ minus one");
158 if ( LL_EQ( bigZero, bigOne ))
159 SetFailed( "LL_EQ", "zero EQ one");
161 if ( LL_EQ( bigOne, bigZero ))
162 SetFailed( "LL_EQ", "one EQ zero" );
164 if ( LL_EQ( bigMinusOne, bigOne ))
165 SetFailed( "LL_EQ", "minus one EQ one");
167 if ( LL_EQ( bigNumber, bigOne ))
168 SetFailed( "LL_EQ", "bigNumber EQ one");
170 /* test not equal */
171 if ( LL_NE( bigZero, bigZero ))
172 SetFailed( "LL_NE", "0 NE 0");
174 if ( LL_NE( bigOne, bigOne ))
175 SetFailed( "LL_NE", "1 NE 1");
177 if ( LL_NE( bigMinusOne, bigMinusOne ))
178 SetFailed( "LL_NE", "-1 NE -1");
180 if ( LL_NE( bigNumber, bigNumber ))
181 SetFailed( "LL_NE", "n NE n");
183 if ( LL_NE( bigMinusNumber, bigMinusNumber ))
184 SetFailed( "LL_NE", "-n NE -n");
186 if ( !LL_NE( bigZero, bigOne))
187 SetFailed( "LL_NE", "0 NE 1");
189 if ( !LL_NE( bigOne, bigMinusNumber))
190 SetFailed( "LL_NE", "1 NE -n");
192 /* Greater than or equal to zero */
193 if ( !LL_GE_ZERO( bigZero ))
194 SetFailed( "LL_GE_ZERO", "0");
196 if ( !LL_GE_ZERO( bigOne ))
197 SetFailed( "LL_GE_ZERO", "1");
199 if ( !LL_GE_ZERO( bigNumber ))
200 SetFailed( "LL_GE_ZERO", "n");
202 if ( LL_GE_ZERO( bigMinusOne ))
203 SetFailed( "LL_GE_ZERO", "-1");
205 if ( LL_GE_ZERO( bigMinusNumber ))
206 SetFailed( "LL_GE_ZERO", "-n");
208 /* Algebraic Compare two values */
209 if ( !LL_CMP( bigZero, ==, bigZero ))
210 SetFailed( "LL_CMP", "0 == 0");
212 if ( LL_CMP( bigZero, >, bigZero ))
213 SetFailed( "LL_CMP", "0 > 0");
215 if ( LL_CMP( bigZero, <, bigZero ))
216 SetFailed( "LL_CMP", "0 < 0");
218 if ( LL_CMP( bigNumber, <, bigOne ))
219 SetFailed( "LL_CMP", "n < 1");
221 if ( !LL_CMP( bigNumber, >, bigOne ))
222 SetFailed( "LL_CMP", "n <= 1");
224 if ( LL_CMP( bigOne, >, bigNumber ))
225 SetFailed( "LL_CMP", "1 > n");
227 if ( LL_CMP( bigMinusNumber, >, bigNumber ))
228 SetFailed( "LL_CMP", "-n > n");
230 if ( LL_CMP( bigNumber, !=, bigNumber))
231 SetFailed( "LL_CMP", "n != n");
233 if ( !LL_CMP( bigMinusOne, >, bigMinusTwo ))
234 SetFailed( "LL_CMP", "-1 <= -2");
236 if ( !LL_CMP( bigMaxInt32, <, big2To31 ))
237 SetFailed( "LL_CMP", "Max 32-bit signed int >= 2^31");
239 /* Two positive numbers */
240 if ( !LL_CMP( bigInt0, <=, bigInt0 ))
241 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
243 if ( !LL_CMP( bigInt0, <=, bigInt1 ))
244 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
246 if ( LL_CMP( bigInt0, <=, bigInt2 ))
247 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
249 if ( !LL_CMP( bigInt0, <=, bigInt3 ))
250 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
252 if ( !LL_CMP( bigInt0, <=, bigInt4 ))
253 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
255 if ( !LL_CMP( bigInt0, <=, bigInt5 ))
256 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
258 /* Two negative numbers */
259 if ( !LL_CMP( bigInt6, <=, bigInt6 ))
260 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
262 if ( !LL_CMP( bigInt6, <=, bigInt7 ))
263 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
265 if ( LL_CMP( bigInt6, <=, bigInt8 ))
266 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
268 if ( !LL_CMP( bigInt6, <=, bigInt9 ))
269 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
271 if ( !LL_CMP( bigInt6, <=, bigInt10 ))
272 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
274 if ( !LL_CMP( bigInt6, <=, bigInt11 ))
275 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
277 /* One positive, one negative */
278 if ( LL_CMP( bigInt0, <=, bigInt6 ))
279 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
281 if ( LL_CMP( bigInt0, <=, bigInt7 ))
282 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
284 if ( LL_CMP( bigInt0, <=, bigInt8 ))
285 SetFailed( "LL_CMP", "LL_CMP(<=) failed");
287 /* Bitwise Compare two numbers */
288 if ( !LL_UCMP( bigZero, ==, bigZero ))
289 SetFailed( "LL_UCMP", "0 == 0");
291 if ( LL_UCMP( bigZero, >, bigZero ))
292 SetFailed( "LL_UCMP", "0 > 0");
294 if ( LL_UCMP( bigZero, <, bigZero ))
295 SetFailed( "LL_UCMP", "0 < 0");
297 if ( LL_UCMP( bigNumber, <, bigOne ))
298 SetFailed( "LL_UCMP", "n < 1");
300 if ( !LL_UCMP( bigNumber, >, bigOne ))
301 SetFailed( "LL_UCMP", "n < 1");
303 if ( LL_UCMP( bigOne, >, bigNumber ))
304 SetFailed( "LL_UCMP", "1 > n");
306 if ( LL_UCMP( bigMinusNumber, <, bigNumber ))
307 SetFailed( "LL_UCMP", "-n < n");
309 /* Two positive numbers */
310 if ( !LL_UCMP( bigInt0, <=, bigInt0 ))
311 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
313 if ( !LL_UCMP( bigInt0, <=, bigInt1 ))
314 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
316 if ( LL_UCMP( bigInt0, <=, bigInt2 ))
317 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
319 if ( !LL_UCMP( bigInt0, <=, bigInt3 ))
320 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
322 if ( !LL_UCMP( bigInt0, <=, bigInt4 ))
323 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
325 if ( !LL_UCMP( bigInt0, <=, bigInt5 ))
326 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
328 /* Two negative numbers */
329 if ( !LL_UCMP( bigInt6, <=, bigInt6 ))
330 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
332 if ( !LL_UCMP( bigInt6, <=, bigInt7 ))
333 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
335 if ( LL_UCMP( bigInt6, <=, bigInt8 ))
336 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
338 if ( !LL_UCMP( bigInt6, <=, bigInt9 ))
339 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
341 if ( !LL_UCMP( bigInt6, <=, bigInt10 ))
342 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
344 if ( !LL_UCMP( bigInt6, <=, bigInt11 ))
345 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
347 /* One positive, one negative */
348 if ( !LL_UCMP( bigInt0, <=, bigInt6 ))
349 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
351 if ( !LL_UCMP( bigInt0, <=, bigInt7 ))
352 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
354 if ( !LL_UCMP( bigInt0, <=, bigInt8 ))
355 SetFailed( "LL_UCMP", "LL_UCMP(<=) failed");
357 return;
361 ** TestLogicalOperations() -- Tests for AND, OR, ...
364 static void
365 TestLogicalOperations( void )
367 PRUint64 result, result2;
369 ReportProgress("Testing Logical Operations\n");
371 /* Test AND */
372 LL_AND( result, bigZero, bigZero );
373 if ( !LL_IS_ZERO( result ))
374 ResultFailed( "LL_AND", "0 & 0", bigZero, result );
376 LL_AND( result, bigOne, bigOne );
377 if ( LL_IS_ZERO( result ))
378 ResultFailed( "LL_AND", "1 & 1", bigOne, result );
380 LL_AND( result, bigZero, bigOne );
381 if ( !LL_IS_ZERO( result ))
382 ResultFailed( "LL_AND", "1 & 1", bigZero, result );
384 LL_AND( result, bigMinusOne, bigMinusOne );
385 if ( !LL_UCMP( result, ==, bigMinusOne ))
386 ResultFailed( "LL_AND", "-1 & -1", bigMinusOne, result );
388 /* test OR */
389 LL_OR( result, bigZero, bigZero );
390 if ( !LL_IS_ZERO( result ))
391 ResultFailed( "LL_OR", "0 | 1", bigZero, result);
393 LL_OR( result, bigZero, bigOne );
394 if ( LL_IS_ZERO( result ))
395 ResultFailed( "LL_OR", "0 | 1", bigOne, result );
397 LL_OR( result, bigZero, bigMinusNumber );
398 if ( !LL_UCMP( result, ==, bigMinusNumber ))
399 ResultFailed( "LL_OR", "0 | -n", bigMinusNumber, result);
401 LL_OR( result, bigMinusNumber, bigZero );
402 if ( !LL_UCMP( result, ==, bigMinusNumber ))
403 ResultFailed( "LL_OR", "-n | 0", bigMinusNumber, result );
405 /* test XOR */
406 LL_XOR( result, bigZero, bigZero );
407 if ( LL_UCMP( result, !=, bigZero ))
408 ResultFailed( "LL_XOR", "0 ^ 0", bigZero, result);
410 LL_XOR( result, bigOne, bigZero );
411 if ( LL_UCMP( result, !=, bigOne ))
412 ResultFailed( "LL_XOR", "1 ^ 0", bigZero, result );
414 LL_XOR( result, bigMinusNumber, bigZero );
415 if ( LL_UCMP( result, !=, bigMinusNumber ))
416 ResultFailed( "LL_XOR", "-n ^ 0", bigMinusNumber, result );
418 LL_XOR( result, bigMinusNumber, bigMinusNumber );
419 if ( LL_UCMP( result, !=, bigZero ))
420 ResultFailed( "LL_XOR", "-n ^ -n", bigMinusNumber, result);
422 /* test OR2. */
423 result = bigZero;
424 LL_OR2( result, bigOne );
425 if ( LL_UCMP( result, !=, bigOne ))
426 ResultFailed( "LL_OR2", "(r=0) |= 1", bigOne, result);
428 result = bigOne;
429 LL_OR2( result, bigNumber );
430 if ( LL_UCMP( result, !=, bigNumber ))
431 ResultFailed( "LL_OR2", "(r=1) |= n", bigNumber, result);
433 result = bigMinusNumber;
434 LL_OR2( result, bigMinusNumber );
435 if ( LL_UCMP( result, !=, bigMinusNumber ))
436 ResultFailed( "LL_OR2", "(r=-n) |= -n", bigMinusNumber, result);
438 /* test NOT */
439 LL_NOT( result, bigMinusNumber);
440 LL_NOT( result2, result);
441 if ( LL_UCMP( result2, !=, bigMinusNumber ))
442 ResultFailed( "LL_NOT", "r != ~(~-n)", bigMinusNumber, result);
444 /* test Negation */
445 LL_NEG( result, bigMinusNumber );
446 LL_NEG( result2, result );
447 if ( LL_CMP( result2, !=, bigMinusNumber ))
448 ResultFailed( "LL_NEG", "r != -(-(-n))", bigMinusNumber, result);
450 return;
456 ** TestConversion() -- Test Conversion Operations
459 static void
460 TestConversion( void )
462 PRInt64 result;
463 PRInt64 resultU;
464 PRInt32 result32;
465 PRUint32 resultU32;
466 float resultF;
467 PRFloat64 resultD;
469 ReportProgress("Testing Conversion Operations\n");
471 /* LL_L2I -- Convert to signed 32bit */
472 LL_L2I(result32, bigOne );
473 if ( result32 != one )
474 SetFailed( "LL_L2I", "r != 1");
476 LL_L2I(result32, bigMinusOne );
477 if ( result32 != minusOne )
478 SetFailed( "LL_L2I", "r != -1");
480 /* LL_L2UI -- Convert 64bit to unsigned 32bit */
481 LL_L2UI( resultU32, bigMinusOne );
482 if ( resultU32 != (PRUint32) minusOne )
483 SetFailed( "LL_L2UI", "r != -1");
485 LL_L2UI( resultU32, bigOne );
486 if ( resultU32 != (PRUint32) one )
487 SetFailed( "LL_L2UI", "r != 1");
489 /* LL_L2F -- Convert to 32bit floating point */
490 LL_L2F( resultF, bigOne );
491 if ( resultF != 1.0 )
492 SetFailed( "LL_L2F", "r != 1.0");
494 LL_L2F( resultF, bigMinusOne );
495 if ( resultF != -1.0 )
496 SetFailed( "LL_L2F", "r != 1.0");
498 /* LL_L2D -- Convert to 64bit floating point */
499 LL_L2D( resultD, bigOne );
500 if ( resultD != 1.0L )
501 SetFailed( "LL_L2D", "r != 1.0");
503 LL_L2D( resultD, bigMinusOne );
504 if ( resultD != -1.0L )
505 SetFailed( "LL_L2D", "r != -1.0");
507 /* LL_I2L -- Convert 32bit signed to 64bit signed */
508 LL_I2L( result, one );
509 if ( LL_CMP(result, !=, bigOne ))
510 SetFailed( "LL_I2L", "r != 1");
512 LL_I2L( result, minusOne );
513 if ( LL_CMP(result, !=, bigMinusOne ))
514 SetFailed( "LL_I2L", "r != -1");
516 /* LL_UI2L -- Convert 32bit unsigned to 64bit unsigned */
517 LL_UI2L( resultU, (PRUint32) one );
518 if ( LL_CMP(resultU, !=, bigOne ))
519 SetFailed( "LL_UI2L", "r != 1");
521 /* [lth.] This did not behave as expected, but it is correct
523 LL_UI2L( resultU, (PRUint32) minusOne );
524 if ( LL_CMP(resultU, !=, bigZeroFox ))
525 ResultFailed( "LL_UI2L", "r != -1", bigZeroFox, resultU);
527 /* LL_F2L -- Convert 32bit float to 64bit signed */
528 LL_F2L( result, 1.0 );
529 if ( LL_CMP(result, !=, bigOne ))
530 SetFailed( "LL_F2L", "r != 1");
532 LL_F2L( result, -1.0 );
533 if ( LL_CMP(result, !=, bigMinusOne ))
534 SetFailed( "LL_F2L", "r != -1");
536 /* LL_D2L -- Convert 64bit Float to 64bit signed */
537 LL_D2L( result, 1.0L );
538 if ( LL_CMP(result, !=, bigOne ))
539 SetFailed( "LL_D2L", "r != 1");
541 LL_D2L( result, -1.0L );
542 if ( LL_CMP(result, !=, bigMinusOne ))
543 SetFailed( "LL_D2L", "r != -1");
545 return;
548 static void ShiftCompileOnly()
551 ** This function is only compiled, never called.
552 ** The real test is to see if it compiles w/o
553 ** warnings. This is no small feat, by the way.
555 PRInt64 ia, ib;
556 PRUint64 ua, ub;
557 LL_SHR(ia, ib, 32);
558 LL_SHL(ia, ib, 32);
560 LL_USHR(ua, ub, 32);
561 LL_ISHL(ia, 49, 32);
563 } /* ShiftCompileOnly */
567 ** TestShift() -- Test Shifting Operations
570 static void
571 TestShift( void )
573 static const PRInt64 largeTwoZero = LL_INIT( 0x00000002, 0x00000000 );
574 PRInt64 result;
575 PRUint64 resultU;
577 ReportProgress("Testing Shifting Operations\n");
579 /* LL_SHL -- Shift left algebraic */
580 LL_SHL( result, bigOne, one );
581 if ( LL_CMP( result, !=, bigTwo ))
582 ResultFailed( "LL_SHL", "r != 2", bigOne, result );
584 LL_SHL( result, bigTwo, thirtyTwo );
585 if ( LL_CMP( result, !=, largeTwoZero ))
586 ResultFailed( "LL_SHL", "r != twoZero", largeTwoZero, result);
588 /* LL_SHR -- Shift right algebraic */
589 LL_SHR( result, bigFoxZero, thirtyTwo );
590 if ( LL_CMP( result, !=, bigMinusOne ))
591 ResultFailed( "LL_SHR", "r != -1", bigMinusOne, result);
593 LL_SHR( result, bigTwo, one );
594 if ( LL_CMP( result, !=, bigOne ))
595 ResultFailed( "LL_SHR", "r != 1", bigOne, result);
597 LL_SHR( result, bigFoxFox, thirtyTwo );
598 if ( LL_CMP( result, !=, bigMinusOne ))
599 ResultFailed( "LL_SHR", "r != -1 (was ff,ff)", bigMinusOne, result);
601 /* LL_USHR -- Logical shift right */
602 LL_USHR( resultU, bigZeroFox, thirtyTwo );
603 if ( LL_UCMP( resultU, !=, bigZero ))
604 ResultFailed( "LL_USHR", "r != 0 ", bigZero, result);
606 LL_USHR( resultU, bigFoxFox, thirtyTwo );
607 if ( LL_UCMP( resultU, !=, bigZeroFox ))
608 ResultFailed( "LL_USHR", "r != 0 ", bigZeroFox, result);
610 /* LL_ISHL -- Shift a 32bit integer into a 64bit result */
611 LL_ISHL( resultU, minusOne, thirtyTwo );
612 if ( LL_UCMP( resultU, !=, bigFoxZero ))
613 ResultFailed( "LL_ISHL", "r != ff,00 ", bigFoxZero, result);
615 LL_ISHL( resultU, one, sixtyThree );
616 if ( LL_UCMP( resultU, !=, bigEightZero ))
617 ResultFailed( "LL_ISHL", "r != 80,00 ", bigEightZero, result);
619 LL_ISHL( resultU, one, sixteen );
620 if ( LL_UCMP( resultU, !=, big64K ))
621 ResultFailed( "LL_ISHL", "r != 64K ", big64K, resultU);
623 return;
628 ** TestArithmetic() -- Test arithmetic operations.
631 static void
632 TestArithmetic( void )
634 PRInt64 largeVal = LL_INIT( 0x00000001, 0xffffffff );
635 PRInt64 largeValPlusOne = LL_INIT( 0x00000002, 0x00000000 );
636 PRInt64 largeValTimesTwo = LL_INIT( 0x00000003, 0xfffffffe );
637 PRInt64 largeMultCand = LL_INIT( 0x00000000, 0x7fffffff );
638 PRInt64 largeMinusMultCand = LL_INIT( 0xffffffff, 0x10000001 );
639 PRInt64 largeMultCandx64K = LL_INIT( 0x00007fff, 0xffff0000 );
640 PRInt64 largeNumSHL5 = LL_INIT( 0x0000001f, 0xffffffe0 );
641 PRInt64 result, result2;
643 /* Addition */
644 LL_ADD( result, bigOne, bigOne );
645 if ( LL_CMP( result, !=, bigTwo ))
646 ResultFailed( "LL_ADD", "r != 1 + 1", bigTwo, result);
648 LL_ADD( result, bigMinusOne, bigOne );
649 if ( LL_CMP( result, !=, bigZero ))
650 ResultFailed( "LL_ADD", "r != -1 + 1", bigOne, result);
652 LL_ADD( result, largeVal, bigOne );
653 if ( LL_CMP( result, !=, largeValPlusOne ))
654 ResultFailed( "LL_ADD", "lVP1 != lV + 1", largeValPlusOne, result);
656 /* Subtraction */
657 LL_SUB( result, bigOne, bigOne );
658 if ( LL_CMP( result, !=, bigZero ))
659 ResultFailed( "LL_SUB", "r != 1 - 1", bigZero, result);
661 LL_SUB( result, bigTwo, bigOne );
662 if ( LL_CMP( result, !=, bigOne ))
663 ResultFailed( "LL_SUB", "r != 2 - 1", bigOne, result);
665 LL_SUB( result, largeValPlusOne, bigOne );
666 if ( LL_CMP( result, !=, largeVal ))
667 ResultFailed( "LL_SUB", "r != lVP1 - 1", largeVal, result);
670 /* Multiply */
671 LL_MUL( result, largeVal, bigTwo );
672 if ( LL_CMP( result, !=, largeValTimesTwo ))
673 ResultFailed( "LL_MUL", "r != lV*2", largeValTimesTwo, result);
675 LL_MUL( result, largeMultCand, big64K );
676 if ( LL_CMP( result, !=, largeMultCandx64K ))
677 ResultFailed( "LL_MUL", "r != lV*64K", largeMultCandx64K, result);
679 LL_NEG( result2, largeMultCand );
680 LL_MUL( result, largeMultCand, bigMinusOne );
681 if ( LL_CMP( result, !=, result2 ))
682 ResultFailed( "LL_MUL", "r != -lMC", result2, result);
684 LL_SHL( result2, bigZeroFox, 5);
685 LL_MUL( result, bigZeroFox, bigThirtyTwo );
686 if ( LL_CMP( result, !=, largeNumSHL5 ))
687 ResultFailed( "LL_MUL", "r != 0f<<5", largeNumSHL5, result );
691 /* LL_DIV() Division */
692 LL_DIV( result, bigOne, bigOne);
693 if ( LL_CMP( result, !=, bigOne ))
694 ResultFailed( "LL_DIV", "1 != 1", bigOne, result);
696 LL_DIV( result, bigNumber, bigOne );
697 if ( LL_CMP( result, !=, bigNumber ))
698 ResultFailed( "LL_DIV", "r != n / 1", bigNumber, result);
700 LL_DIV( result, bigNumber, bigMinusOne );
701 if ( LL_CMP( result, !=, bigMinusNumber ))
702 ResultFailed( "LL_DIV", "r != n / -1", bigMinusNumber, result);
704 LL_DIV( result, bigMinusNumber, bigMinusOne );
705 if ( LL_CMP( result, !=, bigNumber ))
706 ResultFailed( "LL_DIV", "r != -n / -1", bigNumber, result);
708 LL_SHL( result2, bigZeroFox, 5 );
709 LL_DIV( result, result2, bigOne );
710 if ( LL_CMP( result, !=, result2 ))
711 ResultFailed( "LL_DIV", "0f<<5 != 0f<<5", result2, result);
713 LL_SHL( result2, bigZeroFox, 5 );
714 LL_NEG( result2, result2 );
715 LL_DIV( result, result2, bigOne );
716 if ( LL_CMP( result, !=, result2 ))
717 ResultFailed( "LL_DIV", "-0f<<5 != -0f<<5", result2, result);
719 LL_SHL( result2, bigZeroFox, 17 );
720 LL_DIV( result, result2, bigMinusOne );
721 LL_NEG( result2, result2 );
722 if ( LL_CMP( result, !=, result2 ))
723 ResultFailed( "LL_DIV", "-0f<<17 != -0f<<17", result2, result);
726 /* LL_MOD() Modulo Division */
727 LL_ADD( result2, bigThirtyTwo, bigOne );
728 LL_MOD( result, result2, bigSixTeen );
729 if ( LL_CMP( result, !=, bigOne ))
730 ResultFailed( "LL_MOD", "r != 1", bigSixTeen, result);
733 LL_MUL( result2, bigZeroFox, bigThirtyTwo );
734 LL_ADD( result2, result2, bigSixTeen);
735 LL_MOD( result, result2, bigThirtyTwo );
736 if ( LL_CMP( result, !=, bigSixTeen ))
737 ResultFailed( "LL_MOD", "r != 16", bigSixTeen, result);
739 /* LL_UDIVMOD */
740 LL_DIV( result, bigOne, bigOne);
741 if ( LL_CMP( result, !=, bigOne ))
742 ResultFailed( "LL_DIV", "r != 16", bigSixTeen, result);
745 return;
748 static void TestWellknowns(void)
750 PRInt64 max = LL_MAXINT, min = LL_MININT, zero = LL_ZERO;
751 PRInt64 mmax = LL_MaxInt(), mmin = LL_MinInt(), mzero = LL_Zero();
752 if (LL_NE(max, mmax))
753 ResultFailed( "max, mmax", "max != mmax", max, mmax);
754 if (LL_NE(min, mmin))
755 ResultFailed( "min, mmin", "min != mmin", max, mmin);
756 if (LL_NE(zero, mzero))
757 ResultFailed( "zero, mzero", "zero != mzero", zero, mzero);
758 } /* TestWellknowns */
761 ** Initialize() -- Initialize the test case
763 ** Parse command line options
766 static PRIntn
767 Initialize( PRIntn argc, char **argv )
769 PLOptState *opt = PL_CreateOptState(argc, argv, "dvh");
770 PLOptStatus os;
773 ** Parse command line options
775 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
777 if (PL_OPT_BAD == os) continue;
778 switch (opt->option)
780 case 'd': /* set debug mode */
781 debugMode = PR_TRUE;
782 break;
784 case 'v': /* set verbose mode */
785 verboseMode = PR_TRUE;
786 debugMode = PR_TRUE;
787 break;
789 case 'h': /* user wants some guidance */
790 default:
791 PR_fprintf(output, "You get help.\n");
792 return(1);
795 PL_DestroyOptState(opt);
796 return(0);
799 int main(int argc, char **argv)
801 PR_STDIO_INIT();
802 output = PR_GetSpecialFD(PR_StandardError);
804 if ( Initialize( argc, argv ))
805 return(1);
807 TestAssignment();
808 TestComparisons();
809 TestLogicalOperations();
810 TestConversion();
811 TestShift();
812 TestArithmetic();
813 TestWellknowns();
816 ** That's all folks!
818 if ( failedAlready )
820 PR_fprintf(output, "FAIL\n");\
822 else
824 PR_fprintf(output, "PASS\n");\
826 return failedAlready;
827 } /* end main() */