// Large Number Tester // // Author Matthew Caryl // Created 14.4.97 #include #include "LargeNumber.H" const char space[] = " "; void main(void) { // all tests carried out with BIT_INCREMENT = 4 // test large number creation { LargeNumber a; LargeNumber b(0); LargeNumber c(1); LargeNumber d(15); LargeNumber e(16); LargeNumber f(-1); LargeNumber g(-15); LargeNumber h(-16); LargeNumber i(a); LargeNumber j(c); LargeNumber k(d); LargeNumber l(e); LargeNumber m(f); LargeNumber n(g); LargeNumber o(h); cout << "0" << space << a << endl; cout << "0" << space << b << endl; cout << "1" << space << c << endl; cout << "15" << space << d << endl; cout << "0" << space << e << endl; cout << "-1" << space << f << endl; cout << "-15" << space << g << endl; cout << "0" << space << h << endl; cout << "0" << space << i << endl; cout << "1" << space << j << endl; cout << "15" << space << k << endl; cout << "0" << space << l << endl; cout << "-1" << space << m << endl; cout << "-15" << space << n << endl; cout << "0" << space << o << endl; } // test even, odd, length, bit, and sign { LargeNumber a(0); LargeNumber b(1); LargeNumber c(2); LargeNumber d(3); LargeNumber e(-1); cout << "1" << space << a.even() << endl; cout << "0" << space << a.odd() << endl; cout << "1" << space << a.length() << endl; cout << "0" << space << a.bit(0) << endl; cout << "0" << space << a.bit(1) << endl; cout << "0" << space << a.sign() << endl; cout << "0" << space << b.even() << endl; cout << "1" << space << b.odd() << endl; cout << "1" << space << b.length() << endl; cout << "1" << space << b.bit(0) << endl; cout << "0" << space << b.bit(1) << endl; cout << "0" << space << b.sign() << endl; cout << "1" << space << c.even() << endl; cout << "0" << space << c.odd() << endl; cout << "2" << space << c.length() << endl; cout << "0" << space << c.bit(0) << endl; cout << "1" << space << c.bit(1) << endl; cout << "0" << space << c.bit(2) << endl; cout << "0" << space << c.sign() << endl; cout << "0" << space << d.even() << endl; cout << "1" << space << d.odd() << endl; cout << "2" << space << d.length() << endl; cout << "1" << space << d.bit(0) << endl; cout << "1" << space << d.bit(1) << endl; cout << "0" << space << d.bit(2) << endl; cout << "0" << space << d.sign() << endl; cout << "0" << space << e.even() << endl; cout << "1" << space << e.odd() << endl; cout << "1" << space << e.length() << endl; cout << "1" << space << e.bit(0) << endl; cout << "0" << space << e.bit(1) << endl; cout << "1" << space << e.sign() << endl; } // test truncate and complement { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-15); a.complement(); cout << "0" << space << a << endl; a.complement(); cout << "0" << space << a << endl; b.complement(); cout << "-1" << space << b << endl; b.complement(); cout << "1" << space << b << endl; c.complement(); cout << "-15" << space << c << endl; c.complement(); cout << "15" << space << c << endl; a.truncate(3); cout << "0" << space << a << endl; a.truncate(0); cout << "0" << space << a << endl; b.truncate(1); cout << "1" << space << b << endl; b.truncate(0); cout << "0" << space << b << endl; c.truncate(1); cout << "1" << space << c << endl; c.truncate(0); cout << "0" << space << c << endl; d.truncate(1); cout << "-1" << space << d << endl; d.truncate(0); cout << "0" << space << d << endl; } // test ==, !=, <, <=, >, and >= { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); cout << "1" << space << (a == a) << endl; cout << "0" << space << (a == b) << endl; cout << "1" << space << (b == b) << endl; cout << "0" << space << (b == c) << endl; cout << "0" << space << (b == d) << endl; cout << "1" << space << (c == c) << endl; cout << "1" << space << (e < d) << endl; cout << "1" << space << (d < a) << endl; cout << "1" << space << (a < b) << endl; cout << "1" << space << (b < c) << endl; cout << "0" << space << (a < a) << endl; cout << "0" << space << (b < e) << endl; cout << "1" << space << (a <= a) << endl; cout << "0" << space << (b <= d) << endl; cout << "0" << space << (b <= e) << endl; cout << "1" << space << (c > b) << endl; cout << "1" << space << (b > a) << endl; cout << "1" << space << (a > d) << endl; cout << "1" << space << (d > e) << endl; cout << "0" << space << (d > b) << endl; cout << "0" << space << (e > b) << endl; cout << "1" << space << (b >= b) << endl; cout << "1" << space << (b >= a) << endl; cout << "0" << space << (d >= b) << endl; } // test = { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f; f = a; cout << "0" << space << f << endl; f = b; cout << "1" << space << f << endl; f = c; cout << "15" << space << f << endl; f = d; cout << "-1" << space << f << endl; f = e; cout << "-15" << space << f << endl; f = b; cout << "1" << space << f << endl; f = a; cout << "0" << space << f << endl; f = d; cout << "-1" << space << f << endl; f = a; cout << "0" << space << f << endl; } // test +=, -= { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f, g; f += a; cout << "0" << space << f << endl; f += b; cout << "1" << space << f << endl; f += c; cout << "16" << space << f << endl; f += d; cout << "15" << space << f << endl; f += e; cout << "0" << space << f << endl; f += e; cout << "-15" << space << f << endl; f += d; cout << "-16" << space << f << endl; f += e; cout << "-31" << space << f << endl; f += d; cout << "-32" << space << f << endl; g -= a; cout << "0" << space << g << endl; g -= b; cout << "-1" << space << g << endl; g -= c; cout << "-16" << space << g << endl; g -= d; cout << "-15" << space << g << endl; g -= e; cout << "0" << space << g << endl; g -= e; cout << "15" << space << g << endl; g -= d; cout << "16" << space << g << endl; g -= e; cout << "31" << space << g << endl; g -= d; cout << "32" << space << g << endl; } // test <<=, >>= { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f; f = a; f <<= 0; cout << "0" << space << f << endl; f <<= 1; cout << "0" << space << f << endl; f <<= -2; cout << "0" << space << f << endl; f = b; f <<= 0; cout << "1" << space << f << endl; f <<= 1; cout << "2" << space << f << endl; f <<= -2; cout << "0" << space << f << endl; f = c; f <<= 0; cout << "15" << space << f << endl; f <<= 1; cout << "30" << space << f << endl; f <<= -2; cout << "7" << space << f << endl; f = d; f <<= 0; cout << "-1" << space << f << endl; f <<= 1; cout << "-2" << space << f << endl; f <<= -2; cout << "0" << space << f << endl; f = e; f <<= 0; cout << "-15" << space << f << endl; f <<= 1; cout << "-30" << space << f << endl; f <<= -2; cout << "-7" << space << f << endl; } // test ++, -- { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); cout << "1" << space << ++a << endl; cout << "1" << space << a << endl; --a; cout << "0" << space << a << endl; --a; cout << "-1" << space << a << endl; cout << "0" << space << --b << endl; cout << "0" << space << b << endl; --b; cout << "-1" << space << b << endl; --b; cout << "-2" << space << b << endl; ++c; cout << "16" << space << c << endl; --d; cout << "-2" << space << d << endl; ++d; cout << "-1" << space << d << endl; ++d; cout << "0" << space << d << endl; ++d; cout << "1" << space << d << endl; ++e; cout << "-14" << space << e << endl; --e; cout << "-15" << space << e << endl; --e; cout << "-16" << space << e << endl; } { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); cout << "0" << space << a++ << endl; cout << "1" << space << a << endl; a--; cout << "0" << space << a << endl; a--; cout << "-1" << space << a << endl; cout << "1" << space << b-- << endl; cout << "0" << space << b << endl; b--; cout << "-1" << space << b << endl; b--; cout << "-2" << space << b << endl; c++; cout << "16" << space << c << endl; d--; cout << "-2" << space << d << endl; d++; cout << "-1" << space << d << endl; d++; cout << "0" << space << d << endl; d++; cout << "1" << space << d << endl; e++; cout << "-14" << space << e << endl; e--; cout << "-15" << space << e << endl; e--; cout << "-16" << space << e << endl; } // test *= { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f; f *= a; cout << "0" << space << f << endl; f *= b; cout << "0" << space << f << endl; f *= c; cout << "0" << space << f << endl; f *= d; cout << "0" << space << f << endl; f *= e; cout << "0" << space << f << endl; f = b; f *= b; cout << "1" << space << f << endl; f *= c; cout << "15" << space << f << endl; f *= d; cout << "-15" << space << f << endl; f *= e; cout << "225" << space << f << endl; f *= a; cout << "0" << space << f << endl; f = d; f *= b; cout << "-1" << space << f << endl; f *= c; cout << "-15" << space << f << endl; f *= d; cout << "15" << space << f << endl; f *= e; cout << "-225" << space << f << endl; f *= a; cout << "0" << space << f << endl; f = c; f *= b; cout << "15" << space << f << endl; f *= f; cout << "225" << space << f << endl; } // test /= { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f; f = a; f /= b; cout << "0" << space << f << endl; f /= c; cout << "0" << space << f << endl; f /= d; cout << "0" << space << f << endl; f /= e; cout << "0" << space << f << endl; f = b; f /= b; cout << "1" << space << f << endl; f /= c; cout << "0" << space << f << endl; f = b; f /= d; cout << "-1" << space << f << endl; f /= e; cout << "0" << space << f << endl; f = c; f /= b; cout << "15" << space << f << endl; f /= c; cout << "1" << space << f << endl; f = c; f /= d; cout << "-15" << space << f << endl; f /= c; cout << "-1" << space << f << endl; f = d; f /= b; cout << "-1" << space << f << endl; f /= c; cout << "0" << space << f << endl; f = d; f /= d; cout << "1" << space << f << endl; f /= e; cout << "0" << space << f << endl; f = e; f /= b; cout << "-15" << space << f << endl; f /= c; cout << "-1" << space << f << endl; f = e; f /= d; cout << "15" << space << f << endl; f /= e; cout << "-1" << space << f << endl; } // test %= { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f; LargeNumber g(7); LargeNumber h(-7); f = a; f %= b; cout << (0 % 1) << space << f << endl; f %= c; cout << (0 % 15) << space << f << endl; f %= d; cout << (0 % -1) << space << f << endl; f %= e; cout << (0 % -15) << space << f << endl; f = b; f %= b; cout << (1 % 1) << space << f << endl; f = b; f %= c; cout << (1 % 15) << space << f << endl; f = b; f %= d; cout << (1 % -1) << space << f << endl; f = b; f %= e; cout << (1 % -15) << space << f << endl; f = c; f %= b; cout << (15 % 1) << space << f << endl; f = c; f %= c; cout << (15 % 15) << space << f << endl; f = c; f %= d; cout << (15 % -1) << space << f << endl; f = c; f %= e; cout << (15 % -15) << space << f << endl; f = c; f %= g; cout << (15 % 7) << space << f << endl; f = c; f %= h; cout << (15 % -7) << space << f << endl; f = d; f %= b; cout << (-1 % 1) << space << f << endl; f = d; f %= c; cout << (-1 % 15) << space << f << endl; f = d; f %= d; cout << (-1 % -1) << space << f << endl; f = d; f %= e; cout << (-1 % -15) << space << f << endl; f = e; f %= b; cout << (-15 % 1) << space << f << endl; f = e; f %= c; cout << (-15 % 15) << space << f << endl; f = e; f %= d; cout << (-15 % -1) << space << f << endl; f = e; f %= e; cout << (-15 % -15) << space << f << endl; f = e; f %= g; cout << (-15 % 7) << space << f << endl; f = e; f %= h; cout << (-15 % -7) << space << f << endl; } // test &=, |=, and ^= { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f; f = a; f &= b; cout << "0" << space << f << endl; f |= b; cout << "1" << space << f << endl; f ^= b; cout << "0" << space << f << endl; f &= c; cout << "0" << space << f << endl; f |= c; cout << "15" << space << f << endl; f ^= c; cout << "0" << space << f << endl; f &= d; cout << "0" << space << f << endl; f |= d; cout << "1" << space << f << endl; f ^= d; cout << "0" << space << f << endl; f &= e; cout << "0" << space << f << endl; f |= e; cout << "15" << space << f << endl; f ^= e; cout << "0" << space << f << endl; f = c; f &= b; cout << "1" << space << f << endl; f |= c; cout << "15" << space << f << endl; f ^= b; cout << "14" << space << f << endl; f = c; f &= d; cout << "1" << space << f << endl; f |= e; cout << "15" << space << f << endl; f ^= d; cout << "14" << space << f << endl; f = e; f &= b; cout << "-1" << space << f << endl; f |= c; cout << "-15" << space << f << endl; f ^= b; cout << "-14" << space << f << endl; } // test +, - // also counts for *, /, %, &, |, ^ { LargeNumber a(0); LargeNumber b(1); LargeNumber c(15); LargeNumber d(-1); LargeNumber e(-15); LargeNumber f, g; f = f + a; cout << "0" << space << f << endl; f = f + b; cout << "1" << space << f << endl; f = f + c; cout << "16" << space << f << endl; f = f + d; cout << "15" << space << f << endl; f = f + e; cout << "0" << space << f << endl; f = f + e; cout << "-15" << space << f << endl; f = f + d; cout << "-16" << space << f << endl; f = f + e; cout << "-31" << space << f << endl; f = f + d; cout << "-32" << space << f << endl; g = g - a; cout << "0" << space << g << endl; g = g - b; cout << "-1" << space << g << endl; g = g - c; cout << "-16" << space << g << endl; g = g - d; cout << "-15" << space << g << endl; g = g - e; cout << "0" << space << g << endl; g = g - e; cout << "15" << space << g << endl; g = g - d; cout << "16" << space << g << endl; g = g - e; cout << "31" << space << g << endl; g = g - d; cout << "32" << space << g << endl; } // test speed of / { LargeNumber a(1); LargeNumber b; for (int i = 0; i < 200; i++) { b = a; b /= 1; cout << a << endl; a *= 2; a += 1; } } // test >> { LargeNumber a, b; cout << "Enter 0 : "; cin >> a; cout << "Result " << a << endl; cout << "Enter 1 : "; cin >> a; cout << "Result " << a << endl; cout << "Enter 15 : "; cin >> a; cout << "Result " << a << endl; cout << "Enter 16 : "; cin >> a; cout << "Result " << a << endl; cout << "Enter -1 : "; cin >> b; cout << "Result " << b << endl; cout << "Enter -15 : "; cin >> b; cout << "Result " << b << endl; cout << "Enter -16 : "; cin >> b; cout << "Result " << b << endl; } cout << "Finished successfully" << endl; }