diff --git a/bin/auto-sync.txt b/bin/auto-sync.txt index db1ae412f..d62efe04a 100644 --- a/bin/auto-sync.txt +++ b/bin/auto-sync.txt @@ -99,6 +99,7 @@ triangle twelve-days two-bucket two-fer +variable-length-quantity wordy word-count yacht diff --git a/exercises/practice/variable-length-quantity/.docs/instructions.md b/exercises/practice/variable-length-quantity/.docs/instructions.md index eadce28d0..501254826 100644 --- a/exercises/practice/variable-length-quantity/.docs/instructions.md +++ b/exercises/practice/variable-length-quantity/.docs/instructions.md @@ -2,10 +2,10 @@ Implement variable length quantity encoding and decoding. -The goal of this exercise is to implement [VLQ](https://en.wikipedia.org/wiki/Variable-length_quantity) encoding/decoding. +The goal of this exercise is to implement [VLQ][vlq] encoding/decoding. In short, the goal of this encoding is to encode integer values in a way that would save bytes. -Only the first 7 bits of each byte is significant (right-justified; sort of like an ASCII byte). +Only the first 7 bits of each byte are significant (right-justified; sort of like an ASCII byte). So, if you have a 32-bit value, you have to unpack it into a series of 7-bit bytes. Of course, you will have a variable number of bytes depending upon your integer. To indicate which is the last byte of the series, you leave bit #7 clear. @@ -30,3 +30,5 @@ Here are examples of integers as 32-bit values, and the variable length quantiti 08000000 C0 80 80 00 0FFFFFFF FF FF FF 7F ``` + +[vlq]: https://en.wikipedia.org/wiki/Variable-length_quantity diff --git a/exercises/practice/variable-length-quantity/.meta/config.json b/exercises/practice/variable-length-quantity/.meta/config.json index e236bb5a5..2e985a424 100644 --- a/exercises/practice/variable-length-quantity/.meta/config.json +++ b/exercises/practice/variable-length-quantity/.meta/config.json @@ -7,7 +7,9 @@ "Hrumpa", "kunicmarko20", "kytrinyx", - "petemcfarlane" + "petemcfarlane", + "A-O-Emmanuel", + "resu-xuniL" ], "files": { "solution": [ diff --git a/exercises/practice/variable-length-quantity/.meta/example.php b/exercises/practice/variable-length-quantity/.meta/example.php index c11a2487b..0049be50c 100644 --- a/exercises/practice/variable-length-quantity/.meta/example.php +++ b/exercises/practice/variable-length-quantity/.meta/example.php @@ -1,39 +1,13 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); -/** - * @param array $integers - * @return array - */ -function vlq_encode(array $integers) +function vlq_encode(array $integers): array { $result = []; foreach ($integers as $integer) { - if ($integer > 0xfffffff) { + if ($integer > PHP_INT_MAX) { throw new InvalidArgumentException('The value is greater than the maximum allowed.'); } @@ -49,11 +23,7 @@ function vlq_encode(array $integers) return $result; } -/** - * @param array $bytes - * @return array - */ -function vlq_decode(array $bytes) +function vlq_decode(array $bytes): array { $result = []; $integer = 0; @@ -73,7 +43,7 @@ function vlq_decode(array $bytes) } if (($byte & 0x80) !== 0) { - throw new InvalidArgumentException('Incomplete byte sequence.'); + throw new InvalidArgumentException('Error: incomplete sequence.'); } return $result; diff --git a/exercises/practice/variable-length-quantity/.meta/tests.toml b/exercises/practice/variable-length-quantity/.meta/tests.toml index 923fa0c1a..53be789a3 100644 --- a/exercises/practice/variable-length-quantity/.meta/tests.toml +++ b/exercises/practice/variable-length-quantity/.meta/tests.toml @@ -1,81 +1,103 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. [35c9db2e-f781-4c52-b73b-8e76427defd0] -description = "zero" +description = "Encode a series of integers, producing a series of bytes. -> zero" [be44d299-a151-4604-a10e-d4b867f41540] -description = "arbitrary single byte" +description = "Encode a series of integers, producing a series of bytes. -> arbitrary single byte" + +[890bc344-cb80-45af-b316-6806a6971e81] +description = "Encode a series of integers, producing a series of bytes. -> asymmetric single byte" [ea399615-d274-4af6-bbef-a1c23c9e1346] -description = "largest single byte" +description = "Encode a series of integers, producing a series of bytes. -> largest single byte" [77b07086-bd3f-4882-8476-8dcafee79b1c] -description = "smallest double byte" +description = "Encode a series of integers, producing a series of bytes. -> smallest double byte" [63955a49-2690-4e22-a556-0040648d6b2d] -description = "arbitrary double byte" +description = "Encode a series of integers, producing a series of bytes. -> arbitrary double byte" + +[4977d113-251b-4d10-a3ad-2f5a7756bb58] +description = "Encode a series of integers, producing a series of bytes. -> asymmetric double byte" [29da7031-0067-43d3-83a7-4f14b29ed97a] -description = "largest double byte" +description = "Encode a series of integers, producing a series of bytes. -> largest double byte" [3345d2e3-79a9-4999-869e-d4856e3a8e01] -description = "smallest triple byte" +description = "Encode a series of integers, producing a series of bytes. -> smallest triple byte" [5df0bc2d-2a57-4300-a653-a75ee4bd0bee] -description = "arbitrary triple byte" +description = "Encode a series of integers, producing a series of bytes. -> arbitrary triple byte" + +[6731045f-1e00-4192-b5ae-98b22e17e9f7] +description = "Encode a series of integers, producing a series of bytes. -> asymmetric triple byte" [f51d8539-312d-4db1-945c-250222c6aa22] -description = "largest triple byte" +description = "Encode a series of integers, producing a series of bytes. -> largest triple byte" [da78228b-544f-47b7-8bfe-d16b35bbe570] -description = "smallest quadruple byte" +description = "Encode a series of integers, producing a series of bytes. -> smallest quadruple byte" [11ed3469-a933-46f1-996f-2231e05d7bb6] -description = "arbitrary quadruple byte" +description = "Encode a series of integers, producing a series of bytes. -> arbitrary quadruple byte" + +[b45ef770-cbba-48c2-bd3c-c6362679516e] +description = "Encode a series of integers, producing a series of bytes. -> asymmetric quadruple byte" [d5f3f3c3-e0f1-4e7f-aad0-18a44f223d1c] -description = "largest quadruple byte" +description = "Encode a series of integers, producing a series of bytes. -> largest quadruple byte" [91a18b33-24e7-4bfb-bbca-eca78ff4fc47] -description = "smallest quintuple byte" +description = "Encode a series of integers, producing a series of bytes. -> smallest quintuple byte" [5f34ff12-2952-4669-95fe-2d11b693d331] -description = "arbitrary quintuple byte" +description = "Encode a series of integers, producing a series of bytes. -> arbitrary quintuple byte" + +[9be46731-7cd5-415c-b960-48061cbc1154] +description = "Encode a series of integers, producing a series of bytes. -> asymmetric quintuple byte" [7489694b-88c3-4078-9864-6fe802411009] -description = "maximum 32-bit integer input" +description = "Encode a series of integers, producing a series of bytes. -> maximum 32-bit integer input" [f9b91821-cada-4a73-9421-3c81d6ff3661] -description = "two single-byte values" +description = "Encode a series of integers, producing a series of bytes. -> two single-byte values" [68694449-25d2-4974-ba75-fa7bb36db212] -description = "two multi-byte values" +description = "Encode a series of integers, producing a series of bytes. -> two multi-byte values" [51a06b5c-de1b-4487-9a50-9db1b8930d85] -description = "many multi-byte values" +description = "Encode a series of integers, producing a series of bytes. -> many multi-byte values" [baa73993-4514-4915-bac0-f7f585e0e59a] -description = "one byte" +description = "Decode a series of bytes, producing a series of integers. -> one byte" [72e94369-29f9-46f2-8c95-6c5b7a595aee] -description = "two bytes" +description = "Decode a series of bytes, producing a series of integers. -> two bytes" [df5a44c4-56f7-464e-a997-1db5f63ce691] -description = "three bytes" +description = "Decode a series of bytes, producing a series of integers. -> three bytes" [1bb58684-f2dc-450a-8406-1f3452aa1947] -description = "four bytes" +description = "Decode a series of bytes, producing a series of integers. -> four bytes" [cecd5233-49f1-4dd1-a41a-9840a40f09cd] -description = "maximum 32-bit integer" +description = "Decode a series of bytes, producing a series of integers. -> maximum 32-bit integer" [e7d74ba3-8b8e-4bcb-858d-d08302e15695] -description = "incomplete sequence causes error" +description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error" [aa378291-9043-4724-bc53-aca1b4a3fcb6] -description = "incomplete sequence causes error, even if value is zero" +description = "Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error, even if value is zero" [a91e6f5a-c64a-48e3-8a75-ce1a81e0ebee] -description = "multiple values" +description = "Decode a series of bytes, producing a series of integers. -> multiple values" diff --git a/exercises/practice/variable-length-quantity/VariableLengthQuantityTest.php b/exercises/practice/variable-length-quantity/VariableLengthQuantityTest.php index 62e34caea..0e01b4e4d 100644 --- a/exercises/practice/variable-length-quantity/VariableLengthQuantityTest.php +++ b/exercises/practice/variable-length-quantity/VariableLengthQuantityTest.php @@ -1,29 +1,8 @@ . - * - * To disable strict typing, comment out the directive below. - */ - declare(strict_types=1); +use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; class VariableLengthQuantityTest extends TestCase @@ -33,120 +12,310 @@ public static function setUpBeforeClass(): void require_once 'VariableLengthQuantity.php'; } - public function testItEncodesSingleBytes(): void + /** + * uuid 35c9db2e-f781-4c52-b73b-8e76427defd0 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> zero')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesZero(): void { $this->assertEquals([0x00], vlq_encode([0x00])); + } + + /** + * uuid be44d299-a151-4604-a10e-d4b867f41540 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> arbitrary single byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesArbitrarySingleByte(): void + { $this->assertEquals([0x40], vlq_encode([0x40])); + } + + /** + * uuid 890bc344-cb80-45af-b316-6806a6971e81 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> asymmetric single byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesAsymmetricSingleByte(): void + { + $this->assertEquals([0x53], vlq_encode([0x53])); + } + + /** + * uuid ea399615-d274-4af6-bbef-a1c23c9e1346 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> largest single byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesLargestSingleByte(): void + { $this->assertEquals([0x7f], vlq_encode([0x7f])); } - public function testItEncodesDoubleBytes(): void + /** + * uuid 77b07086-bd3f-4882-8476-8dcafee79b1c + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> smallest double byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesSmallestDoubleByte(): void { $this->assertEquals([0x81, 0x00], vlq_encode([0x80])); - $this->assertEquals([0xc0, 0x00], vlq_encode([0x2000])); - $this->assertEquals([0xff, 0x7f], vlq_encode([0x3fff])); } - public function testItEncodesTripleBytes(): void + /** + * uuid 63955a49-2690-4e22-a556-0040648d6b2d + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> arbitrary double byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesArbitraryDoubleByte(): void + { + $this->assertEquals([0xC0, 0x00], vlq_encode([0x2000])); + } + + /** + * uuid 4977d113-251b-4d10-a3ad-2f5a7756bb58 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> asymmetric double byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesAsymmetricDoubleByte(): void + { + $this->assertEquals([0x81, 0x2D], vlq_encode([0xAD])); + } + + /** + * uuid 29da7031-0067-43d3-83a7-4f14b29ed97a + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> largest double byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesLargestDoubleByte(): void + { + $this->assertEquals([0xFF, 0x7F], vlq_encode([0x3FFF])); + } + + /** + * uuid 3345d2e3-79a9-4999-869e-d4856e3a8e01 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> smallest triple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesSmallestTripleByte(): void { $this->assertEquals([0x81, 0x80, 0x00], vlq_encode([0x4000])); - $this->assertEquals([0xc0, 0x80, 0x00], vlq_encode([0x100000])); - $this->assertEquals([0xff, 0xff, 0x7f], vlq_encode([0x1fffff])); } - public function testItEncodesQuadrupleBytes(): void + /** + * uuid 5df0bc2d-2a57-4300-a653-a75ee4bd0bee + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> arbitrary triple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesArbitraryTripleByte(): void + { + $this->assertEquals([0xC0, 0x80, 0x00], vlq_encode([0x100000])); + } + + /** + * uuid 6731045f-1e00-4192-b5ae-98b22e17e9f7 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> asymmetric triple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesAsymmetricTripleByte(): void + { + $this->assertEquals([0x87, 0xAB, 0x1C], vlq_encode([0x1D59C])); + } + + /** + * uuid f51d8539-312d-4db1-945c-250222c6aa22 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> largest triple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesLargestTripleByte(): void + { + $this->assertEquals([0xFF, 0xFF, 0x7F], vlq_encode([0x1FFFFF])); + } + + /** + * uuid da78228b-544f-47b7-8bfe-d16b35bbe570 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> smallest quadruple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesSmallestQuadrupleByte(): void { $this->assertEquals([0x81, 0x80, 0x80, 0x00], vlq_encode([0x200000])); - $this->assertEquals([0xc0, 0x80, 0x80, 0x00], vlq_encode([0x8000000])); - $this->assertEquals([0xff, 0xff, 0xff, 0x7f], vlq_encode([0xfffffff])); } - public function testItEncodesMultipleValues(): void + /** + * uuid 11ed3469-a933-46f1-996f-2231e05d7bb6 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> arbitrary quadruple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesArbitraryQuadrupleByte(): void + { + $this->assertEquals([0xC0, 0x80, 0x80, 0x00], vlq_encode([0x8000000])); + } + + /** + * uuid b45ef770-cbba-48c2-bd3c-c6362679516e + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> asymmetric quadruple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesAsymmetricQuadrupleByte(): void + { + $this->assertEquals([0x81, 0xD5, 0xEE, 0x04], vlq_encode([0x357704])); + } + + /** + * uuid d5f3f3c3-e0f1-4e7f-aad0-18a44f223d1c + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> largest quadruple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesLargestQuadrupleByte(): void { - $this->assertEquals([0x00, 0x00], vlq_encode([0x00, 0x00])); - $this->assertEquals([0x40, 0x7f], vlq_encode([0x40, 0x7f])); - $this->assertEquals([0x81, 0x80, 0x00, 0xc8, 0xe8, 0x56], vlq_encode([0x4000, 0x123456])); - $this->assertEquals([ - 0xc0, 0x00, 0xc8, 0xe8, 0x56, - 0xff, 0xff, 0xff, 0x7f, 0x00, - 0xff, 0x7f, 0x81, 0x80, 0x00, - ], vlq_encode( - [0x2000, 0x123456, 0x0fffffff, 0x00, 0x3fff, 0x4000] - )); + $this->assertEquals([0xFF, 0xFF, 0xFF, 0x7F], vlq_encode([0x0FFFFFFF])); } - public function testItDecodesFromSingleBytes(): void + /** + * uuid 91a18b33-24e7-4bfb-bbca-eca78ff4fc47 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> smallest quintuple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesSmallestQuintupleByte(): void { - $this->assertEquals([0x00], vlq_decode([0x00])); - $this->assertEquals([0x40], vlq_decode([0x40])); - $this->assertEquals([0x7f], vlq_decode([0x7f])); + $this->assertEquals([0x81, 0x80, 0x80, 0x80, 0x00], vlq_encode([0x10000000])); } - public function testItDecodesDoubleBytes(): void + /** + * uuid 5f34ff12-2952-4669-95fe-2d11b693d331 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> arbitrary quintuple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesArbitraryQuintupleByte(): void { - $this->assertEquals([0x80], vlq_decode([0x81, 0x00])); - $this->assertEquals([0x2000], vlq_decode([0xc0, 0x00])); - $this->assertEquals([0x3fff], vlq_decode([0xff, 0x7f])); + $this->assertEquals([0x8F, 0xF8, 0x80, 0x80, 0x00], vlq_encode([0xFF000000])); } - public function testItDecodesTripleBytes(): void + /** + * uuid 9be46731-7cd5-415c-b960-48061cbc1154 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> asymmetric quintuple byte')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesAsymmetricQuintupleByte(): void { - $this->assertEquals([0x4000], vlq_decode([0x81, 0x80, 0x00])); - $this->assertEquals([0x100000], vlq_decode([0xc0, 0x80, 0x00])); - $this->assertEquals([0x1FFFFF], vlq_decode([0xff, 0xff, 0x7f])); + $this->assertEquals([0x88, 0xB3, 0x95, 0xC2, 0x05], vlq_encode([0x86656105])); } - public function testItDecodesQuadrupleBytes(): void + /** + * uuid 7489694b-88c3-4078-9864-6fe802411009 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> maximum 32-bit integer input')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesMaximum32BitIntegerInput(): void { - $this->assertEquals([0x200000], vlq_decode([0x81, 0x80, 0x80, 0x00])); - $this->assertEquals([0x8000000], vlq_decode([0xc0, 0x80, 0x80, 0x00])); - $this->assertEquals([0xFFFFFFF], vlq_decode([0xff, 0xff, 0xff, 0x7f])); + $this->assertEquals([0x8F, 0xFF, 0xFF, 0xFF, 0x7F], vlq_encode([0xFFFFFFFF])); } - public function testItDecodesMultipleValues(): void + /** + * uuid f9b91821-cada-4a73-9421-3c81d6ff3661 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> two single-byte values')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesTwoSingleByteValues(): void + { + $this->assertEquals([0x40, 0x7F], vlq_encode([0x40, 0x7F])); + } + + /** + * uuid 68694449-25d2-4974-ba75-fa7bb36db212 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> two multi-byte values')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesTwoMultiByteValues(): void { - $this->assertEquals([0x00, 0x00], vlq_decode([0x00, 0x00])); - $this->assertEquals([0x40, 0x7f], vlq_decode([0x40, 0x7f])); - $this->assertEquals([0x4000, 0x123456], vlq_decode([0x81, 0x80, 0x00, 0xc8, 0xe8, 0x56])); $this->assertEquals( - [0x2000, 0x123456, 0x0fffffff, 0x00, 0x3fff, 0x4000], - vlq_decode([ - 0xc0, 0x00, 0xc8, 0xe8, 0x56, - 0xff, 0xff, 0xff, 0x7f, 0x00, - 0xff, 0x7f, 0x81, 0x80, 0x00, - ]) + [0x81, 0x80, 0x00, 0xC8, 0xE8, 0x56], + vlq_encode([0x4000, 0x123456]) ); } - public function testIncompleteByteSequence(): void + /** + * uuid 51a06b5c-de1b-4487-9a50-9db1b8930d85 + */ + #[TestDox('Encode a series of integers, producing a series of bytes. -> many multi-byte values')] + public function testEncodeASeriesOfIntegersProducingASeriesOfBytesManyMultiByteValues(): void { - $this->expectException(InvalidArgumentException::class); + $this->assertEquals( + [ + 0xC0, 0x00, + 0xC8, 0xE8, 0x56, + 0xFF, 0xFF, 0xFF, 0x7F, + 0x00, + 0xFF, 0x7F, + 0x81, 0x80, 0x00 + ], + vlq_encode([0x2000, 0x123456, 0x0FFFFFFF, 0x00, 0x3FFF, 0x4000]) + ); + } + + /** + * uuid baa73993-4514-4915-bac0-f7f585e0e59a + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> one byte')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersOneByte(): void + { + $this->assertEquals([0x7F], vlq_decode([0x7F])); + } + + /** + * uuid 72e94369-29f9-46f2-8c95-6c5b7a595aee + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> two bytes')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersTwoBytes(): void + { + $this->assertEquals([8192], vlq_decode([0xC0, 0x00])); + } - vlq_decode([0xff]); + /** + * uuid df5a44c4-56f7-464e-a997-1db5f63ce691 + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> three bytes')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersThreeBytes(): void + { + $this->assertEquals([2097151], vlq_decode([0xFF, 0xFF, 0x7F])); } - public function testOverflow(): void + /** + * uuid 1bb58684-f2dc-450a-8406-1f3452aa1947 + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> four bytes')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersFourBytes(): void { - $this->expectException(OverflowException::class); + $this->assertEquals([2097152], vlq_decode([0x81, 0x80, 0x80, 0x00])); + } - vlq_decode([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]); + /** + * uuid cecd5233-49f1-4dd1-a41a-9840a40f09cd + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> maximum 32-bit integer')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersMaximum32BitInteger(): void + { + $this->assertEquals([4294967295], vlq_decode([0x8F, 0xFF, 0xFF, 0xFF, 0x7F])); } - public function testChainedDecodeEncodeGivesOriginalBytes(): void + /** + * uuid e7d74ba3-8b8e-4bcb-858d-d08302e15695 + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersIncompleteSequenceCausesError(): void { - $bytes = [ - 0xc0, 0x00, 0xc8, 0xe8, 0x56, - 0xff, 0xff, 0xff, 0x7f, 0x00, - 0xff, 0x7f, 0x81, 0x80, 0x00, - ]; + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('incomplete sequence'); - $this->assertTrue($bytes === vlq_encode(vlq_decode($bytes))); + vlq_decode([0xFF]); } - public function testChainedEncodeDecodeGivesOriginalIntegers(): void + /** + * uuid aa378291-9043-4724-bc53-aca1b4a3fcb6 + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> incomplete sequence causes error, even if value is zero')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersIncompleteSequenceZeroValue(): void { - $integers = [0x2000, 0x123456, 0x0fffffff, 0x00, 0x3fff, 0x4000]; + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('incomplete sequence'); + vlq_decode([0x80]); + } - $this->assertEquals($integers, vlq_decode(vlq_encode($integers))); + /** + * uuid a91e6f5a-c64a-48e3-8a75-ce1a81e0ebee + */ + #[TestDox('Decode a series of bytes, producing a series of integers. -> multiple values')] + public function testDecodeASeriesOfBytesProducingASeriesOfIntegersMultipleValues(): void + { + $this->assertEquals( + [8192, 1193046, 268435455, 0, 16383, 16384], + vlq_decode([ + 0xC0, 0x00, + 0xC8, 0xE8, 0x56, + 0xFF, 0xFF, 0xFF, 0x7F, + 0x00, + 0xFF, 0x7F, + 0x81, 0x80, 0x00 + ]) + ); } }