Skip to content

Commit 059e7db

Browse files
committed
add / fix unit tests for variable length quantity feature
1 parent 859518a commit 059e7db

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

exercises/practice/variable-length-quantity/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"muzimuzhi",
1313
"SleeplessByte",
1414
"sshine",
15-
"vpondala"
15+
"vpondala",
16+
"Xinri"
1617
],
1718
"files": {
1819
"solution": [

exercises/practice/variable-length-quantity/src/test/java/VariableLengthQuantityTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.junit.jupiter.api.Disabled;
2+
import org.junit.jupiter.api.DisplayName;
23
import org.junit.jupiter.api.Test;
34

45
import java.util.Arrays;
@@ -13,6 +14,7 @@ public class VariableLengthQuantityTest {
1314
new VariableLengthQuantity();
1415

1516
@Test
17+
@DisplayName("zero")
1618
public void testZero() {
1719
List<String> expected = Arrays.asList("0x0");
1820
List<Long> numbers = Arrays.asList(0x0L);
@@ -22,6 +24,7 @@ public void testZero() {
2224

2325
@Disabled("Remove to run test")
2426
@Test
27+
@DisplayName("arbitrary single byte")
2528
public void testArbitrarySingleByte() {
2629
List<String> expected = Arrays.asList("0x40");
2730
List<Long> numbers = Arrays.asList(0x40L);
@@ -31,6 +34,17 @@ public void testArbitrarySingleByte() {
3134

3235
@Disabled("Remove to run test")
3336
@Test
37+
@DisplayName("asymmetric single byte")
38+
public void testAsymmetricSingleByte() {
39+
List<String> expected = Arrays.asList("0x53");
40+
List<Long> numbers = Arrays.asList(0x53L);
41+
42+
assertThat(variableLengthQuantity.encode(numbers)).isEqualTo(expected);
43+
}
44+
45+
@Disabled("Remove to run test")
46+
@Test
47+
@DisplayName("largest single byte")
3448
public void testLargestSingleByte() {
3549
List<String> expected = Arrays.asList("0x7f");
3650
List<Long> numbers = Arrays.asList(0x7fL);
@@ -40,6 +54,7 @@ public void testLargestSingleByte() {
4054

4155
@Disabled("Remove to run test")
4256
@Test
57+
@DisplayName("smallest double byte")
4358
public void testSmallestDoubleByte() {
4459
List<String> expected = Arrays.asList("0x81", "0x0");
4560
List<Long> numbers = Arrays.asList(0x80L);
@@ -49,6 +64,7 @@ public void testSmallestDoubleByte() {
4964

5065
@Disabled("Remove to run test")
5166
@Test
67+
@DisplayName("arbitrary double byte")
5268
public void testArbitraryDoubleByte() {
5369
List<String> expected = Arrays.asList("0xc0", "0x0");
5470
List<Long> numbers = Arrays.asList(0x2000L);
@@ -58,6 +74,17 @@ public void testArbitraryDoubleByte() {
5874

5975
@Disabled("Remove to run test")
6076
@Test
77+
@DisplayName("asymmetric double byte")
78+
public void testAsymmetricDoubleByte() {
79+
List<String> expected = Arrays.asList("0x81", "0x2d");
80+
List<Long> numbers = Arrays.asList(0xadL);
81+
82+
assertThat(variableLengthQuantity.encode(numbers)).isEqualTo(expected);
83+
}
84+
85+
@Disabled("Remove to run test")
86+
@Test
87+
@DisplayName("largest double byte")
6188
public void testLargestDoubleByte() {
6289
List<String> expected = Arrays.asList("0xff", "0x7f");
6390
List<Long> numbers = Arrays.asList(0x3fffL);
@@ -67,6 +94,7 @@ public void testLargestDoubleByte() {
6794

6895
@Disabled("Remove to run test")
6996
@Test
97+
@DisplayName("smallest triple byte")
7098
public void testSmallestTripleByte() {
7199
List<String> expected = Arrays.asList("0x81", "0x80", "0x0");
72100
List<Long> numbers = Arrays.asList(0x4000L);
@@ -76,6 +104,7 @@ public void testSmallestTripleByte() {
76104

77105
@Disabled("Remove to run test")
78106
@Test
107+
@DisplayName("arbitrary triple byte")
79108
public void testArbitraryTripleByte() {
80109
List<String> expected = Arrays.asList("0xc0", "0x80", "0x0");
81110
List<Long> numbers = Arrays.asList(0x100000L);
@@ -85,6 +114,17 @@ public void testArbitraryTripleByte() {
85114

86115
@Disabled("Remove to run test")
87116
@Test
117+
@DisplayName("asymmetric triple byte")
118+
public void testAsymmetricTripleByte() {
119+
List<String> expected = Arrays.asList("0x87", "0xab", "0x1c");
120+
List<Long> numbers = Arrays.asList(0x1d59cL);
121+
122+
assertThat(variableLengthQuantity.encode(numbers)).isEqualTo(expected);
123+
}
124+
125+
@Disabled("Remove to run test")
126+
@Test
127+
@DisplayName("largest triple byte")
88128
public void testLargestTripleByte() {
89129
List<String> expected = Arrays.asList("0xff", "0xff", "0x7f");
90130
List<Long> numbers = Arrays.asList(0x1fffffL);
@@ -94,6 +134,7 @@ public void testLargestTripleByte() {
94134

95135
@Disabled("Remove to run test")
96136
@Test
137+
@DisplayName("smallest quadruple byte")
97138
public void testSmallestQuadrupleByte() {
98139
List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x0");
99140
List<Long> numbers = Arrays.asList(0x200000L);
@@ -103,6 +144,7 @@ public void testSmallestQuadrupleByte() {
103144

104145
@Disabled("Remove to run test")
105146
@Test
147+
@DisplayName("arbitrary quadruple byte")
106148
public void testArbitraryQuadrupleByte() {
107149
List<String> expected = Arrays.asList("0xc0", "0x80", "0x80", "0x0");
108150
List<Long> numbers = Arrays.asList(0x8000000L);
@@ -112,6 +154,17 @@ public void testArbitraryQuadrupleByte() {
112154

113155
@Disabled("Remove to run test")
114156
@Test
157+
@DisplayName("asymmetric quadruple byte")
158+
public void testAsymmetricQuadrupleByte() {
159+
List<String> expected = Arrays.asList("0x81", "0xd5", "0xee", "0x4");
160+
List<Long> numbers = Arrays.asList(0x357704L);
161+
162+
assertThat(variableLengthQuantity.encode(numbers)).isEqualTo(expected);
163+
}
164+
165+
@Disabled("Remove to run test")
166+
@Test
167+
@DisplayName("largest quadruple byte")
115168
public void testLargestQuadrupleByte() {
116169
List<String> expected = Arrays.asList("0xff", "0xff", "0xff", "0x7f");
117170
List<Long> numbers = Arrays.asList(0xfffffffL);
@@ -121,6 +174,7 @@ public void testLargestQuadrupleByte() {
121174

122175
@Disabled("Remove to run test")
123176
@Test
177+
@DisplayName("smallest quintuple byte")
124178
public void testSmallestQuintupleByte() {
125179
List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x80", "0x0");
126180
List<Long> numbers = Arrays.asList(0x10000000L);
@@ -130,6 +184,7 @@ public void testSmallestQuintupleByte() {
130184

131185
@Disabled("Remove to run test")
132186
@Test
187+
@DisplayName("arbitrary quintuple byte")
133188
public void testArbitraryQuintupleByte() {
134189
List<String> expected = Arrays.asList("0x8f", "0xf8", "0x80", "0x80", "0x0");
135190
List<Long> numbers = Arrays.asList(0xff000000L);
@@ -139,6 +194,17 @@ public void testArbitraryQuintupleByte() {
139194

140195
@Disabled("Remove to run test")
141196
@Test
197+
@DisplayName("asymmetric quintuple byte")
198+
public void testAsymmetricQuintupleByte() {
199+
List<String> expected = Arrays.asList("0x88", "0xb3", "0x95", "0xc2", "0x5");
200+
List<Long> numbers = Arrays.asList(0x86656105L);
201+
202+
assertThat(variableLengthQuantity.encode(numbers)).isEqualTo(expected);
203+
}
204+
205+
@Disabled("Remove to run test")
206+
@Test
207+
@DisplayName("maximum 32-bit integer input")
142208
public void testMaximum32BitIntegerInput() {
143209
List<String> expected = Arrays.asList("0x8f", "0xff", "0xff", "0xff", "0x7f");
144210
List<Long> numbers = Arrays.asList(0xffffffffL);
@@ -148,6 +214,7 @@ public void testMaximum32BitIntegerInput() {
148214

149215
@Disabled("Remove to run test")
150216
@Test
217+
@DisplayName("two single-byte values")
151218
public void testTwoSingleByteValues() {
152219
List<String> expected = Arrays.asList("0x40", "0x7f");
153220
List<Long> numbers = Arrays.asList(0x40L, 0x7fL);
@@ -157,6 +224,7 @@ public void testTwoSingleByteValues() {
157224

158225
@Disabled("Remove to run test")
159226
@Test
227+
@DisplayName("two multi-byte values")
160228
public void testTwoMultiByteValues() {
161229
List<String> expected = Arrays.asList("0x81", "0x80", "0x0", "0xc8", "0xe8", "0x56");
162230
List<Long> numbers = Arrays.asList(0x4000L, 0x123456L);
@@ -166,6 +234,7 @@ public void testTwoMultiByteValues() {
166234

167235
@Disabled("Remove to run test")
168236
@Test
237+
@DisplayName("many multi-byte values")
169238
public void testManyMultiByteValues() {
170239
List<String> expected = Arrays.asList("0xc0", "0x0", "0xc8", "0xe8",
171240
"0x56", "0xff", "0xff", "0xff",
@@ -179,6 +248,7 @@ public void testManyMultiByteValues() {
179248

180249
@Disabled("Remove to run test")
181250
@Test
251+
@DisplayName("one byte")
182252
public void testDecodeOneByte() {
183253
List<String> expected = Arrays.asList("0x7f");
184254
List<Long> bytes = Arrays.asList(0x7fL);
@@ -188,6 +258,7 @@ public void testDecodeOneByte() {
188258

189259
@Disabled("Remove to run test")
190260
@Test
261+
@DisplayName("two bytes")
191262
public void testDecodeTwoBytes() {
192263
List<String> expected = Arrays.asList("0x2000");
193264
List<Long> bytes = Arrays.asList(0xc0L, 0x0L);
@@ -197,6 +268,7 @@ public void testDecodeTwoBytes() {
197268

198269
@Disabled("Remove to run test")
199270
@Test
271+
@DisplayName("three bytes")
200272
public void testDecodeThreeBytes() {
201273
List<String> expected = Arrays.asList("0x1fffff");
202274
List<Long> bytes = Arrays.asList(0xffL, 0xffL, 0x7fL);
@@ -206,6 +278,7 @@ public void testDecodeThreeBytes() {
206278

207279
@Disabled("Remove to run test")
208280
@Test
281+
@DisplayName("four bytes")
209282
public void testDecodeFourBytes() {
210283
List<String> expected = Arrays.asList("0x200000");
211284
List<Long> bytes = Arrays.asList(0x81L, 0x80L, 0x80L, 0x0L);
@@ -215,6 +288,7 @@ public void testDecodeFourBytes() {
215288

216289
@Disabled("Remove to run test")
217290
@Test
291+
@DisplayName("maximum 32-bit integer")
218292
public void testDecodeMaximum32BitInteger() {
219293
List<String> expected = Arrays.asList("0xffffffff");
220294
List<Long> bytes = Arrays.asList(0x8fL, 0xffL, 0xffL, 0xffL, 0x7fL);
@@ -224,6 +298,7 @@ public void testDecodeMaximum32BitInteger() {
224298

225299
@Disabled("Remove to run test")
226300
@Test
301+
@DisplayName("incomplete sequence causes error")
227302
public void testCannotDecodeIncompleteSequence() {
228303
List<Long> bytes = Arrays.asList(0xffL);
229304

@@ -234,6 +309,7 @@ public void testCannotDecodeIncompleteSequence() {
234309

235310
@Disabled("Remove to run test")
236311
@Test
312+
@DisplayName("incomplete sequence causes error, even if value is zero")
237313
public void testCannotDecodeIncompleteSequenceEvenIfValueIsZero() {
238314
List<Long> bytes = Arrays.asList(0x80L);
239315

@@ -244,6 +320,7 @@ public void testCannotDecodeIncompleteSequenceEvenIfValueIsZero() {
244320

245321
@Disabled("Remove to run test")
246322
@Test
323+
@DisplayName("multiple values")
247324
public void testDecodeMultipleBytes() {
248325
List<String> expected = Arrays.asList("0x2000", "0x123456",
249326
"0xfffffff", "0x0", "0x3fff",

0 commit comments

Comments
 (0)