java: updates TestDirectConversion

This commit is contained in:
frsyuki
2010-08-19 00:05:48 +09:00
parent 48da2b8353
commit 193a739749
4 changed files with 292 additions and 181 deletions

View File

@@ -32,5 +32,7 @@ TEST(cases, format)
EXPECT_TRUE( pac_compact.next(&result_compact) ); EXPECT_TRUE( pac_compact.next(&result_compact) );
EXPECT_EQ(result_compact.get(), result.get()); EXPECT_EQ(result_compact.get(), result.get());
} }
EXPECT_FALSE( pac_compact.next(&result) );
} }

View File

@@ -8,141 +8,248 @@ import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class TestDirectConversion { public class TestDirectConversion {
@Test @Test
public void testInt() throws Exception { public void testInt() throws Exception {
testInt(0); testInt(0);
testInt(-1); testInt(-1);
testInt(1); testInt(1);
testInt(Integer.MIN_VALUE); testInt(Integer.MIN_VALUE);
testInt(Integer.MAX_VALUE); testInt(Integer.MAX_VALUE);
Random rand = new Random(); Random rand = new Random();
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testInt(rand.nextInt()); testInt(rand.nextInt());
} }
public void testInt(int val) throws Exception { public void testInt(int val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val); new Packer(out).pack(val);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker upk = new Unpacker(in); Unpacker pac = new Unpacker(in);
assertEquals(val, upk.unpackInt()); assertEquals(val, pac.unpackInt());
} }
@Test @Test
public void testFloat() throws Exception { public void testLong() throws Exception {
testFloat((float)0.0); testLong(0);
testFloat((float)-0.0); testLong(-1);
testFloat((float)1.0); testLong(1);
testFloat((float)-1.0); testLong(Integer.MIN_VALUE);
testFloat((float)Float.MAX_VALUE); testLong(Integer.MAX_VALUE);
testFloat((float)Float.MIN_VALUE); testLong(Long.MIN_VALUE);
testFloat((float)Float.NaN); testLong(Long.MAX_VALUE);
testFloat((float)Float.NEGATIVE_INFINITY); Random rand = new Random();
testFloat((float)Float.POSITIVE_INFINITY); for (int i = 0; i < 1000; i++)
Random rand = new Random(); testLong(rand.nextLong());
for (int i = 0; i < 1000; i++) }
testFloat(rand.nextFloat()); public void testLong(long val) throws Exception {
} ByteArrayOutputStream out = new ByteArrayOutputStream();
public void testFloat(float val) throws Exception { new Packer(out).pack(val);
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
new Packer(out).pack(val); Unpacker pac = new Unpacker(in);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); assertEquals(val, pac.unpackLong());
Unpacker upk = new Unpacker(in); }
float f = upk.unpackFloat();
if(Float.isNaN(val)) {
assertTrue(Float.isNaN(f));
} else {
assertEquals(val, f, 10e-10);
}
}
@Test @Test
public void testDouble() throws Exception { public void testFloat() throws Exception {
testDouble((double)0.0); testFloat((float)0.0);
testDouble((double)-0.0); testFloat((float)-0.0);
testDouble((double)1.0); testFloat((float)1.0);
testDouble((double)-1.0); testFloat((float)-1.0);
testDouble((double)Double.MAX_VALUE); testFloat((float)Float.MAX_VALUE);
testDouble((double)Double.MIN_VALUE); testFloat((float)Float.MIN_VALUE);
testDouble((double)Double.NaN); testFloat((float)Float.NaN);
testDouble((double)Double.NEGATIVE_INFINITY); testFloat((float)Float.NEGATIVE_INFINITY);
testDouble((double)Double.POSITIVE_INFINITY); testFloat((float)Float.POSITIVE_INFINITY);
Random rand = new Random(); Random rand = new Random();
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testDouble(rand.nextDouble()); testFloat(rand.nextFloat());
} }
public void testDouble(double val) throws Exception { public void testFloat(float val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val); new Packer(out).pack(val);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker upk = new Unpacker(in); Unpacker pac = new Unpacker(in);
double f = upk.unpackDouble(); assertEquals(val, pac.unpackFloat(), 10e-10);
if(Double.isNaN(val)) { }
assertTrue(Double.isNaN(f));
} else {
assertEquals(val, f, 10e-10);
}
}
@Test @Test
public void testNil() throws Exception { public void testDouble() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); testDouble((double)0.0);
new Packer(out).packNil(); testDouble((double)-0.0);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); testDouble((double)1.0);
Unpacker upk = new Unpacker(in); testDouble((double)-1.0);
assertEquals(null, upk.unpackNull()); testDouble((double)Double.MAX_VALUE);
} testDouble((double)Double.MIN_VALUE);
testDouble((double)Double.NaN);
testDouble((double)Double.NEGATIVE_INFINITY);
testDouble((double)Double.POSITIVE_INFINITY);
Random rand = new Random();
for (int i = 0; i < 1000; i++)
testDouble(rand.nextDouble());
}
public void testDouble(double val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
assertEquals(val, pac.unpackDouble(), 10e-10);
}
@Test @Test
public void testBoolean() throws Exception { public void testNil() throws Exception {
testBoolean(false); ByteArrayOutputStream out = new ByteArrayOutputStream();
testBoolean(true); new Packer(out).packNil();
} ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
public void testBoolean(boolean val) throws Exception { Unpacker pac = new Unpacker(in);
ByteArrayOutputStream out = new ByteArrayOutputStream(); assertEquals(null, pac.unpackNull());
new Packer(out).pack(val); }
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker upk = new Unpacker(in);
assertEquals(val, upk.unpackBoolean());
}
@Test @Test
public void testString() throws Exception { public void testBoolean() throws Exception {
testString(""); testBoolean(false);
testString("a"); testBoolean(true);
testString("ab"); }
testString("abc"); public void testBoolean(boolean val) throws Exception {
// small size string ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < 100; i++) { new Packer(out).pack(val);
StringBuilder sb = new StringBuilder(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
int len = (int)Math.random() % 31 + 1; Unpacker pac = new Unpacker(in);
for (int j = 0; j < len; j++) assertEquals(val, pac.unpackBoolean());
sb.append('a' + ((int)Math.random()) & 26); }
testString(sb.toString());
}
// medium size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 100 + (1 << 15);
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
// large size string
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 100 + (1 << 31);
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
}
public void testString(String val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker upk = new Unpacker(in);
assertEquals(val, upk.unpackString());
}
// FIXME container types @Test
public void testString() throws Exception {
testString("");
testString("a");
testString("ab");
testString("abc");
// small size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 31 + 1;
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
// medium size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 100 + (1 << 15);
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
// large size string
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 100 + (1 << 31);
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
}
public void testString(String val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
assertEquals(val, pac.unpackString());
}
@Test
public void testArray() throws Exception {
List<Integer> emptyList = new ArrayList<Integer>();
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(emptyList);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
int ulen = pac.unpackArray();
assertEquals(0, ulen);
}
for (int i = 0; i < 1000; i++) {
List<Integer> l = new ArrayList<Integer>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
l.add(j);
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(l);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
int ulen = pac.unpackArray();
assertEquals(len, ulen);
for (int j = 0; j < len; j++) {
assertEquals(l.get(j).intValue(), pac.unpackInt());
}
}
for (int i = 0; i < 1000; i++) {
List<String> l = new ArrayList<String>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
l.add(Integer.toString(j));
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(l);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
int ulen = pac.unpackArray();
assertEquals(len, ulen);
for (int j = 0; j < len; j++) {
assertEquals(l.get(j), pac.unpackString());
}
}
}
@Test
public void testMap() throws Exception {
Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(emptyMap);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
int ulen = pac.unpackMap();
assertEquals(0, ulen);
}
for (int i = 0; i < 1000; i++) {
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
m.put(j, j);
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(m);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
int ulen = pac.unpackMap();
assertEquals(len, ulen);
for (int j = 0; j < len; j++) {
Integer val = m.get(pac.unpackInt());
assertNotNull(val);
assertEquals(val.intValue(), pac.unpackInt());
}
}
for (int i = 0; i < 1000; i++) {
Map<String, Integer> m = new HashMap<String, Integer>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
m.put(Integer.toString(j), j);
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(m);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
int ulen = pac.unpackMap();
assertEquals(len, ulen);
for (int j = 0; j < len; j++) {
Integer val = m.get(pac.unpackString());
assertNotNull(val);
assertEquals(val.intValue(), pac.unpackInt());
}
}
}
}; };

View File

@@ -9,6 +9,17 @@ import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class TestObjectEquals { public class TestObjectEquals {
@Test
public void testInt() throws Exception {
testInt(0);
testInt(-1);
testInt(1);
testInt(Integer.MIN_VALUE);
testInt(Integer.MAX_VALUE);
Random rand = new Random();
for (int i = 0; i < 1000; i++)
testInt(rand.nextInt());
}
public void testInt(int val) throws Exception { public void testInt(int val) throws Exception {
MessagePackObject objInt = IntegerType.create(val); MessagePackObject objInt = IntegerType.create(val);
MessagePackObject objLong = IntegerType.create((long)val); MessagePackObject objLong = IntegerType.create((long)val);
@@ -23,18 +34,20 @@ public class TestObjectEquals {
assertTrue(objBigInt.equals(objLong)); assertTrue(objBigInt.equals(objLong));
assertTrue(objBigInt.equals(objBigInt)); assertTrue(objBigInt.equals(objBigInt));
} }
@Test @Test
public void testInt() throws Exception { public void testLong() throws Exception {
testInt(0); testLong(0);
testInt(-1); testLong(-1);
testInt(1); testLong(1);
testInt(Integer.MIN_VALUE); testLong(Integer.MIN_VALUE);
testInt(Integer.MAX_VALUE); testLong(Integer.MAX_VALUE);
testLong(Long.MIN_VALUE);
testLong(Long.MAX_VALUE);
Random rand = new Random(); Random rand = new Random();
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testInt(rand.nextInt()); testLong(rand.nextLong());
} }
public void testLong(long val) throws Exception { public void testLong(long val) throws Exception {
MessagePackObject objInt = IntegerType.create((int)val); MessagePackObject objInt = IntegerType.create((int)val);
MessagePackObject objLong = IntegerType.create(val); MessagePackObject objLong = IntegerType.create(val);
@@ -61,19 +74,6 @@ public class TestObjectEquals {
assertTrue(objBigInt.equals(objBigInt)); assertTrue(objBigInt.equals(objBigInt));
} }
} }
@Test
public void testLong() throws Exception {
testLong(0);
testLong(-1);
testLong(1);
testLong(Integer.MIN_VALUE);
testLong(Integer.MAX_VALUE);
testLong(Long.MIN_VALUE);
testLong(Long.MAX_VALUE);
Random rand = new Random();
for (int i = 0; i < 1000; i++)
testLong(rand.nextLong());
}
@Test @Test
public void testNil() throws Exception { public void testNil() throws Exception {
@@ -82,9 +82,6 @@ public class TestObjectEquals {
assertFalse(NilType.create().equals(BooleanType.create(false))); assertFalse(NilType.create().equals(BooleanType.create(false)));
} }
public void testString(String str) throws Exception {
assertTrue(RawType.create(str).equals(RawType.create(str)));
}
@Test @Test
public void testString() throws Exception { public void testString() throws Exception {
testString(""); testString("");
@@ -92,5 +89,8 @@ public class TestObjectEquals {
testString("ab"); testString("ab");
testString("abc"); testString("abc");
} }
public void testString(String str) throws Exception {
assertTrue(RawType.create(str).equals(RawType.create(str)));
}
} }

View File

@@ -11,20 +11,14 @@ import static org.junit.Assert.*;
public class TestPackUnpack { public class TestPackUnpack {
public MessagePackObject unpackOne(ByteArrayOutputStream out) { public MessagePackObject unpackOne(ByteArrayOutputStream out) {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker upk = new Unpacker(in); Unpacker pac = new Unpacker(in);
Iterator<MessagePackObject> it = upk.iterator(); Iterator<MessagePackObject> it = pac.iterator();
assertEquals(true, it.hasNext()); assertEquals(true, it.hasNext());
MessagePackObject obj = it.next(); MessagePackObject obj = it.next();
assertEquals(false, it.hasNext()); assertEquals(false, it.hasNext());
return obj; return obj;
} }
public void testInt(int val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asInt());
}
@Test @Test
public void testInt() throws Exception { public void testInt() throws Exception {
testInt(0); testInt(0);
@@ -36,13 +30,13 @@ public class TestPackUnpack {
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testInt(rand.nextInt()); testInt(rand.nextInt());
} }
public void testInt(int val) throws Exception {
public void testLong(long val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val); new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out); MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asLong()); assertEquals(val, obj.asInt());
} }
@Test @Test
public void testLong() throws Exception { public void testLong() throws Exception {
testLong(0); testLong(0);
@@ -56,13 +50,13 @@ public class TestPackUnpack {
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testLong(rand.nextLong()); testLong(rand.nextLong());
} }
public void testLong(long val) throws Exception {
public void testBigInteger(BigInteger val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val); new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out); MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asBigInteger()); assertEquals(val, obj.asLong());
} }
@Test @Test
public void testBigInteger() throws Exception { public void testBigInteger() throws Exception {
testBigInteger(BigInteger.valueOf(0)); testBigInteger(BigInteger.valueOf(0));
@@ -78,13 +72,13 @@ public class TestPackUnpack {
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) );
} }
public void testBigInteger(BigInteger val) throws Exception {
public void testFloat(float val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val); new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out); MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asFloat(), 10e-10); assertEquals(val, obj.asBigInteger());
} }
@Test @Test
public void testFloat() throws Exception { public void testFloat() throws Exception {
testFloat((float)0.0); testFloat((float)0.0);
@@ -100,13 +94,14 @@ public class TestPackUnpack {
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testFloat(rand.nextFloat()); testFloat(rand.nextFloat());
} }
public void testFloat(float val) throws Exception {
public void testDouble(double val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val); new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out); MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asDouble(), 10e-10); float f = obj.asFloat();
assertEquals(val, f, 10e-10);
} }
@Test @Test
public void testDouble() throws Exception { public void testDouble() throws Exception {
testDouble((double)0.0); testDouble((double)0.0);
@@ -122,6 +117,13 @@ public class TestPackUnpack {
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
testDouble(rand.nextDouble()); testDouble(rand.nextDouble());
} }
public void testDouble(double val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out);
double f = obj.asDouble();
assertEquals(val, f, 10e-10);
}
@Test @Test
public void testNil() throws Exception { public void testNil() throws Exception {
@@ -143,12 +145,6 @@ public class TestPackUnpack {
assertEquals(val, obj.asBoolean()); assertEquals(val, obj.asBoolean());
} }
public void testString(String val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asString());
}
@Test @Test
public void testString() throws Exception { public void testString() throws Exception {
testString(""); testString("");
@@ -183,6 +179,12 @@ public class TestPackUnpack {
testString(sb.toString()); testString(sb.toString());
} }
} }
public void testString(String val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out);
assertEquals(val, obj.asString());
}
@Test @Test
public void testArray() throws Exception { public void testArray() throws Exception {