ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# ChristopherJamesN's solution

## to Variable Length Quantity in the Java Track

Published at Sep 13 2020 · 0 comments
Instructions
Test suite
Solution

Implement variable length quantity encoding and decoding.

The goal of this exercise is to implement VLQ encoding/decoding.

In short, the goal of this encoding is to encode integer values in a way that would save bytes. Only the first 7 bits of each byte is significant (right-justified; sort of like an ASCII byte). So, if you have a 32-bit value, you have to unpack it into a series of 7-bit bytes. Of course, you will have a variable number of bytes depending upon your integer. To indicate which is the last byte of the series, you leave bit #7 clear. In all of the preceding bytes, you set bit #7.

So, if an integer is between `0-127`, it can be represented as one byte. Although VLQ can deal with numbers of arbitrary sizes, for this exercise we will restrict ourselves to only numbers that fit in a 32-bit unsigned integer. Here are examples of integers as 32-bit values, and the variable length quantities that they translate to:

`````` NUMBER        VARIABLE QUANTITY
00000000              00
00000040              40
0000007F              7F
00000080             81 00
00002000             C0 00
00003FFF             FF 7F
00004000           81 80 00
00100000           C0 80 00
001FFFFF           FF FF 7F
00200000          81 80 80 00
08000000          C0 80 80 00
0FFFFFFF          FF FF FF 7F
``````

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

## Source

A poor Splice developer having to implement MIDI encoding/decoding. https://splice.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### VariableLengthQuantityTest.java

``````import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class VariableLengthQuantityTest {

private VariableLengthQuantity variableLengthQuantity =
new VariableLengthQuantity();

@Test
public void testZero() {
List<String> expected = Arrays.asList("0x0");
List<Long> numbers = Arrays.asList(0x0L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testArbitrarySingleByte() {
List<String> expected = Arrays.asList("0x40");
List<Long> numbers = Arrays.asList(0x40L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testLargestSingleByte() {
List<String> expected = Arrays.asList("0x7f");
List<Long> numbers = Arrays.asList(0x7fL);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testSmallestDoubleByte() {
List<String> expected = Arrays.asList("0x81", "0x0");
List<Long> numbers = Arrays.asList(0x80L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testArbitraryDoubleByte() {
List<String> expected = Arrays.asList("0xc0", "0x0");
List<Long> numbers = Arrays.asList(0x2000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testLargestDoubleByte() {
List<String> expected = Arrays.asList("0xff", "0x7f");
List<Long> numbers = Arrays.asList(0x3fffL);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testSmallestTripleByte() {
List<String> expected = Arrays.asList("0x81", "0x80", "0x0");
List<Long> numbers = Arrays.asList(0x4000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testArbitraryTripleByte() {
List<String> expected = Arrays.asList("0xc0", "0x80", "0x0");
List<Long> numbers = Arrays.asList(0x100000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testLargestTripleByte() {
List<String> expected = Arrays.asList("0xff", "0xff", "0x7f");
List<Long> numbers = Arrays.asList(0x1fffffL);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x0");
List<Long> numbers = Arrays.asList(0x200000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
List<String> expected = Arrays.asList("0xc0", "0x80", "0x80", "0x0");
List<Long> numbers = Arrays.asList(0x8000000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
List<String> expected = Arrays.asList("0xff", "0xff", "0xff", "0x7f");
List<Long> numbers = Arrays.asList(0xfffffffL);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testSmallestQuintupleByte() {
List<String> expected = Arrays.asList("0x81", "0x80", "0x80", "0x80", "0x0");
List<Long> numbers = Arrays.asList(0x10000000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testArbitraryQuintupleByte() {
List<String> expected = Arrays.asList("0x8f", "0xf8", "0x80", "0x80", "0x0");
List<Long> numbers = Arrays.asList(0xff000000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testMaximum32BitIntegerInput() {
List<String> expected = Arrays.asList("0x8f", "0xff", "0xff", "0xff", "0x7f");
List<Long> numbers = Arrays.asList(0xffffffffL);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testTwoSingleByteValues() {
List<String> expected = Arrays.asList("0x40", "0x7f");
List<Long> numbers = Arrays.asList(0x40L, 0x7fL);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testTwoMultiByteValues() {
List<String> expected = Arrays.asList("0x81", "0x80", "0x0", "0xc8", "0xe8", "0x56");
List<Long> numbers = Arrays.asList(0x4000L, 0x123456L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testManyMultiByteValues() {
List<String> expected = Arrays.asList("0xc0", "0x0", "0xc8", "0xe8",
"0x56", "0xff", "0xff", "0xff",
"0x7f", "0x0", "0xff", "0x7f",
"0x81", "0x80", "0x0");
List<Long> numbers = Arrays.asList(0x2000L, 0x123456L, 0xfffffffL,
0x0L, 0x3fffL, 0x4000L);

assertEquals(expected, variableLengthQuantity.encode(numbers));
}

@Ignore("Remove to run test")
@Test
public void testDecodeOneByte() {
List<String> expected = Arrays.asList("0x7f");
List<Long> bytes = Arrays.asList(0x7fL);

assertEquals(expected, variableLengthQuantity.decode(bytes));
}

@Ignore("Remove to run test")
@Test
public void testDecodeTwoBytes() {
List<String> expected = Arrays.asList("0x2000");
List<Long> bytes = Arrays.asList(0xc0L, 0x0L);

assertEquals(expected, variableLengthQuantity.decode(bytes));
}

@Ignore("Remove to run test")
@Test
public void testDecodeThreeBytes() {
List<String> expected = Arrays.asList("0x1fffff");
List<Long> bytes = Arrays.asList(0xffL, 0xffL, 0x7fL);

assertEquals(expected, variableLengthQuantity.decode(bytes));
}

@Ignore("Remove to run test")
@Test
public void testDecodeFourBytes() {
List<String> expected = Arrays.asList("0x200000");
List<Long> bytes = Arrays.asList(0x81L, 0x80L, 0x80L, 0x0L);

assertEquals(expected, variableLengthQuantity.decode(bytes));
}

@Ignore("Remove to run test")
@Test
public void testDecodeMaximum32BitInteger() {
List<String> expected = Arrays.asList("0xffffffff");
List<Long> bytes = Arrays.asList(0x8fL, 0xffL, 0xffL, 0xffL, 0x7fL);

assertEquals(expected, variableLengthQuantity.decode(bytes));
}

@Ignore("Remove to run test")
@Test
public void testCannotDecodeIncompleteSequence() {
List<Long> bytes = Arrays.asList(0xffL);

IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> variableLengthQuantity.decode(bytes));

assertThat(expected)
.hasMessage("Invalid variable-length quantity encoding");
}

@Ignore("Remove to run test")
@Test
public void testCannotDecodeIncompleteSequenceEvenIfValueIsZero() {
List<Long> bytes = Arrays.asList(0x80L);

IllegalArgumentException expected =
assertThrows(
IllegalArgumentException.class,
() -> variableLengthQuantity.decode(bytes));

assertThat(expected)
.hasMessage("Invalid variable-length quantity encoding");
}

@Ignore("Remove to run test")
@Test
public void testDecodeMultipleBytes() {
List<String> expected = Arrays.asList("0x2000", "0x123456",
"0xfffffff", "0x0", "0x3fff",
"0x4000");
List<Long> bytes = Arrays.asList(0xc0L, 0x0L, 0xc8L, 0xe8L, 0x56L,
0xffL, 0xffL, 0xffL, 0x7fL, 0x0L,
0xffL, 0x7fL, 0x81L, 0x80L, 0x0L);

assertEquals(expected, variableLengthQuantity.decode(bytes));
}
}``````
``````import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class VariableLengthQuantity {
List<String> encode(List<Long> numbers) {
List<String> encoded = new ArrayList<String>();

for (Long number : numbers) {
List<String> binaryStringList = new ArrayList<String>();

number >>= 7;
while (number != 0) {
binaryStringList.add(String.format("0x%x", number & 0x7f | 0x80));
number >>= 7;
}

Collections.reverse(binaryStringList);
}

return encoded;
}

List<String> decode(List<Long> bytes) {
if ((bytes.get(bytes.size() - 1) & 0x80) != 0) {
throw new IllegalArgumentException("Invalid variable-length quantity encoding");
}

List<String> decoded = new ArrayList<String>();

long number = 0L;
for (Long bt : bytes) {
number <<= 7;
number += bt & 0x7f;
if ((bt & 0x80) == 0) {
number = 0L;
}
}

return decoded;
}
}``````