Convert between byte[] and String[] efficiently
This commit is contained in:
committed by
Pavel V. Talanov
parent
2af8dd4298
commit
32c5624531
+29
-9
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.jet.descriptors.serialization;
|
||||
|
||||
import com.intellij.testFramework.UsefulTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
@@ -25,29 +26,48 @@ import static org.jetbrains.jet.descriptors.serialization.JavaProtoBufUtil.encod
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class JavaProtoBufUtilTest extends UsefulTestCase {
|
||||
private static final int[] LENGTHS = new int[] {0, 1, 2, 3, 7, 10, 100, 1000, 32000, 33000, 65000, 65534, 65535, 65536, 65537,
|
||||
100000, 131074, 239017, 314159, 1000000};
|
||||
private static final int[] BIG_LENGTHS = new int[]
|
||||
{1000, 32000, 33000, 65000, 65534, 65535, 65536, 65537, 100000, 131074, 239017, 314159, 1000000};
|
||||
|
||||
private static void doTest(int randSeed, int length) {
|
||||
private static void doTest(int randSeed, int length) throws Exception {
|
||||
byte[] a = new byte[length];
|
||||
new Random(randSeed).nextBytes(a);
|
||||
|
||||
String message = "Failed randSeed = " + randSeed + ", length = " + length;
|
||||
|
||||
String[] b = encodeBytes(a);
|
||||
for (String string : b) {
|
||||
assertStringConformsToJVMS(string);
|
||||
}
|
||||
|
||||
byte[] c = decodeBytes(b);
|
||||
String message = "Failed randSeed = " + randSeed + ", length = " + length;
|
||||
assertArrayEquals(message, a, c);
|
||||
|
||||
String[] d = encodeBytes(c);
|
||||
assertArrayEquals(message, d, b);
|
||||
assertArrayEquals(message, b, d);
|
||||
|
||||
byte[] e = decodeBytes(d);
|
||||
assertArrayEquals(message, a, e);
|
||||
|
||||
}
|
||||
|
||||
public void testEncodeDecode() {
|
||||
for (int randSeed = 1; randSeed <= 3; randSeed++) {
|
||||
for (int length : LENGTHS) {
|
||||
private static void assertStringConformsToJVMS(@NotNull String string) {
|
||||
int effectiveLength = string.length();
|
||||
for (char c : string.toCharArray()) {
|
||||
if (c == 0x0) effectiveLength++;
|
||||
}
|
||||
assertTrue(String.format("String exceeds maximum allowed length in a class file: %d > 65535", effectiveLength),
|
||||
effectiveLength <= 65535);
|
||||
}
|
||||
|
||||
public void testEncodeDecode() throws Exception {
|
||||
for (int length = 0; length <= 100; length++) {
|
||||
for (int randSeed = 1; randSeed <= 100; randSeed++) {
|
||||
doTest(randSeed, length);
|
||||
}
|
||||
}
|
||||
|
||||
for (int length : BIG_LENGTHS) {
|
||||
for (int randSeed = 1; randSeed <= 3; randSeed++) {
|
||||
doTest(randSeed, length);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user