|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+import org.junit.Before;
|
|
|
2
|
+import org.junit.Ignore;
|
|
|
3
|
+import org.junit.Rule;
|
|
|
4
|
+import org.junit.Test;
|
|
|
5
|
+import org.junit.rules.ExpectedException;
|
|
|
6
|
+
|
|
|
7
|
+import static org.junit.Assert.assertEquals;
|
|
|
8
|
+
|
|
|
9
|
+import java.util.Arrays;
|
|
|
10
|
+import java.util.List;
|
|
|
11
|
+
|
|
|
12
|
+public class VariableLengthQuantityTest {
|
|
|
13
|
+ @Rule
|
|
|
14
|
+ public ExpectedException expectedException = ExpectedException.none();
|
|
|
15
|
+
|
|
|
16
|
+ private VariableLengthQuantity variableLengthQuantity;
|
|
|
17
|
+
|
|
|
18
|
+ @Before
|
|
|
19
|
+ public void setup() {
|
|
|
20
|
+ variableLengthQuantity = new VariableLengthQuantity();
|
|
|
21
|
+ }
|
|
|
22
|
+
|
|
|
23
|
+ @Test
|
|
|
24
|
+ public void testZero() {
|
|
|
25
|
+ List<String> expected = Arrays.asList("0x0");
|
|
|
26
|
+ List<Long> numbers = Arrays.asList(0x0L);
|
|
|
27
|
+
|
|
|
28
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ @Ignore("Remove to run test")
|
|
|
32
|
+ @Test
|
|
|
33
|
+ public void testArbitrarySingleByte() {
|
|
|
34
|
+ List<String> expected = Arrays.asList("0x40");
|
|
|
35
|
+ List<Long> numbers = Arrays.asList(0x40L);
|
|
|
36
|
+
|
|
|
37
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ @Ignore("Remove to run test")
|
|
|
41
|
+ @Test
|
|
|
42
|
+ public void testLargestSingleByte() {
|
|
|
43
|
+ List<String> expected = Arrays.asList("0x7f");
|
|
|
44
|
+ List<Long> numbers = Arrays.asList(0x7fL);
|
|
|
45
|
+
|
|
|
46
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ @Ignore("Remove to run test")
|
|
|
50
|
+ @Test
|
|
|
51
|
+ public void testSmallestDoubleByte() {
|
|
|
52
|
+ List<String> expected = Arrays.asList("0x81", "0x0");
|
|
|
53
|
+ List<Long> numbers = Arrays.asList(0x80L);
|
|
|
54
|
+
|
|
|
55
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ @Ignore("Remove to run test")
|
|
|
59
|
+ @Test
|
|
|
60
|
+ public void testArbitraryDoubleByte() {
|
|
|
61
|
+ List<String> expected = Arrays.asList("0xc0", "0x0");
|
|
|
62
|
+ List<Long> numbers = Arrays.asList(0x2000L);
|
|
|
63
|
+
|
|
|
64
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ @Ignore("Remove to run test")
|
|
|
68
|
+ @Test
|
|
|
69
|
+ public void testLargestDoubleByte() {
|
|
|
70
|
+ List<String> expected = Arrays.asList("0xff", "0x7f");
|
|
|
71
|
+ List<Long> numbers = Arrays.asList(0x3fffL);
|
|
|
72
|
+
|
|
|
73
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ @Ignore("Remove to run test")
|
|
|
77
|
+ @Test
|
|
|
78
|
+ public void testSmallestTripleByte() {
|
|
|
79
|
+ List<String> expected = Arrays.asList("0x81", "0x80", "0x0");
|
|
|
80
|
+ List<Long> numbers = Arrays.asList(0x4000L);
|
|
|
81
|
+
|
|
|
82
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ @Ignore("Remove to run test")
|
|
|
86
|
+ @Test
|
|
|
87
|
+ public void testArbitraryTripleByte() {
|
|
|
88
|
+ List<String> expected = Arrays.asList("0xc0", "0x80", "0x0");
|
|
|
89
|
+ List<Long> numbers = Arrays.asList(0x100000L);
|
|
|
90
|
+
|
|
|
91
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ @Ignore("Remove to run test")
|
|
|
95
|
+ @Test
|
|
|
96
|
+ public void testLargestTripleByte() {
|
|
|
97
|
+ List<String> expected = Arrays.asList("0xff", "0xff", "0x7f");
|
|
|
98
|
+ List<Long> numbers = Arrays.asList(0x1fffffL);
|
|
|
99
|
+
|
|
|
100
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ @Ignore("Remove to run test")
|
|
|
104
|
+ @Test
|
|
|
105
|
+ public void testSmallestQuadrupleByte() {
|
|
|
106
|
+ List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x0");
|
|
|
107
|
+ List<Long> numbers = Arrays.asList(0x200000L);
|
|
|
108
|
+
|
|
|
109
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ @Ignore("Remove to run test")
|
|
|
113
|
+ @Test
|
|
|
114
|
+ public void testArbitraryQuadrupleByte() {
|
|
|
115
|
+ List<String> expected = Arrays.asList("0xc0", "0x80", "0x80", "0x0");
|
|
|
116
|
+ List<Long> numbers = Arrays.asList(0x8000000L);
|
|
|
117
|
+
|
|
|
118
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ @Ignore("Remove to run test")
|
|
|
122
|
+ @Test
|
|
|
123
|
+ public void testLargestQuadrupleByte() {
|
|
|
124
|
+ List<String> expected = Arrays.asList("0xff", "0xff", "0xff", "0x7f");
|
|
|
125
|
+ List<Long> numbers = Arrays.asList(0xfffffffL);
|
|
|
126
|
+
|
|
|
127
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
128
|
+ }
|
|
|
129
|
+
|
|
|
130
|
+ @Ignore("Remove to run test")
|
|
|
131
|
+ @Test
|
|
|
132
|
+ public void testSmallestQuintupleByte() {
|
|
|
133
|
+ List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x80", "0x0");
|
|
|
134
|
+ List<Long> numbers = Arrays.asList(0x10000000L);
|
|
|
135
|
+
|
|
|
136
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ @Ignore("Remove to run test")
|
|
|
140
|
+ @Test
|
|
|
141
|
+ public void testArbitraryQuintupleByte() {
|
|
|
142
|
+ List<String> expected = Arrays.asList("0x8f", "0xf8", "0x80", "0x80", "0x0");
|
|
|
143
|
+ List<Long> numbers = Arrays.asList(0xff000000L);
|
|
|
144
|
+
|
|
|
145
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ @Ignore("Remove to run test")
|
|
|
149
|
+ @Test
|
|
|
150
|
+ public void testMaximum32BitIntegerInput() {
|
|
|
151
|
+ List<String> expected = Arrays.asList("0x8f", "0xff", "0xff", "0xff", "0x7f");
|
|
|
152
|
+ List<Long> numbers = Arrays.asList(0xffffffffL);
|
|
|
153
|
+
|
|
|
154
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
155
|
+ }
|
|
|
156
|
+
|
|
|
157
|
+ @Ignore("Remove to run test")
|
|
|
158
|
+ @Test
|
|
|
159
|
+ public void testTwoSingleByteValues() {
|
|
|
160
|
+ List<String> expected = Arrays.asList("0x40", "0x7f");
|
|
|
161
|
+ List<Long> numbers = Arrays.asList(0x40L, 0x7fL);
|
|
|
162
|
+
|
|
|
163
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
164
|
+ }
|
|
|
165
|
+
|
|
|
166
|
+ @Ignore("Remove to run test")
|
|
|
167
|
+ @Test
|
|
|
168
|
+ public void testTwoMultiByteValues() {
|
|
|
169
|
+ List<String> expected = Arrays.asList("0x81", "0x80", "0x0", "0xc8", "0xe8", "0x56");
|
|
|
170
|
+ List<Long> numbers = Arrays.asList(0x4000L, 0x123456L);
|
|
|
171
|
+
|
|
|
172
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ @Ignore("Remove to run test")
|
|
|
176
|
+ @Test
|
|
|
177
|
+ public void testManyMultiByteValues() {
|
|
|
178
|
+ List<String> expected = Arrays.asList("0xc0", "0x0", "0xc8", "0xe8",
|
|
|
179
|
+ "0x56", "0xff", "0xff", "0xff",
|
|
|
180
|
+ "0x7f", "0x0", "0xff", "0x7f",
|
|
|
181
|
+ "0x81", "0x80", "0x0");
|
|
|
182
|
+ List<Long> numbers = Arrays.asList(0x2000L, 0x123456L, 0xfffffffL,
|
|
|
183
|
+ 0x0L, 0x3fffL, 0x4000L);
|
|
|
184
|
+
|
|
|
185
|
+ assertEquals(expected, variableLengthQuantity.encode(numbers));
|
|
|
186
|
+ }
|
|
|
187
|
+
|
|
|
188
|
+ @Ignore("Remove to run test")
|
|
|
189
|
+ @Test
|
|
|
190
|
+ public void testDecodeOneByte() {
|
|
|
191
|
+ List<String> expected = Arrays.asList("0x7f");
|
|
|
192
|
+ List<Long> bytes = Arrays.asList(0x7fL);
|
|
|
193
|
+
|
|
|
194
|
+ assertEquals(expected, variableLengthQuantity.decode(bytes));
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ @Ignore("Remove to run test")
|
|
|
198
|
+ @Test
|
|
|
199
|
+ public void testDecodeTwoBytes() {
|
|
|
200
|
+ List<String> expected = Arrays.asList("0x2000");
|
|
|
201
|
+ List<Long> bytes = Arrays.asList(0xc0L, 0x0L);
|
|
|
202
|
+
|
|
|
203
|
+ assertEquals(expected, variableLengthQuantity.decode(bytes));
|
|
|
204
|
+ }
|
|
|
205
|
+
|
|
|
206
|
+ @Ignore("Remove to run test")
|
|
|
207
|
+ @Test
|
|
|
208
|
+ public void testDecodeThreeBytes() {
|
|
|
209
|
+ List<String> expected = Arrays.asList("0x1fffff");
|
|
|
210
|
+ List<Long> bytes = Arrays.asList(0xffL, 0xffL, 0x7fL);
|
|
|
211
|
+
|
|
|
212
|
+ assertEquals(expected, variableLengthQuantity.decode(bytes));
|
|
|
213
|
+ }
|
|
|
214
|
+
|
|
|
215
|
+ @Ignore("Remove to run test")
|
|
|
216
|
+ @Test
|
|
|
217
|
+ public void testDecodeFourBytes() {
|
|
|
218
|
+ List<String> expected = Arrays.asList("0x200000");
|
|
|
219
|
+ List<Long> bytes = Arrays.asList(0x81L, 0x80L, 0x80L, 0x0L);
|
|
|
220
|
+
|
|
|
221
|
+ assertEquals(expected, variableLengthQuantity.decode(bytes));
|
|
|
222
|
+ }
|
|
|
223
|
+
|
|
|
224
|
+ @Ignore("Remove to run test")
|
|
|
225
|
+ @Test
|
|
|
226
|
+ public void testDecodeMaximum32BitInteger() {
|
|
|
227
|
+ List<String> expected = Arrays.asList("0xffffffff");
|
|
|
228
|
+ List<Long> bytes = Arrays.asList(0x8fL, 0xffL, 0xffL, 0xffL, 0x7fL);
|
|
|
229
|
+
|
|
|
230
|
+ assertEquals(expected, variableLengthQuantity.decode(bytes));
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+ @Ignore("Remove to run test")
|
|
|
234
|
+ @Test
|
|
|
235
|
+ public void testCannotDecodeIncompleteSequence() {
|
|
|
236
|
+ List<Long> bytes = Arrays.asList(0xffL);
|
|
|
237
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
238
|
+ expectedException.expectMessage("Invalid variable-length quantity encoding");
|
|
|
239
|
+
|
|
|
240
|
+ variableLengthQuantity.decode(bytes);
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ @Ignore("Remove to run test")
|
|
|
244
|
+ @Test
|
|
|
245
|
+ public void testCannotDecodeIncompleteSequenceEvenIfValueIsZero() {
|
|
|
246
|
+ List<Long> bytes = Arrays.asList(0x80L);
|
|
|
247
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
248
|
+ expectedException.expectMessage("Invalid variable-length quantity encoding");
|
|
|
249
|
+
|
|
|
250
|
+ variableLengthQuantity.decode(bytes);
|
|
|
251
|
+ }
|
|
|
252
|
+
|
|
|
253
|
+ @Ignore("Remove to run test")
|
|
|
254
|
+ @Test
|
|
|
255
|
+ public void testDecodeMultipleBytes() {
|
|
|
256
|
+ List<String> expected = Arrays.asList("0x2000", "0x123456",
|
|
|
257
|
+ "0xfffffff", "0x0", "0x3fff",
|
|
|
258
|
+ "0x4000");
|
|
|
259
|
+ List<Long> bytes = Arrays.asList(0xc0L, 0x0L, 0xc8L, 0xe8L, 0x56L,
|
|
|
260
|
+ 0xffL, 0xffL, 0xffL, 0x7fL, 0x0L,
|
|
|
261
|
+ 0xffL, 0x7fL, 0x81L, 0x80L, 0x0L);
|
|
|
262
|
+
|
|
|
263
|
+ assertEquals(expected, variableLengthQuantity.decode(bytes));
|
|
|
264
|
+ }
|
|
|
265
|
+}
|