GmpTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Crypt/Math/BigInteger/Gmp.php';
  23. require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Crypt
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Crypt
  31. */
  32. class Zend_Crypt_Math_BigInteger_GmpTest extends PHPUnit_Framework_TestCase
  33. {
  34. private $_math = null;
  35. public function setUp()
  36. {
  37. if (!extension_loaded('gmp')) {
  38. $this->markTestSkipped('Skipped: Zend_Crypt_Math_BigInteger_GmpTest due to ext/gmp being unavailable');
  39. return;
  40. }
  41. $this->_math = new Zend_Crypt_Math_BigInteger_Gmp;
  42. }
  43. public function testAdd()
  44. {
  45. $this->assertEquals('2', $this->_math->add(1,1));
  46. }
  47. public function testSubtract()
  48. {
  49. $this->assertEquals('-2', $this->_math->subtract(2,4));
  50. }
  51. public function testCompare()
  52. {
  53. $this->assertEquals('0', $this->_math->compare(2,2));
  54. $this->assertEquals('-1', $this->_math->compare(2,4));
  55. $this->assertEquals('1', $this->_math->compare(4,2));
  56. }
  57. public function testDivide()
  58. {
  59. $this->assertEquals('2', $this->_math->divide(4,2));
  60. $this->assertEquals('2', $this->_math->divide(9,4));
  61. }
  62. public function testModulus()
  63. {
  64. $this->assertEquals('1', $this->_math->modulus(3,2));
  65. }
  66. public function testMultiply()
  67. {
  68. $this->assertEquals('4', $this->_math->multiply(2,2));
  69. }
  70. public function testPow()
  71. {
  72. $this->assertEquals('4', $this->_math->pow(2,2));
  73. }
  74. public function testPowMod()
  75. {
  76. $this->assertEquals('1', $this->_math->powmod(2,2,3));
  77. }
  78. public function testSqrt()
  79. {
  80. $this->assertEquals('2', $this->_math->sqrt(4));
  81. }
  82. public function testIntegerToBinaryConversion()
  83. {
  84. $binary = base64_decode(
  85. 'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr'
  86. );
  87. $integer = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
  88. $gmpBinary = $this->_math->integerToBinary($integer);
  89. $this->assertEquals($binary, $gmpBinary);
  90. }
  91. public function testBinaryToIntegerConversion()
  92. {
  93. $binary = base64_decode(
  94. 'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr'
  95. );
  96. $integer = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
  97. $gmpInteger = $this->_math->binaryToInteger($binary);
  98. $this->assertEquals($integer, $gmpInteger);
  99. }
  100. }