-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
-package org.opendaylight.openflowjava.protocol.impl.util;\r
-\r
-import io.netty.buffer.ByteBuf;\r
-import io.netty.buffer.UnpooledByteBufAllocator;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.junit.Assert;\r
-import org.junit.Test;\r
-\r
-/**\r
- * @author michal.polkorab\r
- *\r
- */\r
-public class ByteBufUtilsTest {\r
-\r
- private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#hexStringToBytes(String)}\r
- */\r
- @Test\r
- public void testHexStringToBytes() {\r
- byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");\r
-\r
- Assert.assertArrayEquals(expected, data);\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}\r
- */\r
- @Test\r
- public void testHexStringToBytes2() {\r
- byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);\r
-\r
- Assert.assertArrayEquals(expected, data);\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}\r
- */\r
- @Test\r
- public void testHexStringToByteBuf() {\r
- ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");\r
- \r
- Assert.assertArrayEquals(expected, byteBufToByteArray(bb));\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}\r
- */\r
- @Test\r
- public void testHexStringToGivenByteBuf() {\r
- ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();\r
- ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);\r
-\r
- Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));\r
- }\r
- \r
- private static byte[] byteBufToByteArray(ByteBuf bb) {\r
- byte[] result = new byte[bb.readableBytes()];\r
- bb.readBytes(result);\r
- return result;\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskByEmptyMap() {\r
- Map<Integer, Boolean> emptyMap = new HashMap<>();\r
- String expectedBinaryString = "00000000000000000000000000000000";\r
- String bitmaskInBinaryString = toBinaryString(emptyMap, 32);\r
- \r
- Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);\r
- }\r
-\r
- private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {\r
- String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap)); \r
- return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskByFullMap() {\r
- Map<Integer, Boolean> fullMap = new HashMap<>();\r
- String expectedBinaryString = "11111111111111111111111111111111";\r
- String bitmaskValueInBinarySytring;\r
- for(Integer i=0;i<=31;i++) {\r
- fullMap.put(i, true);\r
- }\r
- bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);\r
- Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskByZeroMap() {\r
- Map<Integer, Boolean> zeroMap = new HashMap<>();\r
- String expectedBinaryString = "00000000000000000000000000000000";\r
- String bitmaskValueInBinarySytring;\r
- for(Integer i=0;i<=31;i++) {\r
- zeroMap.put(i, false);\r
- }\r
- bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);\r
- Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskByRandomSet() {\r
- Map<Integer, Boolean> randomMap = new HashMap<>();\r
- String expectedBinaryString = "00000000000000000111100000000000";\r
- String bitmaskValueInBinarySytring;\r
- Boolean mapValue;\r
- for(Integer i=0;i<=31;i++) {\r
- mapValue = false;\r
- if(i>=11 && i<=14) {\r
- mapValue = true;\r
- }\r
- randomMap.put(i, mapValue);\r
- }\r
- bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);\r
- Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskByEmptyList() {\r
- List<Boolean> emptyList = new ArrayList<>();\r
- emptyList.add(null);\r
- String expectedBinaryString = "00000000000000000000000000000000";\r
- String bitmaskInBinaryString = listToBinaryString(emptyList, 32);\r
- \r
- Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);\r
- }\r
-\r
- private static String listToBinaryString(List<Boolean> emptyList, int length) {\r
- int[] bitMaskArray;\r
- bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);\r
- String binaryString = Integer.toBinaryString(bitMaskArray[0]); \r
- return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskByFullList() {\r
- List<Boolean> fullList = new ArrayList<>();\r
- String expectedBinaryString = "11111111111111111111111111111111";\r
- String bitmaskValueInBinarySytring;\r
- for(Integer i=0;i<=31;i++) {\r
- fullList.add(true);\r
- }\r
- bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);\r
- Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskByZeroList() {\r
- List<Boolean> zeroList = new ArrayList<>();\r
- String expectedBinaryString = "00000000000000000000000000000000";\r
- String bitmaskValueInBinarySytring;\r
- for(Integer i=0;i<=31;i++) {\r
- zeroList.add(false);\r
- }\r
- bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);\r
- Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
- }\r
- \r
- /**\r
- * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}\r
- */\r
- @Test\r
- public void testFillBitmaskFromRandomList() {\r
- List<Boolean> randomList = new ArrayList<>();\r
- String expectedBinaryString = "00000000000000000111100000000000";\r
- String bitmaskValueInBinarySytring;\r
- Boolean listValue;\r
- for(Integer i=0;i<=31;i++) {\r
- listValue = false;\r
- if(i>=11 && i<=14) {\r
- listValue = true;\r
- }\r
- randomList.add(listValue);\r
- }\r
- bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);\r
- Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
- }\r
-\r
-}\r
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowjava.protocol.impl.util;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.UnpooledByteBufAllocator;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author michal.polkorab
+ *
+ */
+public class ByteBufUtilsTest {
+
+ private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};
+
+ /**
+ * Test of {@link ByteBufUtils#hexStringToBytes(String)}
+ */
+ @Test
+ public void testHexStringToBytes() {
+ byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");
+
+ Assert.assertArrayEquals(expected, data);
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}
+ */
+ @Test
+ public void testHexStringToBytes2() {
+ byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);
+
+ Assert.assertArrayEquals(expected, data);
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}
+ */
+ @Test
+ public void testHexStringToByteBuf() {
+ ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");
+
+ Assert.assertArrayEquals(expected, byteBufToByteArray(bb));
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}
+ */
+ @Test
+ public void testHexStringToGivenByteBuf() {
+ ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
+ ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);
+
+ Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));
+ }
+
+ private static byte[] byteBufToByteArray(ByteBuf bb) {
+ byte[] result = new byte[bb.readableBytes()];
+ bb.readBytes(result);
+ return result;
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskByEmptyMap() {
+ Map<Integer, Boolean> emptyMap = new HashMap<>();
+ String expectedBinaryString = "00000000000000000000000000000000";
+ String bitmaskInBinaryString = toBinaryString(emptyMap, 32);
+
+ Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
+ }
+
+ private static String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {
+ String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));
+ return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskByFullMap() {
+ Map<Integer, Boolean> fullMap = new HashMap<>();
+ String expectedBinaryString = "11111111111111111111111111111111";
+ String bitmaskValueInBinarySytring;
+ for(Integer i=0;i<=31;i++) {
+ fullMap.put(i, true);
+ }
+ bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);
+ Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskByZeroMap() {
+ Map<Integer, Boolean> zeroMap = new HashMap<>();
+ String expectedBinaryString = "00000000000000000000000000000000";
+ String bitmaskValueInBinarySytring;
+ for(Integer i=0;i<=31;i++) {
+ zeroMap.put(i, false);
+ }
+ bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);
+ Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskByRandomSet() {
+ Map<Integer, Boolean> randomMap = new HashMap<>();
+ String expectedBinaryString = "00000000000000000111100000000000";
+ String bitmaskValueInBinarySytring;
+ Boolean mapValue;
+ for(Integer i=0;i<=31;i++) {
+ mapValue = false;
+ if(i>=11 && i<=14) {
+ mapValue = true;
+ }
+ randomMap.put(i, mapValue);
+ }
+ bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);
+ Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskByEmptyList() {
+ List<Boolean> emptyList = new ArrayList<>();
+ emptyList.add(null);
+ String expectedBinaryString = "00000000000000000000000000000000";
+ String bitmaskInBinaryString = listToBinaryString(emptyList, 32);
+
+ Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);
+ }
+
+ private static String listToBinaryString(List<Boolean> emptyList, int length) {
+ int[] bitMaskArray;
+ bitMaskArray = ByteBufUtils.fillBitMaskFromList(emptyList);
+ String binaryString = Integer.toBinaryString(bitMaskArray[0]);
+ return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskByFullList() {
+ List<Boolean> fullList = new ArrayList<>();
+ String expectedBinaryString = "11111111111111111111111111111111";
+ String bitmaskValueInBinarySytring;
+ for(Integer i=0;i<=31;i++) {
+ fullList.add(true);
+ }
+ bitmaskValueInBinarySytring = listToBinaryString(fullList, 32);
+ Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskByZeroList() {
+ List<Boolean> zeroList = new ArrayList<>();
+ String expectedBinaryString = "00000000000000000000000000000000";
+ String bitmaskValueInBinarySytring;
+ for(Integer i=0;i<=31;i++) {
+ zeroList.add(false);
+ }
+ bitmaskValueInBinarySytring = listToBinaryString(zeroList, 32);
+ Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
+ }
+
+ /**
+ * Test of {@link ByteBufUtils#fillBitMaskFromList(java.util.Map)}
+ */
+ @Test
+ public void testFillBitmaskFromRandomList() {
+ List<Boolean> randomList = new ArrayList<>();
+ String expectedBinaryString = "00000000000000000111100000000000";
+ String bitmaskValueInBinarySytring;
+ Boolean listValue;
+ for(Integer i=0;i<=31;i++) {
+ listValue = false;
+ if(i>=11 && i<=14) {
+ listValue = true;
+ }
+ randomList.add(listValue);
+ }
+ bitmaskValueInBinarySytring = listToBinaryString(randomList, 32);
+ Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);
+ }
+
+}