kafka-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nehanarkh...@apache.org
Subject git commit: KAFKA-1469 Util.abs function does not return correct absolute values for negative values; patched by Guozhang Wang and Neha Narkhede
Date Fri, 06 Jun 2014 17:03:43 GMT
Repository: kafka
Updated Branches:
  refs/heads/trunk ee4456a3f -> d30071711


KAFKA-1469 Util.abs function does not return correct absolute values for negative values;
patched by Guozhang Wang and Neha Narkhede


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/d3007171
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/d3007171
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/d3007171

Branch: refs/heads/trunk
Commit: d3007171102a95cd2979fad6dec5d0bbefb57d7a
Parents: ee4456a
Author: Sebastian Geller <info@cmb-tradecenter.org>
Authored: Fri Jun 6 10:02:38 2014 -0700
Committer: Neha Narkhede <neha.narkhede@gmail.com>
Committed: Fri Jun 6 10:03:28 2014 -0700

----------------------------------------------------------------------
 core/src/main/scala/kafka/utils/Utils.scala          | 2 +-
 core/src/test/scala/unit/kafka/utils/UtilsTest.scala | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/d3007171/core/src/main/scala/kafka/utils/Utils.scala
----------------------------------------------------------------------
diff --git a/core/src/main/scala/kafka/utils/Utils.scala b/core/src/main/scala/kafka/utils/Utils.scala
index 5c5c4e7..b29981b 100644
--- a/core/src/main/scala/kafka/utils/Utils.scala
+++ b/core/src/main/scala/kafka/utils/Utils.scala
@@ -480,7 +480,7 @@ object Utils extends Logging {
    * Get the absolute value of the given number. If the number is Int.MinValue return 0.
    * This is different from java.lang.Math.abs or scala.math.abs in that they return Int.MinValue
(!).
    */
-  def abs(n: Int) = n & 0x7fffffff
+  def abs(n: Int) = if(n == Integer.MIN_VALUE) 0 else math.abs(n)
 
   /**
    * Replace the given string suffix with the new suffix. If the string doesn't end with
the given suffix throw an exception.

http://git-wip-us.apache.org/repos/asf/kafka/blob/d3007171/core/src/test/scala/unit/kafka/utils/UtilsTest.scala
----------------------------------------------------------------------
diff --git a/core/src/test/scala/unit/kafka/utils/UtilsTest.scala b/core/src/test/scala/unit/kafka/utils/UtilsTest.scala
index 920f318..a502349 100644
--- a/core/src/test/scala/unit/kafka/utils/UtilsTest.scala
+++ b/core/src/test/scala/unit/kafka/utils/UtilsTest.scala
@@ -64,6 +64,15 @@ class UtilsTest extends JUnitSuite {
       assertTrue(Arrays.equals(bytes, Utils.readBytes(ByteBuffer.wrap(bytes))))
     }
   }
+
+  @Test
+  def testAbs() {
+    assertEquals(0, Utils.abs(Integer.MIN_VALUE))
+    assertEquals(1, Utils.abs(-1))
+    assertEquals(0, Utils.abs(0))
+    assertEquals(1, Utils.abs(1))
+    assertEquals(Integer.MAX_VALUE, Utils.abs(Integer.MAX_VALUE))
+  }
   
   @Test
   def testReplaceSuffix() {


Mime
View raw message