updated imports. Moved tests to their respective projects

This commit is contained in:
Robinson 2021-01-17 14:35:40 +01:00
parent 41f060dede
commit 519f9c94fe
7 changed files with 6 additions and 2091 deletions

View File

@ -1,781 +0,0 @@
/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Random;
import org.junit.Assert;
import org.junit.Test;
import dorkbox.util.bytes.ByteBuffer2;
public class ByteBuffer2Test {
static public void assertArrayEquals(Object object1, Object object2) {
Assert.assertEquals(arrayToList(object1), arrayToList(object2));
}
static public Object arrayToList(Object array) {
if (array == null || !array.getClass().isArray()) {
return array;
}
ArrayList<Object> list = new ArrayList<Object>(Array.getLength(array));
for (int i = 0, n = Array.getLength(array); i < n; i++) {
list.add(arrayToList(Array.get(array, i)));
}
return list;
}
@Test
public void testWriteBytes() {
ByteBuffer2 buffer = new ByteBuffer2(512);
buffer.writeBytes(new byte[] {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26});
buffer.writeBytes(new byte[] {31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46});
buffer.writeByte(51);
buffer.writeBytes(new byte[] {52,53,54,55,56,57,58});
buffer.writeByte(61);
buffer.writeByte(62);
buffer.writeByte(63);
buffer.writeByte(64);
buffer.writeByte(65);
assertArrayEquals(new byte[] {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,31,32,33,34,35,36,37,38,39,40,41,42,
43,44,45,46,51,52,53,54,55,56,57,58,61,62,63,64,65}, buffer.toBytes());
}
@Test
public void testStrings() {
runStringTest(new ByteBuffer2(4096));
runStringTest(new ByteBuffer2(897));
ByteBuffer2 write = new ByteBuffer2(21);
String value = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234";
write.writeString(value);
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
assertArrayEquals(value, read.readString());
runStringTest(127);
runStringTest(256);
runStringTest(1024 * 1023);
runStringTest(1024 * 1024);
runStringTest(1024 * 1025);
runStringTest(1024 * 1026);
runStringTest(1024 * 1024 * 2);
}
public void runStringTest(ByteBuffer2 write) {
String value1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\rabcdefghijklmnopqrstuvwxyz\n1234567890\t\"!`?'.,;:()[]{}<>|/@\\^$-%+=#_&~*";
String value2 = "abcdef\u00E1\u00E9\u00ED\u00F3\u00FA\u1234";
write.writeString("");
write.writeString("1");
write.writeString("22");
write.writeString("uno");
write.writeString("dos");
write.writeString("tres");
write.writeString(null);
write.writeString(value1);
write.writeString(value2);
for (int i = 0; i < 127; i++) {
write.writeString(String.valueOf((char) i));
}
for (int i = 0; i < 127; i++) {
write.writeString(String.valueOf((char) i) + "abc");
}
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals("", read.readString());
Assert.assertEquals("1", read.readString());
Assert.assertEquals("22", read.readString());
Assert.assertEquals("uno", read.readString());
Assert.assertEquals("dos", read.readString());
Assert.assertEquals("tres", read.readString());
Assert.assertEquals(null, read.readString());
Assert.assertEquals(value1, read.readString());
Assert.assertEquals(value2, read.readString());
for (int i = 0; i < 127; i++) {
Assert.assertEquals(String.valueOf((char) i), read.readString());
}
for (int i = 0; i < 127; i++) {
Assert.assertEquals(String.valueOf((char) i) + "abc", read.readString());
}
read.rewind();
Assert.assertEquals("", read.readStringBuilder().toString());
Assert.assertEquals("1", read.readStringBuilder().toString());
Assert.assertEquals("22", read.readStringBuilder().toString());
Assert.assertEquals("uno", read.readStringBuilder().toString());
Assert.assertEquals("dos", read.readStringBuilder().toString());
Assert.assertEquals("tres", read.readStringBuilder().toString());
Assert.assertEquals(null, read.readStringBuilder());
Assert.assertEquals(value1, read.readStringBuilder().toString());
Assert.assertEquals(value2, read.readStringBuilder().toString());
for (int i = 0; i < 127; i++) {
Assert.assertEquals(String.valueOf((char) i), read.readStringBuilder().toString());
}
for (int i = 0; i < 127; i++) {
Assert.assertEquals(String.valueOf((char) i) + "abc", read.readStringBuilder().toString());
}
}
public void runStringTest(int length) {
ByteBuffer2 write = new ByteBuffer2(1024, -1);
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < length; i++) {
buffer.append((char) i);
}
String value = buffer.toString();
write.writeString(value);
write.writeString(value);
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(value, read.readString());
Assert.assertEquals(value, read.readStringBuilder().toString());
write.clear();
write.writeString(buffer);
write.writeString(buffer);
read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(value, read.readStringBuilder().toString());
Assert.assertEquals(value, read.readString());
if (length <= 127) {
write.clear();
write.writeAscii(value);
write.writeAscii(value);
read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(value, read.readStringBuilder().toString());
Assert.assertEquals(value, read.readString());
}
}
@Test
public void testCanReadInt() {
ByteBuffer2 write = new ByteBuffer2();
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(false, read.canReadInt());
write = new ByteBuffer2(4);
write.writeInt(400, true);
read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(true, read.canReadInt());
read.setPosition(read.capacity());
Assert.assertEquals(false, read.canReadInt());
}
@Test
public void testInts() {
runIntTest(new ByteBuffer2(4096));
}
private void runIntTest(ByteBuffer2 write) {
write.writeInt(0);
write.writeInt(63);
write.writeInt(64);
write.writeInt(127);
write.writeInt(128);
write.writeInt(8192);
write.writeInt(16384);
write.writeInt(2097151);
write.writeInt(1048575);
write.writeInt(134217727);
write.writeInt(268435455);
write.writeInt(134217728);
write.writeInt(268435456);
write.writeInt(-2097151);
write.writeInt(-1048575);
write.writeInt(-134217727);
write.writeInt(-268435455);
write.writeInt(-134217728);
write.writeInt(-268435456);
Assert.assertEquals(1, write.writeInt(0, true));
Assert.assertEquals(1, write.writeInt(0, false));
Assert.assertEquals(1, write.writeInt(63, true));
Assert.assertEquals(1, write.writeInt(63, false));
Assert.assertEquals(1, write.writeInt(64, true));
Assert.assertEquals(2, write.writeInt(64, false));
Assert.assertEquals(1, write.writeInt(127, true));
Assert.assertEquals(2, write.writeInt(127, false));
Assert.assertEquals(2, write.writeInt(128, true));
Assert.assertEquals(2, write.writeInt(128, false));
Assert.assertEquals(2, write.writeInt(8191, true));
Assert.assertEquals(2, write.writeInt(8191, false));
Assert.assertEquals(2, write.writeInt(8192, true));
Assert.assertEquals(3, write.writeInt(8192, false));
Assert.assertEquals(2, write.writeInt(16383, true));
Assert.assertEquals(3, write.writeInt(16383, false));
Assert.assertEquals(3, write.writeInt(16384, true));
Assert.assertEquals(3, write.writeInt(16384, false));
Assert.assertEquals(3, write.writeInt(2097151, true));
Assert.assertEquals(4, write.writeInt(2097151, false));
Assert.assertEquals(3, write.writeInt(1048575, true));
Assert.assertEquals(3, write.writeInt(1048575, false));
Assert.assertEquals(4, write.writeInt(134217727, true));
Assert.assertEquals(4, write.writeInt(134217727, false));
Assert.assertEquals(4, write.writeInt(268435455, true));
Assert.assertEquals(5, write.writeInt(268435455, false));
Assert.assertEquals(4, write.writeInt(134217728, true));
Assert.assertEquals(5, write.writeInt(134217728, false));
Assert.assertEquals(5, write.writeInt(268435456, true));
Assert.assertEquals(5, write.writeInt(268435456, false));
Assert.assertEquals(1, write.writeInt(-64, false));
Assert.assertEquals(5, write.writeInt(-64, true));
Assert.assertEquals(2, write.writeInt(-65, false));
Assert.assertEquals(5, write.writeInt(-65, true));
Assert.assertEquals(2, write.writeInt(-8192, false));
Assert.assertEquals(5, write.writeInt(-8192, true));
Assert.assertEquals(3, write.writeInt(-1048576, false));
Assert.assertEquals(5, write.writeInt(-1048576, true));
Assert.assertEquals(4, write.writeInt(-134217728, false));
Assert.assertEquals(5, write.writeInt(-134217728, true));
Assert.assertEquals(5, write.writeInt(-134217729, false));
Assert.assertEquals(5, write.writeInt(-134217729, true));
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(0, read.readInt());
Assert.assertEquals(63, read.readInt());
Assert.assertEquals(64, read.readInt());
Assert.assertEquals(127, read.readInt());
Assert.assertEquals(128, read.readInt());
Assert.assertEquals(8192, read.readInt());
Assert.assertEquals(16384, read.readInt());
Assert.assertEquals(2097151, read.readInt());
Assert.assertEquals(1048575, read.readInt());
Assert.assertEquals(134217727, read.readInt());
Assert.assertEquals(268435455, read.readInt());
Assert.assertEquals(134217728, read.readInt());
Assert.assertEquals(268435456, read.readInt());
Assert.assertEquals(-2097151, read.readInt());
Assert.assertEquals(-1048575, read.readInt());
Assert.assertEquals(-134217727, read.readInt());
Assert.assertEquals(-268435455, read.readInt());
Assert.assertEquals(-134217728, read.readInt());
Assert.assertEquals(-268435456, read.readInt());
Assert.assertEquals(true, read.canReadInt());
Assert.assertEquals(true, read.canReadInt());
Assert.assertEquals(true, read.canReadInt());
Assert.assertEquals(0, read.readInt(true));
Assert.assertEquals(0, read.readInt(false));
Assert.assertEquals(63, read.readInt(true));
Assert.assertEquals(63, read.readInt(false));
Assert.assertEquals(64, read.readInt(true));
Assert.assertEquals(64, read.readInt(false));
Assert.assertEquals(127, read.readInt(true));
Assert.assertEquals(127, read.readInt(false));
Assert.assertEquals(128, read.readInt(true));
Assert.assertEquals(128, read.readInt(false));
Assert.assertEquals(8191, read.readInt(true));
Assert.assertEquals(8191, read.readInt(false));
Assert.assertEquals(8192, read.readInt(true));
Assert.assertEquals(8192, read.readInt(false));
Assert.assertEquals(16383, read.readInt(true));
Assert.assertEquals(16383, read.readInt(false));
Assert.assertEquals(16384, read.readInt(true));
Assert.assertEquals(16384, read.readInt(false));
Assert.assertEquals(2097151, read.readInt(true));
Assert.assertEquals(2097151, read.readInt(false));
Assert.assertEquals(1048575, read.readInt(true));
Assert.assertEquals(1048575, read.readInt(false));
Assert.assertEquals(134217727, read.readInt(true));
Assert.assertEquals(134217727, read.readInt(false));
Assert.assertEquals(268435455, read.readInt(true));
Assert.assertEquals(268435455, read.readInt(false));
Assert.assertEquals(134217728, read.readInt(true));
Assert.assertEquals(134217728, read.readInt(false));
Assert.assertEquals(268435456, read.readInt(true));
Assert.assertEquals(268435456, read.readInt(false));
Assert.assertEquals(-64, read.readInt(false));
Assert.assertEquals(-64, read.readInt(true));
Assert.assertEquals(-65, read.readInt(false));
Assert.assertEquals(-65, read.readInt(true));
Assert.assertEquals(-8192, read.readInt(false));
Assert.assertEquals(-8192, read.readInt(true));
Assert.assertEquals(-1048576, read.readInt(false));
Assert.assertEquals(-1048576, read.readInt(true));
Assert.assertEquals(-134217728, read.readInt(false));
Assert.assertEquals(-134217728, read.readInt(true));
Assert.assertEquals(-134217729, read.readInt(false));
Assert.assertEquals(-134217729, read.readInt(true));
Assert.assertEquals(false, read.canReadInt());
Random random = new Random();
for (int i = 0; i < 10000; i++) {
int value = random.nextInt();
write.clear();
write.writeInt(value);
write.writeInt(value, true);
write.writeInt(value, false);
read.setBuffer(write.toBytes());
Assert.assertEquals(value, read.readInt());
Assert.assertEquals(value, read.readInt(true));
Assert.assertEquals(value, read.readInt(false));
}
}
@Test
public void testLongs() {
runLongTest(new ByteBuffer2(4096));
}
private void runLongTest(ByteBuffer2 write) {
write.writeLong(0);
write.writeLong(63);
write.writeLong(64);
write.writeLong(127);
write.writeLong(128);
write.writeLong(8192);
write.writeLong(16384);
write.writeLong(2097151);
write.writeLong(1048575);
write.writeLong(134217727);
write.writeLong(268435455);
write.writeLong(134217728);
write.writeLong(268435456);
write.writeLong(-2097151);
write.writeLong(-1048575);
write.writeLong(-134217727);
write.writeLong(-268435455);
write.writeLong(-134217728);
write.writeLong(-268435456);
Assert.assertEquals(1, write.writeLong(0, true));
Assert.assertEquals(1, write.writeLong(0, false));
Assert.assertEquals(1, write.writeLong(63, true));
Assert.assertEquals(1, write.writeLong(63, false));
Assert.assertEquals(1, write.writeLong(64, true));
Assert.assertEquals(2, write.writeLong(64, false));
Assert.assertEquals(1, write.writeLong(127, true));
Assert.assertEquals(2, write.writeLong(127, false));
Assert.assertEquals(2, write.writeLong(128, true));
Assert.assertEquals(2, write.writeLong(128, false));
Assert.assertEquals(2, write.writeLong(8191, true));
Assert.assertEquals(2, write.writeLong(8191, false));
Assert.assertEquals(2, write.writeLong(8192, true));
Assert.assertEquals(3, write.writeLong(8192, false));
Assert.assertEquals(2, write.writeLong(16383, true));
Assert.assertEquals(3, write.writeLong(16383, false));
Assert.assertEquals(3, write.writeLong(16384, true));
Assert.assertEquals(3, write.writeLong(16384, false));
Assert.assertEquals(3, write.writeLong(2097151, true));
Assert.assertEquals(4, write.writeLong(2097151, false));
Assert.assertEquals(3, write.writeLong(1048575, true));
Assert.assertEquals(3, write.writeLong(1048575, false));
Assert.assertEquals(4, write.writeLong(134217727, true));
Assert.assertEquals(4, write.writeLong(134217727, false));
Assert.assertEquals(4, write.writeLong(268435455l, true));
Assert.assertEquals(5, write.writeLong(268435455l, false));
Assert.assertEquals(4, write.writeLong(134217728l, true));
Assert.assertEquals(5, write.writeLong(134217728l, false));
Assert.assertEquals(5, write.writeLong(268435456l, true));
Assert.assertEquals(5, write.writeLong(268435456l, false));
Assert.assertEquals(1, write.writeLong(-64, false));
Assert.assertEquals(9, write.writeLong(-64, true));
Assert.assertEquals(2, write.writeLong(-65, false));
Assert.assertEquals(9, write.writeLong(-65, true));
Assert.assertEquals(2, write.writeLong(-8192, false));
Assert.assertEquals(9, write.writeLong(-8192, true));
Assert.assertEquals(3, write.writeLong(-1048576, false));
Assert.assertEquals(9, write.writeLong(-1048576, true));
Assert.assertEquals(4, write.writeLong(-134217728, false));
Assert.assertEquals(9, write.writeLong(-134217728, true));
Assert.assertEquals(5, write.writeLong(-134217729, false));
Assert.assertEquals(9, write.writeLong(-134217729, true));
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(0, read.readLong());
Assert.assertEquals(63, read.readLong());
Assert.assertEquals(64, read.readLong());
Assert.assertEquals(127, read.readLong());
Assert.assertEquals(128, read.readLong());
Assert.assertEquals(8192, read.readLong());
Assert.assertEquals(16384, read.readLong());
Assert.assertEquals(2097151, read.readLong());
Assert.assertEquals(1048575, read.readLong());
Assert.assertEquals(134217727, read.readLong());
Assert.assertEquals(268435455, read.readLong());
Assert.assertEquals(134217728, read.readLong());
Assert.assertEquals(268435456, read.readLong());
Assert.assertEquals(-2097151, read.readLong());
Assert.assertEquals(-1048575, read.readLong());
Assert.assertEquals(-134217727, read.readLong());
Assert.assertEquals(-268435455, read.readLong());
Assert.assertEquals(-134217728, read.readLong());
Assert.assertEquals(-268435456, read.readLong());
Assert.assertEquals(0, read.readLong(true));
Assert.assertEquals(0, read.readLong(false));
Assert.assertEquals(63, read.readLong(true));
Assert.assertEquals(63, read.readLong(false));
Assert.assertEquals(64, read.readLong(true));
Assert.assertEquals(64, read.readLong(false));
Assert.assertEquals(127, read.readLong(true));
Assert.assertEquals(127, read.readLong(false));
Assert.assertEquals(128, read.readLong(true));
Assert.assertEquals(128, read.readLong(false));
Assert.assertEquals(8191, read.readLong(true));
Assert.assertEquals(8191, read.readLong(false));
Assert.assertEquals(8192, read.readLong(true));
Assert.assertEquals(8192, read.readLong(false));
Assert.assertEquals(16383, read.readLong(true));
Assert.assertEquals(16383, read.readLong(false));
Assert.assertEquals(16384, read.readLong(true));
Assert.assertEquals(16384, read.readLong(false));
Assert.assertEquals(2097151, read.readLong(true));
Assert.assertEquals(2097151, read.readLong(false));
Assert.assertEquals(1048575, read.readLong(true));
Assert.assertEquals(1048575, read.readLong(false));
Assert.assertEquals(134217727, read.readLong(true));
Assert.assertEquals(134217727, read.readLong(false));
Assert.assertEquals(268435455, read.readLong(true));
Assert.assertEquals(268435455, read.readLong(false));
Assert.assertEquals(134217728, read.readLong(true));
Assert.assertEquals(134217728, read.readLong(false));
Assert.assertEquals(268435456, read.readLong(true));
Assert.assertEquals(268435456, read.readLong(false));
Assert.assertEquals(-64, read.readLong(false));
Assert.assertEquals(-64, read.readLong(true));
Assert.assertEquals(-65, read.readLong(false));
Assert.assertEquals(-65, read.readLong(true));
Assert.assertEquals(-8192, read.readLong(false));
Assert.assertEquals(-8192, read.readLong(true));
Assert.assertEquals(-1048576, read.readLong(false));
Assert.assertEquals(-1048576, read.readLong(true));
Assert.assertEquals(-134217728, read.readLong(false));
Assert.assertEquals(-134217728, read.readLong(true));
Assert.assertEquals(-134217729, read.readLong(false));
Assert.assertEquals(-134217729, read.readLong(true));
Random random = new Random();
for (int i = 0; i < 10000; i++) {
long value = random.nextLong();
write.clear();
write.writeLong(value);
write.writeLong(value, true);
write.writeLong(value, false);
read.setBuffer(write.toBytes());
Assert.assertEquals(value, read.readLong());
Assert.assertEquals(value, read.readLong(true));
Assert.assertEquals(value, read.readLong(false));
}
}
@Test
public void testShorts() {
runShortTest(new ByteBuffer2(4096));
}
private void runShortTest(ByteBuffer2 write) {
write.writeShort(0);
write.writeShort(63);
write.writeShort(64);
write.writeShort(127);
write.writeShort(128);
write.writeShort(8192);
write.writeShort(16384);
write.writeShort(32767);
write.writeShort(-63);
write.writeShort(-64);
write.writeShort(-127);
write.writeShort(-128);
write.writeShort(-8192);
write.writeShort(-16384);
write.writeShort(-32768);
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(0, read.readShort());
Assert.assertEquals(63, read.readShort());
Assert.assertEquals(64, read.readShort());
Assert.assertEquals(127, read.readShort());
Assert.assertEquals(128, read.readShort());
Assert.assertEquals(8192, read.readShort());
Assert.assertEquals(16384, read.readShort());
Assert.assertEquals(32767, read.readShort());
Assert.assertEquals(-63, read.readShort());
Assert.assertEquals(-64, read.readShort());
Assert.assertEquals(-127, read.readShort());
Assert.assertEquals(-128, read.readShort());
Assert.assertEquals(-8192, read.readShort());
Assert.assertEquals(-16384, read.readShort());
Assert.assertEquals(-32768, read.readShort());
}
@Test
public void testFloats() {
runFloatTest(new ByteBuffer2(4096));
}
private void runFloatTest(ByteBuffer2 write) {
write.writeFloat(0);
write.writeFloat(63);
write.writeFloat(64);
write.writeFloat(127);
write.writeFloat(128);
write.writeFloat(8192);
write.writeFloat(16384);
write.writeFloat(32767);
write.writeFloat(-63);
write.writeFloat(-64);
write.writeFloat(-127);
write.writeFloat(-128);
write.writeFloat(-8192);
write.writeFloat(-16384);
write.writeFloat(-32768);
Assert.assertEquals(1, write.writeFloat(0, 1000, true));
Assert.assertEquals(1, write.writeFloat(0, 1000, false));
Assert.assertEquals(3, write.writeFloat(63, 1000, true));
Assert.assertEquals(3, write.writeFloat(63, 1000, false));
Assert.assertEquals(3, write.writeFloat(64, 1000, true));
Assert.assertEquals(3, write.writeFloat(64, 1000, false));
Assert.assertEquals(3, write.writeFloat(127, 1000, true));
Assert.assertEquals(3, write.writeFloat(127, 1000, false));
Assert.assertEquals(3, write.writeFloat(128, 1000, true));
Assert.assertEquals(3, write.writeFloat(128, 1000, false));
Assert.assertEquals(4, write.writeFloat(8191, 1000, true));
Assert.assertEquals(4, write.writeFloat(8191, 1000, false));
Assert.assertEquals(4, write.writeFloat(8192, 1000, true));
Assert.assertEquals(4, write.writeFloat(8192, 1000, false));
Assert.assertEquals(4, write.writeFloat(16383, 1000, true));
Assert.assertEquals(4, write.writeFloat(16383, 1000, false));
Assert.assertEquals(4, write.writeFloat(16384, 1000, true));
Assert.assertEquals(4, write.writeFloat(16384, 1000, false));
Assert.assertEquals(4, write.writeFloat(32767, 1000, true));
Assert.assertEquals(4, write.writeFloat(32767, 1000, false));
Assert.assertEquals(3, write.writeFloat(-64, 1000, false));
Assert.assertEquals(5, write.writeFloat(-64, 1000, true));
Assert.assertEquals(3, write.writeFloat(-65, 1000, false));
Assert.assertEquals(5, write.writeFloat(-65, 1000, true));
Assert.assertEquals(4, write.writeFloat(-8192, 1000, false));
Assert.assertEquals(5, write.writeFloat(-8192, 1000, true));
float delta = 0.00000001f;
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(read.readFloat(), 0f, delta);
Assert.assertEquals(read.readFloat(), 63f, delta);
Assert.assertEquals(read.readFloat(), 64f, delta);
Assert.assertEquals(read.readFloat(), 127f, delta);
Assert.assertEquals(read.readFloat(), 128f, delta);
Assert.assertEquals(read.readFloat(), 8192f, delta);
Assert.assertEquals(read.readFloat(), 16384f, delta);
Assert.assertEquals(read.readFloat(), 32767f, delta);
Assert.assertEquals(read.readFloat(), -63f, delta);
Assert.assertEquals(read.readFloat(), -64f, delta);
Assert.assertEquals(read.readFloat(), -127f, delta);
Assert.assertEquals(read.readFloat(), -128f, delta);
Assert.assertEquals(read.readFloat(), -8192f, delta);
Assert.assertEquals(read.readFloat(), -16384f, delta);
Assert.assertEquals(read.readFloat(), -32768f, delta);
Assert.assertEquals(read.readFloat(1000, true), 0f, delta);
Assert.assertEquals(read.readFloat(1000, false), 0f, delta);
Assert.assertEquals(read.readFloat(1000, true), 63f, delta);
Assert.assertEquals(read.readFloat(1000, false), 63f, delta);
Assert.assertEquals(read.readFloat(1000, true), 64f, delta);
Assert.assertEquals(read.readFloat(1000, false), 64f, delta);
Assert.assertEquals(read.readFloat(1000, true), 127f, delta);
Assert.assertEquals(read.readFloat(1000, false), 127f, delta);
Assert.assertEquals(read.readFloat(1000, true), 128f, delta);
Assert.assertEquals(read.readFloat(1000, false), 128f, delta);
Assert.assertEquals(read.readFloat(1000, true), 8191f, delta);
Assert.assertEquals(read.readFloat(1000, false), 8191f, delta);
Assert.assertEquals(read.readFloat(1000, true), 8192f, delta);
Assert.assertEquals(read.readFloat(1000, false), 8192f, delta);
Assert.assertEquals(read.readFloat(1000, true), 16383f, delta);
Assert.assertEquals(read.readFloat(1000, false), 16383f, delta);
Assert.assertEquals(read.readFloat(1000, true), 16384f, delta);
Assert.assertEquals(read.readFloat(1000, false), 16384f, delta);
Assert.assertEquals(read.readFloat(1000, true), 32767f, delta);
Assert.assertEquals(read.readFloat(1000, false), 32767f, delta);
Assert.assertEquals(read.readFloat(1000, false), -64f, delta);
Assert.assertEquals(read.readFloat(1000, true), -64f, delta);
Assert.assertEquals(read.readFloat(1000, false), -65f, delta);
Assert.assertEquals(read.readFloat(1000, true), -65f, delta);
Assert.assertEquals(read.readFloat(1000, false), -8192f, delta);
Assert.assertEquals(read.readFloat(1000, true), -8192f, delta);
}
@Test
public void testDoubles() {
runDoubleTest(new ByteBuffer2(4096));
}
private void runDoubleTest(ByteBuffer2 write) {
write.writeDouble(0);
write.writeDouble(63);
write.writeDouble(64);
write.writeDouble(127);
write.writeDouble(128);
write.writeDouble(8192);
write.writeDouble(16384);
write.writeDouble(32767);
write.writeDouble(-63);
write.writeDouble(-64);
write.writeDouble(-127);
write.writeDouble(-128);
write.writeDouble(-8192);
write.writeDouble(-16384);
write.writeDouble(-32768);
Assert.assertEquals(1, write.writeDouble(0, 1000, true));
Assert.assertEquals(1, write.writeDouble(0, 1000, false));
Assert.assertEquals(3, write.writeDouble(63, 1000, true));
Assert.assertEquals(3, write.writeDouble(63, 1000, false));
Assert.assertEquals(3, write.writeDouble(64, 1000, true));
Assert.assertEquals(3, write.writeDouble(64, 1000, false));
Assert.assertEquals(3, write.writeDouble(127, 1000, true));
Assert.assertEquals(3, write.writeDouble(127, 1000, false));
Assert.assertEquals(3, write.writeDouble(128, 1000, true));
Assert.assertEquals(3, write.writeDouble(128, 1000, false));
Assert.assertEquals(4, write.writeDouble(8191, 1000, true));
Assert.assertEquals(4, write.writeDouble(8191, 1000, false));
Assert.assertEquals(4, write.writeDouble(8192, 1000, true));
Assert.assertEquals(4, write.writeDouble(8192, 1000, false));
Assert.assertEquals(4, write.writeDouble(16383, 1000, true));
Assert.assertEquals(4, write.writeDouble(16383, 1000, false));
Assert.assertEquals(4, write.writeDouble(16384, 1000, true));
Assert.assertEquals(4, write.writeDouble(16384, 1000, false));
Assert.assertEquals(4, write.writeDouble(32767, 1000, true));
Assert.assertEquals(4, write.writeDouble(32767, 1000, false));
Assert.assertEquals(3, write.writeDouble(-64, 1000, false));
Assert.assertEquals(9, write.writeDouble(-64, 1000, true));
Assert.assertEquals(3, write.writeDouble(-65, 1000, false));
Assert.assertEquals(9, write.writeDouble(-65, 1000, true));
Assert.assertEquals(4, write.writeDouble(-8192, 1000, false));
Assert.assertEquals(9, write.writeDouble(-8192, 1000, true));
write.writeDouble(1.23456d);
double delta = 0.00000001D;
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(read.readDouble(), 0d, delta);
Assert.assertEquals(read.readDouble(), 63d, delta);
Assert.assertEquals(read.readDouble(), 64d, delta);
Assert.assertEquals(read.readDouble(), 127d, delta);
Assert.assertEquals(read.readDouble(), 128d, delta);
Assert.assertEquals(read.readDouble(), 8192d, delta);
Assert.assertEquals(read.readDouble(), 16384d, delta);
Assert.assertEquals(read.readDouble(), 32767d, delta);
Assert.assertEquals(read.readDouble(), -63d, delta);
Assert.assertEquals(read.readDouble(), -64d, delta);
Assert.assertEquals(read.readDouble(), -127d, delta);
Assert.assertEquals(read.readDouble(), -128d, delta);
Assert.assertEquals(read.readDouble(), -8192d, delta);
Assert.assertEquals(read.readDouble(), -16384d, delta);
Assert.assertEquals(read.readDouble(), -32768d, delta);
Assert.assertEquals(read.readDouble(1000, true), 0d, delta);
Assert.assertEquals(read.readDouble(1000, false), 0d, delta);
Assert.assertEquals(read.readDouble(1000, true), 63d, delta);
Assert.assertEquals(read.readDouble(1000, false), 63d, delta);
Assert.assertEquals(read.readDouble(1000, true), 64d, delta);
Assert.assertEquals(read.readDouble(1000, false), 64d, delta);
Assert.assertEquals(read.readDouble(1000, true), 127d, delta);
Assert.assertEquals(read.readDouble(1000, false), 127d, delta);
Assert.assertEquals(read.readDouble(1000, true), 128d, delta);
Assert.assertEquals(read.readDouble(1000, false), 128d, delta);
Assert.assertEquals(read.readDouble(1000, true), 8191d, delta);
Assert.assertEquals(read.readDouble(1000, false), 8191d, delta);
Assert.assertEquals(read.readDouble(1000, true), 8192d, delta);
Assert.assertEquals(read.readDouble(1000, false), 8192d, delta);
Assert.assertEquals(read.readDouble(1000, true), 16383d, delta);
Assert.assertEquals(read.readDouble(1000, false), 16383d, delta);
Assert.assertEquals(read.readDouble(1000, true), 16384d, delta);
Assert.assertEquals(read.readDouble(1000, false), 16384d, delta);
Assert.assertEquals(read.readDouble(1000, true), 32767d, delta);
Assert.assertEquals(read.readDouble(1000, false), 32767d, delta);
Assert.assertEquals(read.readDouble(1000, false), -64d, delta);
Assert.assertEquals(read.readDouble(1000, true), -64d, delta);
Assert.assertEquals(read.readDouble(1000, false), -65d, delta);
Assert.assertEquals(read.readDouble(1000, true), -65d, delta);
Assert.assertEquals(read.readDouble(1000, false), -8192d, delta);
Assert.assertEquals(read.readDouble(1000, true), -8192d, delta);
Assert.assertEquals(1.23456d, read.readDouble(), delta);
}
@Test
public void testBooleans() {
runBooleanTest(new ByteBuffer2(4096));
}
private void runBooleanTest(ByteBuffer2 write) {
for (int i = 0; i < 100; i++) {
write.writeBoolean(true);
write.writeBoolean(false);
}
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
for (int i = 0; i < 100; i++) {
Assert.assertEquals(true, read.readBoolean());
Assert.assertEquals(false, read.readBoolean());
}
}
@Test
public void testChars() {
runCharTest(new ByteBuffer2(4096));
}
private void runCharTest(ByteBuffer2 write) {
write.writeChar((char) 0);
write.writeChar((char) 63);
write.writeChar((char) 64);
write.writeChar((char) 127);
write.writeChar((char) 128);
write.writeChar((char) 8192);
write.writeChar((char) 16384);
write.writeChar((char) 32767);
write.writeChar((char) 65535);
ByteBuffer2 read = new ByteBuffer2(write.toBytes());
Assert.assertEquals(0, read.readChar());
Assert.assertEquals(63, read.readChar());
Assert.assertEquals(64, read.readChar());
Assert.assertEquals(127, read.readChar());
Assert.assertEquals(128, read.readChar());
Assert.assertEquals(8192, read.readChar());
Assert.assertEquals(16384, read.readChar());
Assert.assertEquals(32767, read.readChar());
Assert.assertEquals(65535, read.readChar());
}
@Test
public void testInputWithOffset() throws Exception {
final byte[] buf = new byte[30];
final ByteBuffer2 in = new ByteBuffer2(buf);
in.skip(20);
Assert.assertEquals(10, in.remaining());
}
@Test
public void testSmallBuffers() throws Exception {
ByteBuffer buf = ByteBuffer.allocate(1024);
ByteBuffer2 testOutput = new ByteBuffer2(buf.array());
testOutput.writeBytes(new byte[512]);
testOutput.writeBytes(new byte[512]);
ByteBuffer2 testInputs = new ByteBuffer2();
buf.flip();
testInputs.setBuffer(buf.array());
byte[] toRead = new byte[512];
testInputs.readBytes(toRead);
testInputs.readBytes(toRead);
}
}

View File

@ -1,708 +0,0 @@
/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import dorkbox.util.storage.Storage;
import dorkbox.util.storage.StorageKey;
import dorkbox.util.storage.StorageSystem;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public
class StorageTest {
static int total = 10;
// the initial size is specified during disk.storage construction, and is based on the number of padded records.
private static final long initialSize = 1024L;
// this is the size for each record (determined by looking at the output when writing the file)
private static final int sizePerRecord = 23;
private static final File TEST_DB = new File("sampleFile.records");
static
void log(String s) {
System.err.println(s);
}
@Before
public
void deleteDB() {
StorageSystem.delete(TEST_DB);
}
@After
public
void delete2DB() {
StorageSystem.delete(TEST_DB);
}
@Test
public
void testCreateDB() throws IOException {
StorageSystem.shutdown();
StorageSystem.delete(TEST_DB);
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
int numberOfRecords1 = storage.size();
long size1 = storage.getFileSize();
Assert.assertEquals("count is not correct", 0, numberOfRecords1);
Assert.assertEquals("size is not correct", initialSize, size1);
storage.close();
storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
int numberOfRecords2 = storage.size();
long size2 = storage.getFileSize();
Assert.assertEquals("Record count is not the same", numberOfRecords1, numberOfRecords2);
Assert.assertEquals("size is not the same", size1, size2);
StorageSystem.close(storage);
}
@Test
public
void testAddAsOne() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
for (int i = 0; i < total; i++) {
add(storage, i);
}
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
for (int i = 0; i < total; i++) {
String record1Data = createData(i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", record1Data, readRecord);
}
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
/**
* Adds data to storage using the SAME key each time (so each entry is overwritten).
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public
void testAddNoKeyRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
StorageKey storageKey = new StorageKey("foobar!");
for (int i = 0; i < total; i++) {
log("adding record " + i + "...");
String addRecord = createData(i);
storage.put(storageKey, addRecord);
log("reading record " + i + "...");
String readData = storage.get(storageKey);
Assert.assertEquals("Object is not the same", addRecord, readData);
}
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
String dataCheck = createData(total - 1);
log("reading record " + (total - 1) + "...");
String readData = storage.get(storageKey);
// the ONLY entry in storage should be the last one that we added
Assert.assertEquals("Object is not the same", dataCheck, readData);
int numberOfRecords1 = storage.size();
long size1 = storage.getFileSize();
Assert.assertEquals("count is not correct", numberOfRecords1, 1);
Assert.assertEquals("size is not correct", size1, initialSize + sizePerRecord);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
@Test
public
void testAddRecords_DelaySaveA() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
for (int i = 0; i < total; i++) {
add(storage, i);
}
synchronized (Thread.currentThread()) {
Thread.currentThread()
.wait(storage.getSaveDelay() + 1000L);
}
for (int i = 0; i < total; i++) {
String record1Data = createData(i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", record1Data, readRecord);
}
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
for (int i = 0; i < total; i++) {
String dataCheck = createData(i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
}
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
@Test
public
void testAddRecords_DelaySaveB() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
for (int i = 0; i < total; i++) {
add(storage, i);
}
for (int i = 0; i < total; i++) {
String record1Data = createData(i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", record1Data, readRecord);
}
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.build();
for (int i = 0; i < total; i++) {
String dataCheck = createData(i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
}
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
@Test
public
void testLoadRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
for (int i = 0; i < total; i++) {
String addRecord = add(storage, i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", addRecord, readRecord);
}
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
for (int i = 0; i < total; i++) {
String dataCheck = createData(i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
}
// now test loading data
Data data = new Data();
StorageKey createKey = createKey(63);
makeData(data);
storage.put(createKey, data);
Data data2;
data2 = storage.get(createKey, new Data());
Assert.assertEquals("Object is not the same", data, data2);
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
data2 = storage.get(createKey, new Data());
Assert.assertEquals("Object is not the same", data, data2);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
@Test
public
void testAddRecordsDelete1Record() throws IOException, ClassNotFoundException {
if (total < 4) {
throw new IOException("Unable to run test with too few entries.");
}
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
for (int i = 0; i < total; i++) {
String addRecord = add(storage, i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", addRecord, readRecord);
}
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
for (int i = 0; i < total; i++) {
String dataCheck = createData(i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
}
// make sure now that we can delete one of the records.
deleteRecord(storage, 3);
String readRecord = readRecord(storage, 9);
String dataCheck = createData(9);
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
if (storage.contains(createKey(3))) {
Assert.fail("record NOT successfully deleted.");
}
// now we add 3 back
String addRecord = add(storage, 3);
dataCheck = createData(3);
Assert.assertEquals("Object is not the same", dataCheck, addRecord);
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
// check 9 again
readRecord = readRecord(storage, 9);
dataCheck = createData(9);
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
// check 3 again
readRecord = readRecord(storage, 3);
dataCheck = createData(3);
Assert.assertEquals("Object is not the same", dataCheck, readRecord);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
@Test
public
void testUpdateRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
for (int i = 0; i < total; i++) {
String addRecord = add(storage, i);
String readRecord = readRecord(storage, i);
Assert.assertEquals("Object is not the same", addRecord, readRecord);
}
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
String updateRecord = updateRecord(storage, 3, createData(3) + "new");
String readRecord = readRecord(storage, 3);
Assert.assertEquals("Object is not the same", updateRecord, readRecord);
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
readRecord = readRecord(storage, 3);
Assert.assertEquals("Object is not the same", updateRecord, readRecord);
updateRecord = updateRecord(storage, 3, createData(3));
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
readRecord = readRecord(storage, 3);
Assert.assertEquals("Object is not the same", updateRecord, readRecord);
StorageSystem.close(storage);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
updateRecord = updateRecord(storage, 0, createData(0) + "new");
readRecord = readRecord(storage, 0);
Assert.assertEquals("Object is not the same", updateRecord, readRecord);
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
@Test
public
void testSaveAllRecords() throws IOException, ClassNotFoundException {
try {
Storage storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
for (int i = 0; i < total; i++) {
Data data = new Data();
makeData(data);
StorageKey createKey = createKey(i);
storage.put(createKey, data);
}
StorageSystem.close(storage);
Data data = new Data();
makeData(data);
storage = StorageSystem.Disk()
.file(TEST_DB)
.register(StorageTest.Data.class)
.build();
for (int i = 0; i < total; i++) {
StorageKey createKey = createKey(i);
Data data2;
data2 = storage.get(createKey, new Data());
Assert.assertEquals("Object is not the same", data, data2);
}
StorageSystem.close(storage);
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Error!");
}
}
private static
String createData(int number) {
return number + " data for record # " + number;
}
public static
String add(Storage storage, int number) throws IOException {
String record1Data = createData(number);
StorageKey record1Key = createKey(number);
log("adding record " + number + "...");
storage.put(record1Key, record1Data);
return record1Data;
}
public static
String readRecord(Storage storage, int number) throws ClassNotFoundException, IOException {
StorageKey record1Key = createKey(number);
log("reading record " + number + "...");
String readData = storage.get(record1Key);
log("\trecord " + number + " data: '" + readData + "'");
return readData;
}
public static
void deleteRecord(Storage storage, int nNumber) throws ClassNotFoundException, IOException {
StorageKey record1Key = createKey(nNumber);
log("deleting record " + nNumber + "...");
storage.delete(record1Key);
}
private static
String updateRecord(Storage storage, int number, String newData) throws IOException {
StorageKey record1Key = createKey(number);
log("updating record " + number + "...");
storage.put(record1Key, newData);
return newData;
}
private static
StorageKey createKey(int number) {
return new StorageKey("foo" + number);
}
// from kryo unit test.
private static
void makeData(Data data) {
StringBuilder buffer = new StringBuilder(128);
for (int i = 0; i < 3; i++) {
buffer.append('a');
}
data.string = buffer.toString();
data.strings = new String[] {"ab012", "", null, "!@#$", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"};
data.ints = new int[] {-1234567, 1234567, -1, 0, 1, Integer.MAX_VALUE, Integer.MIN_VALUE};
data.shorts = new short[] {(short) -12345, (short) 12345, (short) -1, (short) 0, (short) 1, Short.MAX_VALUE, Short.MIN_VALUE};
data.floats = new float[] {0, -0, 1, -1, 123456, -123456, 0.1f, 0.2f, -0.3f, (float) Math.PI, Float.MAX_VALUE, Float.MIN_VALUE};
data.doubles = new double[] {0, -0, 1, -1, 123456, -123456, 0.1d, 0.2d, -0.3d, Math.PI, Double.MAX_VALUE, Double.MIN_VALUE};
data.longs = new long[] {0, -0, 1, -1, 123456, -123456, 99999999999L, -99999999999L, Long.MAX_VALUE, Long.MIN_VALUE};
data.bytes = new byte[] {(byte) -123, (byte) 123, (byte) -1, (byte) 0, (byte) 1, Byte.MAX_VALUE, Byte.MIN_VALUE};
data.chars = new char[] {32345, 12345, 0, 1, 63, Character.MAX_VALUE, Character.MIN_VALUE};
data.booleans = new boolean[] {true, false};
data.Ints = new Integer[] {-1234567, 1234567, -1, 0, 1, Integer.MAX_VALUE, Integer.MIN_VALUE};
data.Shorts = new Short[] {-12345, 12345, -1, 0, 1, Short.MAX_VALUE, Short.MIN_VALUE};
data.Floats = new Float[] {0.0f, -0.0f, 1.0f, -1.0f, 123456.0f, -123456.0f, 0.1f, 0.2f, -0.3f, (float) Math.PI, Float.MAX_VALUE,
Float.MIN_VALUE};
data.Doubles = new Double[] {0.0d, -0.0d, 1.0d, -1.0d, 123456.0d, -123456.0d, 0.1d, 0.2d, -0.3d, Math.PI, Double.MAX_VALUE, Double.MIN_VALUE};
data.Longs = new Long[] {0L, -0L, 1L, -1L, 123456L, -123456L, 99999999999L, -99999999999L, Long.MAX_VALUE, Long.MIN_VALUE};
data.Bytes = new Byte[] {-123, 123, -1, 0, 1, Byte.MAX_VALUE, Byte.MIN_VALUE};
data.Chars = new Character[] {32345, 12345, 0, 1, 63, Character.MAX_VALUE, Character.MIN_VALUE};
data.Booleans = new Boolean[] {true, false};
}
public static
class Data {
public String string;
public String[] strings;
public int[] ints;
public short[] shorts;
public float[] floats;
public double[] doubles;
public long[] longs;
public byte[] bytes;
public char[] chars;
public boolean[] booleans;
public Integer[] Ints;
public Short[] Shorts;
public Float[] Floats;
public Double[] Doubles;
public Long[] Longs;
public Byte[] Bytes;
public Character[] Chars;
public Boolean[] Booleans;
public
Data() {
}
@Override
public
int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(this.Booleans);
result = prime * result + Arrays.hashCode(this.Bytes);
result = prime * result + Arrays.hashCode(this.Chars);
result = prime * result + Arrays.hashCode(this.Doubles);
result = prime * result + Arrays.hashCode(this.Floats);
result = prime * result + Arrays.hashCode(this.Ints);
result = prime * result + Arrays.hashCode(this.Longs);
result = prime * result + Arrays.hashCode(this.Shorts);
result = prime * result + Arrays.hashCode(this.booleans);
result = prime * result + Arrays.hashCode(this.bytes);
result = prime * result + Arrays.hashCode(this.chars);
result = prime * result + Arrays.hashCode(this.doubles);
result = prime * result + Arrays.hashCode(this.floats);
result = prime * result + Arrays.hashCode(this.ints);
result = prime * result + Arrays.hashCode(this.longs);
result = prime * result + Arrays.hashCode(this.shorts);
result = prime * result + (this.string == null ? 0 : this.string.hashCode());
result = prime * result + Arrays.hashCode(this.strings);
return result;
}
@Override
public
boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Data other = (Data) obj;
if (!Arrays.equals(this.Booleans, other.Booleans)) {
return false;
}
if (!Arrays.equals(this.Bytes, other.Bytes)) {
return false;
}
if (!Arrays.equals(this.Chars, other.Chars)) {
return false;
}
if (!Arrays.equals(this.Doubles, other.Doubles)) {
return false;
}
if (!Arrays.equals(this.Floats, other.Floats)) {
return false;
}
if (!Arrays.equals(this.Ints, other.Ints)) {
return false;
}
if (!Arrays.equals(this.Longs, other.Longs)) {
return false;
}
if (!Arrays.equals(this.Shorts, other.Shorts)) {
return false;
}
if (!Arrays.equals(this.booleans, other.booleans)) {
return false;
}
if (!Arrays.equals(this.bytes, other.bytes)) {
return false;
}
if (!Arrays.equals(this.chars, other.chars)) {
return false;
}
if (!Arrays.equals(this.doubles, other.doubles)) {
return false;
}
if (!Arrays.equals(this.floats, other.floats)) {
return false;
}
if (!Arrays.equals(this.ints, other.ints)) {
return false;
}
if (!Arrays.equals(this.longs, other.longs)) {
return false;
}
if (!Arrays.equals(this.shorts, other.shorts)) {
return false;
}
if (this.string == null) {
if (other.string != null) {
return false;
}
}
else if (!this.string.equals(other.string)) {
return false;
}
if (!Arrays.equals(this.strings, other.strings)) {
return false;
}
return true;
}
@Override
public
String toString() {
return "Data";
}
}
}

View File

@ -1,157 +0,0 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package dorkbox.util;
import static dorkbox.util.bytes.Unsigned.ubyte;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.Test;
import dorkbox.util.bytes.UByte;
public class UByteTest {
@Test
public void testValueOfByte() {
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
assertEquals(i & 0xFF, ubyte((byte) i).intValue());
}
}
@Test
public void testValueOfByteCaching() {
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
UByte a = ubyte((byte) i);
UByte b = ubyte((byte) i);
assertTrue(a == b);
}
}
@Test
public void testValueOfShort() {
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
assertEquals(i & 0xFF, ubyte((short) i).intValue());
}
}
@Test
public void testValueOfShortInvalid() {
try {
ubyte((short) (UByte.MIN_VALUE - 1));
fail();
}
catch (NumberFormatException e) {}
try {
ubyte((short) (UByte.MAX_VALUE + 1));
fail();
}
catch (NumberFormatException e) {}
}
@Test
public void testValueOfInt() {
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
assertEquals(i & 0xFF, ubyte(i).intValue());
}
}
@Test
public void testValueOfIntInvalid() {
try {
ubyte(UByte.MIN_VALUE - 1);
fail();
}
catch (NumberFormatException e) {}
try {
ubyte(UByte.MAX_VALUE + 1);
fail();
}
catch (NumberFormatException e) {}
}
@Test
public void testValueOfLong() {
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
assertEquals(i & 0xFF, ubyte((long) i).intValue());
}
}
@Test
public void testValueOfLongInvalid() {
try {
ubyte((long) UByte.MIN_VALUE - 1);
fail();
}
catch (NumberFormatException e) {}
try {
ubyte((long) UByte.MAX_VALUE + 1);
fail();
}
catch (NumberFormatException e) {}
}
@Test
public void testSerializeDeserialize() throws ClassNotFoundException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
ByteArrayInputStream bais;
ObjectInputStream ois;
UByte expected = ubyte(42);
UByte input = ubyte(42);
UByte actual;
Object o;
oos.writeObject(input);
oos.close();
bais = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bais);
o = ois.readObject();
if (!(o instanceof UByte)) {
fail();
}
actual = (UByte) o;
assertEquals(expected, actual); // same value
assertTrue(expected == actual); // identical objects
}
}

View File

@ -1,146 +0,0 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
* <p/>
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* <p/>
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* <p/>
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* <p/>
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
* <p/>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package dorkbox.util;
import static dorkbox.util.bytes.Unsigned.uint;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.Test;
import dorkbox.util.bytes.UInteger;
public
class UIntegerTest {
private static final int CACHE_SIZE = 256;
private static final int NEAR_MISS_OFFSET = 4;
@Test
public
void testValueOfLong() {
for (long l = 01; l <= UInteger.MAX_VALUE; l <<= 1) {
assertEquals(l, uint(l).longValue());
}
}
@Test
public
void testValueOfLongCachingShift() {
for (long l = 01; l < CACHE_SIZE; l <<= 1) {
UInteger a = uint(l);
UInteger b = uint(l);
assertTrue(a == b);
}
}
@Test
public
void testValueOfLongCachingNear() {
for (long l = CACHE_SIZE - NEAR_MISS_OFFSET; l < CACHE_SIZE; l++) {
UInteger a = uint(l);
UInteger b = uint(l);
assertTrue(a == b);
}
}
@Test
public
void testValueOfLongNoCachingShift() {
for (long l = CACHE_SIZE; l <= CACHE_SIZE; l <<= 1) {
UInteger a = uint(l);
UInteger b = uint(l);
assertFalse(a == b);
}
}
@Test
public
void testValueOfLongNoCachingNear() {
for (long l = CACHE_SIZE; l <= CACHE_SIZE + NEAR_MISS_OFFSET; l++) {
UInteger a = uint(l);
UInteger b = uint(l);
assertFalse(a == b);
}
}
@Test
public
void testValueOfLongInvalid() {
try {
uint(UInteger.MIN_VALUE - 1);
fail();
} catch (NumberFormatException e) {
}
try {
uint(UInteger.MAX_VALUE + 1);
fail();
} catch (NumberFormatException e) {
}
}
@Test
public
void testSerializeDeserialize() throws ClassNotFoundException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
ByteArrayInputStream bais;
ObjectInputStream ois;
UInteger expected = uint(42);
UInteger input = uint(42);
UInteger actual;
Object o;
oos.writeObject(input);
oos.close();
bais = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bais);
o = ois.readObject();
if (!(o instanceof UInteger)) {
fail();
}
actual = (UInteger) o;
assertEquals(expected, actual); // same value
assertTrue(expected == actual); // identical objects
}
}

View File

@ -1,291 +0,0 @@
/*
* Copyright (c) 2011-2013, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOU" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package dorkbox.util;
import static dorkbox.util.bytes.ULong.MAX_VALUE_LONG;
import static dorkbox.util.bytes.Unsigned.ubyte;
import static dorkbox.util.bytes.Unsigned.uint;
import static dorkbox.util.bytes.Unsigned.ulong;
import static dorkbox.util.bytes.Unsigned.ushort;
import static java.math.BigInteger.ONE;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import dorkbox.util.bytes.UByte;
import dorkbox.util.bytes.UInteger;
import dorkbox.util.bytes.ULong;
import dorkbox.util.bytes.UNumber;
import dorkbox.util.bytes.UShort;
/**
* @author Lukas Eder
*/
public class UNumberTest {
public static final float DELTA = 0.00001f;
@Test
public void testRange0() {
testCastable((short) 0, ubyte("0"));
testCastable((short) 0, ubyte((byte) 0));
testCastable((short) 0, ubyte((short) 0));
testCastable(0, ushort("0"));
testCastable(0, ushort((short) 0));
testCastable(0, ushort(0));
testCastable(0L, uint("0"));
testCastable(0L, uint(0));
testCastable(0L, uint(0L));
testCastable(BigInteger.ZERO, ulong("0"));
testCastable(BigInteger.ZERO, ulong(0L));
testCastable(BigInteger.ZERO, ulong(BigInteger.ZERO));
}
@Test
public void testRange1() {
testCastable((short) 1, ubyte("1"));
testCastable((short) 1, ubyte((byte) 1));
testCastable((short) 1, ubyte((short) 1));
testCastable(1, ushort("1"));
testCastable(1, ushort((short) 1));
testCastable(1, ushort(1));
testCastable(1L, uint("1"));
testCastable(1L, uint(1));
testCastable(1L, uint(1L));
testCastable(BigInteger.ONE, ulong("1"));
testCastable(BigInteger.ONE, ulong(1L));
testCastable(BigInteger.ONE, ulong(BigInteger.ONE));
}
@Test
public void testRangeSignedMaxValue() {
testCastable(Byte.MAX_VALUE, ubyte(Byte.toString(Byte.MAX_VALUE)));
testCastable(Byte.MAX_VALUE, ubyte(Byte.MAX_VALUE));
testCastable(Byte.MAX_VALUE, ubyte((short) Byte.MAX_VALUE));
testCastable(Short.MAX_VALUE, ushort(Short.toString(Short.MAX_VALUE)));
testCastable(Short.MAX_VALUE, ushort(Short.MAX_VALUE));
testCastable(Short.MAX_VALUE, ushort((int) Short.MAX_VALUE));
testCastable(Integer.MAX_VALUE, uint(Integer.toString(Integer.MAX_VALUE)));
testCastable(Integer.MAX_VALUE, uint(Integer.MAX_VALUE));
testCastable(Integer.MAX_VALUE, uint((long) Integer.MAX_VALUE));
testCastable(BigInteger.valueOf(Long.MAX_VALUE), ulong(Long.toString(Long.MAX_VALUE)));
testCastable(BigInteger.valueOf(Long.MAX_VALUE), ulong(Long.MAX_VALUE));
testCastable(BigInteger.valueOf(Long.MAX_VALUE), ulong(BigInteger.valueOf(Long.MAX_VALUE)));
}
@Test
public void testRangeSignedMaxValuePlusOne() {
testCastable((short) 0x80, ubyte(Short.toString((short) 0x80)));
testCastable((short) 0x80, ubyte((byte) -0x80));
testCastable((short) 0x80, ubyte((short) 0x80));
testCastable(0x8000, ushort(Integer.toString(0x8000)));
testCastable(0x8000, ushort((short) -0x8000));
testCastable(0x8000, ushort(0x8000));
testCastable(0x80000000L, uint(Long.toString(0x80000000L)));
testCastable(0x80000000L, uint((int) -0x80000000L));
testCastable(0x80000000L, uint(0x80000000L));
testCastable(MAX_VALUE_LONG, ulong(MAX_VALUE_LONG.toString()));
testCastable(MAX_VALUE_LONG, ulong(0x8000000000000000L));
testCastable(MAX_VALUE_LONG, ulong(MAX_VALUE_LONG));
}
@Test
public void testRangeSignedMaxValuePlusTwo() {
testCastable((short) 0x81, ubyte(Short.toString((short) 0x81)));
testCastable((short) 0x81, ubyte((byte) -0x7F));
testCastable((short) 0x81, ubyte((short) 0x81));
testCastable(0x8001, ushort(Integer.toString(0x8001)));
testCastable(0x8001, ushort((short) -0x7FFF));
testCastable(0x8001, ushort(0x8001));
testCastable(0x80000001L, uint(Long.toString(0x80000001L)));
testCastable(0x80000001L, uint((int) -0x7FFFFFFFL));
testCastable(0x80000001L, uint(0x80000001L));
testCastable(MAX_VALUE_LONG.add(ONE), ulong(MAX_VALUE_LONG.add(ONE).toString()));
testCastable(MAX_VALUE_LONG.add(ONE), ulong(0x8000000000000001L));
testCastable(MAX_VALUE_LONG.add(ONE), ulong(MAX_VALUE_LONG.add(ONE)));
}
@Test
public void testRangeUnsignedMaxValue() {
testCastable(UByte.MAX_VALUE, ubyte(Short.toString(UByte.MAX_VALUE)));
testCastable(UByte.MAX_VALUE, ubyte((byte) -1));
testCastable(UByte.MAX_VALUE, ubyte(UByte.MAX_VALUE));
testCastable(UShort.MAX_VALUE, ushort(Integer.toString(UShort.MAX_VALUE)));
testCastable(UShort.MAX_VALUE, ushort((short) -1));
testCastable(UShort.MAX_VALUE, ushort(UShort.MAX_VALUE));
testCastable(UInteger.MAX_VALUE, uint(Long.toString(UInteger.MAX_VALUE)));
testCastable(UInteger.MAX_VALUE, uint(-1));
testCastable(UInteger.MAX_VALUE, uint(UInteger.MAX_VALUE));
testCastable(ULong.MAX_VALUE, ulong(ULong.MAX_VALUE.toString()));
testCastable(ULong.MAX_VALUE, ulong(-1L));
testCastable(ULong.MAX_VALUE, ulong(ULong.MAX_VALUE));
}
@Test
public void testObjectMethods() {
assertEquals(ubyte((byte) 0), ubyte((byte) 0));
assertEquals(ubyte((byte) 1), ubyte((byte) 1));
assertFalse(ubyte((byte) 0).equals(ubyte((byte) 1)));
assertEquals("0", ubyte((byte) 0).toString());
assertEquals("1", ubyte((byte) 1).toString());
assertEquals(Short.toString(UByte.MAX_VALUE), ubyte((byte) -1).toString());
assertEquals(ushort((short) 0), ushort((short) 0));
assertEquals(ushort((short) 1), ushort((short) 1));
assertFalse(ushort((short) 0).equals(ushort((short) 1)));
assertEquals("0", ushort((short) 0).toString());
assertEquals("1", ushort((short) 1).toString());
assertEquals(Integer.toString(UShort.MAX_VALUE), ushort((short) -1).toString());
assertEquals(uint(0), uint(0));
assertEquals(uint(1), uint(1));
assertFalse(uint(0).equals(uint(1)));
assertEquals("0", uint(0).toString());
assertEquals("1", uint(1).toString());
assertEquals(Long.toString(UInteger.MAX_VALUE), uint(-1).toString());
assertEquals(ulong(0), ulong(0));
assertEquals(ulong(1), ulong(1));
assertFalse(ulong(0).equals(ulong(1)));
assertEquals("0", ulong(0).toString());
assertEquals("1", ulong(1).toString());
assertEquals(ULong.MAX_VALUE.toString(), ulong(-1).toString());
}
@Test
public void testComparable() {
testComparable(asList("1", "2", "3"), ubyte((byte) 1), ubyte((byte) 2), ubyte((byte) 3));
testComparable(asList("1", "2", "3"), ubyte((byte) 3), ubyte((byte) 2), ubyte((byte) 1));
testComparable(asList("1", "2", "3"), ubyte((byte) 3), ubyte((byte) 1), ubyte((byte) 2));
testComparable(asList("1", "1", "2", "3"), ubyte((byte) 3), ubyte((byte) 1), ubyte((byte) 2), ubyte((byte) 1));
testComparable(asList("1", "2", "3"), ushort(1), ushort(2), ushort(3));
testComparable(asList("1", "2", "3"), ushort(3), ushort(2), ushort(1));
testComparable(asList("1", "2", "3"), ushort(3), ushort(1), ushort(2));
testComparable(asList("1", "1", "2", "3"), ushort(3), ushort(1), ushort(2), ushort(1));
testComparable(asList("1", "2", "3"), uint(1), uint(2), uint(3));
testComparable(asList("1", "2", "3"), uint(3), uint(2), uint(1));
testComparable(asList("1", "2", "3"), uint(3), uint(1), uint(2));
testComparable(asList("1", "1", "2", "3"), uint(3), uint(1), uint(2), uint(1));
testComparable(asList("1", "2", "3"), ulong(1), ulong(2), ulong(3));
testComparable(asList("1", "2", "3"), ulong(3), ulong(2), ulong(1));
testComparable(asList("1", "2", "3"), ulong(3), ulong(1), ulong(2));
testComparable(asList("1", "1", "2", "3"), ulong(3), ulong(1), ulong(2), ulong(1));
}
// Test utility methods
@SuppressWarnings({ "rawtypes", "unchecked"})
private void testComparable(List<String> strings, UNumber... numbers) {
List<UNumber> list = new ArrayList<UNumber>(asList(numbers));
Collections.sort((List) list);
for (int i = 0; i < numbers.length; i++) {
assertEquals(strings.get(i), list.get(i).toString());
}
}
@SuppressWarnings("deprecation")
private void testCastable(short value, UByte u) {
assertEquals("Byte failed", (byte) value, u.byteValue());
assertEquals("Short failed", value, u.shortValue());
assertEquals("Int failed", value, u.intValue());
assertEquals("Long failed", value, u.longValue());
assertEquals("Double failed", (double) value, u.doubleValue(), DELTA);
assertEquals("Float failed", (float) value, u.floatValue(), DELTA);
assertEquals("BigInteger failed", new BigInteger("" + value), u.toBigInteger());
}
@SuppressWarnings("deprecation")
private void testCastable(int value, UShort u) {
assertEquals("Byte failed", (byte) value, u.byteValue());
assertEquals("Short failed", (short) value, u.shortValue());
assertEquals("Int failed", value, u.intValue());
assertEquals("Long failed", value, u.longValue());
assertEquals("Double failed", (double) value, u.doubleValue(), DELTA);
assertEquals("Float failed", (float) value, u.floatValue(), DELTA);
assertEquals("BigInteger failed", new BigInteger("" + value), u.toBigInteger());
}
@SuppressWarnings("deprecation")
private void testCastable(long value, UInteger u) {
assertEquals("Byte failed", (byte) value, u.byteValue());
assertEquals("Short failed", (short) value, u.shortValue());
assertEquals("Int failed", (int) value, u.intValue());
assertEquals("Long failed", value, u.longValue());
assertEquals("Double failed", (double) value, u.doubleValue(), DELTA);
assertEquals("Float failed", (float) value, u.floatValue(), DELTA);
assertEquals("BigInteger failed", new BigInteger("" + value), u.toBigInteger());
}
@SuppressWarnings("deprecation")
private void testCastable(BigInteger value, ULong u) {
assertEquals("Byte failed", value.byteValue(), u.byteValue());
assertEquals("Short failed", value.shortValue(), u.shortValue());
assertEquals("Int failed", value.intValue(), u.intValue());
assertEquals("Long failed", value.longValue(), u.longValue());
assertEquals("Double failed", value.doubleValue(), u.doubleValue(), DELTA);
assertEquals("Float failed", value.floatValue(), u.floatValue(), DELTA);
assertEquals("BigInteger failed", new BigInteger("" + value), u.toBigInteger());
}
}

View File

@ -51,11 +51,10 @@ import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import dorkbox.util.serialization.EccPrivateKeySerializer;
import dorkbox.util.serialization.EccPublicKeySerializer;
import dorkbox.util.serialization.IesParametersSerializer;
import dorkbox.util.serialization.IesWithCipherParametersSerializer;
import dorkbox.serializers.EccPrivateKeySerializer;
import dorkbox.serializers.EccPublicKeySerializer;
import dorkbox.serializers.IesParametersSerializer;
import dorkbox.serializers.IesWithCipherParametersSerializer;
public class EccTest {

View File

@ -40,9 +40,8 @@ import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import dorkbox.util.serialization.RsaPrivateKeySerializer;
import dorkbox.util.serialization.RsaPublicKeySerializer;
import dorkbox.serializers.RsaPrivateKeySerializer;
import dorkbox.serializers.RsaPublicKeySerializer;
public class RsaTest {
org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());