~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/math/test/testBigInt.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

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