From 0c4a06f8409feac5ba99c0a8961210225dbd4936 Mon Sep 17 00:00:00 2001 From: liu fengyun Date: Thu, 6 Apr 2017 15:36:37 +0200 Subject: exhaustivity support for enums --- tests/patmat/enumColor.scala | 12 ++++++++++++ tests/patmat/patmat-indent.check | 2 +- tests/patmat/patmat-indent.scala | 2 +- tests/patmat/t6420.check | 2 +- tests/patmat/t7285.check | 2 +- tests/patmat/t7466.check | 2 +- 6 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 tests/patmat/enumColor.scala (limited to 'tests/patmat') diff --git a/tests/patmat/enumColor.scala b/tests/patmat/enumColor.scala new file mode 100644 index 000000000..60d610d0d --- /dev/null +++ b/tests/patmat/enumColor.scala @@ -0,0 +1,12 @@ + enum Color { + case Red, Green, Blue + } + + object Test { + def f(color: Color) = { + import Color._ + color match { + case Red | Green | Blue => + } + } +} diff --git a/tests/patmat/patmat-indent.check b/tests/patmat/patmat-indent.check index 79845ebcf..4f0ec4dd9 100644 --- a/tests/patmat/patmat-indent.check +++ b/tests/patmat/patmat-indent.check @@ -1,3 +1,3 @@ 9: Pattern Match Exhaustivity: Nil -23: Pattern Match Exhaustivity: _: Boolean +23: Pattern Match Exhaustivity: true, false 27: Pattern Match Exhaustivity: _: Int diff --git a/tests/patmat/patmat-indent.scala b/tests/patmat/patmat-indent.scala index ef25bb2c7..a2b18e7fb 100644 --- a/tests/patmat/patmat-indent.scala +++ b/tests/patmat/patmat-indent.scala @@ -1,5 +1,5 @@ object Test { - val Nil = scala.Nil + val Nil: scala.collection.immutable.Nil.type = scala.collection.immutable.Nil val X = 5 object Inner { diff --git a/tests/patmat/t6420.check b/tests/patmat/t6420.check index 73acf1454..c15701594 100644 --- a/tests/patmat/t6420.check +++ b/tests/patmat/t6420.check @@ -1 +1 @@ -5: Pattern Match Exhaustivity: (Nil, _), (List(_, _), _), (Nil, Nil), (Nil, List(_, _)), (List(_, _), Nil), (List(_, _), List(_, _)), (_, Nil), (_, List(_, _)) +5: Pattern Match Exhaustivity: (Nil, _), (List(true, _), _), (List(false, _), _), (_, Nil), (_, List(true, _)), (_, List(false, _)) diff --git a/tests/patmat/t7285.check b/tests/patmat/t7285.check index 1c2841920..d40b77e4b 100644 --- a/tests/patmat/t7285.check +++ b/tests/patmat/t7285.check @@ -1,3 +1,3 @@ 15: Pattern Match Exhaustivity: (Up, Down) 33: Pattern Match Exhaustivity: Down -51: Pattern Match Exhaustivity: (Base.Up, Base.Down) +51: Pattern Match Exhaustivity: (Up, Down) diff --git a/tests/patmat/t7466.check b/tests/patmat/t7466.check index 35227484e..af596399b 100644 --- a/tests/patmat/t7466.check +++ b/tests/patmat/t7466.check @@ -1 +1 @@ -8: Pattern Match Exhaustivity: (_, _) +8: Pattern Match Exhaustivity: (true, _), (false, _), (_, true), (_, false) -- cgit v1.2.3 From fe9103d95531bac71adbf47b6d40610bc7ec6000 Mon Sep 17 00:00:00 2001 From: liu fengyun Date: Thu, 6 Apr 2017 15:50:56 +0200 Subject: add more enum exhaustivity tests --- tests/patmat/enum-HList.scala | 22 ++++++++++++++++++++++ tests/patmat/enum-Tree.scala | 29 +++++++++++++++++++++++++++++ tests/patmat/planets.scala | 26 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 tests/patmat/enum-HList.scala create mode 100644 tests/patmat/enum-Tree.scala create mode 100644 tests/patmat/planets.scala (limited to 'tests/patmat') diff --git a/tests/patmat/enum-HList.scala b/tests/patmat/enum-HList.scala new file mode 100644 index 000000000..c019cb6cc --- /dev/null +++ b/tests/patmat/enum-HList.scala @@ -0,0 +1,22 @@ +enum HLst { + case HCons[+Hd, +Tl <: HLst](hd: Hd, tl: Tl) + case HNil +} + +object Test { + import HLst._ + def length(hl: HLst): Int = hl match { + case HCons(_, tl) => 1 + length(tl) + case HNil => 0 + } + def sumInts(hl: HLst): Int = hl match { + case HCons(x: Int, tl) => x + sumInts(tl) + case HCons(_, tl) => sumInts(tl) + case HNil => 0 + } + def main(args: Array[String]) = { + val hl = HCons(1, HCons("A", HNil)) + assert(length(hl) == 2, length(hl)) + assert(sumInts(hl) == 1) + } +} diff --git a/tests/patmat/enum-Tree.scala b/tests/patmat/enum-Tree.scala new file mode 100644 index 000000000..ef5bd7a57 --- /dev/null +++ b/tests/patmat/enum-Tree.scala @@ -0,0 +1,29 @@ +enum Tree[T] { + case True extends Tree[Boolean] + case False extends Tree[Boolean] + case Zero extends Tree[Int] + case Succ(n: Tree[Int]) extends Tree[Int] + case Pred(n: Tree[Int]) extends Tree[Int] + case IsZero(n: Tree[Int]) extends Tree[Boolean] + case If(cond: Tree[Boolean], thenp: Tree[T], elsep: Tree[T]) +} + +object Test { + import Tree._ + + def eval[T](e: Tree[T]): T = e match { + case True => true + case False => false + case Zero => 0 + case Succ(f) => eval(f) + 1 + case Pred(f) => eval(f) - 1 + case IsZero(f) => eval(f) == 0 + case If(cond, thenp, elsep) => if (eval(cond)) eval(thenp) else eval(elsep) + } + + val data = If(IsZero(Pred(Succ(Zero))), Succ(Succ(Zero)), Pred(Pred(Zero))) + + def main(args: Array[String]) = { + println(s"$data --> ${eval(data)}") + } +} diff --git a/tests/patmat/planets.scala b/tests/patmat/planets.scala new file mode 100644 index 000000000..bcbfd7eeb --- /dev/null +++ b/tests/patmat/planets.scala @@ -0,0 +1,26 @@ +enum class Planet(mass: Double, radius: Double) { + private final val G = 6.67300E-11 + def surfaceGravity = G * mass / (radius * radius) + def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity +} +object Planet { + case MERCURY extends Planet(3.303e+23, 2.4397e6) + case VENUS extends Planet(4.869e+24, 6.0518e6) + case EARTH extends Planet(5.976e+24, 6.37814e6) + case MARS extends Planet(6.421e+23, 3.3972e6) + case JUPITER extends Planet(1.9e+27, 7.1492e7) + case SATURN extends Planet(5.688e+26, 6.0268e7) + case URANUS extends Planet(8.686e+25, 2.5559e7) + case NEPTUNE extends Planet(1.024e+26, 2.4746e7) +} +object Test { + def main(args: Array[String]) = { + import Planet._ + assert(enumValueNamed("SATURN") == SATURN) + assert(enumValue(2) == EARTH) + val earthWeight = 100 + val mass = earthWeight/EARTH.surfaceGravity + for (p <- enumValues) + println(s"Your weight on $p is ${p.surfaceWeight(mass)}") + } +} -- cgit v1.2.3