Fixes Integer and IntegerTest to unbreak the build.

Patch by: fabbott
Review by: me


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1658 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/Integer.java b/user/super/com/google/gwt/emul/java/lang/Integer.java
index 4427deb..c5b0c15 100644
--- a/user/super/com/google/gwt/emul/java/lang/Integer.java
+++ b/user/super/com/google/gwt/emul/java/lang/Integer.java
@@ -54,9 +54,11 @@
   public static int highestOneBit(int i) {
     if (i < 0) {
       return MIN_VALUE;
+    } else if (i == 0) {
+      return 0;
     } else {
       int rtn;
-      for (rtn = 0x40000000; (rtn >> 1) > i; rtn = rtn >> 1) {
+      for (rtn = 0x40000000; (rtn & i) == 0; rtn = rtn >> 1) {
         // loop down until smaller
       }
       return rtn;
@@ -65,10 +67,12 @@
 
   public static int lowestOneBit(int i) {
     if (i == 0) {
-      return 32;
+      return 0;
+    } else if (i == Integer.MIN_VALUE) {
+      return 0x80000000;
     } else {
       int r = 1;
-      while ((r & i) != 0) {
+      while ((r & i) == 0) {
         r = r * 2;
       }
       return r;
@@ -86,13 +90,11 @@
   }
 
   public static int numberOfTrailingZeros(int i) {
-    if (i < 0) {
-      return 0;
-    } else if (i == 0) {
-      return SIZE;
+    if (i == 0) {
+      return 32;
     } else {
       int rtn = 0;
-      for (int r = 1; (r & i) != 0; r = r * 2) {
+      for (int r = 1; (r & i) == 0; r = r * 2) {
         rtn++;
       }
       return rtn;
@@ -108,16 +110,20 @@
   }
 
   public static int reverse(int i) {
-    int acc = 0;
+    int ui = i & 0x7fffffff; // avoid sign extension
+    int acc = 0;  
     int front = 0x80000000;
     int back = 1;
     int swing = 31;
-    while (swing > 15) {
-      acc = acc | ((i & front) >> swing) | ((i & back) << swing);
-      swing--;
+    while (swing > 0) {
+      acc = acc | ((ui & front) >> swing) | ((ui & back) << swing);
+      swing -= 2;
       front = front >> 1;
       back = back << 1;
     }
+    if (i < 0) {
+      acc = acc | 0x1; // restore the real value of 0x80000000
+    }
     return acc;
   }
 
@@ -134,10 +140,17 @@
   }
 
   public static int rotateRight(int i, int distance) {
+    int ui = i & 0x7fffffff; // avoid sign extension
+    int carry = (i < 0) ? 0x40000000 : 0; // 0x80000000 rightshifted 1
     while (distance-- > 0) {
-      i = ((i & 1) == 0 ? 0 : 0x80000000) | i >> 1;
+      int nextcarry = ui & 1;
+      ui = carry | (ui >> 1);
+      carry = (nextcarry == 0) ? 0 : 0x40000000;
     }
-    return i;
+    if (carry != 0) {
+      ui = ui | 0x80000000;
+    }
+    return ui;
   }
 
   public static int signum(int i) {
diff --git a/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java b/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java
index 1a43fbc..e33cf6e 100644
--- a/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java
+++ b/user/test/com/google/gwt/emultest/java/lang/IntegerTest.java
@@ -137,12 +137,6 @@
     assertEquals(1234, new Integer(1234).hashCode());
   }
 
-  public void testHexString() {
-    assertEquals("3039", Integer.toHexString(12345));
-    assertEquals("0", Integer.toHexString(0));
-    assertEquals("ffffcfc7", Integer.toHexString(-12345));
-  }
-
   public void testHighestOneBit() {
     assertEquals(0, Integer.highestOneBit(0));
     assertEquals(Integer.MIN_VALUE, Integer.highestOneBit(-1));
@@ -190,6 +184,10 @@
   }
 
   public void testReverseBytes() {
+    assertEquals(0, Integer.reverseBytes(0));
+    // two-complement bugs?
+    assertEquals(0x84218421, Integer.reverseBytes(0x21842184));
+    assertEquals(0x12481248, Integer.reverseBytes(0x48124812));
   }
 
   public void testRotateLeft() {
@@ -223,6 +221,10 @@
         Integer.valueOf(Integer.MAX_VALUE).intValue());
   }
 
+  public void testToHexString() {
+    // TODO: not implemented in our JRE
+  }
+
   public void testToString() {
     assertEquals("12345", new Integer(12345).toString());
     assertEquals("-12345", new Integer("-12345").toString());