This is an automated email from the ASF dual-hosted git repository.
cbickel pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git
The following commit(s) were added to refs/heads/master by this push:
new d89e1d0 Add equals method to ensure that ByteSize instances satisfy
Comparable contract. (#3697)
d89e1d0 is described below
commit d89e1d01fd7b14c8c86e2c35ffbc0996e29d70d5
Author: rodric rabbah <[email protected]>
AuthorDate: Tue Jun 5 05:03:38 2018 -0400
Add equals method to ensure that ByteSize instances satisfy Comparable
contract. (#3697)
Also adjust tests which were not checking conditions correctly for tests to
fail when invariants are violated.
---
.../src/main/scala/whisk/core/entity/Size.scala | 7 +-
.../scala/whisk/core/entity/test/SizeTests.scala | 78 ++++++++++++----------
2 files changed, 49 insertions(+), 36 deletions(-)
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 2d5b7d9..a51eb2e 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 250cec0..c74f146 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 {
--
To stop receiving notification emails like this one, please contact
[email protected].