1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* Modification History
*
* 2002-May-25 Jason Rohrer
* Created.
*/
#include "minorGems/math/BigInt.h"
#include <stdio.h>
int main() {
unsigned char intA[] = { 100, 200, 99, 23, 89, 100, 233 };
unsigned char intB[] = { 10, 78, 243, 9, 0 };
BigInt *bigIntA = new BigInt( 1, 7, intA );
BigInt *bigIntB = new BigInt( -1, 5, intB );
char *hexA = bigIntA->convertToHexString();
char *hexB = bigIntB->convertToHexString();
printf( "A = %s\n", hexA );
printf( "B = %s\n", hexB );
BigInt *sum = bigIntA->subtract( bigIntB );
char *hexSum = sum->convertToHexString();
printf( "Sum = %s\n", hexSum );
delete [] hexA;
delete [] hexB;
delete [] hexSum;
delete bigIntA;
delete bigIntB;
delete sum;
int c = 0x58B11F;
BigInt *intC = new BigInt( c );
char *hexC = intC->convertToHexString();
printf( "C = %s\n", hexC );
int extractedC = intC->convertToInt();
if( extractedC == c ) {
printf( "equal\n" );
}
else {
printf( "not equal\n" );
}
delete [] hexC;
delete intC;
int limit = 300;
printf( "Testing pair operations for all pairs in -%d..%d ...\n",
limit, limit );
char failed = false;
for( int i=-limit; i<limit && !failed; i++ ) {
BigInt *intI = new BigInt( i );
for( int j=-limit; j<limit && !failed; j++ ) {
BigInt *intJ = new BigInt( j );
BigInt *intSum = intI->add( intJ );
BigInt *intDiff = intI->subtract( intJ );
int sum = i + j;
int diff = i - j;
if( sum != intSum->convertToInt() ) {
printf( "sum test failed for %d, %d\n", i, j );
printf( " real sum = %d, computed sum = %d\n",
sum, intSum->convertToInt() );
failed = true;
}
if( diff != intDiff->convertToInt() ) {
printf( "diff test failed for %d, %d\n", i, j );
printf( " real diff = %d, computed diff = %d\n",
diff, intDiff->convertToInt() );
failed = true;
}
if( intI->isLessThan( intJ ) && ( i >= j ) ) {
printf( "first less than test failed for %d, %d\n", i, j );
failed = true;
}
if( intJ->isLessThan( intI ) && ( j >= i ) ) {
printf( "second less than test failed for %d, %d\n", i, j );
failed = true;
}
if( intI->isEqualTo( intJ ) && ( i != j ) ) {
printf( "equality test failed for %d, %d\n", i, j );
failed = true;
}
delete intSum;
delete intDiff;
delete intJ;
}
delete intI;
}
if( !failed ) {
printf( "test passed\n" );
}
return 0;
}
|