xref: /php-src/ext/gmp/tests/gmp_export.phpt (revision e9f783fc)
1--TEST--
2gmp_export() basic tests
3--EXTENSIONS--
4gmp
5--FILE--
6<?php
7
8// Tests taken from GMPs own test suite.
9
10// format is [output, size, options, expected]
11$export = [
12    ['0',1,GMP_BIG_ENDIAN,''],
13    ['0',2,GMP_BIG_ENDIAN,''],
14    ['0',3,GMP_BIG_ENDIAN,''],
15    ['12345678',1,GMP_BIG_ENDIAN,'12345678'],
16    ['12345678',4,GMP_BIG_ENDIAN,'12345678'],
17    ['12345678',4,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'12345678'],
18    ['12345678',1,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'78563412'],
19    ['12345678',4,GMP_LITTLE_ENDIAN,'78563412'],
20    ['12345678',4,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'78563412'],
21    ['123456789ABC',2,GMP_BIG_ENDIAN,'123456789abc'],
22    ['123456789ABC',2,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'9abc56781234'],
23    ['123456789ABC',2,GMP_LITTLE_ENDIAN,'34127856bc9a'],
24    ['123456789ABC',2,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'bc9a78563412'],
25    ['112233445566778899AABBCC',4,GMP_BIG_ENDIAN,'112233445566778899aabbcc'],
26    ['112233445566778899AABBCC',4,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'99aabbcc5566778811223344'],
27    ['112233445566778899AABBCC',4,GMP_LITTLE_ENDIAN,'4433221188776655ccbbaa99'],
28    ['112233445566778899AABBCC',4,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'ccbbaa998877665544332211'],
29    ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_BIG_ENDIAN,'100120023003400450056006700780089009a00ab00bc00c'],
30    ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_LSW_FIRST | GMP_BIG_ENDIAN,'9009a00ab00bc00c50056006700780081001200230034004'],
31    ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_LITTLE_ENDIAN,'044003300220011008800770066005500cc00bb00aa00990'],
32    ['100120023003400450056006700780089009A00AB00BC00C',8,GMP_LSW_FIRST | GMP_LITTLE_ENDIAN,'0cc00bb00aa0099008800770066005500440033002200110']
33];
34
35$passed = true;
36foreach ($export as $k => $test) {
37    $gmp = gmp_init($test[0], 16);
38    $str = gmp_export($gmp, $test[1], $test[2]);
39    if (is_string($str)) {
40        $result = bin2hex($str);
41        if ($result !== $test[3]) {
42            echo "$k: '$result' !== '{$test[3]}'\n";
43            $passed = false;
44        }
45    } else {
46        $type = gettype($str);
47        echo "$k: $type !== '{$test[3]}'\n";
48    }
49}
50
51var_dump($passed);
52
53// Argument converted from int to GMP
54var_dump(bin2hex(gmp_export(0xff)));
55
56// Invalid word sizes
57try {
58    var_dump(gmp_export(123, -1));
59} catch (\ValueError $e) {
60    echo $e->getMessage() . \PHP_EOL;
61}
62try {
63    var_dump(gmp_export(123, 0));
64} catch (\ValueError $e) {
65    echo $e->getMessage() . \PHP_EOL;
66}
67
68// Invalid options
69try {
70    var_dump(gmp_export(123, 1, GMP_MSW_FIRST | GMP_LSW_FIRST));
71} catch (\ValueError $e) {
72    echo $e->getMessage() . \PHP_EOL;
73}
74try {
75    var_dump(gmp_export(123, 1, GMP_BIG_ENDIAN | GMP_LITTLE_ENDIAN));
76} catch (\ValueError $e) {
77    echo $e->getMessage() . \PHP_EOL;
78}
79?>
80--EXPECT--
81bool(true)
82string(2) "ff"
83gmp_export(): Argument #2 ($word_size) must be greater than or equal to 1
84gmp_export(): Argument #2 ($word_size) must be greater than or equal to 1
85gmp_export(): Argument #3 ($flags) cannot use multiple word order options
86gmp_export(): Argument #3 ($flags) cannot use multiple endian options
87