xref: /PHP-7.4/ext/gmp/tests/gmp_import.phpt (revision d679f022)
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--EXPECTF--
66bool(true)
67
68Warning: gmp_import() expects at least 1 parameter, 0 given in %s on line %d
69NULL
70
71Warning: gmp_import(): Word size must be positive, -1 given in %s on line %d
72bool(false)
73
74Warning: gmp_import(): Word size must be positive, 0 given in %s on line %d
75bool(false)
76
77Warning: gmp_import(): Input length must be a multiple of word size in %s on line %d
78bool(false)
79
80Warning: gmp_import(): Input length must be a multiple of word size in %s on line %d
81bool(false)
82
83Warning: gmp_import(): Input length must be a multiple of word size in %s on line %d
84bool(false)
85
86Warning: gmp_import(): Invalid options: Conflicting word orders in %s on line %d
87bool(false)
88
89Warning: gmp_import(): Invalid options: Conflicting word endianness in %s on line %d
90bool(false)
91