Skip to content

Commit

Permalink
test: tests for all implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Art Shendrik <[email protected]>
  • Loading branch information
amal committed Jun 29, 2024
1 parent 05603bc commit b745140
Show file tree
Hide file tree
Showing 13 changed files with 1,559 additions and 18 deletions.
16 changes: 8 additions & 8 deletions fluxo-io-rad/api/android/fluxo-io-rad.api
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,21 @@ public final class fluxo/io/rad/Rad {
public static final fun forDataInputFactory (JLkotlin/jvm/functions/Function0;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forDataInputFactory$default (JJJILkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileDescriptor;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileDescriptor;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileDescriptor;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileInputStream;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileInputStream;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileInputStream;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/RandomAccessFile;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/RandomAccessFile;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/RandomAccessFile;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;JJ[Ljava/lang/AutoCloseable;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/File;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/File;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/FileDescriptor;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/FileInputStream;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/RandomAccessFile;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/nio/channels/FileChannel;JJ[Ljava/lang/AutoCloseable;ILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forInputStreamFactory (JJJILkotlin/jvm/functions/Function0;)Lfluxo/io/rad/RandomAccessData;
public static final fun forInputStreamFactory (JJJLkotlin/jvm/functions/Function0;)Lfluxo/io/rad/RandomAccessData;
Expand All @@ -117,6 +109,14 @@ public final class fluxo/io/rad/Rad {
public static final fun forMemoryMappedFile (Ljava/nio/channels/FileChannel;JI)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forMemoryMappedFile$default (Ljava/io/File;JIILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forMemoryMappedFile$default (Ljava/nio/channels/FileChannel;JIILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/RandomAccessFile;)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/RandomAccessFile;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/RandomAccessFile;JJ)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forRandomAccessFile$default (Ljava/io/File;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forRandomAccessFile$default (Ljava/io/RandomAccessFile;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forSeekableByteChannel (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forSeekableByteChannel (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forSeekableByteChannel (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
Expand Down
16 changes: 8 additions & 8 deletions fluxo-io-rad/api/jvm/fluxo-io-rad.api
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,21 @@ public final class fluxo/io/rad/Rad {
public static final fun forDataInputFactory (JLkotlin/jvm/functions/Function0;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forDataInputFactory$default (JJJILkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileDescriptor;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileDescriptor;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileDescriptor;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileInputStream;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileInputStream;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/FileInputStream;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/RandomAccessFile;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/RandomAccessFile;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/io/RandomAccessFile;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forFileChannel (Ljava/nio/channels/FileChannel;JJ[Ljava/lang/AutoCloseable;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/File;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/File;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/FileDescriptor;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/FileInputStream;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/io/RandomAccessFile;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forFileChannel$default (Ljava/nio/channels/FileChannel;JJ[Ljava/lang/AutoCloseable;ILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forInputStreamFactory (JJJILkotlin/jvm/functions/Function0;)Lfluxo/io/rad/RandomAccessData;
public static final fun forInputStreamFactory (JJJLkotlin/jvm/functions/Function0;)Lfluxo/io/rad/RandomAccessData;
Expand All @@ -117,6 +109,14 @@ public final class fluxo/io/rad/Rad {
public static final fun forMemoryMappedFile (Ljava/nio/channels/FileChannel;JI)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forMemoryMappedFile$default (Ljava/io/File;JIILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forMemoryMappedFile$default (Ljava/nio/channels/FileChannel;JIILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/RandomAccessFile;)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/RandomAccessFile;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forRandomAccessFile (Ljava/io/RandomAccessFile;JJ)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forRandomAccessFile$default (Ljava/io/File;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static synthetic fun forRandomAccessFile$default (Ljava/io/RandomAccessFile;JJILjava/lang/Object;)Lfluxo/io/rad/RandomAccessData;
public static final fun forSeekableByteChannel (Ljava/io/File;)Lfluxo/io/rad/RandomAccessData;
public static final fun forSeekableByteChannel (Ljava/io/File;J)Lfluxo/io/rad/RandomAccessData;
public static final fun forSeekableByteChannel (Ljava/io/File;JJ)Lfluxo/io/rad/RandomAccessData;
Expand Down
4 changes: 4 additions & 0 deletions fluxo-io-rad/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ fkcSetupMultiplatform(
implementation(libs.assertj)
}

sourceSets.jvmTest.configure {
dependsOn(commonJvm.main)
}

val commonJs = commonJs
commonJs.main.dependencies {
// Fix JS build KLIB issue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.io.RandomAccessFile
*/
@Blocking
@JvmOverloads
@JvmName("forFileChannel")
@JvmName("forRandomAccessFile")
public fun RandomAccessFileRadAccessor(
data: RandomAccessFile,
offset: Long = 0L,
Expand Down Expand Up @@ -55,7 +55,7 @@ public fun RandomAccessFileRadAccessor(
*/
@Blocking
@JvmOverloads
@JvmName("forFileChannel")
@JvmName("forRandomAccessFile")
@Throws(FileNotFoundException::class)
public fun RandomAccessFileRadAccessor(
data: File,
Expand Down
151 changes: 151 additions & 0 deletions fluxo-io-rad/src/jvmTest/kotlin/fluxo/io/IoUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
@file:Suppress("KDocUnresolvedReference")

package fluxo.io

import fluxo.io.nio.markCompat
import fluxo.io.nio.resetCompat
import fluxo.io.util.EMPTY_BYTE_ARRAY
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStream
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import kotlin.math.ln
import kotlin.math.pow


fun readableFileSize(bytes: Int): String = readableFileSize(bytes.toLong())

/**
* Convert byte size into the human-readable format.
*/
fun readableFileSize(bytes: Long): String {
val unit = 1024.0
if (bytes < unit) return "$bytes B"
val exp = (ln(bytes.toDouble()) / ln(unit)).toInt()
val pre = "kMGTPE"[exp - 1]
return "%.1f %sB".format(bytes / unit.pow(exp.toDouble()), pre)
}


/**
* Reads this stream completely into a byte array.
*
* **Note**: It is the caller's responsibility to close this stream.
*
* @param size Expected size of the data. Will use [readBytes] when [size] == `-1`.
*
* @see readBytesExact
* @see kotlin.io.readBytes
* @see java.io.DataInputStream.readFully
* @see java.io.InputStream.readNBytes
*
* @throws EOFException On unexpected stream end or when stream is not ended on expected size.
* @throws IllegalArgumentException When [size] < `-1`
*/
@Throws(IOException::class)
fun InputStream.readBytesFully(size: Int): ByteArray {
if (size < 0) {
require(size == -1) { "size < -1: $size" }

// simple reading of stream with unknown size
return readBytes()
}

if (size == 0) {
return EMPTY_BYTE_ARRAY
}

var offset = 0
var remaining = size
val result = ByteArray(remaining)
while (remaining > 0) {
val read = read(result, offset, remaining)
if (read < 0) {
break
}
remaining -= read
offset += read
}
if (remaining > 0) {
throw EOFException("$offset < expected size: $size")
}
if (read() != -1) {
throw EOFException("Stream is not ended at expected size: $size")
}
return result
}

/**
* Reads up to a specified number of bytes from the input stream.
*
* **Note**: It is the caller's responsibility to close this stream.
*
* @param size Expected size of the data.
* @param strict Allows disabling exception on the unexpected end of the stream.
*
* @see readBytesFully
* @see java.io.InputStream.readNBytes
*
* @throws EOFException On unexpected stream end or when stream
*/
@Throws(IOException::class)
fun InputStream.readBytesExact(size: Int, strict: Boolean = true): ByteArray {
if (size == 0) {
return EMPTY_BYTE_ARRAY
}
val result = ByteArray(size)
var offset = 0
var read: Int
while (offset < size) {
read = read(result, offset, size - offset)
if (read < 0) {
if (strict) {
throw EOFException("$offset < expected size: $size")
}
return result.copyOf(offset)
}
offset += read
}
return result
}


fun ByteBuffer.toArray(): ByteArray {
val array = ByteArray(remaining())
markCompat()
get(array)
resetCompat()
return array
}


/**
* @see kotlin.io.use
* @see java.nio.channels.FileChannel.lock
*/
inline fun <R> FileInputStream.useLocked(block: (FileInputStream) -> R): R =
use { s -> s.channel.useReadLocked { block(s) } }

/**
* @see kotlin.io.use
* @see java.nio.channels.FileChannel.lock
*/
inline fun <R> FileOutputStream.useLocked(block: (FileOutputStream) -> R): R =
use { s -> s.channel.useLocked { block(s) } }

/**
* @see kotlin.io.use
* @see java.nio.channels.FileChannel.lock
*/
inline fun <R> FileChannel.useLocked(block: (FileChannel) -> R): R =
// lock will be auto-closed on channel close.
use { ch -> ch.lock(); block(ch) }

/**
* @see kotlin.io.use
* @see java.nio.channels.FileChannel.lock
*/
inline fun <R> FileChannel.useReadLocked(block: (FileChannel) -> R): R =
// lock will be auto-closed on channel close.
use { ch -> ch.lock(0, java.lang.Long.MAX_VALUE, true); block(ch) }
Loading

0 comments on commit b745140

Please sign in to comment.