Skip to content

Commit

Permalink
test: Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wyskoj committed Jul 8, 2024
1 parent 7dfcb3b commit b77d4aa
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public object Polyphony {
}

private fun buildPolyphonyIndex(noteEvents: List<NoteEvent>): Map<Pair<Int, Int>, Int> {
if (noteEvents.isEmpty()) return emptyMap()

val events = noteEvents.sortedBy { it.tick }
val polyphonyIndex = mutableMapOf<Pair<Int, Int>, Int>()

Expand Down
25 changes: 0 additions & 25 deletions src/commonTest/kotlin/SmfExamples.kt

This file was deleted.

109 changes: 109 additions & 0 deletions src/commonTest/kotlin/org/wysko/kmidi/ArrayInputStreamTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.wysko.kmidi

import junit.framework.TestCase.assertEquals
import org.junit.Assert.assertArrayEquals
import org.wysko.kmidi.midi.UnexpectedEndOfFileException
import kotlin.test.Test

class ArrayInputStreamTest {
@Test
fun `Test read`() {
val inputStream = ArrayInputStream(byteArrayOf(1, 2, 3))
assertEquals(1, inputStream.read())
assertEquals(2, inputStream.read())
assertEquals(3, inputStream.read())
}

@Test
fun `Test read word`() {
val inputStream = ArrayInputStream(byteArrayOf(69, 42))
assertEquals(17706, inputStream.readWord())
}

@Test
fun `Test read dword`() {
val inputStream = ArrayInputStream(byteArrayOf(1, 2, 3, 4))
assertEquals(16909060, inputStream.readDWord())
}

@Test
fun `Test read n bytes`() {
val inputStream = ArrayInputStream(byteArrayOf(0, 1, 2, 3, 4, 5))
assertArrayEquals(byteArrayOf(0, 1, 2), inputStream.readNBytes(3))
assertArrayEquals(byteArrayOf(3, 4, 5), inputStream.readNBytes(3))
}

@Test
fun `Test skip`() {
val inputStream = ArrayInputStream(byteArrayOf(0, 1, 2, 3, 4))
inputStream.skip(3)
assertEquals(3, inputStream.read())
assertEquals(4, inputStream.read())
}

@Test
fun `Test 24 bit int`() {
val inputStream = ArrayInputStream(byteArrayOf(4, 20, 69))
assertEquals(267333, inputStream.read24BitInt())
}

@Test(expected = UnexpectedEndOfFileException::class)
fun `Test read empty stream`() {
val inputStream = ArrayInputStream(byteArrayOf())
inputStream.read()
}

@Test(expected = IndexOutOfBoundsException::class)
fun `Test skip too many bytes`() {
val inputStream = ArrayInputStream(byteArrayOf(0, 1, 2))
inputStream.skip(4)
}

@Test(expected = UnexpectedEndOfFileException::class)
fun `Test read word with unexpected end of file`() {
val inputStream = ArrayInputStream(byteArrayOf(0))
inputStream.readWord()
}

@Test(expected = UnexpectedEndOfFileException::class)
fun `Test read dword with unexpected end of file`() {
val inputStream = ArrayInputStream(byteArrayOf(0))
inputStream.readDWord()
}

@Test(expected = UnexpectedEndOfFileException::class)
fun `Test read n bytes with unexpected end of file`() {
val inputStream = ArrayInputStream(byteArrayOf(0))
inputStream.readNBytes(2)
}

@Test(expected = UnexpectedEndOfFileException::class)
fun `Test read 24 bit int with unexpected end of file`() {
val inputStream = ArrayInputStream(byteArrayOf(0))
inputStream.read24BitInt()
}

@Test(expected = UnexpectedEndOfFileException::class)
fun `Test read VLQ with unexpected end of file`() {
val inputStream =
ArrayInputStream(byteArrayOf(0x81.toByte(), 0x80.toByte()))
inputStream.readVlq()
}

@Test
fun `Test read vlq`() {
val inputStream =
ArrayInputStream(byteArrayOf(0x81.toByte(), 0x80.toByte(), 0x00))
assertEquals(0x4000 to 3, inputStream.readVlq())
}

@Test
fun `Test position`() {
val inputStream = ArrayInputStream(byteArrayOf(0, 1, 2, 3, 4))
assertEquals(0, inputStream.position)
inputStream.read()
assertEquals(1, inputStream.position)
inputStream.skip(2)
assertEquals(3, inputStream.position)
}
}
88 changes: 88 additions & 0 deletions src/commonTest/kotlin/org/wysko/kmidi/midi/SmfExamples.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.wysko.kmidi.midi

object SmfExamples {
val example1 =
intArrayOf(
0x4D,
0x54,
0x68,
0x64,
0x00,
0x00,
0x00,
0x06,
0x00,
0x00,
0x00,
0x01,
0x00,
0x60,
0x4D,
0x54,
0x72,
0x6B,
0x00,
0x00,
0x00,
0x3B,
0x00,
0xFF,
0x58,
0x04,
0x04,
0x02,
0x18,
0x08,
0x00,
0xFF,
0x51,
0x03,
0x07,
0xA1,
0x20,
0x00,
0xC0,
0x05,
0x00,
0xC1,
0x2E,
0x00,
0xC2,
0x46,
0x00,
0x92,
0x30,
0x60,
0x00,
0x3C,
0x60,
0x60,
0x91,
0x43,
0x40,
0x60,
0x90,
0x4C,
0x20,
0x81,
0x40,
0x82,
0x30,
0x40,
0x00,
0x3C,
0x40,
0x00,
0x81,
0x43,
0x40,
0x00,
0x80,
0x4C,
0x40,
0x00,
0xFF,
0x2F,
0x00,
).map { it.toByte() }.toByteArray()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*/

import org.wysko.kmidi.midi.StandardMidiFile
import org.wysko.kmidi.midi.StandardMidiFileReader
package org.wysko.kmidi.midi

import org.wysko.kmidi.midi.event.MetaEvent
import org.wysko.kmidi.midi.event.NoteEvent
import org.wysko.kmidi.midi.event.ProgramEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.wysko.kmidi.midi.analysis

import org.wysko.kmidi.midi.event.NoteEvent
import kotlin.test.Test
import kotlin.test.assertEquals

class PolyphonyTest {
@Test
fun `Test calculateMaximumPolyphony empty list`() {
val events = emptyList<NoteEvent>()
val result = Polyphony.calculateMaximumPolyphony(events)
assertEquals(0, result)
}

@Test
fun `Test calculateMaximumPolyphony single note`() {
val events =
listOf(
NoteEvent.NoteOn(0, 0, 60, 127),
NoteEvent.NoteOff(1, 0, 60),
)
val result = Polyphony.calculateMaximumPolyphony(events)
assertEquals(1, result)
}

@Test
fun `Test calculateMaximumPolyphony two simultaneous notes`() {
val events =
listOf(
NoteEvent.NoteOn(0, 0, 60, 127),
NoteEvent.NoteOn(0, 0, 61, 127),
NoteEvent.NoteOff(1, 0, 60),
NoteEvent.NoteOff(1, 0, 61),
)
val result = Polyphony.calculateMaximumPolyphony(events)
assertEquals(2, result)
}

@Test
fun `Test calculateMaximumPolyphony two notes with overlap`() {
val events =
listOf(
NoteEvent.NoteOn(0, 0, 60, 127),
NoteEvent.NoteOn(1, 0, 61, 127),
NoteEvent.NoteOff(2, 0, 60),
NoteEvent.NoteOff(3, 0, 61),
)
val result = Polyphony.calculateMaximumPolyphony(events)
assertEquals(2, result)
}

@Test
fun `Test averagePolyphony empty list`() {
val events = emptyList<NoteEvent>()
val result = Polyphony.averagePolyphony(events)
assertEquals(0.0, result)
}

@Test
fun `Test averagePolyphony single note`() {
val events =
listOf(
NoteEvent.NoteOn(0, 0, 60, 127),
NoteEvent.NoteOff(1, 0, 60),
)
val result = Polyphony.averagePolyphony(events)
assertEquals(1.0, result)
}

@Test
fun `Test averagePolyphony two simultaneous notes`() {
val events =
listOf(
NoteEvent.NoteOn(0, 0, 60, 127),
NoteEvent.NoteOn(0, 0, 61, 127),
NoteEvent.NoteOff(1, 0, 60),
NoteEvent.NoteOff(1, 0, 61),
)
val result = Polyphony.averagePolyphony(events)
assertEquals(2.0, result)
}

@Test
fun `Test averagePolyphony two notes with overlap`() {
val events =
listOf(
NoteEvent.NoteOn(0, 0, 60, 127),
NoteEvent.NoteOn(1, 0, 61, 127),
NoteEvent.NoteOff(2, 0, 60),
NoteEvent.NoteOff(3, 0, 61),
)
val result = Polyphony.averagePolyphony(events)
assertEquals(4 / 3.0, result, 0.0001)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wysko.kmidi.midi

import org.junit.Before
import org.wysko.kmidi.midi.StandardMidiFileReader
import org.wysko.kmidi.midi.TimeBasedSequence.Companion.toTimeBasedSequence
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

package org.wysko.kmidi.midi

import org.junit.Before
import org.wysko.kmidi.midi.StandardMidiFileReader
import org.wysko.kmidi.readFile
import org.wysko.kmidi.readInputStream
import java.io.File
Expand Down

0 comments on commit b77d4aa

Please sign in to comment.