1
exercism fetch scala variable-length-quantity

src/test/scala/VariableLengthQuantityTest.scala

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
150
151
152
153
import org.scalatest.{Matchers, FunSuite}

/** @version 1.0.0 */
class VariableLengthQuantityTest extends FunSuite with Matchers {

  test("zero") {
    VariableLengthQuantity.encode(List(0x0)) should be(List(0x0))
  }

  test("arbitrary single byte") {
    pending
    VariableLengthQuantity.encode(List(0x40)) should be(List(0x40))
  }

  test("largest single byte") {
    pending
    VariableLengthQuantity.encode(List(0x7F)) should be(List(0x7F))
  }

  test("smallest double byte") {
    pending
    VariableLengthQuantity.encode(List(0x80)) should be(List(0x81, 0x0))
  }

  test("arbitrary double byte") {
    pending
    VariableLengthQuantity.encode(List(0x2000)) should be(List(0xC0, 0x0))
  }

  test("largest double byte") {
    pending
    VariableLengthQuantity.encode(List(0x3FFF)) should be(List(0xFF, 0x7F))
  }

  test("smallest triple byte") {
    pending
    VariableLengthQuantity.encode(List(0x4000)) should be(List(0x81, 0x80, 0x0))
  }

  test("arbitrary triple byte") {
    pending
    VariableLengthQuantity.encode(List(0x100000)) should be(
      List(0xC0, 0x80, 0x0))
  }

  test("largest triple byte") {
    pending
    VariableLengthQuantity.encode(List(0x1FFFFF)) should be(
      List(0xFF, 0xFF, 0x7F))
  }

  test("smallest quadruple byte") {
    pending
    VariableLengthQuantity.encode(List(0x200000)) should be(
      List(0x81, 0x80, 0x80, 0x0))
  }

  test("arbitrary quadruple byte") {
    pending
    VariableLengthQuantity.encode(List(0x8000000)) should be(
      List(0xC0, 0x80, 0x80, 0x0))
  }

  test("largest quadruple byte") {
    pending
    VariableLengthQuantity.encode(List(0xFFFFFFF)) should be(
      List(0xFF, 0xFF, 0xFF, 0x7F))
  }

  test("smallest quintuple byte") {
    pending
    VariableLengthQuantity.encode(List(0x10000000)) should be(
      List(0x81, 0x80, 0x80, 0x80, 0x0))
  }

  test("arbitrary quintuple byte") {
    pending
    VariableLengthQuantity.encode(List(0xFF000000)) should be(
      List(0x8F, 0xF8, 0x80, 0x80, 0x0))
  }

  test("maximum 32-bit integer input") {
    pending
    VariableLengthQuantity.encode(List(0xFFFFFFFF)) should be(
      List(0x8F, 0xFF, 0xFF, 0xFF, 0x7F))
  }

  test("two single-byte values") {
    pending
    VariableLengthQuantity.encode(List(0x40, 0x7F)) should be(List(0x40, 0x7F))
  }

  test("two multi-byte values") {
    pending
    VariableLengthQuantity.encode(List(0x4000, 0x123456)) should be(
      List(0x81, 0x80, 0x0, 0xC8, 0xE8, 0x56))
  }

  test("many multi-byte values") {
    pending
    VariableLengthQuantity.encode(
      List(0x2000, 0x123456, 0xFFFFFFF, 0x0, 0x3FFF, 0x4000)) should be(
      List(0xC0, 0x0, 0xC8, 0xE8, 0x56, 0xFF, 0xFF, 0xFF, 0x7F, 0x0, 0xFF, 0x7F,
        0x81, 0x80, 0x0))
  }

  test("one byte") {
    pending
    VariableLengthQuantity.decode(List(0x7F)) should be(Right(List(0x7F)))
  }

  test("two bytes") {
    pending
    VariableLengthQuantity.decode(List(0xC0, 0x0)) should be(
      Right(List(0x2000)))
  }

  test("three bytes") {
    pending
    VariableLengthQuantity.decode(List(0xFF, 0xFF, 0x7F)) should be(
      Right(List(0x1FFFFF)))
  }

  test("four bytes") {
    pending
    VariableLengthQuantity.decode(List(0x81, 0x80, 0x80, 0x0)) should be(
      Right(List(0x200000)))
  }

  test("maximum 32-bit integer") {
    pending
    VariableLengthQuantity.decode(List(0x8F, 0xFF, 0xFF, 0xFF, 0x7F)) should be(
      Right(List(0xFFFFFFFF)))
  }

  test("incomplete sequence causes error") {
    pending
    VariableLengthQuantity.decode(List(0xFF)).isLeft should be(true)
  }

  test("incomplete sequence causes error, even if value is zero") {
    pending
    VariableLengthQuantity.decode(List(0x80)).isLeft should be(true)
  }

  test("multiple values") {
    pending
    VariableLengthQuantity.decode(
      List(0xC0, 0x0, 0xC8, 0xE8, 0x56, 0xFF, 0xFF, 0xFF, 0x7F, 0x0, 0xFF, 0x7F,
        0x81, 0x80, 0x0)) should be(
      Right(List(0x2000, 0x123456, 0xFFFFFFF, 0x0, 0x3FFF, 0x4000)))
  }
}