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. */
5 #include "BigIntegerLibrary.hh"
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;
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());
29 #define THROWY(x) try {x; cout << "Expected error not thrown!" << endl; } catch (const char *err) {}
35 BigUnsigned z(0), one(1), ten(10);
36 cout << z << ',' << one << ',' << ten << endl; //0,1,10
38 // TODO: Comprehensively test the general and special cases of each function.
41 cout << check(BigUnsigned(0) + 0) << endl; //0
42 cout << check(BigUnsigned(0) + 1) << endl; //1
43 cout << check(BigUnsigned(0xFFFFFFFFU) + 1) << endl; //4294967296
45 // Negative BigUnsigneds
46 THROWY(BigUnsigned(-1));
47 THROWY(BigUnsigned(5) - BigUnsigned(6));
48 cout << check(BigUnsigned(5) - BigUnsigned(5)) << endl; //0
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);
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);
63 num.divideWithRemainder(denom, quotient);
68 /* Test that BigInteger subtraction sets the sign properly.
69 * Bug reported by Samuel Larkin. */
70 BigInteger zero(0), three(3), ans;
72 cout << check(ans).getSign() << endl; //-1
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. */
81 cout<<check(f)<<endl; //12
85 /* Test that bitwise XOR allocates the larger length.
86 * Bug reported by Sriram Sankararaman. */
87 BigUnsigned a(0), b(3), ans;
89 cout << ans << endl; //3
93 /* Test that an aliased multiplication works.
94 * Bug reported by Boris Dessy. */
97 cout << check(num) << endl; //25
100 } catch (const char *err) {
101 cout << "ERROR: " << err << endl;