1
exercism fetch csharp variable-length-quantity

VariableLengthQuantityTest.cs

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
using System;
using Xunit;

public class VariableLengthQuantityTest
{
    [Fact]
    public void To_single_byte()
    {
        Assert.Equal(new[] { 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x00u }));
        Assert.Equal(new[] { 0x40u }, VariableLengthQuantity.ToBytes(new[] { 0x40u }));
        Assert.Equal(new[] { 0x7fu }, VariableLengthQuantity.ToBytes(new[] { 0x7fu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void To_double_byte()
    {
        Assert.Equal(new[] { 0x81u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x80u }));
        Assert.Equal(new[] { 0xc0u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x2000u }));
        Assert.Equal(new[] { 0xffu, 0x7fu }, VariableLengthQuantity.ToBytes(new[] { 0x3fffu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void To_triple_byte()
    {
        Assert.Equal(new[] { 0x81u, 0x80u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x4000u }));
        Assert.Equal(new[] { 0xc0u, 0x80u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x100000u }));
        Assert.Equal(new[] { 0xffu, 0xffu, 0x7fu }, VariableLengthQuantity.ToBytes(new[] { 0x1fffffu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void To_quadruple_byte()
    {
        Assert.Equal(new[] { 0x81u, 0x80u, 0x80u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x200000u }));
        Assert.Equal(new[] { 0xc0u, 0x80u, 0x80u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x08000000u }));
        Assert.Equal(new[] { 0xffu, 0xffu, 0xffu, 0x7fu }, VariableLengthQuantity.ToBytes(new[] { 0x0fffffffu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void To_quintuple_byte()
    {
        Assert.Equal(new[] { 0x81u, 0x80u, 0x80u, 0x80u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x10000000u }));
        Assert.Equal(new[] { 0x8fu, 0xf8u, 0x80u, 0x80u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0xff000000u }));
        Assert.Equal(new[] { 0x8fu, 0xffu, 0xffu, 0xffu, 0x7fu }, VariableLengthQuantity.ToBytes(new[] { 0xffffffffu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void From_bytes()
    {
        Assert.Equal(new[] { 0x7fu }, VariableLengthQuantity.FromBytes(new[] { 0x7fu }));
        Assert.Equal(new[] { 0x2000u }, VariableLengthQuantity.FromBytes(new[] { 0xc0u, 0x00u }));
        Assert.Equal(new[] { 0x1fffffu }, VariableLengthQuantity.FromBytes(new[] { 0xffu, 0xffu, 0x7fu }));
        Assert.Equal(new[] { 0x200000u }, VariableLengthQuantity.FromBytes(new[] { 0x81u, 0x80u, 0x80u, 0x00u }));
        Assert.Equal(new[] { 0xffffffffu }, VariableLengthQuantity.FromBytes(new[] { 0x8fu, 0xffu, 0xffu, 0xffu, 0x7fu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void To_bytes_multiple_values()
    {
        Assert.Equal(new[] { 0x40u, 0x7fu }, VariableLengthQuantity.ToBytes(new[] { 0x40u, 0x7fu }));
        Assert.Equal(new[] { 0x81u, 0x80u, 0x00u, 0xc8u, 0xe8u, 0x56u }, VariableLengthQuantity.ToBytes(new[] { 0x4000u, 0x123456u }));
        Assert.Equal(new[] { 0xc0u, 0x00u, 0xc8u, 0xe8u, 0x56u, 0xffu, 0xffu, 0xffu, 0x7fu, 0x00u, 0xffu, 0x7fu, 0x81u, 0x80u, 0x00u }, VariableLengthQuantity.ToBytes(new[] { 0x2000u, 0x123456u, 0x0fffffffu, 0x00u, 0x3fffu, 0x4000u }));
    }

    [Fact(Skip = "Remove to run test")]
    public void From_bytes_multiple_values()
    {
        Assert.Equal(new[] { 0x2000u, 0x123456u, 0x0fffffffu, 0x00u, 0x3fffu, 0x4000u }, VariableLengthQuantity.FromBytes(new[] { 0xc0u, 0x00u, 0xc8u, 0xe8u, 0x56u, 0xffu, 0xffu, 0xffu, 0x7fu, 0x00u, 0xffu, 0x7fu, 0x81u, 0x80u, 0x00u }));
    }

    [Fact(Skip = "Remove to run test")]
    public void Incomplete_byte_sequence()
    {
        Assert.Throws<InvalidOperationException>(() => VariableLengthQuantity.FromBytes(new[] { 0xffu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void Overflow()
    {
        Assert.Throws<InvalidOperationException>(() => VariableLengthQuantity.FromBytes(new[] { 0xffu, 0xffu, 0xffu, 0xffu, 0x7fu }));
    }

    [Fact(Skip = "Remove to run test")]
    public void Chained_execution_is_identity()
    {
        var test = new[] { 0xf2u, 0xf6u, 0x96u, 0x9cu, 0x3bu, 0x39u, 0x2eu, 0x30u, 0xb3u, 0x24u };
        Assert.Equal(test, VariableLengthQuantity.FromBytes(VariableLengthQuantity.ToBytes(test)));
    }
}