Commit | Line | Data |
---|---|---|
0afe80d5 MM |
1 | /* Test suite for the library. First, it ``tests'' that all the constructs it |
2 | * uses compile successfully. Then, its output to stdout is compared to the | |
3 | * expected output automatically extracted from slash-slash comments below. */ | |
4 | ||
5 | #include "BigIntegerLibrary.hh" | |
6 | ||
7 | #include <string> | |
8 | #include <iostream> | |
9 | using namespace std; | |
10 | ||
706f6a7e MM |
11 | const BigUnsigned &check(const BigUnsigned &x) { |
12 | unsigned int l = x.getLength(); | |
13 | if (l != 0 && x.getBlock(l-1) == 0) | |
14 | cout << "Unzapped number!" << endl; | |
15 | if (l > x.getCapacity()) | |
16 | cout << "Capacity inconsistent with length!" << endl; | |
17 | return x; | |
18 | } | |
19 | ||
20 | const BigInteger &check(const BigInteger &x) { | |
21 | if (x.getSign() == 0 && !x.getMagnitude().isZero()) | |
22 | cout << "Sign should not be zero!" << endl; | |
23 | if (x.getSign() != 0 && x.getMagnitude().isZero()) | |
24 | cout << "Sign should be zero!" << endl; | |
25 | check(x.getMagnitude()); | |
26 | return x; | |
27 | } | |
28 | ||
29 | #define THROWY(x) try {x; cout << "Expected error not thrown!" << endl; } catch (const char *err) {} | |
30 | ||
0afe80d5 | 31 | int main() { |
706f6a7e MM |
32 | |
33 | try { | |
34 | ||
0afe80d5 MM |
35 | BigUnsigned z(0), one(1), ten(10); |
36 | cout << z << ',' << one << ',' << ten << endl; //0,1,10 | |
37 | ||
706f6a7e MM |
38 | // TODO: Comprehensively test the general and special cases of each function. |
39 | ||
40 | // Addition | |
41 | cout << check(BigUnsigned(0) + 0) << endl; //0 | |
42 | cout << check(BigUnsigned(0) + 1) << endl; //1 | |
43 | cout << check(BigUnsigned(0xFFFFFFFFU) + 1) << endl; //4294967296 | |
44 | ||
45 | // Negative BigUnsigneds | |
46 | THROWY(BigUnsigned(-1)); | |
47 | THROWY(BigUnsigned(5) - BigUnsigned(6)); | |
48 | cout << check(BigUnsigned(5) - BigUnsigned(5)) << endl; //0 | |
49 | ||
50 | // Multiplication and division | |
51 | BigUnsigned a = check(BigUnsigned(314159265) * 358979323); | |
52 | cout << a << ',' << (a / 123) << ',' << (a % 123) << endl; | |
53 | //112776680263877595,916883579381118,81 | |
54 | THROWY(BigUnsigned(5) / 0); | |
55 | ||
56 | { | |
57 | /* Test that BigInteger division sets the sign to zero. | |
58 | * Bug reported by David Allen. */ | |
59 | BigInteger num(3), denom(5), quotient; | |
60 | num.divideWithRemainder(denom, quotient); | |
61 | check(quotient); | |
62 | num = 5; | |
63 | num.divideWithRemainder(denom, quotient); | |
64 | check(num); | |
65 | } | |
66 | ||
67 | { | |
68 | /* Test that BigInteger subtraction sets the sign properly. | |
69 | * Bug reported by Samuel Larkin. */ | |
70 | BigInteger zero(0), three(3), ans; | |
71 | ans = zero - three; | |
72 | cout << check(ans).getSign() << endl; //-1 | |
73 | } | |
74 | ||
75 | { | |
76 | /* Test that BigInteger multiplication shifts bits properly on systems | |
77 | * where long is bigger than int. (Obviously, this would only catch the | |
78 | * bug when run on such a system.) | |
79 | * Bug reported by Mohand Mezmaz. */ | |
80 | BigInteger f=4; f*=3; | |
81 | cout<<check(f)<<endl; //12 | |
82 | } | |
83 | ||
84 | { | |
85 | /* Test that bitwise XOR allocates the larger length. | |
86 | * Bug reported by Sriram Sankararaman. */ | |
87 | BigUnsigned a(0), b(3), ans; | |
88 | ans = a ^ b; | |
89 | cout << ans << endl; //3 | |
90 | } | |
91 | ||
92 | { | |
93 | /* Test that an aliased multiplication works. | |
94 | * Bug reported by Boris Dessy. */ | |
95 | BigInteger num(5); | |
96 | num *= num; | |
97 | cout << check(num) << endl; //25 | |
98 | } | |
99 | ||
100 | } catch (const char *err) { | |
101 | cout << "ERROR: " << err << endl; | |
102 | } | |
103 | ||
0afe80d5 MM |
104 | return 0; |
105 | } |