diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt index 14d871db..c9d32c9f 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/TaglessEnvelopeFormat.kt @@ -43,6 +43,7 @@ class TaglessEnvelopeFormat( writeText(dataStart + "\r\n") writeFully(data.toBytes()) } + flush() } override fun Input.readObject(): Envelope { diff --git a/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/BinaryTest.kt b/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/BinaryTest.kt new file mode 100644 index 00000000..f7018e5e --- /dev/null +++ b/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/BinaryTest.kt @@ -0,0 +1,82 @@ +package hep.dataforge.io + +import hep.dataforge.context.Global +import kotlinx.io.core.readAvailable +import org.junit.Test +import java.nio.file.Files +import java.nio.file.Paths +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class BinaryTest { + val envelope = Envelope { + meta { + "a" put "AAA" + "b" put 22.2 + } + dataType = "hep.dataforge.test" + dataID = "myData" // добавил только что + data { + writeDouble(16.7) + + } + } + + @Test + fun testSize() { + val binary = envelope.data!! + assertEquals(binary.size.toInt(), binary.toBytes().size) + } + + + val envelopeFromFile = Envelope { + meta { + "a" put "AAA" + "b" put 22.2 + } + dataType = "hep.dataforge.satellite" + dataID = "cellDepositTest" // добавил только что + this@Envelope.data = Paths.get("test_data", "binaryBlock.bin").asBinary() + } + + @Test + fun testFileDataSize() { + println(System.getProperty("user.dir")) + val binary = envelopeFromFile.data!! + println(binary.toBytes().size) + assertEquals(binary.size.toInt(), binary.toBytes().size) + } + + @Test + fun testFileDataSizeRewriting() { + println(System.getProperty("user.dir")) + val tmpPath = Files.createTempFile("dataforge_test", ".df") + Global.io.writeEnvelopeFile(tmpPath, envelopeFromFile) + + val binary = Global.io.readEnvelopeFile(tmpPath).data!! + assertEquals(binary.size.toInt(), binary.toBytes().size) + } + + @Test + fun testDataReading() { + println(System.getProperty("user.dir")) + val tmpPath = Files.createTempFile("dataforge_test", ".df") + Global.io.writeEnvelopeFile(tmpPath, envelopeFromFile) + + val binary = Global.io.readEnvelopeFile(tmpPath).data!! + + var sum = 0.0 + binary.read { + val dst = DoubleArray(100) { 0.0 } + do { + val flag = readAvailable(dst) + println("$flag : ${dst[0]}") + dst.map { + sum += it + } + } while (flag != -1) + } + assertTrue(sum > 0.0) + } + +} \ No newline at end of file diff --git a/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileEnvelopeTest.kt b/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileEnvelopeTest.kt index ba7f7cc5..0c934d80 100644 --- a/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileEnvelopeTest.kt +++ b/dataforge-io/src/jvmTest/kotlin/hep/dataforge/io/FileEnvelopeTest.kt @@ -1,9 +1,10 @@ package hep.dataforge.io import hep.dataforge.context.Global +import java.nio.ByteBuffer import java.nio.file.Files -import kotlin.test.Test -import kotlin.test.assertTrue +import java.util.* +import kotlin.test.* class FileEnvelopeTest { @@ -21,11 +22,66 @@ class FileEnvelopeTest { } @Test - fun testFileWriteRead() { + fun testFileWriteTagged() { + val tmpPath = Files.createTempFile("dataforge_test", ".df") + Global.io.writeEnvelopeFile(tmpPath, envelope) + assertTrue { tmpPath.toFile().length() > 0 } + } + + @Test + fun testFileWriteReadTagged() { val tmpPath = Files.createTempFile("dataforge_test", ".df") Global.io.writeEnvelopeFile(tmpPath,envelope) println(tmpPath.toUri()) val restored: Envelope = Global.io.readEnvelopeFile(tmpPath) assertTrue { envelope.contentEquals(restored) } } + + @Test + fun testFileWriteTagless() { + val tmpPath = Files.createTempFile("dataforge_test", ".df") + Global.io.writeEnvelopeFile(tmpPath, envelope, formatFactory = TaglessEnvelopeFormat) + assertTrue { tmpPath.toFile().length() > 0 } + } + + @Test + fun testFileWriteReadTagless() { + val tmpPath = Files.createTempFile("dataforge_test", ".df") + Global.io.writeEnvelopeFile(tmpPath, envelope, formatFactory = TaglessEnvelopeFormat) + println(tmpPath.toUri()) + val restored: Envelope = Global.io.readEnvelopeFile(tmpPath, formatFactory = TaglessEnvelopeFormat) + assertTrue { envelope.contentEquals(restored) } + } + + + @Test + fun testDataSize() { + val tmpPath = Files.createTempFile("dataforge_test", ".df") + Global.io.writeEnvelopeFile(tmpPath,envelope) + println(tmpPath.toUri()) + val scan = Scanner(tmpPath.toFile().inputStream()).useDelimiter("\n").nextLine() + println(scan) + val format = scan.slice(2..5) + when(format){ + "DF03" -> { + val buff = ByteBuffer.allocate(4) + buff.put(scan.slice(12..19).toByteArray()) + buff.flip() + val size = buff.long + println(size) + assertEquals(8, size) + } + "DF02" -> { + val buff = ByteBuffer.allocate(4) + buff.put(scan.slice(12..15).toByteArray()) + buff.flip() + val size = buff.int + println(size) + assertEquals(8, size) + } + else -> { + fail("Format $format don't have test") + } + } + } } \ No newline at end of file diff --git a/dataforge-io/test_data/binaryBlock.bin b/dataforge-io/test_data/binaryBlock.bin new file mode 100644 index 00000000..557584c4 Binary files /dev/null and b/dataforge-io/test_data/binaryBlock.bin differ