BcmathTest.php 4.0 KB

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