From: Alessandro Boch Date: Mon, 10 Feb 2014 16:49:34 +0000 (+0000) Subject: Merge "Fixed bugs in getUnsigned methods of NetUtils class." X-Git-Tag: autorelease-tag-v20140601202136_82eb3f9~505 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=589b6309f5356e92b10a66ccc0fc302b0289dbd0;hp=78aac68702240a5e01c250030811b56cf1c5dd8e Merge "Fixed bugs in getUnsigned methods of NetUtils class." --- diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java index 6c3424c616..dc341625af 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 Cisco Systems, Inc. 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, @@ -482,7 +482,7 @@ public abstract class NetUtils { * @return the int variable containing the unsigned byte value */ public static int getUnsignedByte(byte b) { - return (b > 0) ? (int) b : (b & 0x7F | 0x80); + return b & 0xFF; } /** @@ -493,7 +493,7 @@ public abstract class NetUtils { * @return the int variable containing the unsigned short value */ public static int getUnsignedShort(short s) { - return (s > 0) ? (int) s : (s & 0x7FFF | 0x8000); + return s & 0xFFFF; } /** @@ -520,5 +520,4 @@ public abstract class NetUtils { public static byte[] getBroadcastMACAddr() { return Arrays.copyOf(BroadcastMACAddr, BroadcastMACAddr.length); } - } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java index b8bc6fb447..a2b12782ac 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java @@ -1,6 +1,5 @@ - /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 Cisco Systems, Inc. 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, @@ -449,4 +448,24 @@ public class NetUtilsTest { Assert.assertTrue(NetUtils .isIPAddressValid("2001:420:281:1004:407a:57f4:4d15:c355")); } + + @Test + public void testGetUnsignedByte() { + Assert.assertEquals(0, NetUtils.getUnsignedByte((byte) 0x00)); + Assert.assertEquals(1, NetUtils.getUnsignedByte((byte) 0x01)); + Assert.assertEquals(127, NetUtils.getUnsignedByte((byte) 0x7f)); + + Assert.assertEquals(128, NetUtils.getUnsignedByte((byte) 0x80)); + Assert.assertEquals(255, NetUtils.getUnsignedByte((byte) 0xff)); + } + + @Test + public void testGetUnsignedShort() { + Assert.assertEquals(0, NetUtils.getUnsignedShort((short) 0x0000)); + Assert.assertEquals(1, NetUtils.getUnsignedShort((short) 0x0001)); + Assert.assertEquals(32767, NetUtils.getUnsignedShort((short) 0x7fff)); + + Assert.assertEquals(32768, NetUtils.getUnsignedShort((short) 0x8000)); + Assert.assertEquals(65535, NetUtils.getUnsignedShort((short) 0xffff)); + } }