test[protocol]: kotlin test

This commit is contained in:
godotg
2024-07-07 17:05:38 +08:00
parent 6ad98bb687
commit e96120dd07
2 changed files with 18 additions and 143 deletions
+9 -66
View File
@@ -2,6 +2,7 @@ import com.zfoo.java.ByteBuffer;
import com.zfoo.java.ProtocolManager;
import java.io.*;
import java.nio.file.Files;
import java.util.List;
/**
@@ -150,7 +151,7 @@ public class Main {
// -----------------------------------------------------------------------------------------------------------------
public static void compatibleTest() throws IOException {
var bytes = toByteArray(new FileInputStream("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\complexObject.bytes"));
var bytes = Files.readAllBytes(new File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\complexObject.bytes").toPath());
var buffer = new ByteBuffer();
buffer.writeBytes(bytes);
@@ -173,17 +174,18 @@ public class Main {
notEqual++;
}
}
System.out.println(format("equal [{}], not equal [{}]", equal, notEqual));
System.out.println("equal: " + equal);
System.out.println("not equal: " + notEqual);
}
public static void normalReadTest() throws IOException {
// var bytes = toByteArray(new FileInputStream("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes"));
// var bytes = toByteArray(new FileInputStream("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes"));
// var bytes = toByteArray(new FileInputStream("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes"));
// var bytes = toByteArray(new FileInputStream("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes"));
var bytes = toByteArray(new FileInputStream("D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes"));
// var bytes = Files.readAllBytes(new File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes").toPath());
// var bytes = Files.readAllBytes(new File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes").toPath());
// var bytes = Files.readAllBytes(new File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes").toPath());
// var bytes = Files.readAllBytes(new File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes").toPath());
var bytes = Files.readAllBytes(new File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes").toPath());
var buffer = new ByteBuffer();
buffer.writeBytes(bytes);
@@ -193,63 +195,4 @@ public class Main {
}
// -----------------------------------------------------------------------------------------------------------------
public static byte[] toByteArray(final InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
var bytes = output.toByteArray();
return bytes;
}
public static final int EOF = -1;
// The number of bytes in a byte
public static final int ONE_BYTE = 1;
// The number of bytes in a kilobyte
public static final int BYTES_PER_KB = ONE_BYTE * 1024;
// The number of bytes in a megabyte
public static final int BYTES_PER_MB = BYTES_PER_KB * 1024;
// The number of bytes in a gigabyte
public static final long BYTES_PER_GB = BYTES_PER_MB * 1024;
public static int copy(final InputStream input, final OutputStream output) throws IOException {
byte[] buffer = new byte[BYTES_PER_KB];
long count = 0;
int n;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
if (count > BYTES_PER_GB * 2L) {
return -1;
}
return (int) count;
}
public static String format(final String template, final Object... args) {
// 初始化定义好的长度以获得更好的性能
var builder = new StringBuilder(template.length() + 50);
// 记录已经处理到的位置
var readIndex = 0;
for (int i = 0; i < args.length; i++) {
// 占位符所在位置
var placeholderIndex = template.indexOf("{}", readIndex);
// 剩余部分无占位符
if (placeholderIndex == -1) {
// 不带占位符的模板直接返回
if (readIndex == 0) {
return template;
}
break;
}
builder.append(template, readIndex, placeholderIndex);
builder.append(args[i]);
readIndex = placeholderIndex + 2;
}
// 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果
builder.append(template, readIndex, template.length());
return builder.toString();
}
}
+9 -77
View File
@@ -4,6 +4,7 @@ import com.zfoo.kotlin.ProtocolManager.Companion.initProtocol
import com.zfoo.kotlin.ProtocolManager.Companion.read
import com.zfoo.kotlin.ProtocolManager.Companion.write
import java.io.*
import java.nio.file.Files
/**
* @author godotg
@@ -148,8 +149,7 @@ fun assertArrayEquals(a: ByteArray?, b: ByteArray?) {
// -----------------------------------------------------------------------------------------------------------------
@Throws(IOException::class)
fun compatibleTest() {
val bytes =
toByteArray(FileInputStream("C:\\github\\zfoo\\protocol\\src\\test\\resources\\complexObject.bytes"))
val bytes = Files.readAllBytes(File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\complexObject.bytes").toPath())
val buffer = ByteBuffer()
buffer.writeBytes(bytes)
@@ -172,18 +172,18 @@ fun compatibleTest() {
notEqual++
}
}
println(format("equal [{}], not equal [{}]", equal, notEqual))
println("equal: " + equal)
println("not equal:" + notEqual)
}
@Throws(IOException::class)
fun normalReadTest() {
// var bytes = toByteArray(FileInputStream("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes"));
// var bytes = toByteArray(FileInputStream("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes"));
// var bytes = toByteArray(FileInputStream ("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes"));
var bytes =
toByteArray(FileInputStream("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes"));
// val bytes = toByteArray(FileInputStream("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes"))
// var bytes = Files.readAllBytes(File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes").toPath())
// var bytes = Files.readAllBytes(File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes").toPath())
// var bytes = Files.readAllBytes(File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes").toPath())
// var bytes = Files.readAllBytes(File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes").toPath())
val bytes = Files.readAllBytes(File("C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes").toPath())
val buffer = ByteBuffer()
buffer.writeBytes(bytes)
@@ -196,71 +196,3 @@ fun normalReadTest() {
println(packet)
println(newPacket)
}
// -----------------------------------------------------------------------------------------------------------------
@Throws(IOException::class)
fun toByteArray(input: InputStream): ByteArray {
val output = ByteArrayOutputStream()
copy(input, output)
val bytes = output.toByteArray()
return bytes
}
const val EOF: Int = -1
// The number of bytes in a byte
const val ONE_BYTE: Int = 1
// The number of bytes in a kilobyte
const val BYTES_PER_KB: Int = ONE_BYTE * 1024
// The number of bytes in a megabyte
const val BYTES_PER_MB: Int = BYTES_PER_KB * 1024
// The number of bytes in a gigabyte
const val BYTES_PER_GB: Long = (BYTES_PER_MB * 1024).toLong()
@Throws(IOException::class)
fun copy(input: InputStream, output: OutputStream): Int {
val buffer = ByteArray(BYTES_PER_KB)
var count: Long = 0
var n: Int
while (EOF != (input.read(buffer).also { n = it })) {
output.write(buffer, 0, n)
count += n.toLong()
}
if (count > BYTES_PER_GB * 2L) {
return -1
}
return count.toInt()
}
fun format(template: String, vararg args: Any?): String {
// 初始化定义好的长度以获得更好的性能
val builder = StringBuilder(template.length + 50)
// 记录已经处理到的位置
var readIndex = 0
for (i in args.indices) {
// 占位符所在位置
val placeholderIndex = template.indexOf("{}", readIndex)
// 剩余部分无占位符
if (placeholderIndex == -1) {
// 不带占位符的模板直接返回
if (readIndex == 0) {
return template
}
break
}
builder.append(template, readIndex, placeholderIndex)
builder.append(args[i])
readIndex = placeholderIndex + 2
}
// 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果
builder.append(template, readIndex, template.length)
return builder.toString()
}