diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-01-09 18:20:23 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2014-01-09 18:20:23 -0800 |
commit | 513b9e0a715b4cd515da63cf1a20b195d7a3fee0 (patch) | |
tree | 6aa6bc57571ba645b784e99c1f4cd5196a061383 /test | |
parent | 8e62486cc9b519e783fc194a63ae61a7b20e2fce (diff) | |
parent | 4b6a0a999e935a94501da272a12956c024141cb2 (diff) | |
download | scala-513b9e0a715b4cd515da63cf1a20b195d7a3fee0.tar.gz scala-513b9e0a715b4cd515da63cf1a20b195d7a3fee0.tar.bz2 scala-513b9e0a715b4cd515da63cf1a20b195d7a3fee0.zip |
Merge pull request #3124 from DarkDimius/fix-7443
Fix SI-7443 Range.sum ignoring Numeric argument and always assuming default 'plus' implementation
Diffstat (limited to 'test')
-rw-r--r-- | test/files/scalacheck/range.scala | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/test/files/scalacheck/range.scala b/test/files/scalacheck/range.scala index 6c7c32bfdf..1eb186f303 100644 --- a/test/files/scalacheck/range.scala +++ b/test/files/scalacheck/range.scala @@ -127,6 +127,47 @@ abstract class RangeTest(kind: String) extends Properties("Range "+kind) { (visited == expectedSize(r)) :| str(r) } + property("sum") = forAll(myGen) { r => +// println("----------") +// println("sum "+str(r)) + val rSum = r.sum + val expected = r.length match { + case 0 => 0 + case 1 => r.head + case _ => ((r.head + r.last).toLong * r.length / 2).toInt + } +// println("size: " + r.length) +// println("expected: " + expected) +// println("obtained: " + rSum) + + (rSum == expected) :| str(r) + } + +/* checks that sum respects custom Numeric */ + property("sumCustomNumeric") = forAll(myGen) { r => + val mod = 65536 + object mynum extends Numeric[Int] { + def plus(x: Int, y: Int): Int = (x + y) % mod + override def zero = 0 + + def fromInt(x: Int): Int = ??? + def minus(x: Int, y: Int): Int = ??? + def negate(x: Int): Int = ??? + def times(x: Int, y: Int): Int = ??? + def toDouble(x: Int): Double = ??? + def toFloat(x: Int): Float = ??? + def toInt(x: Int): Int = ((x % mod) + mod * 2) % mod + def toLong(x: Int): Long = ??? + def compare(x: Int, y: Int): Int = ??? + } + + val rSum = r.sum(mynum) + val expected = mynum.toInt(r.sum) + + (rSum == expected) :| str(r) + } + + property("length") = forAll(myGen suchThat (r => expectedSize(r).toInt == expectedSize(r))) { r => // println("length "+str(r)) (r.length == expectedSize(r)) :| str(r) |