You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/06/05 09:03:39 UTC

[GitHub] cbickel closed pull request #3697: Add equals method to ensure that ByteSize instances satisfy Comparable contract

cbickel closed pull request #3697: Add equals method to ensure that ByteSize instances satisfy Comparable contract
URL: https://github.com/apache/incubator-openwhisk/pull/3697
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/scala/src/main/scala/whisk/core/entity/Size.scala b/common/scala/src/main/scala/whisk/core/entity/Size.scala
index 2d5b7d9a20..a51eb2e95d 100644
--- a/common/scala/src/main/scala/whisk/core/entity/Size.scala
+++ b/common/scala/src/main/scala/whisk/core/entity/Size.scala
@@ -22,7 +22,7 @@ import java.nio.charset.StandardCharsets
 import com.typesafe.config.ConfigValue
 import pureconfig._
 import spray.json._
-import whisk.core.entity.ByteSize.formatError
+import ByteSize.formatError
 
 object SizeUnits extends Enumeration {
 
@@ -71,6 +71,11 @@ case class ByteSize(size: Long, unit: SizeUnits.Unit) extends Ordered[ByteSize]
 
   def compare(other: ByteSize) = toBytes compare other.toBytes
 
+  override def equals(that: Any): Boolean = that match {
+    case t: ByteSize => compareTo(t) == 0
+    case _           => false
+  }
+
   override def toString = {
     unit match {
       case SizeUnits.BYTE => s"$size B"
diff --git a/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala b/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala
index 250cec0479..c74f146ac8 100644
--- a/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala
+++ b/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala
@@ -23,6 +23,7 @@ import org.junit.runner.RunWith
 import org.scalatest.FlatSpec
 import org.scalatest.Matchers
 import org.scalatest.junit.JUnitRunner
+import spray.json._
 import whisk.core.entity.size.SizeInt
 import whisk.core.entity.ByteSize
 
@@ -37,8 +38,8 @@ class SizeTests extends FlatSpec with Matchers {
     val oneKB = 1 KB
     val oneMB = 1 MB
 
-    oneByte < oneKB should be(true)
-    oneKB < oneMB should be(true)
+    oneByte should be < oneKB
+    oneKB should be < oneMB
   }
 
   it should "3 Bytes smaller than 2 KB smaller than 1 MB" in {
@@ -46,8 +47,8 @@ class SizeTests extends FlatSpec with Matchers {
     val myKBs = 2 KB
     val myMBs = 1 MB
 
-    myBytes < myKBs should be(true)
-    myKBs < myMBs should be(true)
+    myBytes should be < myKBs
+    myKBs should be < myMBs
   }
 
   it should "1 MB greater than 1 KB greater than 1 Byte" in {
@@ -55,17 +56,17 @@ class SizeTests extends FlatSpec with Matchers {
     val oneKB = 1 KB
     val oneMB = 1 MB
 
-    oneMB > oneKB should be(true)
-    oneKB > oneByte should be(true)
+    oneMB should be > oneKB
+    oneKB should be > oneByte
   }
 
   it should "1 MB == 1024 KB == 1048576 B" in {
-    val myBytes = 1048576 B
+    val myBytes = (1 << 20) B
     val myKBs = 1024 KB
     val myMBs = 1 MB
 
-    myBytes equals (myKBs)
-    myKBs equals (myMBs)
+    myBytes should equal(myKBs)
+    myKBs should equal(myMBs)
   }
 
   // Addition
@@ -98,7 +99,7 @@ class SizeTests extends FlatSpec with Matchers {
   }
 
   it should "1048576 B to MB = 1" in {
-    (1048576 B).toMB should be(1)
+    ((1 << 20) B).toMB should be(1)
   }
 
   it should "1 KB to B = 1024" in {
@@ -110,7 +111,7 @@ class SizeTests extends FlatSpec with Matchers {
   }
 
   it should "1 MB to B = 1048576" in {
-    (1 MB).toBytes should be(1048576)
+    (1 MB).toBytes should be(1 << 20)
   }
 
   it should "1 MB to KB = 1024" in {
@@ -119,36 +120,43 @@ class SizeTests extends FlatSpec with Matchers {
 
   // Create ObjectSize from String
   it should "create ObjectSize from String 3B" in {
-    ByteSize.fromString("3b") equals (3 B)
-    ByteSize.fromString("3B") equals (3 B)
-    ByteSize.fromString("3 b") equals (3 B)
-    ByteSize.fromString("3 B") equals (3 B)
+    ByteSize.fromString("3b") should be(3 B)
+    ByteSize.fromString("3B") should be(3 B)
+    ByteSize.fromString("3 b") should be(3 B)
+    ByteSize.fromString("3 B") should be(3 B)
   }
 
   it should "create ObjectSize from String 7K" in {
-    ByteSize.fromString("7k") equals (7 KB)
-    ByteSize.fromString("7K") equals (7 KB)
-    ByteSize.fromString("7KB") equals (7 KB)
-    ByteSize.fromString("7kB") equals (7 KB)
-    ByteSize.fromString("7kb") equals (7 KB)
-    ByteSize.fromString("7 k") equals (7 KB)
-    ByteSize.fromString("7 K") equals (7 KB)
-    ByteSize.fromString("7 KB") equals (7 KB)
-    ByteSize.fromString("7 kB") equals (7 KB)
-    ByteSize.fromString("7 kb") equals (7 KB)
+    ByteSize.fromString("7k") should be(7 KB)
+    ByteSize.fromString("7K") should be(7 KB)
+    ByteSize.fromString("7KB") should be(7 KB)
+    ByteSize.fromString("7kB") should be(7 KB)
+    ByteSize.fromString("7kb") should be(7 KB)
+    ByteSize.fromString("7 k") should be(7 KB)
+    ByteSize.fromString("7 K") should be(7 KB)
+    ByteSize.fromString("7 KB") should be(7 KB)
+    ByteSize.fromString("7 kB") should be(7 KB)
+    ByteSize.fromString("7 kb") should be(7 KB)
   }
 
   it should "create ObjectSize from String 120M" in {
-    ByteSize.fromString("120m") equals (120 MB)
-    ByteSize.fromString("120M") equals (120 MB)
-    ByteSize.fromString("120MB") equals (120 MB)
-    ByteSize.fromString("120mB") equals (120 MB)
-    ByteSize.fromString("120mb") equals (120 MB)
-    ByteSize.fromString("120 m") equals (120 MB)
-    ByteSize.fromString("120 M") equals (120 MB)
-    ByteSize.fromString("120 MB") equals (120 MB)
-    ByteSize.fromString("120 mB") equals (120 MB)
-    ByteSize.fromString("120 mb") equals (120 MB)
+    ByteSize.fromString("120m") should be(120 MB)
+    ByteSize.fromString("120M") should be(120 MB)
+    ByteSize.fromString("120MB") should be(120 MB)
+    ByteSize.fromString("120mB") should be(120 MB)
+    ByteSize.fromString("120mb") should be(120 MB)
+    ByteSize.fromString("120 m") should be(120 MB)
+    ByteSize.fromString("120 M") should be(120 MB)
+    ByteSize.fromString("120 MB") should be(120 MB)
+    ByteSize.fromString("120 mB") should be(120 MB)
+    ByteSize.fromString("120 mb") should be(120 MB)
+  }
+
+  it should "read and write size as JSON" in {
+    import whisk.core.entity.size.serdes
+    serdes.read(JsString("3b")) should be(3 B)
+    serdes.write(3 B) should be(JsString("3 B"))
+    a[DeserializationException] should be thrownBy (serdes.read(JsNumber(3)))
   }
 
   it should "throw error on creating ObjectSize from String 120A" in {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services