|
| 1 | +--TEST-- |
| 2 | +pack() float and double value conversions |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +$values = [ |
| 7 | + 'null' => null, |
| 8 | + 'false' => false, |
| 9 | + 'true' => true, |
| 10 | + 'int' => 42, |
| 11 | + 'float' => 42.5, |
| 12 | + 'numeric integer string' => '42', |
| 13 | + 'numeric float string' => '42.5', |
| 14 | + 'numeric scientific string' => '1e3', |
| 15 | +]; |
| 16 | + |
| 17 | +foreach ($values as $name => $value) { |
| 18 | + echo "$name: "; |
| 19 | + var_dump(unpack('d', pack('d', $value))[1]); |
| 20 | +} |
| 21 | + |
| 22 | +echo "reference: "; |
| 23 | +$value = 1.5; |
| 24 | +$reference =& $value; |
| 25 | +var_dump(unpack('d', pack('d', $reference))[1]); |
| 26 | + |
| 27 | +echo "trailing data:\n"; |
| 28 | +var_dump(unpack('d', pack('d', '42 with trailing data'))[1]); |
| 29 | + |
| 30 | +$invalidValues = [ |
| 31 | + 'empty string' => '', |
| 32 | + 'whitespace string' => ' ', |
| 33 | + 'non-numeric string' => 'not numeric', |
| 34 | + 'array' => [], |
| 35 | + 'object' => new stdClass(), |
| 36 | + 'resource' => fopen(__FILE__, 'r'), |
| 37 | +]; |
| 38 | + |
| 39 | +foreach ($invalidValues as $name => $value) { |
| 40 | + echo "$name:\n"; |
| 41 | + try { |
| 42 | + pack('d', $value); |
| 43 | + } catch (Throwable $e) { |
| 44 | + echo $e::class, ': ', $e->getMessage(), "\n"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +echo "all format codes:\n"; |
| 49 | +foreach (['f', 'g', 'G', 'd', 'e', 'E'] as $format) { |
| 50 | + try { |
| 51 | + pack($format, []); |
| 52 | + } catch (Throwable $e) { |
| 53 | + echo "$format: ", $e->getMessage(), "\n"; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +echo "later argument:\n"; |
| 58 | +try { |
| 59 | + pack('d2', 1.0, []); |
| 60 | +} catch (Throwable $e) { |
| 61 | + echo $e::class, ': ', $e->getMessage(), "\n"; |
| 62 | +} |
| 63 | + |
| 64 | +echo "warning converted to exception:\n"; |
| 65 | +set_error_handler(static function (int $errno, string $errstr): never { |
| 66 | + throw new Exception($errstr); |
| 67 | +}); |
| 68 | +try { |
| 69 | + pack('d', '42 with trailing data'); |
| 70 | +} catch (Throwable $e) { |
| 71 | + echo $e::class, ': ', $e->getMessage(), "\n"; |
| 72 | +} |
| 73 | +restore_error_handler(); |
| 74 | +fclose($invalidValues['resource']); |
| 75 | + |
| 76 | +?> |
| 77 | +--EXPECTF-- |
| 78 | +null: float(0) |
| 79 | +false: float(0) |
| 80 | +true: float(1) |
| 81 | +int: float(42) |
| 82 | +float: float(42.5) |
| 83 | +numeric integer string: float(42) |
| 84 | +numeric float string: float(42.5) |
| 85 | +numeric scientific string: float(1000) |
| 86 | +reference: float(1.5) |
| 87 | +trailing data: |
| 88 | + |
| 89 | +Warning: A non-numeric value encountered in %s on line %d |
| 90 | +float(42) |
| 91 | +empty string: |
| 92 | +TypeError: pack(): Argument #2 must be of type float, string given |
| 93 | +whitespace string: |
| 94 | +TypeError: pack(): Argument #2 must be of type float, string given |
| 95 | +non-numeric string: |
| 96 | +TypeError: pack(): Argument #2 must be of type float, string given |
| 97 | +array: |
| 98 | +TypeError: pack(): Argument #2 must be of type float, array given |
| 99 | +object: |
| 100 | +TypeError: pack(): Argument #2 must be of type float, stdClass given |
| 101 | +resource: |
| 102 | +TypeError: pack(): Argument #2 must be of type float, resource given |
| 103 | +all format codes: |
| 104 | +f: pack(): Argument #2 must be of type float, array given |
| 105 | +g: pack(): Argument #2 must be of type float, array given |
| 106 | +G: pack(): Argument #2 must be of type float, array given |
| 107 | +d: pack(): Argument #2 must be of type float, array given |
| 108 | +e: pack(): Argument #2 must be of type float, array given |
| 109 | +E: pack(): Argument #2 must be of type float, array given |
| 110 | +later argument: |
| 111 | +TypeError: pack(): Argument #3 must be of type float, array given |
| 112 | +warning converted to exception: |
| 113 | +Exception: A non-numeric value encountered |
0 commit comments