[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r2905 - freeway/src/org/gnu/freeway/cwrappers
From: |
mdonoughe |
Subject: |
[GNUnet-SVN] r2905 - freeway/src/org/gnu/freeway/cwrappers |
Date: |
Fri, 26 May 2006 11:40:55 -0700 (PDT) |
Author: mdonoughe
Date: 2006-05-26 11:40:53 -0700 (Fri, 26 May 2006)
New Revision: 2905
Added:
freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java
freeway/src/org/gnu/freeway/cwrappers/CUnsignedIntTest.java
Log:
CUnsignedInt
Added: freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java 2006-05-26
18:37:44 UTC (rev 2904)
+++ freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java 2006-05-26
18:40:53 UTC (rev 2905)
@@ -0,0 +1,81 @@
+ /*
+ This file is part of Freeway
+
+ Freeway is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ Freeway is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Freeway; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+package org.gnu.freeway.cwrappers;
+
+import org.gnu.freeway.cwrappers.util.CWrapper;
+
+/**
+ * @file freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java
+ * @brief A wrapper for using unsigned integers with JNI
+ * @author mdonoughe
+ */
+public class CUnsignedInt implements CWrapper {
+
+ private long value;
+
+ public CUnsignedInt(long value) {
+ validate(value);
+ this.value = value;
+ }
+
+ private void validate(long value) {
+ if(value < 0 || value > 4294967295l)
+ throw new IllegalArgumentException("Unsigned integers
cannot be less than 0 or more than 4,294,967,295.");
+ return;
+ }
+
+ /**
+ * Returns a byte array containing the integer in big endian format
+ */
+ public byte[] serializeToByteArray() {
+ byte[] retValue = {(byte) (value >> 24), (byte) (value >> 16),
(byte) (value >> 8), (byte) value};
+ return retValue;
+ }
+
+ public void deserializeFromByteArray(byte[] serializedData) {
+ // those bitmasks seem to keep the sign bit from moving around
+ value = ((serializedData[0] & 0xffl) << 24) |
((serializedData[1] & 0xffl) << 16) | ((serializedData[2] & 0xffl) << 8) |
((serializedData[3] & 0xffl));
+ }
+
+ public long getValue() {
+ return value;
+ }
+
+ public void setValue(long value) {
+ validate(value);
+ this.value = value;
+ }
+
+ public int getSerializedSize() {
+ return 4;
+ }
+
+ public static CUnsignedInt boxSignedInt(int value) {
+ return new CUnsignedInt(unsignedIntToLong(value));
+ }
+
+ public static long unsignedIntToLong(int value) {
+ return value & 0xFFFFFFFFl;
+ }
+
+ public static int longToUnsignedInt(long value) {
+ return (int) (value & 0xFFFFFFFFl);
+ }
+}
Added: freeway/src/org/gnu/freeway/cwrappers/CUnsignedIntTest.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/CUnsignedIntTest.java 2006-05-26
18:37:44 UTC (rev 2904)
+++ freeway/src/org/gnu/freeway/cwrappers/CUnsignedIntTest.java 2006-05-26
18:40:53 UTC (rev 2905)
@@ -0,0 +1,102 @@
+/*
+ This file is part of Freeway
+
+ Freeway is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
+
+ Freeway is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Freeway; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+ */
+
+package org.gnu.freeway.cwrappers;
+
+import junit.framework.TestCase;
+
+/**
+ * @file freeway/src/org/gnu/freeway/cwrappers/CUnsignedIntTest.java
+ * @brief A JUnit test for CUnsignedInt
+ * @author mdonoughe
+ */
+public class CUnsignedIntTest extends TestCase {
+
+ /*
+ * Test method for
'org.gnu.freeway.cwrappers.CUnsignedInt.CUnsignedInt(long)'
+ */
+ public void testCUnsignedInt() {
+ assertFalse(tryCUnsignedInt(-1));
+ assertTrue(tryCUnsignedInt(0));
+ assertTrue(tryCUnsignedInt(1));
+ assertTrue(tryCUnsignedInt(0xFFFFFFFFl));
+ assertFalse(tryCUnsignedInt(0 - 0xFFFFFFFFl));
+ assertFalse(tryCUnsignedInt(0x100000000l));
+ }
+
+ private boolean tryCUnsignedInt(long value) {
+ boolean exception = false;
+ try {
+ new CUnsignedInt(value);
+ } catch(IllegalArgumentException e) {
+ exception = true;
+ }
+ return !exception;
+ }
+
+ /*
+ * Test method for
'org.gnu.freeway.cwrappers.CUnsignedInt.serializeToByteArray()'
+ */
+ public void testSerializeToByteArray() {
+ CUnsignedInt cUnsignedInt = new CUnsignedInt(0x075BCD15l);
+ byte[] byteArray = cUnsignedInt.serializeToByteArray();
+ assertTrue(byteArray[0] == 0x07 && byteArray[1] == 0x5B &&
byteArray[2] == -51 && byteArray[3] == 0x15);
+ cUnsignedInt = new CUnsignedInt(0xC521974Fl);
+ byteArray = cUnsignedInt.serializeToByteArray();
+ assertTrue(byteArray[0] == -59 && byteArray[1] == 0x21 &&
byteArray[2] == -105 && byteArray[3] == 0x4F);
+ cUnsignedInt = new CUnsignedInt(0x9534D2B7l);
+ byteArray = cUnsignedInt.serializeToByteArray();
+ assertTrue(byteArray[0] == -107 && byteArray[1] == 0x34 &&
byteArray[2] == -46 && byteArray[3] == -73);
+ }
+
+ /*
+ * Test method for
'org.gnu.freeway.cwrappers.CUnsignedInt.deserializeFromByteArray(byte[])'
+ */
+ public void testDeserializeFromByteArray() {
+ CUnsignedInt cUnsignedInt = new CUnsignedInt(0);
+ byte[] byteArray1 = {0x7B, 0x2D, 0x43, 0x59};
+ cUnsignedInt.deserializeFromByteArray(byteArray1);
+ assertTrue(cUnsignedInt.getValue() == 0x7B2D4359l);
+ byte[] byteArray2 = {-107, 0x34, -46, -73};
+ cUnsignedInt.deserializeFromByteArray(byteArray2);
+ assertTrue(cUnsignedInt.getValue() == 0x9534D2B7l);
+ byte[] byteArray3 = {-59, 0x21, -105, 0x4F};
+ cUnsignedInt.deserializeFromByteArray(byteArray3);
+ assertTrue(cUnsignedInt.getValue() == 0xC521974Fl);
+ }
+
+ /*
+ * Test method for
'org.gnu.freeway.cwrappers.CUnsignedInt.unsignedIntToLong(int)'
+ */
+ public void testUnsignedIntToLong() {
+ assertTrue(CUnsignedInt.unsignedIntToLong(1) == 1l);
+ assertTrue(CUnsignedInt.unsignedIntToLong(-1) == 4294967295l);
+ assertTrue(CUnsignedInt.unsignedIntToLong(0xffffffff) ==
0xffffffffl);
+ }
+
+ /*
+ * Test method for
'org.gnu.freeway.cwrappers.CUnsignedInt.longToUnsignedInt(long)'
+ */
+ public void testLongToUnsignedInt() {
+ assertTrue(CUnsignedInt.longToUnsignedInt(1l) == 1);
+ assertTrue(CUnsignedInt.longToUnsignedInt(4294967295l) == -1);
+ assertTrue(CUnsignedInt.longToUnsignedInt(0xffffffffl) ==
0xffffffff);
+ }
+
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r2905 - freeway/src/org/gnu/freeway/cwrappers,
mdonoughe <=