gmp_export.phpt 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --TEST--
  2. gmp_export() basic tests
  3. --SKIPIF--
  4. <?php if (!extension_loaded("gmp")) echo "skip"; ?>
  5. --FILE--
  6. <?php
  7. // Tests taken from GMPs own test suite.
  8. // format is [output, size, options, expected]
  9. $export = [
  10. ['0',1,GMP_BIG_ENDIAN,''],
  11. ['0',2,GMP_BIG_ENDIAN,''],
  12. ['0',3,GMP_BIG_ENDIAN,''],
  13. ['12345678',1,GMP_BIG_ENDIAN,'12345678'],
  14. ['12345678',4,GMP_BIG_ENDIAN,'12345678'],
  15. ['12345678',4,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'12345678'],
  16. ['12345678',1,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'78563412'],
  17. ['12345678',4,GMP_LITTLE_ENDIAN,'78563412'],
  18. ['12345678',4,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'78563412'],
  19. ['123456789ABC',2,GMP_BIG_ENDIAN,'123456789abc'],
  20. ['123456789ABC',2,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'9abc56781234'],
  21. ['123456789ABC',2,GMP_LITTLE_ENDIAN,'34127856bc9a'],
  22. ['123456789ABC',2,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'bc9a78563412'],
  23. ['112233445566778899AABBCC',4,GMP_BIG_ENDIAN,'112233445566778899aabbcc'],
  24. ['112233445566778899AABBCC',4,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'99aabbcc5566778811223344'],
  25. ['112233445566778899AABBCC',4,GMP_LITTLE_ENDIAN,'4433221188776655ccbbaa99'],
  26. ['112233445566778899AABBCC',4,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'ccbbaa998877665544332211'],
  27. ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_BIG_ENDIAN,'100120023003400450056006700780089009a00ab00bc00c'],
  28. ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'9009a00ab00bc00c50056006700780081001200230034004'],
  29. ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_LITTLE_ENDIAN,'044003300220011008800770066005500cc00bb00aa00990'],
  30. ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'0cc00bb00aa0099008800770066005500440033002200110']
  31. ];
  32. $passed = true;
  33. foreach ($export as $k => $test) {
  34. $gmp = gmp_init($test[0], 16);
  35. $str = gmp_export($gmp, $test[1], $test[2]);
  36. if (is_string($str)) {
  37. $result = bin2hex($str);
  38. if ($result !== $test[3]) {
  39. echo "$k: '$result' !== '{$test[3]}'\n";
  40. $passed = false;
  41. }
  42. } else {
  43. $type = gettype($str);
  44. echo "$k: $type !== '{$test[3]}'\n";
  45. }
  46. }
  47. var_dump($passed);
  48. // Invalid arguments (zpp failure)
  49. var_dump(gmp_export());
  50. // Invalid word sizes
  51. var_dump(gmp_export(123, -1));
  52. var_dump(gmp_export(123, 0));
  53. // Invalid options
  54. var_dump(gmp_export(123, 1, GMP_MSW_FIRST | GMP_LSW_FIRST));
  55. var_dump(gmp_export(123, 1, GMP_BIG_ENDIAN | GMP_LITTLE_ENDIAN));
  56. --EXPECTF--
  57. bool(true)
  58. Warning: gmp_export() expects at least 1 parameter, 0 given in %s on line %d
  59. NULL
  60. Warning: gmp_export(): Word size must be positive, -1 given in %s on line %d
  61. bool(false)
  62. Warning: gmp_export(): Word size must be positive, 0 given in %s on line %d
  63. bool(false)
  64. Warning: gmp_export(): Invalid options: Conflicting word orders in %s on line %d
  65. bool(false)
  66. Warning: gmp_export(): Invalid options: Conflicting word endianness in %s on line %d
  67. bool(false)