Extract Base64.main() as a unit test.
This commit is contained in:
parent
75ece3442f
commit
f191e47f5b
@ -81,7 +81,7 @@ public class Base64 {
|
|||||||
if (p==0)
|
if (p==0)
|
||||||
break;
|
break;
|
||||||
if (p!=4) {
|
if (p!=4) {
|
||||||
throw new IllegalArgumentException("Data ended when decoding Base64, p="+p);
|
throw new IllegalArgumentException("Data ended when decoding Base64, data=" + data + ", p="+p);
|
||||||
}
|
}
|
||||||
|
|
||||||
int l = decodeGroup(block, array, length);
|
int l = decodeGroup(block, array, length);
|
||||||
@ -166,67 +166,4 @@ public class Base64 {
|
|||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] arg) {
|
|
||||||
Random rnd = new Random();
|
|
||||||
|
|
||||||
for (int round=0; round < 1000; round++) {
|
|
||||||
int n = rnd.nextInt(1000);
|
|
||||||
n = 100000;
|
|
||||||
|
|
||||||
byte[] array = new byte[n];
|
|
||||||
rnd.nextBytes(array);
|
|
||||||
|
|
||||||
String encoded = encode(array);
|
|
||||||
|
|
||||||
System.out.println(encoded);
|
|
||||||
System.exit(0);
|
|
||||||
// for (int i=0; i<1000; i++) {
|
|
||||||
// int pos = rnd.nextInt(encoded.length());
|
|
||||||
// String s1 = encoded.substring(0, pos);
|
|
||||||
// String s2 = encoded.substring(pos);
|
|
||||||
// switch (rnd.nextInt(15)) {
|
|
||||||
// case 0:
|
|
||||||
// encoded = s1 + " " + s2;
|
|
||||||
// break;
|
|
||||||
// case 1:
|
|
||||||
// encoded = s1 + "\u0009" + s2;
|
|
||||||
// break;
|
|
||||||
// case 2:
|
|
||||||
// encoded = s1 + "\n" + s2;
|
|
||||||
// break;
|
|
||||||
// case 3:
|
|
||||||
// encoded = s1 + "\u000B" + s2;
|
|
||||||
// break;
|
|
||||||
// case 4:
|
|
||||||
// encoded = s1 + "\r" + s2;
|
|
||||||
// break;
|
|
||||||
// case 5:
|
|
||||||
// encoded = s1 + "\u000C" + s2;
|
|
||||||
// break;
|
|
||||||
// case 6:
|
|
||||||
// encoded = s1 + "\u001C" + s2;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
byte[] decoded = null;
|
|
||||||
try {
|
|
||||||
decoded = decode(encoded);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
System.err.println("Bad data:\n"+encoded);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Arrays.equals(array, decoded)) {
|
|
||||||
System.err.println("Data differs! n="+n);
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
System.out.println("n="+n+" ok!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
42
core/test/net/sf/openrocket/util/Base64Test.java
Normal file
42
core/test/net/sf/openrocket/util/Base64Test.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package net.sf.openrocket.util;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Base64Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void oldMainTest() throws Exception {
|
||||||
|
|
||||||
|
// TODO - this test case should probably be less random and more targeted to
|
||||||
|
// special cases such as:
|
||||||
|
// null input
|
||||||
|
// empty input
|
||||||
|
// decoding bad string
|
||||||
|
|
||||||
|
Random rnd = new Random();
|
||||||
|
|
||||||
|
for (int round=0; round < 1000; round++) {
|
||||||
|
int n = rnd.nextInt(1000);
|
||||||
|
n = 100000;
|
||||||
|
|
||||||
|
byte[] array = new byte[n];
|
||||||
|
rnd.nextBytes(array);
|
||||||
|
|
||||||
|
String encoded = Base64.encode(array);
|
||||||
|
|
||||||
|
byte[] decoded = null;
|
||||||
|
decoded = Base64.decode(encoded);
|
||||||
|
|
||||||
|
if (!Arrays.equals(array, decoded)) {
|
||||||
|
fail("Data differs! n="+n);
|
||||||
|
}
|
||||||
|
System.out.println("n="+n+" ok!");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user