BcmathTest.php 2.9 KB

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