1
exercism fetch php variable-length-quantity

variable-length-quantity_test.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

require_once 'variable-length-quantity.php';

class VariableLengthQuantityTest extends PHPUnit\Framework\TestCase
{
    public function testItEncodesSingleBytes()
    {
        $this->assertEquals([0x00], vlq_encode([0x00]));
        $this->assertEquals([0x40], vlq_encode([0x40]));
        $this->assertEquals([0x7f], vlq_encode([0x7f]));
    }

    public function testItEncodesDoubleBytes()
    {
        $this->markTestSkipped();

        $this->assertEquals([0x81, 0x00], vlq_encode([0x80]));
        $this->assertEquals([0xc0, 0x00], vlq_encode([0x2000]));
        $this->assertEquals([0xff, 0x7f], vlq_encode([0x3fff]));
    }

    public function testItEncodesTripleBytes()
    {
        $this->markTestSkipped();

        $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()
    {
        $this->markTestSkipped();

        $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()
    {
        $this->markTestSkipped();

        $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]
        ));
    }

    public function testItDecodesFromSyngleBytes()
    {
        $this->markTestSkipped();

        $this->assertEquals([0x00], vlq_decode([0x00]));
        $this->assertEquals([0x40], vlq_decode([0x40]));
        $this->assertEquals([0x7f], vlq_decode([0x7f]));
    }

    public function testItDecodesDoubleBytes()
    {
        $this->markTestSkipped();

        $this->assertEquals([0x80], vlq_decode([0x81, 0x00]));
        $this->assertEquals([0x2000], vlq_decode([0xc0, 0x00]));
        $this->assertEquals([0x3fff], vlq_decode([0xff, 0x7f]));
    }

    public function testItDecodesTripleBytes()
    {
        $this->markTestSkipped();

        $this->assertEquals([0x4000], vlq_decode([0x81, 0x80, 0x00]));
        $this->assertEquals([0x100000], vlq_decode([0xc0, 0x80, 0x00]));
        $this->assertEquals([0x1FFFFF], vlq_decode([0xff, 0xff, 0x7f]));
    }

    public function testItDecodesQuadrupleBytes()
    {
        $this->markTestSkipped();

        $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]));
    }

    public function testItDecodesMultipleValues()
    {
        $this->markTestSkipped();

        $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,
            ])
        );
    }

    public function testIncompleteByteSequence()
    {
        $this->markTestSkipped();

        $this->expectException(InvalidArgumentException::class);

        vlq_decode([0xff]);
    }

    public function testOverflow()
    {
        $this->markTestSkipped();

        $this->expectException(OverflowException::class);

        vlq_decode([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]);
    }

    public function testChainedDecodeEncodeGivesOriginalBytes()
    {
        $this->markTestSkipped();

        $bytes = [
            0xc0, 0x00, 0xc8, 0xe8, 0x56,
            0xff, 0xff, 0xff, 0x7f, 0x00,
            0xff, 0x7f, 0x81, 0x80, 0x00,
        ];

        $this->assertTrue($bytes === vlq_encode(vlq_decode($bytes)));
    }

    public function testChainedEncodeDecodeGivesOriginalIntegers()
    {
        $this->markTestSkipped();

        $integers = [0x2000, 0x123456, 0x0fffffff, 0x00, 0x3fff, 0x4000];

        $this->assertEquals($integers, vlq_decode(vlq_encode($integers)));
    }
}