📄 lltest.c
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape Portable Runtime (NSPR). * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */ /*** testll.c -- test suite for 64bit integer (longlong) operations**** Summary: testll [-d] | [-h]**** Where:** -d set debug mode on; displays individual test failures** -v verbose mode; displays progress in test, plus -d** -h gives usage message.**** Description:** lltest.c tests the functions defined in NSPR 2.0's prlong.h.** ** Successive tests begin to depend on other LL functions working** correctly. So, ... Do not change the order of the tests as run** from main().** ** Caveats:** Do not even begin to think that this is an exhaustive test!**** These tests try a little of everything, but not all boundary** conditions and limits are tested.** You want better coverage? ... Add it.**** ---** Author: Lawrence Hardiman <larryh@netscape.com>.** ---** Revision History:** 01-Oct-1997. Original implementation.***/#include "nspr.h"#include "plgetopt.h"/* --- Local Definitions --- */#define ReportProgress(m) if (verboseMode) PR_fprintf(output, (m));/* --- Global variables --- */static PRIntn failedAlready = 0;static PRFileDesc* output = NULL;static PRBool debugMode = PR_FALSE;static PRBool verboseMode = PR_FALSE;/*** Constants used in tests.*/const PRInt64 bigZero = LL_INIT( 0, 0 );const PRInt64 bigOne = LL_INIT( 0, 1 );const PRInt64 bigTwo = LL_INIT( 0, 2 );const PRInt64 bigSixTeen = LL_INIT( 0, 16 ); const PRInt64 bigThirtyTwo = LL_INIT( 0, 32 ); const PRInt64 bigMinusOne = LL_INIT( 0xffffffff, 0xffffffff );const PRInt64 bigMinusTwo = LL_INIT( 0xffffffff, 0xfffffffe );const PRInt64 bigNumber = LL_INIT( 0x7fffffff, 0xffffffff );const PRInt64 bigMinusNumber = LL_INIT( 0x80000000, 0x00000001 );const PRInt64 bigMaxInt32 = LL_INIT( 0x00000000, 0x7fffffff );const PRInt64 big2To31 = LL_INIT( 0x00000000, 0x80000000 );const PRUint64 bigZeroFox = LL_INIT( 0x00000000, 0xffffffff );const PRUint64 bigFoxFox = LL_INIT( 0xffffffff, 0xffffffff );const PRUint64 bigFoxZero = LL_INIT( 0xffffffff, 0x00000000 );const PRUint64 bigEightZero = LL_INIT( 0x80000000, 0x00000000 );const PRUint64 big64K = LL_INIT( 0x00000000, 0x00010000 );const PRInt64 bigInt0 = LL_INIT( 0x01a00000, 0x00001000 );const PRInt64 bigInt1 = LL_INIT( 0x01a00000, 0x00001100 );const PRInt64 bigInt2 = LL_INIT( 0x01a00000, 0x00000100 );const PRInt64 bigInt3 = LL_INIT( 0x01a00001, 0x00001000 );const PRInt64 bigInt4 = LL_INIT( 0x01a00001, 0x00001100 );const PRInt64 bigInt5 = LL_INIT( 0x01a00001, 0x00000100 );const PRInt64 bigInt6 = LL_INIT( 0xb1a00000, 0x00001000 );const PRInt64 bigInt7 = LL_INIT( 0xb1a00000, 0x00001100 );const PRInt64 bigInt8 = LL_INIT( 0xb1a00000, 0x00000100 );const PRInt64 bigInt9 = LL_INIT( 0xb1a00001, 0x00001000 );const PRInt64 bigInt10 = LL_INIT( 0xb1a00001, 0x00001100 );const PRInt64 bigInt11 = LL_INIT( 0xb1a00001, 0x00000100 );const PRInt32 one = 1l;const PRInt32 minusOne = -1l;const PRInt32 sixteen = 16l;const PRInt32 thirtyTwo = 32l;const PRInt32 sixtyThree = 63l;/*** SetFailed() -- Report individual test failure***/static voidSetFailed( char *what, char *how ){ failedAlready = 1; if ( debugMode ) PR_fprintf(output, "%s: failed: %s\n", what, how ); return;}static voidResultFailed( char *what, char *how, PRInt64 expected, PRInt64 got){ if ( debugMode) { SetFailed( what, how ); PR_fprintf(output, "Expected: 0x%llx Got: 0x%llx\n", expected, got ); } return;} /*** TestAssignment() -- Test the assignment*/static void TestAssignment( void ){ PRInt64 zero = LL_Zero(); PRInt64 min = LL_MinInt(); PRInt64 max = LL_MaxInt(); if (!LL_EQ(zero, bigZero)) SetFailed("LL_EQ(zero, bigZero)", "!="); if (!LL_CMP(max, >, min)) SetFailed("LL_CMP(max, >, min)", "!>");}/*** TestComparisons() -- Test the longlong comparison operations*/static void TestComparisons( void ){ ReportProgress("Testing Comparisons Operations\n"); /* test for zero */ if ( !LL_IS_ZERO( bigZero )) SetFailed( "LL_IS_ZERO", "Zero is not zero" ); if ( LL_IS_ZERO( bigOne )) SetFailed( "LL_IS_ZERO", "One tests as zero" ); if ( LL_IS_ZERO( bigMinusOne )) SetFailed( "LL_IS_ZERO", "Minus One tests as zero" ); /* test equal */ if ( !LL_EQ( bigZero, bigZero )) SetFailed( "LL_EQ", "zero EQ zero"); if ( !LL_EQ( bigOne, bigOne )) SetFailed( "LL_EQ", "one EQ one" ); if ( !LL_EQ( bigNumber, bigNumber )) SetFailed( "LL_EQ", "bigNumber EQ bigNumber" ); if ( !LL_EQ( bigMinusOne, bigMinusOne )) SetFailed( "LL_EQ", "minus one EQ minus one"); if ( LL_EQ( bigZero, bigOne )) SetFailed( "LL_EQ", "zero EQ one"); if ( LL_EQ( bigOne, bigZero )) SetFailed( "LL_EQ", "one EQ zero" ); if ( LL_EQ( bigMinusOne, bigOne )) SetFailed( "LL_EQ", "minus one EQ one"); if ( LL_EQ( bigNumber, bigOne )) SetFailed( "LL_EQ", "bigNumber EQ one"); /* test not equal */ if ( LL_NE( bigZero, bigZero )) SetFailed( "LL_NE", "0 NE 0"); if ( LL_NE( bigOne, bigOne )) SetFailed( "LL_NE", "1 NE 1"); if ( LL_NE( bigMinusOne, bigMinusOne )) SetFailed( "LL_NE", "-1 NE -1"); if ( LL_NE( bigNumber, bigNumber )) SetFailed( "LL_NE", "n NE n"); if ( LL_NE( bigMinusNumber, bigMinusNumber )) SetFailed( "LL_NE", "-n NE -n"); if ( !LL_NE( bigZero, bigOne)) SetFailed( "LL_NE", "0 NE 1"); if ( !LL_NE( bigOne, bigMinusNumber)) SetFailed( "LL_NE", "1 NE -n"); /* Greater than or equal to zero */ if ( !LL_GE_ZERO( bigZero )) SetFailed( "LL_GE_ZERO", "0"); if ( !LL_GE_ZERO( bigOne )) SetFailed( "LL_GE_ZERO", "1"); if ( !LL_GE_ZERO( bigNumber )) SetFailed( "LL_GE_ZERO", "n"); if ( LL_GE_ZERO( bigMinusOne )) SetFailed( "LL_GE_ZERO", "-1"); if ( LL_GE_ZERO( bigMinusNumber )) SetFailed( "LL_GE_ZERO", "-n"); /* Algebraic Compare two values */ if ( !LL_CMP( bigZero, ==, bigZero )) SetFailed( "LL_CMP", "0 == 0"); if ( LL_CMP( bigZero, >, bigZero )) SetFailed( "LL_CMP", "0 > 0"); if ( LL_CMP( bigZero, <, bigZero )) SetFailed( "LL_CMP", "0 < 0"); if ( LL_CMP( bigNumber, <, bigOne )) SetFailed( "LL_CMP", "n < 1"); if ( !LL_CMP( bigNumber, >, bigOne )) SetFailed( "LL_CMP", "n <= 1"); if ( LL_CMP( bigOne, >, bigNumber )) SetFailed( "LL_CMP", "1 > n"); if ( LL_CMP( bigMinusNumber, >, bigNumber )) SetFailed( "LL_CMP", "-n > n"); if ( LL_CMP( bigNumber, !=, bigNumber)) SetFailed( "LL_CMP", "n != n"); if ( !LL_CMP( bigMinusOne, >, bigMinusTwo )) SetFailed( "LL_CMP", "-1 <= -2"); if ( !LL_CMP( bigMaxInt32, <, big2To31 )) SetFailed( "LL_CMP", "Max 32-bit signed int >= 2^31"); /* Two positive numbers */ if ( !LL_CMP( bigInt0, <=, bigInt0 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt0, <=, bigInt1 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( LL_CMP( bigInt0, <=, bigInt2 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt0, <=, bigInt3 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt0, <=, bigInt4 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt0, <=, bigInt5 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); /* Two negative numbers */ if ( !LL_CMP( bigInt6, <=, bigInt6 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt6, <=, bigInt7 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( LL_CMP( bigInt6, <=, bigInt8 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt6, <=, bigInt9 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt6, <=, bigInt10 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( !LL_CMP( bigInt6, <=, bigInt11 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); /* One positive, one negative */ if ( LL_CMP( bigInt0, <=, bigInt6 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( LL_CMP( bigInt0, <=, bigInt7 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); if ( LL_CMP( bigInt0, <=, bigInt8 )) SetFailed( "LL_CMP", "LL_CMP(<=) failed"); /* Bitwise Compare two numbers */ if ( !LL_UCMP( bigZero, ==, bigZero )) SetFailed( "LL_UCMP", "0 == 0"); if ( LL_UCMP( bigZero, >, bigZero )) SetFailed( "LL_UCMP", "0 > 0"); if ( LL_UCMP( bigZero, <, bigZero )) SetFailed( "LL_UCMP", "0 < 0"); if ( LL_UCMP( bigNumber, <, bigOne )) SetFailed( "LL_UCMP", "n < 1"); if ( !LL_UCMP( bigNumber, >, bigOne )) SetFailed( "LL_UCMP", "n < 1"); if ( LL_UCMP( bigOne, >, bigNumber )) SetFailed( "LL_UCMP", "1 > n"); if ( LL_UCMP( bigMinusNumber, <, bigNumber )) SetFailed( "LL_UCMP", "-n < n"); /* Two positive numbers */ if ( !LL_UCMP( bigInt0, <=, bigInt0 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt0, <=, bigInt1 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( LL_UCMP( bigInt0, <=, bigInt2 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt0, <=, bigInt3 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt0, <=, bigInt4 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt0, <=, bigInt5 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); /* Two negative numbers */ if ( !LL_UCMP( bigInt6, <=, bigInt6 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt6, <=, bigInt7 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( LL_UCMP( bigInt6, <=, bigInt8 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt6, <=, bigInt9 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt6, <=, bigInt10 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt6, <=, bigInt11 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); /* One positive, one negative */ if ( !LL_UCMP( bigInt0, <=, bigInt6 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt0, <=, bigInt7 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); if ( !LL_UCMP( bigInt0, <=, bigInt8 )) SetFailed( "LL_UCMP", "LL_UCMP(<=) failed"); return;}/*** TestLogicalOperations() -- Tests for AND, OR, ...***/static voidTestLogicalOperations( void ){ PRUint64 result, result2; ReportProgress("Testing Logical Operations\n"); /* Test AND */ LL_AND( result, bigZero, bigZero ); if ( !LL_IS_ZERO( result )) ResultFailed( "LL_AND", "0 & 0", bigZero, result ); LL_AND( result, bigOne, bigOne ); if ( LL_IS_ZERO( result )) ResultFailed( "LL_AND", "1 & 1", bigOne, result ); LL_AND( result, bigZero, bigOne ); if ( !LL_IS_ZERO( result )) ResultFailed( "LL_AND", "1 & 1", bigZero, result ); LL_AND( result, bigMinusOne, bigMinusOne ); if ( !LL_UCMP( result, ==, bigMinusOne )) ResultFailed( "LL_AND", "-1 & -1", bigMinusOne, result ); /* test OR */ LL_OR( result, bigZero, bigZero ); if ( !LL_IS_ZERO( result )) ResultFailed( "LL_OR", "0 | 1", bigZero, result); LL_OR( result, bigZero, bigOne ); if ( LL_IS_ZERO( result )) ResultFailed( "LL_OR", "0 | 1", bigOne, result ); LL_OR( result, bigZero, bigMinusNumber ); if ( !LL_UCMP( result, ==, bigMinusNumber )) ResultFailed( "LL_OR", "0 | -n", bigMinusNumber, result);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -