Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JUnit and unit tests for VariableLengthInt #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@

<groupId>com.github.leffelmania</groupId>
<artifactId>android-midi-lib</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yea, I had to do this initially in order to build the artifact, but would be happy to revert this if it's inappropriate for this changeset :)

</properties>
</project>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
30 changes: 28 additions & 2 deletions src/main/java/com/leff/midi/util/VariableLengthInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@
import java.io.IOException;
import java.io.InputStream;

/**
* From the official "Standard MIDI File" specification:
* <p>
* Some numbers in MIDI Files are represented in
* a form called a variable-length quantity. These numbers are represented 7 bits per byte, most significant
* bits first. All bytes except the last have bit 7 (the most significant bit) set, and the last byte has bit
* 7 clear. If the number is between 0 and 127, it is thus represented exactly as one byte.
* <p>
* Here are some examples of numbers represented as variable-length quantities:
* <p>
* Number (hex) = Representation (hex)<br>
* <code>00 00 00 00 = 00 </code><br>
* <code>00 00 00 40 = 40 </code><br>
* <code>00 00 00 7F = 7F </code><br>
* <code>00 00 00 80 = 81 00 </code><br>
* <code>00 00 20 00 = C0 00 </code><br>
* <code>00 00 3F FF = FF 7F </code><br>
* <code>00 00 40 00 = 81 80 00 </code><br>
* <code>00 10 00 00 = C0 80 00 </code><br>
* <code>00 1F FF FF = FF FF 7F </code><br>
* <code>00 20 00 00 = 81 80 80 00 </code><br>
* <code>08 00 00 00 = C0 80 80 00 </code><br>
* <code>0F FF FF FF = FF FF FF 7F </code><br>
* <p>
* The largest number which is allowed is 0FFFFFFF
*/
public class VariableLengthInt
{
private int mValue;
Expand Down Expand Up @@ -75,7 +101,7 @@ private void parseBytes(InputStream in) throws IOException
ints[mSizeInBytes - 1] = (b & 0x7F);
break;
}
ints[mSizeInBytes - 1] = (b & 0x7F);
ints[mSizeInBytes - 1] = (b & 0xFF);

b = in.read();
}
Expand All @@ -90,7 +116,7 @@ private void parseBytes(InputStream in) throws IOException
{
mBytes[i] = (byte) ints[i];

mValue += ints[i] << shift;
mValue += (ints[i] & 0x7F) << shift;
shift -= 7;
}
}
Expand Down
177 changes: 177 additions & 0 deletions src/test/java/com/leff/midi/util/TestVariableLengthInt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.leff.midi.util;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import static org.junit.Assert.*;

public class TestVariableLengthInt
{
@Test
public void testMinimumOneByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x00);
assertEquals(0x00, vli.getValue());
assertEquals(1, vli.getByteCount());
assertArrayEquals(new byte[] { 0x00 }, vli.getBytes());
}

@Test
public void testMinimumOneByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] { 0x00 });
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x00, vli.getValue());
assertEquals(1, vli.getByteCount());
assertArrayEquals(new byte[] { 0x00 }, vli.getBytes());
}

@Test
public void testMaximumOneByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x7F);
assertEquals(0x7F, vli.getValue());
assertEquals(1, vli.getByteCount());
assertArrayEquals(new byte[] { 0x7F }, vli.getBytes());
}

@Test
public void testMaximumOneByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] {0x7F});
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x7F, vli.getValue());
assertEquals(1, vli.getByteCount());
assertArrayEquals(new byte[] { 0x7F }, vli.getBytes());
}

@Test
public void testMinimumTwoByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x80);
assertEquals(0x80, vli.getValue());
assertEquals(2, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0x81, 0x00 }, vli.getBytes());
}

@Test
public void testMinimumTwoByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] { (byte) 0x81, 0x00 });
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x80, vli.getValue());
assertEquals(2, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0x81, 0x00 }, vli.getBytes());
}

@Test
public void testMaximumTwoByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x3F_FF);
assertEquals(0x3F_FF, vli.getValue());
assertEquals(2, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xFF, 0x7F }, vli.getBytes());
}

@Test
public void testMaximumTwoByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] { (byte) 0xFF, 0x7F });
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x3F_FF, vli.getValue());
assertEquals(2, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xFF, 0x7F }, vli.getBytes());
}

@Test
public void testMinimumThreeByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x40_00);
assertEquals(0x40_00, vli.getValue());
assertEquals(3, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0x81, (byte) 0x80, 0x00 }, vli.getBytes());
}

@Test
public void testMinimumThreeByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] { (byte) 0x81, (byte) 0x80, 0x00 });
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x40_00, vli.getValue());
assertEquals(3, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0x81, (byte) 0x80, 0x00 }, vli.getBytes());
}

@Test
public void testMaximumThreeByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x1F_FF_FF);
assertEquals(0x1F_FF_FF, vli.getValue());
assertEquals(3, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xFF, (byte) 0xFF, 0x7F }, vli.getBytes());
}

@Test
public void testMaximumThreeByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] { (byte) 0xFF, (byte) 0xFF, 0x7F });
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x1F_FF_FF, vli.getValue());
assertEquals(3, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xFF, (byte) 0xFF, 0x7F }, vli.getBytes());
}

@Test
public void testMinimumFourByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x20_00_00);
assertEquals(0x20_00_00, vli.getValue());
assertEquals(4, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0x81, (byte) 0x80, (byte) 0x80, 0x00 }, vli.getBytes());
}

@Test
public void testMinimumFourByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] { (byte) 0x81, (byte) 0x80, (byte) 0x80, 0x00 });
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x20_00_00, vli.getValue());
assertEquals(4, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0x81, (byte) 0x80, (byte) 0x80, 0x00 }, vli.getBytes());
}

@Test
public void testMaximumFourByteValueFromInt() {
VariableLengthInt vli = new VariableLengthInt(0x0F_FF_FF_FF);
assertEquals(0x0F_FF_FF_FF, vli.getValue());
assertEquals(4, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x7F }, vli.getBytes());
}

@Test
public void testMaximumFourByteValueFromStream() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x7F });
VariableLengthInt vli = new VariableLengthInt(stream);
assertEquals(0x0F_FF_FF_FF, vli.getValue());
assertEquals(4, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x7F }, vli.getBytes());
}

@Test
public void testSetValue() {
VariableLengthInt vli = new VariableLengthInt(0);

vli.setValue(0x40);
assertEquals(0x40, vli.getValue());
assertEquals(1, vli.getByteCount());
assertArrayEquals(new byte[] { 0x40 }, vli.getBytes());

vli.setValue(0x08_00_00_00);
assertEquals(0x08_00_00_00, vli.getValue());
assertEquals(4, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xC0, (byte) 0x80, (byte) 0x80, 0x00 }, vli.getBytes());

vli.setValue(0x20_00);
assertEquals(0x20_00, vli.getValue());
assertEquals(2, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xC0, 0x00 }, vli.getBytes());

vli.setValue(0x10_00_00);
assertEquals(0x10_00_00, vli.getValue());
assertEquals(3, vli.getByteCount());
assertArrayEquals(new byte[] { (byte) 0xC0, (byte) 0x80, 0x00 }, vli.getBytes());
}

@Test
public void testToString() {
assertEquals("00 (0)", new VariableLengthInt(0).toString());
assertEquals("8F FF FF 7F (33554431)", new VariableLengthInt(0x01_FF_FF_FF).toString());
}
}