Author: toad
Date: 2008-09-05 11:52:34 +0000 (Fri, 05 Sep 2008)
New Revision: 22443

Modified:
   trunk/freenet/src/freenet/support/Fields.java
   trunk/freenet/test/freenet/support/FieldsTest.java
Log:
Add int unit tests.


Modified: trunk/freenet/src/freenet/support/Fields.java
===================================================================
--- trunk/freenet/src/freenet/support/Fields.java       2008-09-05 11:17:22 UTC 
(rev 22442)
+++ trunk/freenet/src/freenet/support/Fields.java       2008-09-05 11:52:34 UTC 
(rev 22443)
@@ -570,7 +570,16 @@
                }
                return buf;
        }
-
+       
+       public static byte[] intToBytes(int x) {
+               byte[] buf = new byte[4];
+                       for(int j = 0; j < 4; j++) {
+                               buf[j] = (byte) x;
+                               x >>>= 8;
+                       }
+               return buf;
+       }
+       
        public static long parseLong(String s, long defaultValue) {
                try {
                        return Long.parseLong(s);

Modified: trunk/freenet/test/freenet/support/FieldsTest.java
===================================================================
--- trunk/freenet/test/freenet/support/FieldsTest.java  2008-09-05 11:17:22 UTC 
(rev 22442)
+++ trunk/freenet/test/freenet/support/FieldsTest.java  2008-09-05 11:52:34 UTC 
(rev 22443)
@@ -241,6 +241,73 @@
                assertTrue(l3.equals(l1)); // should be same due to 
Fields.longHashcode
        }       

+       public void testIntsToBytes() {
+               int[] longs = new int[] {};
+               doRoundTripIntsArrayToBytesArray(longs);
+               
+               longs = new int[] {Integer.MIN_VALUE};
+               doRoundTripIntsArrayToBytesArray(longs);
+               
+               longs = new int[] {0, Integer.MAX_VALUE, Integer.MIN_VALUE};
+               doRoundTripIntsArrayToBytesArray(longs);
+               
+               longs = new int[] {33685760, 51511577};
+               doRoundTripIntsArrayToBytesArray(longs);                
+       }
+
+       private void doRoundTripIntsArrayToBytesArray(int[] ints) {
+               byte[] bytes = Fields.intsToBytes(ints);
+               assert(bytes.length == ints.length * 8);
+               
+               int[] outLongs = Fields.bytesToInts(bytes);
+               for(int i = 0; i < ints.length; i++) {
+                       assertTrue(outLongs[i] == ints[i]);
+               }
+       }
+       
+       public void testBytesToIntException() {
+               byte[] bytes = new byte[3];
+               try {
+                       Fields.bytesToLongs(bytes, 0, bytes.length);
+                       fail();
+               }
+               catch(IllegalArgumentException e){
+                       // expect this
+               }
+       }
+       
+       public void testBytesToInt() {
+               
+               byte[] bytes = new byte[] { 0, 1, 2, 2 };
+               
+               int outLong = Fields.bytesToInt(bytes, 0);
+               assertEquals(outLong, 33685760);
+               
+               doTestRoundTripBytesArrayToInt(bytes);
+               
+               bytes = new byte[] {};
+               try{
+                       doTestRoundTripBytesArrayToInt(bytes);
+                       fail();
+               }
+               catch(IllegalArgumentException e) {
+                       //expect this
+               }
+               
+               bytes = new byte[] {1, 1, 1, 1};
+               doTestRoundTripBytesArrayToInt(bytes);
+               
+       }
+       
+       private void doTestRoundTripBytesArrayToInt(byte[] inBytes) {
+
+               int outLong = Fields.bytesToInt(inBytes, 0);
+               byte[] outBytes = Fields.intToBytes(outLong);
+               for(int i = 0; i < inBytes.length; i++) {
+                       assertEquals(outBytes[i], inBytes[i]);
+               }
+       }
+       
        public void testLongsToBytes() {
                long[] longs = new long[] {};
                doRoundTripLongsArrayToBytesArray(longs);
@@ -307,4 +374,5 @@
                        assertEquals(outBytes[i], inBytes[i]);
                }
        }
+
 }


Reply via email to