Skip to content

Commit 1ff792d

Browse files
committed
added TestHashCode
1 parent 3d5db89 commit 1ff792d

7 files changed

Lines changed: 158 additions & 46 deletions

File tree

src/main/java/org/msgpack/value/BigIntegerValueImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ public boolean equals(Object o) {
117117
}
118118

119119
public int hashCode() {
120-
// TODO BigIntegerValueImpl(v).hashCode() == IntValueImpl(v).hashCode()
120+
if(INT_MIN.compareTo(value) <= 0 && value.compareTo(INT_MAX) <= 0) {
121+
return (int)value.longValue();
122+
} else if(LONG_MIN.compareTo(value) <= 0 && value.compareTo(LONG_MAX) <= 0) {
123+
long v = value.longValue();
124+
return (int)(v^(v>>>32));
125+
}
121126
return value.hashCode();
122127
}
123128

src/main/java/org/msgpack/value/LongValueImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ public boolean equals(Object o) {
117117
}
118118

119119
public int hashCode() {
120-
return (int)(value^(value>>>32));
120+
if(INT_MIN <= value && value <= INT_MAX) {
121+
return (int)value;
122+
} else {
123+
return (int)(value^(value>>>32));
124+
}
121125
}
122126

123127
public String toString() {

src/test/java/org/msgpack/TestCrossLang.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package org.msgpack;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertArrayEquals;
7+
38
import java.math.BigInteger;
49
import java.nio.ByteBuffer;
510
import java.io.IOException;
@@ -10,17 +15,15 @@
1015
import java.util.Iterator;
1116
import java.util.List;
1217
import java.util.Map;
13-
import java.util.Arrays;
1418

15-
import org.junit.Test;
1619
import org.msgpack.MessagePack;
1720
import org.msgpack.value.Value;
1821
import org.msgpack.packer.StreamPacker;
1922
import org.msgpack.unpacker.BufferUnpacker;
2023

21-
import junit.framework.TestCase;
24+
import org.junit.Test;
2225

23-
public class TestCrossLang extends TestCase {
26+
public class TestCrossLang {
2427
private byte[] readData(String path) throws IOException {
2528
ByteArrayOutputStream bo = new ByteArrayOutputStream();
2629
FileInputStream input = new FileInputStream(path);
@@ -81,7 +84,7 @@ public void testCompactSerialize() throws IOException {
8184
byte[] c = out.toByteArray();
8285

8386
assertEquals(b.length, c.length);
84-
assertTrue(Arrays.equals(b, c));
87+
assertArrayEquals(b, c);
8588
}
8689
}
8790

src/test/java/org/msgpack/TestSimpleConvertUnconvert.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.msgpack;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.assertArrayEquals;
6+
37
import java.math.BigInteger;
48
import java.nio.ByteBuffer;
59
import java.io.IOException;
@@ -8,17 +12,15 @@
812
import java.util.Iterator;
913
import java.util.List;
1014
import java.util.Map;
11-
import java.util.Arrays;
1215

13-
import org.junit.Test;
1416
import org.msgpack.MessagePack;
1517
import org.msgpack.value.Value;
1618
import org.msgpack.unpacker.Converter;
1719
import org.msgpack.packer.Unconverter;
1820

19-
import junit.framework.TestCase;
21+
import org.junit.Test;
2022

21-
public class TestSimpleConvertUnconvert extends TestCase {
23+
public class TestSimpleConvertUnconvert {
2224
@Test
2325
public void testSimpleConvert() throws IOException {
2426
MessagePack msgpack = new MessagePack();
@@ -27,7 +29,7 @@ public void testSimpleConvert() throws IOException {
2729
Value v = msgpack.unpack(raw);
2830

2931
int[] array = msgpack.convert(v, new int[3]);
30-
assertTrue(Arrays.equals(new int[] {1,2,3}, array));
32+
assertArrayEquals(new int[] {1,2,3}, array);
3133

3234
Value v2 = msgpack.unconvert(array);
3335
assertEquals(v, v2);

src/test/java/org/msgpack/io/TestLinkedBufferInput.java

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.msgpack.io;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.fail;
6+
37
import java.math.BigInteger;
48
import java.nio.ByteBuffer;
59
import java.io.IOException;
@@ -15,13 +19,12 @@
1519
import java.util.Map;
1620
import java.util.Arrays;
1721

18-
import org.junit.Test;
1922
import org.msgpack.MessagePack;
2023
import org.msgpack.value.Value;
2124

22-
import junit.framework.TestCase;
25+
import org.junit.Test;
2326

24-
public class TestLinkedBufferInput extends TestCase {
27+
public class TestLinkedBufferInput {
2528
@Test
2629
public void testReadByte() throws IOException {
2730
byte[] src = new byte[8];
@@ -164,9 +167,8 @@ public void testFeedByteArrayCopyNoCopy() throws IOException {
164167

165168
try {
166169
b2.readByte();
167-
assertTrue(false);
170+
fail();
168171
} catch(EndOfBufferException eof) {
169-
assertTrue(true);
170172
}
171173
}
172174

@@ -199,11 +201,11 @@ public void testGetPrimitives() throws IOException {
199201
assertEquals(2L, b.getLong());
200202
assertEquals(2L, b.getLong());
201203
b.advance();
202-
assertEquals(1.1f, b.getFloat());
203-
assertEquals(1.1f, b.getFloat());
204+
assertEquals(1.1f, b.getFloat(), 0.000001f);
205+
assertEquals(1.1f, b.getFloat(), 0.000001f);
204206
b.advance();
205-
assertEquals(1.1, b.getDouble());
206-
assertEquals(1.1, b.getDouble());
207+
assertEquals(1.1, b.getDouble(), 0.000001);
208+
assertEquals(1.1, b.getDouble(), 0.000001);
207209
b.advance();
208210
}
209211
}
@@ -233,9 +235,8 @@ public void testGetPrimitivesChunks() throws IOException {
233235
for(int j=0; j < 2; j++) {
234236
try {
235237
b.getShort();
236-
assertTrue(false);
238+
fail();
237239
} catch(EndOfBufferException eof) {
238-
assertTrue(true);
239240
}
240241
b.feed(src, p++, 1, true);
241242
}
@@ -246,9 +247,8 @@ public void testGetPrimitivesChunks() throws IOException {
246247
for(int j=0; j < 4; j++) {
247248
try {
248249
b.getInt();
249-
assertTrue(false);
250+
fail();
250251
} catch(EndOfBufferException eof) {
251-
assertTrue(true);
252252
}
253253
b.feed(src, p++, 1, true);
254254
}
@@ -259,9 +259,8 @@ public void testGetPrimitivesChunks() throws IOException {
259259
for(int j=0; j < 8; j++) {
260260
try {
261261
b.getLong();
262-
assertTrue(false);
262+
fail();
263263
} catch(EndOfBufferException eof) {
264-
assertTrue(true);
265264
}
266265
b.feed(src, p++, 1, true);
267266
}
@@ -272,27 +271,25 @@ public void testGetPrimitivesChunks() throws IOException {
272271
for(int j=0; j < 4; j++) {
273272
try {
274273
b.getFloat();
275-
assertTrue(false);
274+
fail();
276275
} catch(EndOfBufferException eof) {
277-
assertTrue(true);
278276
}
279277
b.feed(src, p++, 1, true);
280278
}
281-
assertEquals(1.1f, b.getFloat());
282-
assertEquals(1.1f, b.getFloat());
279+
assertEquals(1.1f, b.getFloat(), 0.000001f);
280+
assertEquals(1.1f, b.getFloat(), 0.000001f);
283281
b.advance();
284282

285283
for(int j=0; j < 8; j++) {
286284
try {
287285
b.getDouble();
288-
assertTrue(false);
286+
fail();
289287
} catch(EndOfBufferException eof) {
290-
assertTrue(true);
291288
}
292289
b.feed(src, p++, 1, true);
293290
}
294-
assertEquals(1.1, b.getDouble());
295-
assertEquals(1.1, b.getDouble());
291+
assertEquals(1.1, b.getDouble(), 0.000001);
292+
assertEquals(1.1, b.getDouble(), 0.000001);
296293
b.advance();
297294
}
298295
}
@@ -432,9 +429,8 @@ public void testClear() throws IOException {
432429
for(int i=0; i < 2; i++) {
433430
try {
434431
b.readByte();
435-
assertTrue(false);
432+
fail();
436433
} catch(EndOfBufferException eof) {
437-
assertTrue(true);
438434
}
439435

440436
b.feed(src);
@@ -453,13 +449,11 @@ public void testClear() throws IOException {
453449
}
454450
}
455451

456-
@Test
457452
private void assertEndOfBuffer(LinkedBufferInput b) throws IOException {
458453
try {
459454
b.readByte();
460-
assertTrue(false);
455+
fail();
461456
} catch(EndOfBufferException eof) {
462-
assertTrue(true);
463457
}
464458
}
465459
}

src/test/java/org/msgpack/io/TestLinkedBufferOutput.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.msgpack.io;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertArrayEquals;
5+
36
import java.math.BigInteger;
47
import java.nio.ByteBuffer;
58
import java.io.IOException;
@@ -12,13 +15,12 @@
1215
import java.util.Map;
1316
import java.util.Arrays;
1417

15-
import org.junit.Test;
1618
import org.msgpack.MessagePack;
1719
import org.msgpack.value.Value;
1820

19-
import junit.framework.TestCase;
21+
import org.junit.Test;
2022

21-
public class TestLinkedBufferOutput extends TestCase {
23+
public class TestLinkedBufferOutput {
2224
@Test
2325
public void testGetSize() throws IOException {
2426
LinkedBufferOutput o = new LinkedBufferOutput(10);
@@ -48,7 +50,7 @@ public void testWritePrimitives() throws IOException {
4850
byte[] b1 = bo.toByteArray();
4951
byte[] b2 = o2.toByteArray();
5052
assertEquals(b1.length, b2.length);
51-
assertTrue(Arrays.equals(b1, b2));
53+
assertArrayEquals(b1, b2);
5254
}
5355

5456
@Test
@@ -77,7 +79,7 @@ public void testWriteByteAndPrimitives() throws IOException {
7779
byte[] b1 = bo.toByteArray();
7880
byte[] b2 = o2.toByteArray();
7981
assertEquals(b1.length, b2.length);
80-
assertTrue(Arrays.equals(b1, b2));
82+
assertArrayEquals(b1, b2);
8183
}
8284

8385
@Test
@@ -95,7 +97,7 @@ public void testWrite() throws IOException {
9597
byte[] b1 = bo.toByteArray();
9698
byte[] b2 = o2.toByteArray();
9799
assertEquals(b1.length, b2.length);
98-
assertTrue(Arrays.equals(b1, b2));
100+
assertArrayEquals(b1, b2);
99101
}
100102
}
101103

0 commit comments

Comments
 (0)