0
0
mirror of https://github.com/pmmp/ext-encoding.git synced 2025-09-21 01:10:01 +00:00
Files
ext-encoding/tests/symmetry.phpt
Dylan K. Taylor fbf325a283 Drop array-of-type functions (for now)
While these functions do, in themselves, provide a performance advantage
over writing a plain array, this doesn't consider hidden costs.

- When encoding a chunk palette, the palette has to first be converted
  from a native array to a PHP array just for this extension to accept
  and turn back into an std::vector. This makes no sense and is
  needlessly costing performance.
- Encoding chunk palettes in batch also requires allocating a *second*
  array, just for this extension's consumption, because the values in
  the palette have to be transformed before encoding. We could modify
  the arrays in-place, but it's optimising the wrong problem.
- Generally speaking, the places that actually benefit from these
  functions are either cold paths, or they'd benefit more generally from
  not using PHP arrays, or both. So it's hard to make the case for
  optimising this particular use case.
- The cost of turning PHP arrays into std::vector in this extension (a
  necessary step) is rather high.

The overarching theme here is that arrays need to be avoided everywhere
where performance or memory usage is an issue, and having this extension
accept and especially *return* them is hobbling consumers of the API.

The logical next step would be to add classes like IntArray, LongArray
etc to act as a thin PHP wrapper around native uint32_t[] etc, and then
have array APIs in ext-encoding that would accept and return these
specialised classes instead of PHP arrays. However, this is an endeavour
I'd prefer to undertake speculatively *after* getting the lion's share of
the benefits from ext-encoding's regular (non array) functions, given the
rather limited benefit expected anyway (we barely have any use cases for
writing direct array-of-type anyway). In the meantime, having these
barely-useful functions in the API would give us backwards-compatibility
constraints if they made it to a stable release, which would be an
obstacle for implementing actually good array-of-type functions later.

So, bye-bye array-of-type, for now...
2025-09-11 17:04:47 +01:00

620 lines
17 KiB
PHP

--TEST--
Test symmetry of all encode/decode methods and their array variants
--FILE--
<?php
use pmmp\encoding\Byte;
use pmmp\encoding\VarInt;
require __DIR__ . '/type-samples.inc';
require __DIR__ . '/symmetry-tests.inc';
//these have no array equivalents
echo "########## TEST unsigned byte ##########\n";
testSingleSymmetry([0, 1, 127, 128, 254, 255], Byte::readUnsigned(...), Byte::writeUnsigned(...), Byte::unpackUnsigned(...), Byte::packUnsigned(...));
echo "########## END TEST unsigned byte ##########\n\n";
echo "########## TEST signed byte ##########\n";
testSingleSymmetry([-128, -1, 0, 1, 126, 127], Byte::readSigned(...), Byte::writeSigned(...), Byte::unpackSigned(...), Byte::packSigned(...));
echo "########## END TEST signed byte ##########\n\n";
foreach(EndianInts::cases() as $case){
$samples = $case->getSamples();
testFullSymmetry("big endian " . $case->name, $samples, ...$case->getMethods(true));
testFullSymmetry("little endian " . $case->name, $samples, ...$case->getMethods(false));
}
foreach(FloatSamples::cases() as $case){
$samples = [-1.5, -1.0, 0.0, 1.0, 1.5]; //TODO: float bounds are hard to test
testFullSymmetry("big endian " . $case->name, $samples, ...$case->getMethods(true));
testFullSymmetry("little endian " . $case->name, $samples, ...$case->getMethods(false));
}
testFullSymmetry("varint", EndianInts::UNSIGNED_INT->getSamples(), VarInt::readUnsignedInt(...), VarInt::writeUnsignedInt(...), VarInt::unpackUnsignedInt(...), VarInt::packUnsignedInt(...));
testFullSymmetry("varint zigzag", EndianInts::SIGNED_INT->getSamples(), VarInt::readSignedInt(...), VarInt::writeSignedInt(...), VarInt::unpackSignedInt(...), VarInt::packSignedInt(...));
testFullSymmetry("varlong", EndianInts::UNSIGNED_LONG->getSamples(), VarInt::readUnsignedLong(...), VarInt::writeUnsignedLong(...), VarInt::unpackUnsignedLong(...), VarInt::packUnsignedLong(...));
testFullSymmetry("varlong zigzag", EndianInts::SIGNED_LONG->getSamples(), VarInt::readSignedLong(...), VarInt::writeSignedLong(...), VarInt::unpackSignedLong(...), VarInt::packSignedLong(...));
?>
--EXPECT--
########## TEST unsigned byte ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(127): match = YES
(128): match = YES
(254): match = YES
(255): match = YES
########## END TEST unsigned byte ##########
########## TEST signed byte ##########
--- single read symmetry ---
(-128): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(126): match = YES
(127): match = YES
########## END TEST signed byte ##########
########## TEST big endian UNSIGNED_SHORT ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(65534): match = YES
(65535): match = YES
--- single pack vs buffer read symmetry ---
(0): match = YES
(1): match = YES
(65534): match = YES
(65535): match = YES
--- single buffer write vs unpack symmetry ---
(0): match = YES
(1): match = YES
(65534): match = YES
(65535): match = YES
########## END TEST big endian UNSIGNED_SHORT ##########
########## TEST little endian UNSIGNED_SHORT ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(65534): match = YES
(65535): match = YES
--- single pack vs buffer read symmetry ---
(0): match = YES
(1): match = YES
(65534): match = YES
(65535): match = YES
--- single buffer write vs unpack symmetry ---
(0): match = YES
(1): match = YES
(65534): match = YES
(65535): match = YES
########## END TEST little endian UNSIGNED_SHORT ##########
########## TEST big endian SIGNED_SHORT ##########
--- single read symmetry ---
(-32768): match = YES
(-32767): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(32766): match = YES
(32767): match = YES
--- single pack vs buffer read symmetry ---
(-32768): match = YES
(-32767): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(32766): match = YES
(32767): match = YES
--- single buffer write vs unpack symmetry ---
(-32768): match = YES
(-32767): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(32766): match = YES
(32767): match = YES
########## END TEST big endian SIGNED_SHORT ##########
########## TEST little endian SIGNED_SHORT ##########
--- single read symmetry ---
(-32768): match = YES
(-32767): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(32766): match = YES
(32767): match = YES
--- single pack vs buffer read symmetry ---
(-32768): match = YES
(-32767): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(32766): match = YES
(32767): match = YES
--- single buffer write vs unpack symmetry ---
(-32768): match = YES
(-32767): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(32766): match = YES
(32767): match = YES
########## END TEST little endian SIGNED_SHORT ##########
########## TEST big endian UNSIGNED_TRIAD ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(16777214): match = YES
(16777215): match = YES
--- single pack vs buffer read symmetry ---
(0): match = YES
(1): match = YES
(16777214): match = YES
(16777215): match = YES
--- single buffer write vs unpack symmetry ---
(0): match = YES
(1): match = YES
(16777214): match = YES
(16777215): match = YES
########## END TEST big endian UNSIGNED_TRIAD ##########
########## TEST little endian UNSIGNED_TRIAD ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(16777214): match = YES
(16777215): match = YES
--- single pack vs buffer read symmetry ---
(0): match = YES
(1): match = YES
(16777214): match = YES
(16777215): match = YES
--- single buffer write vs unpack symmetry ---
(0): match = YES
(1): match = YES
(16777214): match = YES
(16777215): match = YES
########## END TEST little endian UNSIGNED_TRIAD ##########
########## TEST big endian SIGNED_TRIAD ##########
--- single read symmetry ---
(-8388608): match = YES
(-8388607): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(8388606): match = YES
(8388607): match = YES
--- single pack vs buffer read symmetry ---
(-8388608): match = YES
(-8388607): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(8388606): match = YES
(8388607): match = YES
--- single buffer write vs unpack symmetry ---
(-8388608): match = YES
(-8388607): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(8388606): match = YES
(8388607): match = YES
########## END TEST big endian SIGNED_TRIAD ##########
########## TEST little endian SIGNED_TRIAD ##########
--- single read symmetry ---
(-8388608): match = YES
(-8388607): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(8388606): match = YES
(8388607): match = YES
--- single pack vs buffer read symmetry ---
(-8388608): match = YES
(-8388607): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(8388606): match = YES
(8388607): match = YES
--- single buffer write vs unpack symmetry ---
(-8388608): match = YES
(-8388607): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(8388606): match = YES
(8388607): match = YES
########## END TEST little endian SIGNED_TRIAD ##########
########## TEST big endian UNSIGNED_INT ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
--- single pack vs buffer read symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
--- single buffer write vs unpack symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
########## END TEST big endian UNSIGNED_INT ##########
########## TEST little endian UNSIGNED_INT ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
--- single pack vs buffer read symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
--- single buffer write vs unpack symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
########## END TEST little endian UNSIGNED_INT ##########
########## TEST big endian SIGNED_INT ##########
--- single read symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
--- single pack vs buffer read symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
--- single buffer write vs unpack symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
########## END TEST big endian SIGNED_INT ##########
########## TEST little endian SIGNED_INT ##########
--- single read symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
--- single pack vs buffer read symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
--- single buffer write vs unpack symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
########## END TEST little endian SIGNED_INT ##########
########## TEST big endian UNSIGNED_LONG ##########
--- single read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single pack vs buffer read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single buffer write vs unpack symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
########## END TEST big endian UNSIGNED_LONG ##########
########## TEST little endian UNSIGNED_LONG ##########
--- single read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single pack vs buffer read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single buffer write vs unpack symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
########## END TEST little endian UNSIGNED_LONG ##########
########## TEST big endian SIGNED_LONG ##########
--- single read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single pack vs buffer read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single buffer write vs unpack symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
########## END TEST big endian SIGNED_LONG ##########
########## TEST little endian SIGNED_LONG ##########
--- single read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single pack vs buffer read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single buffer write vs unpack symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
########## END TEST little endian SIGNED_LONG ##########
########## TEST big endian FLOAT ##########
--- single read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single pack vs buffer read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single buffer write vs unpack symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
########## END TEST big endian FLOAT ##########
########## TEST little endian FLOAT ##########
--- single read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single pack vs buffer read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single buffer write vs unpack symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
########## END TEST little endian FLOAT ##########
########## TEST big endian DOUBLE ##########
--- single read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single pack vs buffer read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single buffer write vs unpack symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
########## END TEST big endian DOUBLE ##########
########## TEST little endian DOUBLE ##########
--- single read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single pack vs buffer read symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
--- single buffer write vs unpack symmetry ---
(-1.5): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(1.5): match = YES
########## END TEST little endian DOUBLE ##########
########## TEST varint ##########
--- single read symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
--- single pack vs buffer read symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
--- single buffer write vs unpack symmetry ---
(0): match = YES
(1): match = YES
(4294967294): match = YES
(4294967295): match = YES
########## END TEST varint ##########
########## TEST varint zigzag ##########
--- single read symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
--- single pack vs buffer read symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
--- single buffer write vs unpack symmetry ---
(-2147483648): match = YES
(-2147483647): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(2147483646): match = YES
(2147483647): match = YES
########## END TEST varint zigzag ##########
########## TEST varlong ##########
--- single read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single pack vs buffer read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single buffer write vs unpack symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
########## END TEST varlong ##########
########## TEST varlong zigzag ##########
--- single read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single pack vs buffer read symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
--- single buffer write vs unpack symmetry ---
(-9223372036854775808): match = YES
(-9223372036854775807): match = YES
(-1): match = YES
(0): match = YES
(1): match = YES
(9223372036854775806): match = YES
(9223372036854775807): match = YES
########## END TEST varlong zigzag ##########