xref: /PHP-7.0/ext/gmp/tests/gmp_import.phpt (revision 03797a73)
1--TEST--
2gmp_import() basic tests
3--SKIPIF--
4<?php if (!extension_loaded("gmp")) echo "skip"; ?>
5--FILE--
6<?php
7
8// Tests taken from GMPs own test suite.
9
10// format is [expected, size, options, input]
11$import = [
12    ['0',1,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
33$passed = true;
34foreach ($import as $k => $test) {
35    $gmp = gmp_import(hex2bin($test[3]), $test[1], $test[2]);
36    if ($gmp instanceof GMP) {
37        $result = gmp_strval($gmp, 16);
38        if ($result !== $test[0]) {
39            echo "$k: '$result' !== '{$test[0]}'\n";
40            $passed = false;
41        }
42    } else {
43        $type = gettype($gmp);
44        echo "$k: $type !== '{$test[0]}'\n";
45    }
46}
47
48var_dump($passed);
49
50// Invalid arguments (zpp failure)
51var_dump(gmp_import());
52
53// Invalid word sizes
54var_dump(gmp_import('a', -1));
55var_dump(gmp_import('a', 0));
56
57// Invalid data lengths
58var_dump(gmp_import('a', 2));
59var_dump(gmp_import('aa', 3));
60var_dump(gmp_import(str_repeat('a', 100), 64));
61
62// Invalid options
63var_dump(gmp_import('a', 1, GMP_MSW_FIRST | GMP_LSW_FIRST));
64var_dump(gmp_import('a', 1, GMP_BIG_ENDIAN | GMP_LITTLE_ENDIAN));
65
66--EXPECTF--
67bool(true)
68
69Warning: gmp_import() expects at least 1 parameter, 0 given in %s on line %d
70NULL
71
72Warning: gmp_import(): Word size must be positive, -1 given in %s on line %d
73bool(false)
74
75Warning: gmp_import(): Word size must be positive, 0 given in %s on line %d
76bool(false)
77
78Warning: gmp_import(): Input length must be a multiple of word size in %s on line %d
79bool(false)
80
81Warning: gmp_import(): Input length must be a multiple of word size in %s on line %d
82bool(false)
83
84Warning: gmp_import(): Input length must be a multiple of word size in %s on line %d
85bool(false)
86
87Warning: gmp_import(): Invalid options: Conflicting word orders in %s on line %d
88bool(false)
89
90Warning: gmp_import(): Invalid options: Conflicting word endianness in %s on line %d
91bool(false)
92