|
| 1 | +import org.scalacheck._ |
| 2 | +import Gen._ |
| 3 | +import Prop._ |
| 4 | + |
| 5 | +object Test extends Properties("NaN-Ordering") { |
| 6 | + |
| 7 | + val specFloats: Gen[Float] = oneOf( |
| 8 | + Float.MaxValue, |
| 9 | + Float.MinPositiveValue, |
| 10 | + Float.MinValue, |
| 11 | + Float.NaN, |
| 12 | + Float.NegativeInfinity, |
| 13 | + Float.PositiveInfinity, |
| 14 | + -0.0f, |
| 15 | + +0.0f |
| 16 | + ) |
| 17 | + |
| 18 | + property("Float min") = forAll(specFloats, specFloats) { (d1, d2) => { |
| 19 | + val mathmin = math.min(d1, d2) |
| 20 | + val numericmin = d1 min d2 |
| 21 | + mathmin == numericmin || mathmin.isNaN && numericmin.isNaN |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + property("Float max") = forAll(specFloats, specFloats) { (d1, d2) => { |
| 26 | + val mathmax = math.max(d1, d2) |
| 27 | + val numericmax = d1 max d2 |
| 28 | + mathmax == numericmax || mathmax.isNaN && numericmax.isNaN |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + val numFloat = implicitly[Numeric[Float]] |
| 33 | + |
| 34 | + property("Float lt") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.lt(d1, d2) == d1 < d2 } |
| 35 | + |
| 36 | + property("Float lteq") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.lteq(d1, d2) == d1 <= d2 } |
| 37 | + |
| 38 | + property("Float gt") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.gt(d1, d2) == d1 > d2 } |
| 39 | + |
| 40 | + property("Float gteq") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.gteq(d1, d2) == d1 >= d2 } |
| 41 | + |
| 42 | + property("Float equiv") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.equiv(d1, d2) == (d1 == d2) } |
| 43 | + |
| 44 | + property("Float reverse.min") = forAll(specFloats, specFloats) { (d1, d2) => { |
| 45 | + val mathmin = math.min(d1, d2) |
| 46 | + val numericmin = numFloat.reverse.min(d1, d2) |
| 47 | + mathmin == numericmin || mathmin.isNaN && numericmin.isNaN |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + property("Float reverse.max") = forAll(specFloats, specFloats) { (d1, d2) => { |
| 52 | + val mathmax = math.max(d1, d2) |
| 53 | + val numericmax = numFloat.reverse.max(d1, d2) |
| 54 | + mathmax == numericmax || mathmax.isNaN && numericmax.isNaN |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + property("Float reverse.lt") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.reverse.lt(d1, d2) == d2 < d1 } |
| 59 | + |
| 60 | + property("Float reverse.lteq") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.reverse.lteq(d1, d2) == d2 <= d1 } |
| 61 | + |
| 62 | + property("Float reverse.gt") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.reverse.gt(d1, d2) == d2 > d1 } |
| 63 | + |
| 64 | + property("Float reverse.gteq") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.reverse.gteq(d1, d2) == d2 >= d1 } |
| 65 | + |
| 66 | + property("Float reverse.equiv") = forAll(specFloats, specFloats) { (d1, d2) => numFloat.reverse.equiv(d1, d2) == (d1 == d2) } |
| 67 | + |
| 68 | + |
| 69 | + val specDoubles: Gen[Double] = oneOf( |
| 70 | + Double.MaxValue, |
| 71 | + Double.MinPositiveValue, |
| 72 | + Double.MinValue, |
| 73 | + Double.NaN, |
| 74 | + Double.NegativeInfinity, |
| 75 | + Double.PositiveInfinity, |
| 76 | + -0.0, |
| 77 | + +0.0 |
| 78 | + ) |
| 79 | + |
| 80 | + // ticket #5104 |
| 81 | + property("Double min") = forAll(specDoubles, specDoubles) { (d1, d2) => { |
| 82 | + val mathmin = math.min(d1, d2) |
| 83 | + val numericmin = d1 min d2 |
| 84 | + mathmin == numericmin || mathmin.isNaN && numericmin.isNaN |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + property("Double max") = forAll(specDoubles, specDoubles) { (d1, d2) => { |
| 89 | + val mathmax = math.max(d1, d2) |
| 90 | + val numericmax = d1 max d2 |
| 91 | + mathmax == numericmax || mathmax.isNaN && numericmax.isNaN |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + val numDouble = implicitly[Numeric[Double]] |
| 96 | + |
| 97 | + property("Double lt") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.lt(d1, d2) == d1 < d2 } |
| 98 | + |
| 99 | + property("Double lteq") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.lteq(d1, d2) == d1 <= d2 } |
| 100 | + |
| 101 | + property("Double gt") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.gt(d1, d2) == d1 > d2 } |
| 102 | + |
| 103 | + property("Double gteq") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.gteq(d1, d2) == d1 >= d2 } |
| 104 | + |
| 105 | + property("Double equiv") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.equiv(d1, d2) == (d1 == d2) } |
| 106 | + |
| 107 | + property("Double reverse.min") = forAll(specDoubles, specDoubles) { (d1, d2) => { |
| 108 | + val mathmin = math.min(d1, d2) |
| 109 | + val numericmin = numDouble.reverse.min(d1, d2) |
| 110 | + mathmin == numericmin || mathmin.isNaN && numericmin.isNaN |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + property("Double reverse.max") = forAll(specDoubles, specDoubles) { (d1, d2) => { |
| 115 | + val mathmax = math.max(d1, d2) |
| 116 | + val numericmax = numDouble.reverse.max(d1, d2) |
| 117 | + mathmax == numericmax || mathmax.isNaN && numericmax.isNaN |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + property("Double reverse.lt") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.reverse.lt(d1, d2) == d2 < d1 } |
| 122 | + |
| 123 | + property("Double reverse.lteq") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.reverse.lteq(d1, d2) == d2 <= d1 } |
| 124 | + |
| 125 | + property("Double reverse.gt") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.reverse.gt(d1, d2) == d2 > d1 } |
| 126 | + |
| 127 | + property("Double reverse.gteq") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.reverse.gteq(d1, d2) == d2 >= d1 } |
| 128 | + |
| 129 | + property("Double reverse.equiv") = forAll(specDoubles, specDoubles) { (d1, d2) => numDouble.reverse.equiv(d1, d2) == (d1 == d2) } |
| 130 | +} |
0 commit comments