2
0

GmpTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-2009 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 'PHPUnit/Framework/TestCase.php';
  24. require_once 'Zend/Crypt/Math/BigInteger/Bcmath.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Crypt
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Crypt
  32. */
  33. class Zend_Crypt_Math_BigInteger_GmpTest extends PHPUnit_Framework_TestCase
  34. {
  35. private $_math = null;
  36. public function setUp()
  37. {
  38. if (!extension_loaded('gmp')) {
  39. $this->markTestSkipped('Skipped: Zend_Crypt_Math_BigInteger_GmpTest due to ext/gmp being unavailable');
  40. return;
  41. }
  42. $this->_math = new Zend_Crypt_Math_BigInteger_Gmp;
  43. }
  44. public function testAdd()
  45. {
  46. $this->assertEquals('2', $this->_math->add(1,1));
  47. }
  48. public function testSubtract()
  49. {
  50. $this->assertEquals('-2', $this->_math->subtract(2,4));
  51. }
  52. public function testCompare()
  53. {
  54. $this->assertEquals('0', $this->_math->compare(2,2));
  55. $this->assertEquals('-1', $this->_math->compare(2,4));
  56. $this->assertEquals('1', $this->_math->compare(4,2));
  57. }
  58. public function testDivide()
  59. {
  60. $this->assertEquals('2', $this->_math->divide(4,2));
  61. $this->assertEquals('2', $this->_math->divide(9,4));
  62. }
  63. public function testModulus()
  64. {
  65. $this->assertEquals('1', $this->_math->modulus(3,2));
  66. }
  67. public function testMultiply()
  68. {
  69. $this->assertEquals('4', $this->_math->multiply(2,2));
  70. }
  71. public function testPow()
  72. {
  73. $this->assertEquals('4', $this->_math->pow(2,2));
  74. }
  75. public function testPowMod()
  76. {
  77. $this->assertEquals('1', $this->_math->powmod(2,2,3));
  78. }
  79. public function testSqrt()
  80. {
  81. $this->assertEquals('2', $this->_math->sqrt(4));
  82. }
  83. public function testIntegerToBinaryConversion()
  84. {
  85. $binary = base64_decode(
  86. 'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr'
  87. );
  88. $integer = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
  89. $gmpBinary = $this->_math->integerToBinary($integer);
  90. $this->assertEquals($binary, $gmpBinary);
  91. }
  92. public function testBinaryToIntegerConversion()
  93. {
  94. $binary = base64_decode(
  95. 'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr'
  96. );
  97. $integer = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
  98. $gmpInteger = $this->_math->binaryToInteger($binary);
  99. $this->assertEquals($integer, $gmpInteger);
  100. }
  101. }