Commit 9c3c7fb8 authored by Tomm0017's avatar Tomm0017
Browse files

Remove `I26BitCode`

parent 77774cf7
Showing with 0 additions and 110 deletions
+0 -110
package org.rsmod.plugins.info.player.buffer
import org.rsmod.plugins.info.player.model.bitcode.I26BitCode
import org.rsmod.plugins.info.player.model.coord.HighResCoord
import org.rsmod.plugins.info.player.model.coord.LowResCoord
internal fun BitBuffer.getI26BitCode(bits: Int): I26BitCode {
return I26BitCode(getBits(bits), bits)
}
internal fun BitBuffer.putI26BitCode(bitCode: I26BitCode): BitBuffer {
if (bitCode.bitCount == 0) return this
putBits(bitCode.bitCount, bitCode.value)
return this
}
internal fun BitBuffer.putHighResUpdate(
extended: Boolean,
currCoords: HighResCoord,
......
package org.rsmod.plugins.info.player.model.bitcode
@JvmInline
public value class I26BitCode private constructor(private val packed: Int) {
public val value: Int get() = packed and VALUE_BITMASK
public val bitCount: Int get() = (packed shr VALUE_BITS) and VALUE_BITMASK
public constructor(value: Int, bitCount: Int) : this(
(value and VALUE_BITMASK) or ((bitCount and BIT_COUNT_BITMASK) shl VALUE_BITS)
)
public operator fun component1(): Int = value
public operator fun component2(): Int = bitCount
public override fun toString(): String {
return "I26BitCode(value=$value, bitCount=$bitCount)"
}
public companion object {
public val ZERO: I26BitCode = I26BitCode(0)
public const val VALUE_BITS: Int = 26
public const val VALUE_BITMASK: Int = (1 shl VALUE_BITS) - 1
public const val BIT_COUNT_BITS: Int = 5
public const val BIT_COUNT_BITMASK: Int = (1 shl BIT_COUNT_BITS) - 1
}
}
package org.rsmod.plugins.info.player.model.bitcode
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import org.rsmod.plugins.info.player.buffer.BitBuffer
import org.rsmod.plugins.info.player.model.bitcode.I26BitCode.Companion.VALUE_BITS
import java.nio.ByteBuffer
import java.util.stream.Stream
class I26BitCodeTest {
@Test
fun testBitBufferTransmission() {
ByteBuffer.allocate(3).let { buf ->
BitBuffer(buf).use { bitBuf ->
bitBuf.putBits(len = 8, value = 250)
bitBuf.putBoolean(true)
bitBuf.putBits(len = 5, value = 1)
bitBuf.putBits(len = 10, value = 1023)
val bitCount = bitBuf.position()
val value = bitBuf.flip().getBits(bitCount)
val code = I26BitCode(value, bitCount)
check(code.value == value)
check(code.bitCount == bitCount)
bitBuf.setBits(index = 0, len = code.bitCount, value = code.value)
bitBuf.flip()
Assertions.assertEquals(250, bitBuf.getBits(8))
Assertions.assertTrue(bitBuf.getBoolean())
Assertions.assertEquals(1, bitBuf.getBits(5))
Assertions.assertEquals(1023, bitBuf.getBits(10))
}
}
}
@ParameterizedTest
@ArgumentsSource(BitCodeProvider::class)
fun testConstructBitCode(value: Int, bitCount: Int) {
val bitCode = I26BitCode(value, bitCount)
require(bitCount <= VALUE_BITS)
require(value.countOneBits() <= VALUE_BITS)
Assertions.assertEquals(value, bitCode.value)
Assertions.assertEquals(bitCount, bitCode.bitCount)
}
private object BitCodeProvider : ArgumentsProvider {
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> {
return Stream.of(
Arguments.of(0, 0),
Arguments.of((1 shl 8) - 1, 8),
Arguments.of((1 shl 16) - 1, 16),
Arguments.of((1 shl 20) - 1, 20),
Arguments.of((1 shl 23) - 1, 23),
Arguments.of((1 shl 24) - 1, 24),
Arguments.of((1 shl 26) - 1, 26)
)
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment