restructuring
[lwes-java.git] / src / test / lwes / util / NumberCodecTest.java
blob6193b39dca6e10b8c8de888c85920659a0a56bfb
1 package org.lwes.util;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import org.junit.Test;
7 import java.math.BigInteger;
9 /**
10 * @author fmaritato
13 public class NumberCodecTest {
15 @Test
16 public void testToHexString() {
17 String s = NumberCodec.toHexString((byte) 1);
18 assertNotNull(s);
19 assertEquals("Byte to hex incorrect", "01", s);
21 s = NumberCodec.toHexString((short) 1);
22 assertNotNull(s);
23 assertEquals("Short to hex incorrect", "0001", s);
25 s = NumberCodec.toHexString(1);
26 assertNotNull(s);
27 assertEquals("Int to hex incorrect", "00000001", s);
29 s = NumberCodec.toHexString((long) 1);
30 assertNotNull(s);
31 assertEquals("Long to hex incorrect", "0000000000000001", s);
33 s = NumberCodec.toHexString(new BigInteger("1000"));
34 assertNotNull(s);
35 assertEquals("BigInteger to hex incorrect", "3e8", s);
38 @Test
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());
59 @Test
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);
71 @Test
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);