java: adds ObjectEquals test

This commit is contained in:
frsyuki 2010-08-18 22:24:51 +09:00
parent 8b79e6d3c7
commit 5658ca5b90
9 changed files with 152 additions and 22 deletions

View File

@ -124,7 +124,7 @@ public class UnpackerImpl {
if((b & 0xe0) == 0xa0) { // FixRaw if((b & 0xe0) == 0xa0) { // FixRaw
trail = b & 0x1f; trail = b & 0x1f;
if(trail == 0) { if(trail == 0) {
obj = new RawType(new byte[0]); obj = RawType.create(new byte[0]);
break _push; break _push;
} }
cs = ACS_RAW_VALUE; cs = ACS_RAW_VALUE;
@ -139,7 +139,7 @@ public class UnpackerImpl {
//System.out.println("fixarray count:"+count); //System.out.println("fixarray count:"+count);
obj = new MessagePackObject[count]; obj = new MessagePackObject[count];
if(count == 0) { if(count == 0) {
obj = new ArrayType((MessagePackObject[])obj); obj = ArrayType.create((MessagePackObject[])obj);
break _push; break _push;
} }
++top; ++top;
@ -159,7 +159,7 @@ public class UnpackerImpl {
count = b & 0x0f; count = b & 0x0f;
obj = new MessagePackObject[count*2]; obj = new MessagePackObject[count*2];
if(count == 0) { if(count == 0) {
obj = new MapType((MessagePackObject[])obj); obj = MapType.create((MessagePackObject[])obj);
break _push; break _push;
} }
//System.out.println("fixmap count:"+count); //System.out.println("fixmap count:"+count);
@ -175,13 +175,13 @@ public class UnpackerImpl {
switch(b & 0xff) { // FIXME switch(b & 0xff) { // FIXME
case 0xc0: // nil case 0xc0: // nil
obj = new NilType(); obj = NilType.create();
break _push; break _push;
case 0xc2: // false case 0xc2: // false
obj = new BooleanType(false); obj = BooleanType.create(false);
break _push; break _push;
case 0xc3: // true case 0xc3: // true
obj = new BooleanType(true); obj = BooleanType.create(true);
break _push; break _push;
case 0xca: // float case 0xca: // float
case 0xcb: // double case 0xcb: // double
@ -293,7 +293,7 @@ public class UnpackerImpl {
castBuffer.put(src, n, 2); castBuffer.put(src, n, 2);
trail = ((int)castBuffer.getShort(0)) & 0xffff; trail = ((int)castBuffer.getShort(0)) & 0xffff;
if(trail == 0) { if(trail == 0) {
obj = new RawType(new byte[0]); obj = RawType.create(new byte[0]);
break _push; break _push;
} }
cs = ACS_RAW_VALUE; cs = ACS_RAW_VALUE;
@ -304,14 +304,14 @@ public class UnpackerImpl {
// FIXME overflow check // FIXME overflow check
trail = castBuffer.getInt(0) & 0x7fffffff; trail = castBuffer.getInt(0) & 0x7fffffff;
if(trail == 0) { if(trail == 0) {
obj = new RawType(new byte[0]); obj = RawType.create(new byte[0]);
break _push; break _push;
} }
cs = ACS_RAW_VALUE; cs = ACS_RAW_VALUE;
case ACS_RAW_VALUE: { case ACS_RAW_VALUE: {
byte[] raw = new byte[trail]; byte[] raw = new byte[trail];
System.arraycopy(src, n, raw, 0, trail); System.arraycopy(src, n, raw, 0, trail);
obj = new RawType(raw); obj = RawType.create(raw);
} }
break _push; break _push;
case CS_ARRAY_16: case CS_ARRAY_16:
@ -323,7 +323,7 @@ public class UnpackerImpl {
count = ((int)castBuffer.getShort(0)) & 0xffff; count = ((int)castBuffer.getShort(0)) & 0xffff;
obj = new MessagePackObject[count]; obj = new MessagePackObject[count];
if(count == 0) { if(count == 0) {
obj = new ArrayType((MessagePackObject[])obj); obj = ArrayType.create((MessagePackObject[])obj);
break _push; break _push;
} }
++top; ++top;
@ -344,7 +344,7 @@ public class UnpackerImpl {
count = castBuffer.getInt(0) & 0x7fffffff; count = castBuffer.getInt(0) & 0x7fffffff;
obj = new MessagePackObject[count]; obj = new MessagePackObject[count];
if(count == 0) { if(count == 0) {
obj = new ArrayType((MessagePackObject[])obj); obj = ArrayType.create((MessagePackObject[])obj);
break _push; break _push;
} }
++top; ++top;
@ -364,7 +364,7 @@ public class UnpackerImpl {
count = ((int)castBuffer.getShort(0)) & 0xffff; count = ((int)castBuffer.getShort(0)) & 0xffff;
obj = new MessagePackObject[count*2]; obj = new MessagePackObject[count*2];
if(count == 0) { if(count == 0) {
obj = new MapType((MessagePackObject[])obj); obj = MapType.create((MessagePackObject[])obj);
break _push; break _push;
} }
//System.out.println("fixmap count:"+count); //System.out.println("fixmap count:"+count);
@ -386,7 +386,7 @@ public class UnpackerImpl {
count = castBuffer.getInt(0) & 0x7fffffff; count = castBuffer.getInt(0) & 0x7fffffff;
obj = new MessagePackObject[count*2]; obj = new MessagePackObject[count*2];
if(count == 0) { if(count == 0) {
obj = new MapType((MessagePackObject[])obj); obj = MapType.create((MessagePackObject[])obj);
break _push; break _push;
} }
//System.out.println("fixmap count:"+count); //System.out.println("fixmap count:"+count);
@ -425,7 +425,7 @@ public class UnpackerImpl {
top_obj = stack_obj[top]; top_obj = stack_obj[top];
top_ct = stack_ct[top]; top_ct = stack_ct[top];
top_count = stack_count[top]; top_count = stack_count[top];
obj = new ArrayType((MessagePackObject[])ar); obj = ArrayType.create((MessagePackObject[])ar);
stack_obj[top] = null; stack_obj[top] = null;
--top; --top;
break _push; break _push;
@ -447,7 +447,7 @@ public class UnpackerImpl {
top_obj = stack_obj[top]; top_obj = stack_obj[top];
top_ct = stack_ct[top]; top_ct = stack_ct[top];
top_count = stack_count[top]; top_count = stack_count[top];
obj = new MapType((MessagePackObject[])mp); obj = MapType.create((MessagePackObject[])mp);
stack_obj[top] = null; stack_obj[top] = null;
--top; --top;
break _push; break _push;

View File

@ -25,10 +25,14 @@ import org.msgpack.*;
public class ArrayType extends MessagePackObject { public class ArrayType extends MessagePackObject {
private MessagePackObject[] array; private MessagePackObject[] array;
public ArrayType(MessagePackObject[] array) { ArrayType(MessagePackObject[] array) {
this.array = array; this.array = array;
} }
public static ArrayType create(MessagePackObject[] array) {
return new ArrayType(array);
}
@Override @Override
public boolean isArrayType() { public boolean isArrayType() {
return true; return true;

View File

@ -109,7 +109,7 @@ class BigIntegerTypeIMPL extends IntegerType {
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(obj.getClass() != getClass()) { if(obj.getClass() != getClass()) {
if(obj.getClass() == ShortIntegerTypeIMPL.class) { if(obj.getClass() == ShortIntegerTypeIMPL.class) {
return BigInteger.valueOf((long)((ShortIntegerTypeIMPL)obj).shortValue()).equals(value); return BigInteger.valueOf(((ShortIntegerTypeIMPL)obj).longValue()).equals(value);
} else if(obj.getClass() == LongIntegerTypeIMPL.class) { } else if(obj.getClass() == LongIntegerTypeIMPL.class) {
return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value); return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value);
} }

View File

@ -23,10 +23,14 @@ import org.msgpack.*;
public class BooleanType extends MessagePackObject { public class BooleanType extends MessagePackObject {
private boolean value; private boolean value;
public BooleanType(boolean value) { BooleanType(boolean value) {
this.value = value; this.value = value;
} }
public static BooleanType create(boolean value) {
return new BooleanType(value);
}
@Override @Override
public boolean isBooleanType() { public boolean isBooleanType() {
return true; return true;

View File

@ -108,7 +108,7 @@ class LongIntegerTypeIMPL extends IntegerType {
if(obj.getClass() == ShortIntegerTypeIMPL.class) { if(obj.getClass() == ShortIntegerTypeIMPL.class) {
return value == ((ShortIntegerTypeIMPL)obj).longValue(); return value == ((ShortIntegerTypeIMPL)obj).longValue();
} else if(obj.getClass() == BigIntegerTypeIMPL.class) { } else if(obj.getClass() == BigIntegerTypeIMPL.class) {
return (long)value == ((BigIntegerTypeIMPL)obj).longValue(); return BigInteger.valueOf(value).equals(((BigIntegerTypeIMPL)obj).bigIntegerValue());
} }
return false; return false;
} }

View File

@ -26,10 +26,14 @@ import org.msgpack.*;
public class MapType extends MessagePackObject { public class MapType extends MessagePackObject {
private MessagePackObject[] map; private MessagePackObject[] map;
public MapType(MessagePackObject[] map) { MapType(MessagePackObject[] map) {
this.map = map; this.map = map;
} }
public static MapType create(MessagePackObject[] map) {
return new MapType(map);
}
@Override @Override
public boolean isMapType() { public boolean isMapType() {
return true; return true;

View File

@ -21,6 +21,12 @@ import java.io.IOException;
import org.msgpack.*; import org.msgpack.*;
public class NilType extends MessagePackObject { public class NilType extends MessagePackObject {
private static NilType instance = new NilType();
public static NilType create() {
return instance;
}
@Override @Override
public boolean isNull() { public boolean isNull() {
return true; return true;

View File

@ -24,10 +24,26 @@ import org.msgpack.*;
public class RawType extends MessagePackObject { public class RawType extends MessagePackObject {
private byte[] bytes; private byte[] bytes;
public RawType(byte[] bytes) { RawType(byte[] bytes) {
this.bytes = bytes; this.bytes = bytes;
} }
RawType(String str) {
try {
this.bytes = str.getBytes("UTF-8");
} catch (Exception e) {
throw new MessageTypeException("type error");
}
}
public static RawType create(byte[] bytes) {
return new RawType(bytes);
}
public static RawType create(String str) {
return new RawType(str);
}
@Override @Override
public boolean isRawType() { public boolean isRawType() {
return true; return true;
@ -58,7 +74,7 @@ public class RawType extends MessagePackObject {
if(obj.getClass() != getClass()) { if(obj.getClass() != getClass()) {
return false; return false;
} }
return ((RawType)obj).bytes.equals(bytes); return Arrays.equals(((RawType)obj).bytes, bytes);
} }
@Override @Override

View File

@ -0,0 +1,96 @@
package org.msgpack;
import org.msgpack.*;
import org.msgpack.object.*;
import java.math.BigInteger;
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestObjectEquals {
public void testInt(int val) throws Exception {
MessagePackObject objInt = IntegerType.create(val);
MessagePackObject objLong = IntegerType.create((long)val);
MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf((long)val));
assertTrue(objInt.equals(objInt));
assertTrue(objInt.equals(objLong));
assertTrue(objInt.equals(objBigInt));
assertTrue(objLong.equals(objInt));
assertTrue(objLong.equals(objLong));
assertTrue(objLong.equals(objBigInt));
assertTrue(objBigInt.equals(objInt));
assertTrue(objBigInt.equals(objLong));
assertTrue(objBigInt.equals(objBigInt));
}
@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 testLong(long val) throws Exception {
MessagePackObject objInt = IntegerType.create((int)val);
MessagePackObject objLong = IntegerType.create(val);
MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf(val));
if(val > (long)Integer.MAX_VALUE || val < (long)Integer.MIN_VALUE) {
assertTrue(objInt.equals(objInt));
assertFalse(objInt.equals(objLong));
assertFalse(objInt.equals(objBigInt));
assertFalse(objLong.equals(objInt));
assertTrue(objLong.equals(objLong));
assertTrue(objLong.equals(objBigInt));
assertFalse(objBigInt.equals(objInt));
assertTrue(objBigInt.equals(objLong));
assertTrue(objBigInt.equals(objBigInt));
} else {
assertTrue(objInt.equals(objInt));
assertTrue(objInt.equals(objLong));
assertTrue(objInt.equals(objBigInt));
assertTrue(objLong.equals(objInt));
assertTrue(objLong.equals(objLong));
assertTrue(objLong.equals(objBigInt));
assertTrue(objBigInt.equals(objInt));
assertTrue(objBigInt.equals(objLong));
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
public void testNil() throws Exception {
assertTrue(NilType.create().equals(NilType.create()));
assertFalse(NilType.create().equals(IntegerType.create(0)));
assertFalse(NilType.create().equals(BooleanType.create(false)));
}
public void testString(String str) throws Exception {
assertTrue(RawType.create(str).equals(RawType.create(str)));
}
@Test
public void testString() throws Exception {
testString("");
testString("a");
testString("ab");
testString("abc");
}
}