3 import static org
.junit
.Assert
.assertEquals
;
4 import static org
.junit
.Assert
.assertNotNull
;
7 import java
.math
.BigInteger
;
13 public class NumberCodecTest
{
16 public void testToHexString() {
17 String s
= NumberCodec
.toHexString((byte) 1);
19 assertEquals("Byte to hex incorrect", "01", s
);
21 s
= NumberCodec
.toHexString((short) 1);
23 assertEquals("Short to hex incorrect", "0001", s
);
25 s
= NumberCodec
.toHexString(1);
27 assertEquals("Int to hex incorrect", "00000001", s
);
29 s
= NumberCodec
.toHexString((long) 1);
31 assertEquals("Long to hex incorrect", "0000000000000001", s
);
33 s
= NumberCodec
.toHexString(new BigInteger("1000"));
35 assertEquals("BigInteger to hex incorrect", "3e8", s
);
39 public void testWriteHexStringToBuffer() {
40 StringBuffer buf
= new StringBuffer(16);
42 NumberCodec
.writeHexString((byte) 1, buf
);
43 assertEquals("01", buf
.toString());
44 buf
.delete(0, buf
.length());
46 NumberCodec
.writeHexString((short) 1, buf
);
47 assertEquals("0001", buf
.toString());
48 buf
.delete(0, buf
.length());
50 NumberCodec
.writeHexString(1, buf
);
51 assertEquals("00000001", buf
.toString());
52 buf
.delete(0, buf
.length());
54 NumberCodec
.writeHexString((long) 1, buf
);
55 assertEquals("0000000000000001", buf
.toString());
56 buf
.delete(0, buf
.length());
60 public void testByteArrayToHexString() {
61 byte[] buf
= NumberCodec
.hexStringToByteArray("011e");
62 String hex1
= NumberCodec
.byteArrayToHexString(buf
);
63 assertEquals("byteArrayToHexString failed", "011e", hex1
);
64 String hex2
= NumberCodec
.byteArrayToHexString(buf
, 0, buf
.length
);
65 assertEquals("byteArrayToHexString failed", "011e", hex2
);
67 byte b
= NumberCodec
.byteFromHexString("0a");
68 assertEquals("byteFromHexString fail", 10, b
);
72 public void testEncodeDecode() {
74 // encodeLong and decodeLong call the unchecked versions underneath.
75 byte[] buf
= new byte[64];
76 NumberCodec
.encodeLong(123l, buf
, 0, buf
.length
);
77 long lval
= NumberCodec
.decodeLong(buf
, 0, buf
.length
);
78 assertEquals("encode/decode long unchecked fail", 123l, lval
);
80 NumberCodec
.encodeInt(123, buf
, 0, buf
.length
);
81 int ival
= NumberCodec
.decodeInt(buf
, 0, buf
.length
);
82 assertEquals("encode/decode int unchecked fail", 123, ival
);
84 NumberCodec
.encodeShort((short) 12, buf
, 0, buf
.length
);
85 short sval
= NumberCodec
.decodeShort(buf
, 0, buf
.length
);
86 assertEquals("encode/decode short unchecked fail", 12, sval
);