package ru.dbotthepony.mc.otm.tests

import it.unimi.dsi.fastutil.io.FastByteArrayInputStream
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import ru.dbotthepony.mc.otm.core.util.readInt
import ru.dbotthepony.mc.otm.core.util.readVarIntLE
import ru.dbotthepony.mc.otm.core.util.readVarLongLE
import ru.dbotthepony.mc.otm.core.util.writeInt
import ru.dbotthepony.mc.otm.core.util.writeVarIntLE
import ru.dbotthepony.mc.otm.core.util.writeVarLongLE

object FriendlyStreams {
	@Test
	@DisplayName("Stream extension functions")
	fun test() {
		val output = FastByteArrayOutputStream()

		for (i in 0 .. 65565) {
			output.writeInt(i)
		}

		for (i in 0 .. 65565) {
			output.writeVarIntLE(i)
		}

		for (i in 0 .. 65565) {
			output.writeVarLongLE(i.toLong())
		}

		for (i in 0 .. 65565) {
			output.writeInt(-i)
		}

		for (i in 0 .. 65565) {
			output.writeVarIntLE(-i)
		}

		for (i in 0 .. 65565) {
			output.writeVarLongLE(-i.toLong())
		}

		val input = FastByteArrayInputStream(output.array, 0, output.length)

		for (i in 0 .. 65565) {
			assertEquals(i, input.readInt())
		}

		for (i in 0 .. 65565) {
			assertEquals(i, input.readVarIntLE())
		}

		for (i in 0 .. 65565) {
			assertEquals(i.toLong(), input.readVarLongLE())
		}

		for (i in 0 .. 65565) {
			assertEquals(-i, input.readInt())
		}

		for (i in 0 .. 65565) {
			assertEquals(-i, input.readVarIntLE())
		}

		for (i in 0 .. 65565) {
			assertEquals(-i.toLong(), input.readVarLongLE())
		}
	}
}
