Add additional Integer class tests

pull/605/head
Jeffrey Walton 2018-03-25 14:12:51 -04:00
parent 932f392b2d
commit 2f61c159b0
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
1 changed files with 93 additions and 33 deletions

View File

@ -3137,7 +3137,8 @@ bool TestIntegerOps()
std::cout << "\nTesting Integer operations...\n\n"; std::cout << "\nTesting Integer operations...\n\n";
bool pass=true, result; bool pass=true, result;
// Integer is missing a couple of tests... // ****************************** DivideByZero ******************************
try { try {
Integer x = Integer::Two().Power2(128) / Integer::Zero(); Integer x = Integer::Two().Power2(128) / Integer::Zero();
pass=false; pass=false;
@ -3151,13 +3152,15 @@ bool TestIntegerOps()
std::cout << "FAILED:"; std::cout << "FAILED:";
std::cout << " Integer DivideByZero\n"; std::cout << " Integer DivideByZero\n";
// Integer is missing a couple of tests... // ****************************** RandomNumberNotFound ******************************
pass=true;
try { try {
// A run of 71 composites; see http://en.wikipedia.org/wiki/Prime_gap // A run of 71 composites; see http://en.wikipedia.org/wiki/Prime_gap
Integer x = Integer(GlobalRNG(), 31398, 31468, Integer::PRIME); Integer x = Integer(GlobalRNG(), 31398, 31468, Integer::PRIME);
pass=false; pass=false;
} catch (const Exception&) { } } catch (const Exception&) {
pass=true;
}
if (pass) if (pass)
std::cout << "passed:"; std::cout << "passed:";
@ -3165,7 +3168,8 @@ bool TestIntegerOps()
std::cout << "FAILED:"; std::cout << "FAILED:";
std::cout << " Integer RandomNumberNotFound\n"; std::cout << " Integer RandomNumberNotFound\n";
// Carmichael pseudo-primes // ****************************** Carmichael pseudo-primes ******************************
pass=true; pass=true;
if (IsPrime(Integer("561"))) if (IsPrime(Integer("561")))
pass = false; pass = false;
@ -3186,7 +3190,8 @@ bool TestIntegerOps()
std::cout << "FAILED:"; std::cout << "FAILED:";
std::cout << " Carmichael pseudo-primes\n"; std::cout << " Carmichael pseudo-primes\n";
// Integer is missing a couple of tests... // ****************************** Integer::Double ******************************
try { try {
Integer x = Integer::One().Doubled(); Integer x = Integer::One().Doubled();
pass=(x == Integer::Two()); pass=(x == Integer::Two());
@ -3197,6 +3202,37 @@ bool TestIntegerOps()
if (!pass) if (!pass)
std::cout << "FAILED: Integer Doubled\n"; std::cout << "FAILED: Integer Doubled\n";
// ****************************** Integer::Double ******************************
try {
Integer x = Integer::Two().Squared();
pass=(x == 4);
} catch (const Exception&) {
pass=false;
}
if (!pass)
std::cout << "FAILED: Integer Squared\n";
try {
Integer x = Integer::Two().Squared();
pass=(x == 4) && x.IsSquare();
} catch (const Exception&) {
pass=false;
}
if (!pass)
std::cout << "FAILED: Integer IsSquare\n";
if (pass)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " Squaring operations\n";
// ****************************** Integer::Modulo and Integer::InverseMod ******************************
{
// http://github.com/weidai11/cryptopp/issues/602 // http://github.com/weidai11/cryptopp/issues/602
Integer a("0x2F0500010000018000000000001C1C000000000000000A000B0000000000000000000000000000FDFFFFFF00000000"); Integer a("0x2F0500010000018000000000001C1C000000000000000A000B0000000000000000000000000000FDFFFFFF00000000");
Integer b("0x3D2F050001"); Integer b("0x3D2F050001");
@ -3205,9 +3241,10 @@ bool TestIntegerOps()
pass = result && pass; pass = result && pass;
if (!result) if (!result)
std::cout << "FAILED: InverseMod operation\n"; std::cout << "FAILED: InverseMod operation\n";
}
RandomNumberGenerator& prng = GlobalRNG(); RandomNumberGenerator& prng = GlobalRNG();
for (unsigned int i=0; i<128; ++i) for (unsigned int i=0; i<128+64; ++i)
{ {
Integer a(prng, 1024), m(prng, 1024); Integer a(prng, 1024), m(prng, 1024);
a++, m++; // make non-0 a++, m++; // make non-0
@ -3216,34 +3253,29 @@ bool TestIntegerOps()
switch (i) switch (i)
{ {
case 0: case 0:
a = 0; a = -1; break;
break;
case 1: case 1:
a = m-1; a = 0; break;
break;
case 2: case 2:
a = m; a = 1; break;
break;
case 3: case 3:
a = m+1; a = m-1; break;
break;
case 4: case 4:
a = 2*m-1; a = m; break;
break;
case 5: case 5:
a = 2*m; a = m+1; break;
break;
case 6: case 6:
a = 2*m+1; a = 2*m-1; break;
break;
case 7: case 7:
a = (m<<256)-1; a = 2*m; break;
break;
case 8: case 8:
a = (m<<256); a = 2*m+1; break;
break;
case 9: case 9:
a = (m<<256)+1; a = (m<<256)-1; break;
case 10:
a = (m<<256); break;
case 11:
a = (m<<256)+1; break;
default: default:
;; ;;
} }
@ -3337,6 +3369,34 @@ bool TestIntegerOps()
std::cout << "FAILED:"; std::cout << "FAILED:";
std::cout << " InverseMod operations\n"; std::cout << " InverseMod operations\n";
// ****************************** Integer::Power2 ******************************
{
Integer x, y;
x = Integer::Power2(0);
result = (x == 1);
pass = result && pass;
if (!result)
std::cout << "FAILED: Power2 (0) operation\n";
x = Integer::Power2(1);
result = (x == 2);
pass = result && pass;
if (!result)
std::cout << "FAILED: Power2 (1) operation\n";
if (pass)
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " Power2 operations\n";
}
// ****************************** Integer::Power2 ******************************
return pass; return pass;
} }
#endif #endif