Skip to content

Commit

Permalink
test[protocol]: kotlin test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 7, 2024
1 parent 6ad98bb commit e96120d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 143 deletions.
75 changes: 9 additions & 66 deletions protocol/src/test/kotlin/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import com.zfoo.java.ProtocolManager;

import java.io.*;
import java.nio.file.Files;
import java.util.List;

/**
Expand Down Expand Up @@ -150,7 +151,7 @@ public static void assertEquals(byte[] a, byte[] b) {

// -----------------------------------------------------------------------------------------------------------------
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);

Expand All @@ -173,17 +174,18 @@ public static void compatibleTest() throws IOException {
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);
Expand All @@ -193,63 +195,4 @@ public static void normalReadTest() throws IOException {
}


// -----------------------------------------------------------------------------------------------------------------
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();
}
}
86 changes: 9 additions & 77 deletions protocol/src/test/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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()
}

0 comments on commit e96120d

Please sign in to comment.