2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.umu.cops.stack;
7 import org.junit.Assert;
9 import org.pcmm.rcd.IPCMMClient;
11 import java.util.Random;
14 * Tests the public static COPSMsgParser methods
16 public class COPSMsgParserTest {
19 public void testCombineTwoBytesToOne() {
22 byte byte2 = (byte)15;
23 byte combined = (byte)((byte1) * 16 + (byte2));
25 System.out.println("Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
30 public void testCombineAndSplitForAllValidCombinations() {
31 for (byte byte1 = 0; byte1 < 16; byte1++ ) {
32 for (byte byte2 = 0; byte2 < 16; byte2++ ) {
33 int combined = COPSMsgParser.combineNibbles(byte1, byte2);
34 System.out.println("byte1 = " + byte1 + " byte2 = " + byte2 + " Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
36 Assert.assertTrue(combined >= 0 && combined < 256);
37 byte[] nibbles = COPSMsgParser.splitByteToNibbles((byte) combined);
39 Assert.assertEquals("Nibble 1 value = " + nibbles[0], byte1, nibbles[0]);
40 Assert.assertEquals("Nibble 2 value = " + nibbles[1], byte2, nibbles[1]);
46 public void testBytesToShortMin() {
47 final byte byte1 = (byte)0;
48 final byte byte2 = (byte)0;
49 final short val = COPSMsgParser.bytesToShort(byte1, byte2);
50 final byte[] outBytes = COPSMsgParser.shortToBytes(val);
51 Assert.assertEquals(byte1, outBytes[0]);
52 Assert.assertEquals(byte2, outBytes[1]);
53 Assert.assertEquals(0, val);
57 public void testBytesToShortMax() {
58 final byte byte1 = (byte)255;
59 final byte byte2 = (byte)255;
60 final short val = COPSMsgParser.bytesToShort(byte1, byte2);
61 final byte[] outBytes = COPSMsgParser.shortToBytes(val);
62 Assert.assertEquals(byte1, outBytes[0]);
63 Assert.assertEquals(byte2, outBytes[1]);
67 public void bytesToShortAndBack() {
68 final Random rnd = new Random();
69 final short val = (short)rnd.nextInt();
70 final byte[] bytes = COPSMsgParser.shortToBytes(val);
71 final short parsed = COPSMsgParser.bytesToShort(bytes[0], bytes[1]);
72 Assert.assertEquals(val, parsed);
76 public void testBytesToIntMin() {
77 final byte byte1 = (byte)0;
78 final byte byte2 = (byte)0;
79 final byte byte3 = (byte)0;
80 final byte byte4 = (byte)0;
81 final int val = COPSMsgParser.bytesToInt(byte1, byte2, byte3, byte4);
82 final byte[] outBytes = COPSMsgParser.intToBytes(val);
83 Assert.assertEquals(byte1, outBytes[0]);
84 Assert.assertEquals(byte2, outBytes[1]);
85 Assert.assertEquals(byte3, outBytes[2]);
86 Assert.assertEquals(byte4, outBytes[3]);
87 Assert.assertEquals(0, val);
91 public void intToBytesAndBack() {
92 final int val = 100001;
93 final byte[] bytes = COPSMsgParser.intToBytes(val);
94 final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
95 Assert.assertEquals(val, parsed);
96 System.out.println("Sucessfully converted value - " + val);
100 public void randomIntToBytesAndBack() {
101 for (int i = 0; i < 5; i++) {
102 final Random rnd = new Random();
103 final int val = rnd.nextInt();
104 final byte[] bytes = COPSMsgParser.intToBytes(val);
105 final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
106 Assert.assertEquals(val, parsed);
107 System.out.println("Sucessfully converted value - " + val);
112 public void testBytesToShortPCMMClientType() {
113 final byte[] outBytes = COPSMsgParser.shortToBytes(IPCMMClient.CLIENT_TYPE);
114 final short val = COPSMsgParser.bytesToShort(outBytes[0], outBytes[1]);
115 Assert.assertEquals(IPCMMClient.CLIENT_TYPE, val);
118 @Test(expected = IllegalArgumentException.class)
119 public void testCombineByte1TooBig() {
120 COPSMsgParser.combineNibbles((byte)16, (byte)0);
123 @Test(expected = IllegalArgumentException.class)
124 public void testCombineByte2TooBig() {
125 COPSMsgParser.combineNibbles((byte)0, (byte)16);
128 @Test(expected = IllegalArgumentException.class)
129 public void testCombineByte1TooSmall() {
130 COPSMsgParser.combineNibbles((byte)-1, (byte)0);
133 @Test(expected = IllegalArgumentException.class)
134 public void testCombineByte2TooSmall() {
135 COPSMsgParser.combineNibbles((byte)0, (byte)-1);
138 // TODO - determine if tests for marshalling & un should be done with this class or implicitly via the COPSMsg objects