Skip to content

Commit

Permalink
KotlinIoOscSource.kt updated according to unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Sep 3, 2024
1 parent 63cd10c commit c3c0161
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 38 deletions.
61 changes: 33 additions & 28 deletions xemantic-osc-api/src/commonMain/kotlin/io/KotlinIoOscSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,13 @@ import com.xemantic.osc.OscTimeTag
import com.xemantic.osc.oscPadding
import kotlinx.io.*

/**
* Creates a [Source] from supplied bytes.
* Useful for testing.
*
* @param bytes the sequence of bytes.
*/
public fun Source(
vararg bytes: Byte,
): Source = Buffer().apply {
write(bytes)
}

/**
* Creates a [Source] from supplied chars.
* Useful for testing.
*
* @param chars the sequence of characters.
*/
public fun Source(
vararg chars: Char
): Source = Buffer().apply {
write(
chars.map {
it.code.toByte()
}.toByteArray()
)
}

/**
* Removes bytes from this source interpreting them as OSC String.
* The OSC String should be `0`-terminated and padded up to 4 bytes.
*
* @return the OSC String.
* @throws OscInputException on unexpected input data.
* @throws EOFException on unexpected input data.
*/
public fun Source.readOscString(): String {
val terminatorIndex = indexOf(0.toByte())
Expand All @@ -77,6 +51,7 @@ public fun Source.readOscString(): String {
* Removes 4 bytes from this source, interpreting them as OSC char.
*
* @return the OSC Char.
* @throws EOFException on unexpected input data.
*/
public fun Source.readOscChar(): Char = readInt().toChar()

Expand All @@ -85,6 +60,7 @@ public fun Source.readOscChar(): Char = readInt().toChar()
* The first 4-bytes encode an [Int] describing the size of the blob.
*
* @return the OSC Blob.
* @throws EOFException on unexpected input data.
*/
public fun Source.readOscBlob(): ByteArray {
val size = readInt()
Expand All @@ -97,8 +73,37 @@ public fun Source.readOscBlob(): ByteArray {
* Removes 8 bytes from this source, interpreting them as OSC time tag.
*
* @return the OSC Time Tag.
* @throws EOFException on insufficient input data.
*/
public fun Source.readOscTimeTag(): OscTimeTag = OscTimeTag(
seconds = readUInt(),
fraction = readUInt()
)

/**
* Creates a [Source] from supplied bytes.
* Useful for testing.
*
* @param bytes the sequence of bytes.
*/
public fun Source(
vararg bytes: Byte,
): Source = Buffer().apply {
write(bytes)
}

/**
* Creates a [Source] from supplied chars.
* Useful for testing.
*
* @param chars the sequence of characters.
*/
public fun Source(
vararg chars: Char
): Source = Buffer().apply {
write(
chars.map {
it.code.toByte()
}.toByteArray()
)
}
30 changes: 20 additions & 10 deletions xemantic-osc-api/src/commonTest/kotlin/io/KotlinIoOscSourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ class KotlinIoOscSourceTest {
}
}

/**
* Test case taken from OSC protocol specification:
*
* https://opensoundcontrol.stanford.edu/spec-1_0-examples.html
*/
@Test
fun shouldReadSingleCharacterOscString() {
Source('a', ZERO, ZERO, ZERO).apply {
readOscString() shouldBe "a"
fun shouldReadOscStringAccordingToSpecExample1() {
Source('O', 'S', 'C', ZERO).apply {
readOscString() shouldBe "OSC"
exhausted() shouldBe true
}
}

/**
* Test case taken from OSC protocol specification:
*
* https://opensoundcontrol.stanford.edu/spec-1_0-examples.html
*/
@Test
fun shouldRead3CharacterOscString() {
Source('f', 'o', 'o', ZERO).apply {
Source('d', 'a', 't', 'a').apply {
readOscString() shouldBe "foo"
exhausted() shouldBe true
}
Expand Down Expand Up @@ -235,21 +245,21 @@ class KotlinIoOscSourceTest {
}

@Test
fun shouldReadImmediateTimeTag() {
fun shouldReadSpecificTimeTag() {
Source(
0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 1, 0, 0, 0, 2
).apply {
readOscTimeTag() shouldBe OscTimeTag.IMMEDIATE
readOscTimeTag() shouldBe OscTimeTag(1u, 2u)
exhausted() shouldBe true
}
}

@Test
fun shouldReadSpecificTimeTag() {
fun shouldReadImmediateTimeTag() {
Source(
0, 0, 0, 1, 0, 0, 0, 2
0, 0, 0, 0, 0, 0, 0, 1
).apply {
readOscTimeTag() shouldBe OscTimeTag(1u, 2u)
readOscTimeTag() shouldBe OscTimeTag.IMMEDIATE
exhausted() shouldBe true
}
}
Expand Down

0 comments on commit c3c0161

Please sign in to comment.