1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/lltest.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,827 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** testll.c -- test suite for 64bit integer (longlong) operations 1.11 +** 1.12 +** Summary: testll [-d] | [-h] 1.13 +** 1.14 +** Where: 1.15 +** -d set debug mode on; displays individual test failures 1.16 +** -v verbose mode; displays progress in test, plus -d 1.17 +** -h gives usage message. 1.18 +** 1.19 +** Description: 1.20 +** lltest.c tests the functions defined in NSPR 2.0's prlong.h. 1.21 +** 1.22 +** Successive tests begin to depend on other LL functions working 1.23 +** correctly. So, ... Do not change the order of the tests as run 1.24 +** from main(). 1.25 +** 1.26 +** Caveats: 1.27 +** Do not even begin to think that this is an exhaustive test! 1.28 +** 1.29 +** These tests try a little of everything, but not all boundary 1.30 +** conditions and limits are tested. 1.31 +** You want better coverage? ... Add it. 1.32 +** 1.33 +** --- 1.34 +** Author: Lawrence Hardiman <larryh@netscape.com>. 1.35 +** --- 1.36 +** Revision History: 1.37 +** 01-Oct-1997. Original implementation. 1.38 +** 1.39 +*/ 1.40 + 1.41 +#include "nspr.h" 1.42 +#include "plgetopt.h" 1.43 + 1.44 +/* --- Local Definitions --- */ 1.45 +#define ReportProgress(m) if (verboseMode) PR_fprintf(output, (m)); 1.46 + 1.47 + 1.48 +/* --- Global variables --- */ 1.49 +static PRIntn failedAlready = 0; 1.50 +static PRFileDesc* output = NULL; 1.51 +static PRBool debugMode = PR_FALSE; 1.52 +static PRBool verboseMode = PR_FALSE; 1.53 + 1.54 +/* 1.55 +** Constants used in tests. 1.56 +*/ 1.57 +const PRInt64 bigZero = LL_INIT( 0, 0 ); 1.58 +const PRInt64 bigOne = LL_INIT( 0, 1 ); 1.59 +const PRInt64 bigTwo = LL_INIT( 0, 2 ); 1.60 +const PRInt64 bigSixTeen = LL_INIT( 0, 16 ); 1.61 +const PRInt64 bigThirtyTwo = LL_INIT( 0, 32 ); 1.62 +const PRInt64 bigMinusOne = LL_INIT( 0xffffffff, 0xffffffff ); 1.63 +const PRInt64 bigMinusTwo = LL_INIT( 0xffffffff, 0xfffffffe ); 1.64 +const PRInt64 bigNumber = LL_INIT( 0x7fffffff, 0xffffffff ); 1.65 +const PRInt64 bigMinusNumber = LL_INIT( 0x80000000, 0x00000001 ); 1.66 +const PRInt64 bigMaxInt32 = LL_INIT( 0x00000000, 0x7fffffff ); 1.67 +const PRInt64 big2To31 = LL_INIT( 0x00000000, 0x80000000 ); 1.68 +const PRUint64 bigZeroFox = LL_INIT( 0x00000000, 0xffffffff ); 1.69 +const PRUint64 bigFoxFox = LL_INIT( 0xffffffff, 0xffffffff ); 1.70 +const PRUint64 bigFoxZero = LL_INIT( 0xffffffff, 0x00000000 ); 1.71 +const PRUint64 bigEightZero = LL_INIT( 0x80000000, 0x00000000 ); 1.72 +const PRUint64 big64K = LL_INIT( 0x00000000, 0x00010000 ); 1.73 +const PRInt64 bigInt0 = LL_INIT( 0x01a00000, 0x00001000 ); 1.74 +const PRInt64 bigInt1 = LL_INIT( 0x01a00000, 0x00001100 ); 1.75 +const PRInt64 bigInt2 = LL_INIT( 0x01a00000, 0x00000100 ); 1.76 +const PRInt64 bigInt3 = LL_INIT( 0x01a00001, 0x00001000 ); 1.77 +const PRInt64 bigInt4 = LL_INIT( 0x01a00001, 0x00001100 ); 1.78 +const PRInt64 bigInt5 = LL_INIT( 0x01a00001, 0x00000100 ); 1.79 +const PRInt64 bigInt6 = LL_INIT( 0xb1a00000, 0x00001000 ); 1.80 +const PRInt64 bigInt7 = LL_INIT( 0xb1a00000, 0x00001100 ); 1.81 +const PRInt64 bigInt8 = LL_INIT( 0xb1a00000, 0x00000100 ); 1.82 +const PRInt64 bigInt9 = LL_INIT( 0xb1a00001, 0x00001000 ); 1.83 +const PRInt64 bigInt10 = LL_INIT( 0xb1a00001, 0x00001100 ); 1.84 +const PRInt64 bigInt11 = LL_INIT( 0xb1a00001, 0x00000100 ); 1.85 +const PRInt32 one = 1l; 1.86 +const PRInt32 minusOne = -1l; 1.87 +const PRInt32 sixteen = 16l; 1.88 +const PRInt32 thirtyTwo = 32l; 1.89 +const PRInt32 sixtyThree = 63l; 1.90 + 1.91 +/* 1.92 +** SetFailed() -- Report individual test failure 1.93 +** 1.94 +*/ 1.95 +static void 1.96 +SetFailed( char *what, char *how ) 1.97 +{ 1.98 + failedAlready = 1; 1.99 + if ( debugMode ) 1.100 + PR_fprintf(output, "%s: failed: %s\n", what, how ); 1.101 + return; 1.102 +} 1.103 + 1.104 +static void 1.105 +ResultFailed( char *what, char *how, PRInt64 expected, PRInt64 got) 1.106 +{ 1.107 + if ( debugMode) 1.108 + { 1.109 + SetFailed( what, how ); 1.110 + PR_fprintf(output, "Expected: 0x%llx Got: 0x%llx\n", expected, got ); 1.111 + } 1.112 + return; 1.113 +} 1.114 + 1.115 + 1.116 +/* 1.117 +** TestAssignment() -- Test the assignment 1.118 +*/ 1.119 +static void TestAssignment( void ) 1.120 +{ 1.121 + PRInt64 zero = LL_Zero(); 1.122 + PRInt64 min = LL_MinInt(); 1.123 + PRInt64 max = LL_MaxInt(); 1.124 + if (!LL_EQ(zero, bigZero)) 1.125 + SetFailed("LL_EQ(zero, bigZero)", "!="); 1.126 + if (!LL_CMP(max, >, min)) 1.127 + SetFailed("LL_CMP(max, >, min)", "!>"); 1.128 +} 1.129 + 1.130 +/* 1.131 +** TestComparisons() -- Test the longlong comparison operations 1.132 +*/ 1.133 +static void 1.134 +TestComparisons( void ) 1.135 +{ 1.136 + ReportProgress("Testing Comparisons Operations\n"); 1.137 + 1.138 + /* test for zero */ 1.139 + if ( !LL_IS_ZERO( bigZero )) 1.140 + SetFailed( "LL_IS_ZERO", "Zero is not zero" ); 1.141 + 1.142 + if ( LL_IS_ZERO( bigOne )) 1.143 + SetFailed( "LL_IS_ZERO", "One tests as zero" ); 1.144 + 1.145 + if ( LL_IS_ZERO( bigMinusOne )) 1.146 + SetFailed( "LL_IS_ZERO", "Minus One tests as zero" ); 1.147 + 1.148 + /* test equal */ 1.149 + if ( !LL_EQ( bigZero, bigZero )) 1.150 + SetFailed( "LL_EQ", "zero EQ zero"); 1.151 + 1.152 + if ( !LL_EQ( bigOne, bigOne )) 1.153 + SetFailed( "LL_EQ", "one EQ one" ); 1.154 + 1.155 + if ( !LL_EQ( bigNumber, bigNumber )) 1.156 + SetFailed( "LL_EQ", "bigNumber EQ bigNumber" ); 1.157 + 1.158 + if ( !LL_EQ( bigMinusOne, bigMinusOne )) 1.159 + SetFailed( "LL_EQ", "minus one EQ minus one"); 1.160 + 1.161 + if ( LL_EQ( bigZero, bigOne )) 1.162 + SetFailed( "LL_EQ", "zero EQ one"); 1.163 + 1.164 + if ( LL_EQ( bigOne, bigZero )) 1.165 + SetFailed( "LL_EQ", "one EQ zero" ); 1.166 + 1.167 + if ( LL_EQ( bigMinusOne, bigOne )) 1.168 + SetFailed( "LL_EQ", "minus one EQ one"); 1.169 + 1.170 + if ( LL_EQ( bigNumber, bigOne )) 1.171 + SetFailed( "LL_EQ", "bigNumber EQ one"); 1.172 + 1.173 + /* test not equal */ 1.174 + if ( LL_NE( bigZero, bigZero )) 1.175 + SetFailed( "LL_NE", "0 NE 0"); 1.176 + 1.177 + if ( LL_NE( bigOne, bigOne )) 1.178 + SetFailed( "LL_NE", "1 NE 1"); 1.179 + 1.180 + if ( LL_NE( bigMinusOne, bigMinusOne )) 1.181 + SetFailed( "LL_NE", "-1 NE -1"); 1.182 + 1.183 + if ( LL_NE( bigNumber, bigNumber )) 1.184 + SetFailed( "LL_NE", "n NE n"); 1.185 + 1.186 + if ( LL_NE( bigMinusNumber, bigMinusNumber )) 1.187 + SetFailed( "LL_NE", "-n NE -n"); 1.188 + 1.189 + if ( !LL_NE( bigZero, bigOne)) 1.190 + SetFailed( "LL_NE", "0 NE 1"); 1.191 + 1.192 + if ( !LL_NE( bigOne, bigMinusNumber)) 1.193 + SetFailed( "LL_NE", "1 NE -n"); 1.194 + 1.195 + /* Greater than or equal to zero */ 1.196 + if ( !LL_GE_ZERO( bigZero )) 1.197 + SetFailed( "LL_GE_ZERO", "0"); 1.198 + 1.199 + if ( !LL_GE_ZERO( bigOne )) 1.200 + SetFailed( "LL_GE_ZERO", "1"); 1.201 + 1.202 + if ( !LL_GE_ZERO( bigNumber )) 1.203 + SetFailed( "LL_GE_ZERO", "n"); 1.204 + 1.205 + if ( LL_GE_ZERO( bigMinusOne )) 1.206 + SetFailed( "LL_GE_ZERO", "-1"); 1.207 + 1.208 + if ( LL_GE_ZERO( bigMinusNumber )) 1.209 + SetFailed( "LL_GE_ZERO", "-n"); 1.210 + 1.211 + /* Algebraic Compare two values */ 1.212 + if ( !LL_CMP( bigZero, ==, bigZero )) 1.213 + SetFailed( "LL_CMP", "0 == 0"); 1.214 + 1.215 + if ( LL_CMP( bigZero, >, bigZero )) 1.216 + SetFailed( "LL_CMP", "0 > 0"); 1.217 + 1.218 + if ( LL_CMP( bigZero, <, bigZero )) 1.219 + SetFailed( "LL_CMP", "0 < 0"); 1.220 + 1.221 + if ( LL_CMP( bigNumber, <, bigOne )) 1.222 + SetFailed( "LL_CMP", "n < 1"); 1.223 + 1.224 + if ( !LL_CMP( bigNumber, >, bigOne )) 1.225 + SetFailed( "LL_CMP", "n <= 1"); 1.226 + 1.227 + if ( LL_CMP( bigOne, >, bigNumber )) 1.228 + SetFailed( "LL_CMP", "1 > n"); 1.229 + 1.230 + if ( LL_CMP( bigMinusNumber, >, bigNumber )) 1.231 + SetFailed( "LL_CMP", "-n > n"); 1.232 + 1.233 + if ( LL_CMP( bigNumber, !=, bigNumber)) 1.234 + SetFailed( "LL_CMP", "n != n"); 1.235 + 1.236 + if ( !LL_CMP( bigMinusOne, >, bigMinusTwo )) 1.237 + SetFailed( "LL_CMP", "-1 <= -2"); 1.238 + 1.239 + if ( !LL_CMP( bigMaxInt32, <, big2To31 )) 1.240 + SetFailed( "LL_CMP", "Max 32-bit signed int >= 2^31"); 1.241 + 1.242 + /* Two positive numbers */ 1.243 + if ( !LL_CMP( bigInt0, <=, bigInt0 )) 1.244 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.245 + 1.246 + if ( !LL_CMP( bigInt0, <=, bigInt1 )) 1.247 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.248 + 1.249 + if ( LL_CMP( bigInt0, <=, bigInt2 )) 1.250 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.251 + 1.252 + if ( !LL_CMP( bigInt0, <=, bigInt3 )) 1.253 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.254 + 1.255 + if ( !LL_CMP( bigInt0, <=, bigInt4 )) 1.256 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.257 + 1.258 + if ( !LL_CMP( bigInt0, <=, bigInt5 )) 1.259 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.260 + 1.261 + /* Two negative numbers */ 1.262 + if ( !LL_CMP( bigInt6, <=, bigInt6 )) 1.263 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.264 + 1.265 + if ( !LL_CMP( bigInt6, <=, bigInt7 )) 1.266 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.267 + 1.268 + if ( LL_CMP( bigInt6, <=, bigInt8 )) 1.269 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.270 + 1.271 + if ( !LL_CMP( bigInt6, <=, bigInt9 )) 1.272 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.273 + 1.274 + if ( !LL_CMP( bigInt6, <=, bigInt10 )) 1.275 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.276 + 1.277 + if ( !LL_CMP( bigInt6, <=, bigInt11 )) 1.278 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.279 + 1.280 + /* One positive, one negative */ 1.281 + if ( LL_CMP( bigInt0, <=, bigInt6 )) 1.282 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.283 + 1.284 + if ( LL_CMP( bigInt0, <=, bigInt7 )) 1.285 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.286 + 1.287 + if ( LL_CMP( bigInt0, <=, bigInt8 )) 1.288 + SetFailed( "LL_CMP", "LL_CMP(<=) failed"); 1.289 + 1.290 + /* Bitwise Compare two numbers */ 1.291 + if ( !LL_UCMP( bigZero, ==, bigZero )) 1.292 + SetFailed( "LL_UCMP", "0 == 0"); 1.293 + 1.294 + if ( LL_UCMP( bigZero, >, bigZero )) 1.295 + SetFailed( "LL_UCMP", "0 > 0"); 1.296 + 1.297 + if ( LL_UCMP( bigZero, <, bigZero )) 1.298 + SetFailed( "LL_UCMP", "0 < 0"); 1.299 + 1.300 + if ( LL_UCMP( bigNumber, <, bigOne )) 1.301 + SetFailed( "LL_UCMP", "n < 1"); 1.302 + 1.303 + if ( !LL_UCMP( bigNumber, >, bigOne )) 1.304 + SetFailed( "LL_UCMP", "n < 1"); 1.305 + 1.306 + if ( LL_UCMP( bigOne, >, bigNumber )) 1.307 + SetFailed( "LL_UCMP", "1 > n"); 1.308 + 1.309 + if ( LL_UCMP( bigMinusNumber, <, bigNumber )) 1.310 + SetFailed( "LL_UCMP", "-n < n"); 1.311 + 1.312 + /* Two positive numbers */ 1.313 + if ( !LL_UCMP( bigInt0, <=, bigInt0 )) 1.314 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.315 + 1.316 + if ( !LL_UCMP( bigInt0, <=, bigInt1 )) 1.317 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.318 + 1.319 + if ( LL_UCMP( bigInt0, <=, bigInt2 )) 1.320 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.321 + 1.322 + if ( !LL_UCMP( bigInt0, <=, bigInt3 )) 1.323 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.324 + 1.325 + if ( !LL_UCMP( bigInt0, <=, bigInt4 )) 1.326 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.327 + 1.328 + if ( !LL_UCMP( bigInt0, <=, bigInt5 )) 1.329 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.330 + 1.331 + /* Two negative numbers */ 1.332 + if ( !LL_UCMP( bigInt6, <=, bigInt6 )) 1.333 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.334 + 1.335 + if ( !LL_UCMP( bigInt6, <=, bigInt7 )) 1.336 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.337 + 1.338 + if ( LL_UCMP( bigInt6, <=, bigInt8 )) 1.339 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.340 + 1.341 + if ( !LL_UCMP( bigInt6, <=, bigInt9 )) 1.342 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.343 + 1.344 + if ( !LL_UCMP( bigInt6, <=, bigInt10 )) 1.345 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.346 + 1.347 + if ( !LL_UCMP( bigInt6, <=, bigInt11 )) 1.348 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.349 + 1.350 + /* One positive, one negative */ 1.351 + if ( !LL_UCMP( bigInt0, <=, bigInt6 )) 1.352 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.353 + 1.354 + if ( !LL_UCMP( bigInt0, <=, bigInt7 )) 1.355 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.356 + 1.357 + if ( !LL_UCMP( bigInt0, <=, bigInt8 )) 1.358 + SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); 1.359 + 1.360 + return; 1.361 +} 1.362 + 1.363 +/* 1.364 +** TestLogicalOperations() -- Tests for AND, OR, ... 1.365 +** 1.366 +*/ 1.367 +static void 1.368 +TestLogicalOperations( void ) 1.369 +{ 1.370 + PRUint64 result, result2; 1.371 + 1.372 + ReportProgress("Testing Logical Operations\n"); 1.373 + 1.374 + /* Test AND */ 1.375 + LL_AND( result, bigZero, bigZero ); 1.376 + if ( !LL_IS_ZERO( result )) 1.377 + ResultFailed( "LL_AND", "0 & 0", bigZero, result ); 1.378 + 1.379 + LL_AND( result, bigOne, bigOne ); 1.380 + if ( LL_IS_ZERO( result )) 1.381 + ResultFailed( "LL_AND", "1 & 1", bigOne, result ); 1.382 + 1.383 + LL_AND( result, bigZero, bigOne ); 1.384 + if ( !LL_IS_ZERO( result )) 1.385 + ResultFailed( "LL_AND", "1 & 1", bigZero, result ); 1.386 + 1.387 + LL_AND( result, bigMinusOne, bigMinusOne ); 1.388 + if ( !LL_UCMP( result, ==, bigMinusOne )) 1.389 + ResultFailed( "LL_AND", "-1 & -1", bigMinusOne, result ); 1.390 + 1.391 + /* test OR */ 1.392 + LL_OR( result, bigZero, bigZero ); 1.393 + if ( !LL_IS_ZERO( result )) 1.394 + ResultFailed( "LL_OR", "0 | 1", bigZero, result); 1.395 + 1.396 + LL_OR( result, bigZero, bigOne ); 1.397 + if ( LL_IS_ZERO( result )) 1.398 + ResultFailed( "LL_OR", "0 | 1", bigOne, result ); 1.399 + 1.400 + LL_OR( result, bigZero, bigMinusNumber ); 1.401 + if ( !LL_UCMP( result, ==, bigMinusNumber )) 1.402 + ResultFailed( "LL_OR", "0 | -n", bigMinusNumber, result); 1.403 + 1.404 + LL_OR( result, bigMinusNumber, bigZero ); 1.405 + if ( !LL_UCMP( result, ==, bigMinusNumber )) 1.406 + ResultFailed( "LL_OR", "-n | 0", bigMinusNumber, result ); 1.407 + 1.408 + /* test XOR */ 1.409 + LL_XOR( result, bigZero, bigZero ); 1.410 + if ( LL_UCMP( result, !=, bigZero )) 1.411 + ResultFailed( "LL_XOR", "0 ^ 0", bigZero, result); 1.412 + 1.413 + LL_XOR( result, bigOne, bigZero ); 1.414 + if ( LL_UCMP( result, !=, bigOne )) 1.415 + ResultFailed( "LL_XOR", "1 ^ 0", bigZero, result ); 1.416 + 1.417 + LL_XOR( result, bigMinusNumber, bigZero ); 1.418 + if ( LL_UCMP( result, !=, bigMinusNumber )) 1.419 + ResultFailed( "LL_XOR", "-n ^ 0", bigMinusNumber, result ); 1.420 + 1.421 + LL_XOR( result, bigMinusNumber, bigMinusNumber ); 1.422 + if ( LL_UCMP( result, !=, bigZero )) 1.423 + ResultFailed( "LL_XOR", "-n ^ -n", bigMinusNumber, result); 1.424 + 1.425 + /* test OR2. */ 1.426 + result = bigZero; 1.427 + LL_OR2( result, bigOne ); 1.428 + if ( LL_UCMP( result, !=, bigOne )) 1.429 + ResultFailed( "LL_OR2", "(r=0) |= 1", bigOne, result); 1.430 + 1.431 + result = bigOne; 1.432 + LL_OR2( result, bigNumber ); 1.433 + if ( LL_UCMP( result, !=, bigNumber )) 1.434 + ResultFailed( "LL_OR2", "(r=1) |= n", bigNumber, result); 1.435 + 1.436 + result = bigMinusNumber; 1.437 + LL_OR2( result, bigMinusNumber ); 1.438 + if ( LL_UCMP( result, !=, bigMinusNumber )) 1.439 + ResultFailed( "LL_OR2", "(r=-n) |= -n", bigMinusNumber, result); 1.440 + 1.441 + /* test NOT */ 1.442 + LL_NOT( result, bigMinusNumber); 1.443 + LL_NOT( result2, result); 1.444 + if ( LL_UCMP( result2, !=, bigMinusNumber )) 1.445 + ResultFailed( "LL_NOT", "r != ~(~-n)", bigMinusNumber, result); 1.446 + 1.447 + /* test Negation */ 1.448 + LL_NEG( result, bigMinusNumber ); 1.449 + LL_NEG( result2, result ); 1.450 + if ( LL_CMP( result2, !=, bigMinusNumber )) 1.451 + ResultFailed( "LL_NEG", "r != -(-(-n))", bigMinusNumber, result); 1.452 + 1.453 + return; 1.454 +} 1.455 + 1.456 + 1.457 + 1.458 +/* 1.459 +** TestConversion() -- Test Conversion Operations 1.460 +** 1.461 +*/ 1.462 +static void 1.463 +TestConversion( void ) 1.464 +{ 1.465 + PRInt64 result; 1.466 + PRInt64 resultU; 1.467 + PRInt32 result32; 1.468 + PRUint32 resultU32; 1.469 + float resultF; 1.470 + PRFloat64 resultD; 1.471 + 1.472 + ReportProgress("Testing Conversion Operations\n"); 1.473 + 1.474 + /* LL_L2I -- Convert to signed 32bit */ 1.475 + LL_L2I(result32, bigOne ); 1.476 + if ( result32 != one ) 1.477 + SetFailed( "LL_L2I", "r != 1"); 1.478 + 1.479 + LL_L2I(result32, bigMinusOne ); 1.480 + if ( result32 != minusOne ) 1.481 + SetFailed( "LL_L2I", "r != -1"); 1.482 + 1.483 + /* LL_L2UI -- Convert 64bit to unsigned 32bit */ 1.484 + LL_L2UI( resultU32, bigMinusOne ); 1.485 + if ( resultU32 != (PRUint32) minusOne ) 1.486 + SetFailed( "LL_L2UI", "r != -1"); 1.487 + 1.488 + LL_L2UI( resultU32, bigOne ); 1.489 + if ( resultU32 != (PRUint32) one ) 1.490 + SetFailed( "LL_L2UI", "r != 1"); 1.491 + 1.492 + /* LL_L2F -- Convert to 32bit floating point */ 1.493 + LL_L2F( resultF, bigOne ); 1.494 + if ( resultF != 1.0 ) 1.495 + SetFailed( "LL_L2F", "r != 1.0"); 1.496 + 1.497 + LL_L2F( resultF, bigMinusOne ); 1.498 + if ( resultF != -1.0 ) 1.499 + SetFailed( "LL_L2F", "r != 1.0"); 1.500 + 1.501 + /* LL_L2D -- Convert to 64bit floating point */ 1.502 + LL_L2D( resultD, bigOne ); 1.503 + if ( resultD != 1.0L ) 1.504 + SetFailed( "LL_L2D", "r != 1.0"); 1.505 + 1.506 + LL_L2D( resultD, bigMinusOne ); 1.507 + if ( resultD != -1.0L ) 1.508 + SetFailed( "LL_L2D", "r != -1.0"); 1.509 + 1.510 + /* LL_I2L -- Convert 32bit signed to 64bit signed */ 1.511 + LL_I2L( result, one ); 1.512 + if ( LL_CMP(result, !=, bigOne )) 1.513 + SetFailed( "LL_I2L", "r != 1"); 1.514 + 1.515 + LL_I2L( result, minusOne ); 1.516 + if ( LL_CMP(result, !=, bigMinusOne )) 1.517 + SetFailed( "LL_I2L", "r != -1"); 1.518 + 1.519 + /* LL_UI2L -- Convert 32bit unsigned to 64bit unsigned */ 1.520 + LL_UI2L( resultU, (PRUint32) one ); 1.521 + if ( LL_CMP(resultU, !=, bigOne )) 1.522 + SetFailed( "LL_UI2L", "r != 1"); 1.523 + 1.524 + /* [lth.] This did not behave as expected, but it is correct 1.525 + */ 1.526 + LL_UI2L( resultU, (PRUint32) minusOne ); 1.527 + if ( LL_CMP(resultU, !=, bigZeroFox )) 1.528 + ResultFailed( "LL_UI2L", "r != -1", bigZeroFox, resultU); 1.529 + 1.530 + /* LL_F2L -- Convert 32bit float to 64bit signed */ 1.531 + LL_F2L( result, 1.0 ); 1.532 + if ( LL_CMP(result, !=, bigOne )) 1.533 + SetFailed( "LL_F2L", "r != 1"); 1.534 + 1.535 + LL_F2L( result, -1.0 ); 1.536 + if ( LL_CMP(result, !=, bigMinusOne )) 1.537 + SetFailed( "LL_F2L", "r != -1"); 1.538 + 1.539 + /* LL_D2L -- Convert 64bit Float to 64bit signed */ 1.540 + LL_D2L( result, 1.0L ); 1.541 + if ( LL_CMP(result, !=, bigOne )) 1.542 + SetFailed( "LL_D2L", "r != 1"); 1.543 + 1.544 + LL_D2L( result, -1.0L ); 1.545 + if ( LL_CMP(result, !=, bigMinusOne )) 1.546 + SetFailed( "LL_D2L", "r != -1"); 1.547 + 1.548 + return; 1.549 +} 1.550 + 1.551 +static void ShiftCompileOnly() 1.552 +{ 1.553 + /* 1.554 + ** This function is only compiled, never called. 1.555 + ** The real test is to see if it compiles w/o 1.556 + ** warnings. This is no small feat, by the way. 1.557 + */ 1.558 + PRInt64 ia, ib; 1.559 + PRUint64 ua, ub; 1.560 + LL_SHR(ia, ib, 32); 1.561 + LL_SHL(ia, ib, 32); 1.562 + 1.563 + LL_USHR(ua, ub, 32); 1.564 + LL_ISHL(ia, 49, 32); 1.565 + 1.566 +} /* ShiftCompileOnly */ 1.567 + 1.568 + 1.569 +/* 1.570 +** TestShift() -- Test Shifting Operations 1.571 +** 1.572 +*/ 1.573 +static void 1.574 +TestShift( void ) 1.575 +{ 1.576 + static const PRInt64 largeTwoZero = LL_INIT( 0x00000002, 0x00000000 ); 1.577 + PRInt64 result; 1.578 + PRUint64 resultU; 1.579 + 1.580 + ReportProgress("Testing Shifting Operations\n"); 1.581 + 1.582 + /* LL_SHL -- Shift left algebraic */ 1.583 + LL_SHL( result, bigOne, one ); 1.584 + if ( LL_CMP( result, !=, bigTwo )) 1.585 + ResultFailed( "LL_SHL", "r != 2", bigOne, result ); 1.586 + 1.587 + LL_SHL( result, bigTwo, thirtyTwo ); 1.588 + if ( LL_CMP( result, !=, largeTwoZero )) 1.589 + ResultFailed( "LL_SHL", "r != twoZero", largeTwoZero, result); 1.590 + 1.591 + /* LL_SHR -- Shift right algebraic */ 1.592 + LL_SHR( result, bigFoxZero, thirtyTwo ); 1.593 + if ( LL_CMP( result, !=, bigMinusOne )) 1.594 + ResultFailed( "LL_SHR", "r != -1", bigMinusOne, result); 1.595 + 1.596 + LL_SHR( result, bigTwo, one ); 1.597 + if ( LL_CMP( result, !=, bigOne )) 1.598 + ResultFailed( "LL_SHR", "r != 1", bigOne, result); 1.599 + 1.600 + LL_SHR( result, bigFoxFox, thirtyTwo ); 1.601 + if ( LL_CMP( result, !=, bigMinusOne )) 1.602 + ResultFailed( "LL_SHR", "r != -1 (was ff,ff)", bigMinusOne, result); 1.603 + 1.604 + /* LL_USHR -- Logical shift right */ 1.605 + LL_USHR( resultU, bigZeroFox, thirtyTwo ); 1.606 + if ( LL_UCMP( resultU, !=, bigZero )) 1.607 + ResultFailed( "LL_USHR", "r != 0 ", bigZero, result); 1.608 + 1.609 + LL_USHR( resultU, bigFoxFox, thirtyTwo ); 1.610 + if ( LL_UCMP( resultU, !=, bigZeroFox )) 1.611 + ResultFailed( "LL_USHR", "r != 0 ", bigZeroFox, result); 1.612 + 1.613 + /* LL_ISHL -- Shift a 32bit integer into a 64bit result */ 1.614 + LL_ISHL( resultU, minusOne, thirtyTwo ); 1.615 + if ( LL_UCMP( resultU, !=, bigFoxZero )) 1.616 + ResultFailed( "LL_ISHL", "r != ff,00 ", bigFoxZero, result); 1.617 + 1.618 + LL_ISHL( resultU, one, sixtyThree ); 1.619 + if ( LL_UCMP( resultU, !=, bigEightZero )) 1.620 + ResultFailed( "LL_ISHL", "r != 80,00 ", bigEightZero, result); 1.621 + 1.622 + LL_ISHL( resultU, one, sixteen ); 1.623 + if ( LL_UCMP( resultU, !=, big64K )) 1.624 + ResultFailed( "LL_ISHL", "r != 64K ", big64K, resultU); 1.625 + 1.626 + return; 1.627 +} 1.628 + 1.629 + 1.630 +/* 1.631 +** TestArithmetic() -- Test arithmetic operations. 1.632 +** 1.633 +*/ 1.634 +static void 1.635 +TestArithmetic( void ) 1.636 +{ 1.637 + PRInt64 largeVal = LL_INIT( 0x00000001, 0xffffffff ); 1.638 + PRInt64 largeValPlusOne = LL_INIT( 0x00000002, 0x00000000 ); 1.639 + PRInt64 largeValTimesTwo = LL_INIT( 0x00000003, 0xfffffffe ); 1.640 + PRInt64 largeMultCand = LL_INIT( 0x00000000, 0x7fffffff ); 1.641 + PRInt64 largeMinusMultCand = LL_INIT( 0xffffffff, 0x10000001 ); 1.642 + PRInt64 largeMultCandx64K = LL_INIT( 0x00007fff, 0xffff0000 ); 1.643 + PRInt64 largeNumSHL5 = LL_INIT( 0x0000001f, 0xffffffe0 ); 1.644 + PRInt64 result, result2; 1.645 + 1.646 + /* Addition */ 1.647 + LL_ADD( result, bigOne, bigOne ); 1.648 + if ( LL_CMP( result, !=, bigTwo )) 1.649 + ResultFailed( "LL_ADD", "r != 1 + 1", bigTwo, result); 1.650 + 1.651 + LL_ADD( result, bigMinusOne, bigOne ); 1.652 + if ( LL_CMP( result, !=, bigZero )) 1.653 + ResultFailed( "LL_ADD", "r != -1 + 1", bigOne, result); 1.654 + 1.655 + LL_ADD( result, largeVal, bigOne ); 1.656 + if ( LL_CMP( result, !=, largeValPlusOne )) 1.657 + ResultFailed( "LL_ADD", "lVP1 != lV + 1", largeValPlusOne, result); 1.658 + 1.659 + /* Subtraction */ 1.660 + LL_SUB( result, bigOne, bigOne ); 1.661 + if ( LL_CMP( result, !=, bigZero )) 1.662 + ResultFailed( "LL_SUB", "r != 1 - 1", bigZero, result); 1.663 + 1.664 + LL_SUB( result, bigTwo, bigOne ); 1.665 + if ( LL_CMP( result, !=, bigOne )) 1.666 + ResultFailed( "LL_SUB", "r != 2 - 1", bigOne, result); 1.667 + 1.668 + LL_SUB( result, largeValPlusOne, bigOne ); 1.669 + if ( LL_CMP( result, !=, largeVal )) 1.670 + ResultFailed( "LL_SUB", "r != lVP1 - 1", largeVal, result); 1.671 + 1.672 + 1.673 + /* Multiply */ 1.674 + LL_MUL( result, largeVal, bigTwo ); 1.675 + if ( LL_CMP( result, !=, largeValTimesTwo )) 1.676 + ResultFailed( "LL_MUL", "r != lV*2", largeValTimesTwo, result); 1.677 + 1.678 + LL_MUL( result, largeMultCand, big64K ); 1.679 + if ( LL_CMP( result, !=, largeMultCandx64K )) 1.680 + ResultFailed( "LL_MUL", "r != lV*64K", largeMultCandx64K, result); 1.681 + 1.682 + LL_NEG( result2, largeMultCand ); 1.683 + LL_MUL( result, largeMultCand, bigMinusOne ); 1.684 + if ( LL_CMP( result, !=, result2 )) 1.685 + ResultFailed( "LL_MUL", "r != -lMC", result2, result); 1.686 + 1.687 + LL_SHL( result2, bigZeroFox, 5); 1.688 + LL_MUL( result, bigZeroFox, bigThirtyTwo ); 1.689 + if ( LL_CMP( result, !=, largeNumSHL5 )) 1.690 + ResultFailed( "LL_MUL", "r != 0f<<5", largeNumSHL5, result ); 1.691 + 1.692 + 1.693 + 1.694 + /* LL_DIV() Division */ 1.695 + LL_DIV( result, bigOne, bigOne); 1.696 + if ( LL_CMP( result, !=, bigOne )) 1.697 + ResultFailed( "LL_DIV", "1 != 1", bigOne, result); 1.698 + 1.699 + LL_DIV( result, bigNumber, bigOne ); 1.700 + if ( LL_CMP( result, !=, bigNumber )) 1.701 + ResultFailed( "LL_DIV", "r != n / 1", bigNumber, result); 1.702 + 1.703 + LL_DIV( result, bigNumber, bigMinusOne ); 1.704 + if ( LL_CMP( result, !=, bigMinusNumber )) 1.705 + ResultFailed( "LL_DIV", "r != n / -1", bigMinusNumber, result); 1.706 + 1.707 + LL_DIV( result, bigMinusNumber, bigMinusOne ); 1.708 + if ( LL_CMP( result, !=, bigNumber )) 1.709 + ResultFailed( "LL_DIV", "r != -n / -1", bigNumber, result); 1.710 + 1.711 + LL_SHL( result2, bigZeroFox, 5 ); 1.712 + LL_DIV( result, result2, bigOne ); 1.713 + if ( LL_CMP( result, !=, result2 )) 1.714 + ResultFailed( "LL_DIV", "0f<<5 != 0f<<5", result2, result); 1.715 + 1.716 + LL_SHL( result2, bigZeroFox, 5 ); 1.717 + LL_NEG( result2, result2 ); 1.718 + LL_DIV( result, result2, bigOne ); 1.719 + if ( LL_CMP( result, !=, result2 )) 1.720 + ResultFailed( "LL_DIV", "-0f<<5 != -0f<<5", result2, result); 1.721 + 1.722 + LL_SHL( result2, bigZeroFox, 17 ); 1.723 + LL_DIV( result, result2, bigMinusOne ); 1.724 + LL_NEG( result2, result2 ); 1.725 + if ( LL_CMP( result, !=, result2 )) 1.726 + ResultFailed( "LL_DIV", "-0f<<17 != -0f<<17", result2, result); 1.727 + 1.728 + 1.729 + /* LL_MOD() Modulo Division */ 1.730 + LL_ADD( result2, bigThirtyTwo, bigOne ); 1.731 + LL_MOD( result, result2, bigSixTeen ); 1.732 + if ( LL_CMP( result, !=, bigOne )) 1.733 + ResultFailed( "LL_MOD", "r != 1", bigSixTeen, result); 1.734 + 1.735 + 1.736 + LL_MUL( result2, bigZeroFox, bigThirtyTwo ); 1.737 + LL_ADD( result2, result2, bigSixTeen); 1.738 + LL_MOD( result, result2, bigThirtyTwo ); 1.739 + if ( LL_CMP( result, !=, bigSixTeen )) 1.740 + ResultFailed( "LL_MOD", "r != 16", bigSixTeen, result); 1.741 + 1.742 + /* LL_UDIVMOD */ 1.743 + LL_DIV( result, bigOne, bigOne); 1.744 + if ( LL_CMP( result, !=, bigOne )) 1.745 + ResultFailed( "LL_DIV", "r != 16", bigSixTeen, result); 1.746 + 1.747 + 1.748 + return; 1.749 +} 1.750 + 1.751 +static void TestWellknowns(void) 1.752 +{ 1.753 + PRInt64 max = LL_MAXINT, min = LL_MININT, zero = LL_ZERO; 1.754 + PRInt64 mmax = LL_MaxInt(), mmin = LL_MinInt(), mzero = LL_Zero(); 1.755 + if (LL_NE(max, mmax)) 1.756 + ResultFailed( "max, mmax", "max != mmax", max, mmax); 1.757 + if (LL_NE(min, mmin)) 1.758 + ResultFailed( "min, mmin", "min != mmin", max, mmin); 1.759 + if (LL_NE(zero, mzero)) 1.760 + ResultFailed( "zero, mzero", "zero != mzero", zero, mzero); 1.761 +} /* TestWellknowns */ 1.762 + 1.763 +/* 1.764 +** Initialize() -- Initialize the test case 1.765 +** 1.766 +** Parse command line options 1.767 +** 1.768 +*/ 1.769 +static PRIntn 1.770 +Initialize( PRIntn argc, char **argv ) 1.771 +{ 1.772 + PLOptState *opt = PL_CreateOptState(argc, argv, "dvh"); 1.773 + PLOptStatus os; 1.774 + 1.775 + /* 1.776 + ** Parse command line options 1.777 + */ 1.778 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.779 + { 1.780 + if (PL_OPT_BAD == os) continue; 1.781 + switch (opt->option) 1.782 + { 1.783 + case 'd': /* set debug mode */ 1.784 + debugMode = PR_TRUE; 1.785 + break; 1.786 + 1.787 + case 'v': /* set verbose mode */ 1.788 + verboseMode = PR_TRUE; 1.789 + debugMode = PR_TRUE; 1.790 + break; 1.791 + 1.792 + case 'h': /* user wants some guidance */ 1.793 + default: 1.794 + PR_fprintf(output, "You get help.\n"); 1.795 + return(1); 1.796 + } 1.797 + } 1.798 + PL_DestroyOptState(opt); 1.799 + return(0); 1.800 +} 1.801 + 1.802 +int main(int argc, char **argv) 1.803 +{ 1.804 + PR_STDIO_INIT(); 1.805 + output = PR_GetSpecialFD(PR_StandardError); 1.806 + 1.807 + if ( Initialize( argc, argv )) 1.808 + return(1); 1.809 + 1.810 + TestAssignment(); 1.811 + TestComparisons(); 1.812 + TestLogicalOperations(); 1.813 + TestConversion(); 1.814 + TestShift(); 1.815 + TestArithmetic(); 1.816 + TestWellknowns(); 1.817 + 1.818 + /* 1.819 + ** That's all folks! 1.820 + */ 1.821 + if ( failedAlready ) 1.822 + { 1.823 + PR_fprintf(output, "FAIL\n");\ 1.824 + } 1.825 + else 1.826 + { 1.827 + PR_fprintf(output, "PASS\n");\ 1.828 + } 1.829 + return failedAlready; 1.830 +} /* end main() */