GmpTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. require_once 'Zend/Crypt/Math/BigInteger/Gmp.php';
  3. require_once 'PHPUnit/Framework/TestCase.php';
  4. require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
  5. class Zend_Crypt_Math_BigInteger_GmpTest extends PHPUnit_Framework_TestCase
  6. {
  7. private $_math = null;
  8. public function setUp()
  9. {
  10. if (!extension_loaded('gmp')) {
  11. $this->markTestSkipped('Skipped: Zend_Crypt_Math_BigInteger_GmpTest due to ext/gmp being unavailable');
  12. return;
  13. }
  14. $this->_math = new Zend_Crypt_Math_BigInteger_Gmp;
  15. }
  16. public function testAdd()
  17. {
  18. $this->assertEquals('2', $this->_math->add(1,1));
  19. }
  20. public function testSubtract()
  21. {
  22. $this->assertEquals('-2', $this->_math->subtract(2,4));
  23. }
  24. public function testCompare()
  25. {
  26. $this->assertEquals('0', $this->_math->compare(2,2));
  27. $this->assertEquals('-1', $this->_math->compare(2,4));
  28. $this->assertEquals('1', $this->_math->compare(4,2));
  29. }
  30. public function testDivide()
  31. {
  32. $this->assertEquals('2', $this->_math->divide(4,2));
  33. $this->assertEquals('2', $this->_math->divide(9,4));
  34. }
  35. public function testModulus()
  36. {
  37. $this->assertEquals('1', $this->_math->modulus(3,2));
  38. }
  39. public function testMultiply()
  40. {
  41. $this->assertEquals('4', $this->_math->multiply(2,2));
  42. }
  43. public function testPow()
  44. {
  45. $this->assertEquals('4', $this->_math->pow(2,2));
  46. }
  47. public function testPowMod()
  48. {
  49. $this->assertEquals('1', $this->_math->powmod(2,2,3));
  50. }
  51. public function testSqrt()
  52. {
  53. $this->assertEquals('2', $this->_math->sqrt(4));
  54. }
  55. public function testIntegerToBinaryConversion()
  56. {
  57. $binary = base64_decode(
  58. 'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr'
  59. );
  60. $integer = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
  61. $gmpBinary = $this->_math->integerToBinary($integer);
  62. $this->assertEquals($binary, $gmpBinary);
  63. }
  64. public function testBinaryToIntegerConversion()
  65. {
  66. $binary = base64_decode(
  67. 'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr'
  68. );
  69. $integer = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
  70. $gmpInteger = $this->_math->binaryToInteger($binary);
  71. $this->assertEquals($integer, $gmpInteger);
  72. }
  73. }