1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.opendaylight.openflowjava.protocol.impl.util;
\r
4 import io.netty.buffer.ByteBuf;
\r
5 import io.netty.buffer.UnpooledByteBufAllocator;
\r
7 import java.util.HashMap;
\r
8 import java.util.Map;
\r
10 import org.junit.Assert;
\r
11 import org.junit.Test;
\r
14 * @author michal.polkorab
\r
17 public class ByteBufUtilsTest {
\r
19 private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
\r
22 * Test of {@link ByteBufUtils#hexStringToBytes(String)}
\r
25 public void testHexStringToBytes() {
\r
26 byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
\r
28 Assert.assertArrayEquals(expected, data);
\r
32 * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
\r
35 public void testHexStringToBytes2() {
\r
36 byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
\r
38 Assert.assertArrayEquals(expected, data);
\r
42 * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
\r
45 public void testHexStringToByteBuf() {
\r
46 ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
\r
48 Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
\r
52 * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
\r
55 public void testHexStringToGivenByteBuf() {
\r
56 ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
57 ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
\r
59 Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));
\r
62 private static byte[] byteBufToByteArray(ByteBuf bb) {
\r
63 byte[] result = new byte[bb.readableBytes()];
\r
64 bb.readBytes(result);
\r
69 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
\r
72 public void testFillBitmaskByEmptyMap() {
\r
73 Map<Integer, Boolean> emptyMap = new HashMap<>();
\r
74 String expectedBinaryString = "00000000000000000000000000000000";
\r
75 String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
\r
77 Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
\r
80 private String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
\r
81 String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));;
\r
82 return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
\r
86 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
\r
89 public void testFillBitmaskByFullMap() {
\r
90 Map<Integer, Boolean> fullMap = new HashMap<>();
\r
91 String expectedBinaryString = "11111111111111111111111111111111";
\r
92 String bitmaskValueInBinarySytring;
\r
93 for(Integer i=0;i<=31;i++) {
\r
94 fullMap.put(i, true);
\r
96 bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
\r
97 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
\r
101 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
\r
104 public void testFillBitmaskByZeroMap() {
\r
105 Map<Integer, Boolean> zeroMap = new HashMap<>();
\r
106 String expectedBinaryString = "00000000000000000000000000000000";
\r
107 String bitmaskValueInBinarySytring;
\r
108 for(Integer i=0;i<=31;i++) {
\r
109 zeroMap.put(i, false);
\r
111 bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
\r
112 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
\r
116 * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
\r
119 public void testFillBitmaskByRandomSet() {
\r
120 Map<Integer, Boolean> randomMap = new HashMap<>();
\r
121 String expectedBinaryString = "00000000000000000111100000000000";
\r
122 String bitmaskValueInBinarySytring;
\r
124 for(Integer i=0;i<=31;i++) {
\r
126 if(i>=11 && i<=14) {
\r
129 randomMap.put(i, mapValue);
\r
131 bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
\r
132 Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
\r