xref: /PHP-8.3/ext/gmp/tests/gmp_import.phpt (revision e9f783fc)
1--TEST--
2gmp_import() basic tests
3--EXTENSIONS--
4gmp
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 word sizes
51try {
52    var_dump(gmp_import('a', -1));
53} catch (\ValueError $e) {
54    echo $e->getMessage() . \PHP_EOL;
55}
56try {
57    var_dump(gmp_import('a', 0));
58} catch (\ValueError $e) {
59    echo $e->getMessage() . \PHP_EOL;
60}
61
62// Invalid data lengths
63try {
64    var_dump(gmp_import('a', 2));
65} catch (\ValueError $e) {
66    echo $e->getMessage() . \PHP_EOL;
67}
68try {
69    var_dump(gmp_import('aa', 3));
70} catch (\ValueError $e) {
71    echo $e->getMessage() . \PHP_EOL;
72}
73try {
74    var_dump(gmp_import(str_repeat('a', 100), 64));
75} catch (\ValueError $e) {
76    echo $e->getMessage() . \PHP_EOL;
77}
78
79// Invalid options
80try {
81    var_dump(gmp_import('a', 1, GMP_MSW_FIRST | GMP_LSW_FIRST));
82} catch (\ValueError $e) {
83    echo $e->getMessage() . \PHP_EOL;
84}
85try {
86    var_dump(gmp_import('a', 1, GMP_BIG_ENDIAN | GMP_LITTLE_ENDIAN));
87} catch (\ValueError $e) {
88    echo $e->getMessage() . \PHP_EOL;
89}
90?>
91--EXPECT--
92bool(true)
93gmp_import(): Argument #2 ($word_size) must be greater than or equal to 1
94gmp_import(): Argument #2 ($word_size) must be greater than or equal to 1
95gmp_import(): Argument #1 ($data) must be a multiple of argument #2 ($word_size)
96gmp_import(): Argument #1 ($data) must be a multiple of argument #2 ($word_size)
97gmp_import(): Argument #1 ($data) must be a multiple of argument #2 ($word_size)
98gmp_import(): Argument #3 ($flags) cannot use multiple word order options
99gmp_import(): Argument #3 ($flags) cannot use multiple endian options
100