aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorodersky <odersky@gmail.com>2017-04-10 10:33:32 +0200
committerGitHub <noreply@github.com>2017-04-10 10:33:32 +0200
commit318700fdaacd7e6707519e03f15a5ede99ca558a (patch)
tree809f66b68f9200afd8daef9172921405469a39c7 /tests
parent80f9b6da66cb4eeabfd5ab97e422752ff134d590 (diff)
parent2bf1e1913feb36d90d1c4e857c6cd9610d80ff61 (diff)
downloaddotty-318700fdaacd7e6707519e03f15a5ede99ca558a.tar.gz
dotty-318700fdaacd7e6707519e03f15a5ede99ca558a.tar.bz2
dotty-318700fdaacd7e6707519e03f15a5ede99ca558a.zip
Merge pull request #2197 from dotty-staging/add-enum-exhaustiveness
Add enum exhaustivity checking
Diffstat (limited to 'tests')
-rw-r--r--tests/patmat/enum-HList.scala22
-rw-r--r--tests/patmat/enum-Tree.scala29
-rw-r--r--tests/patmat/enumColor.scala12
-rw-r--r--tests/patmat/patmat-indent.check2
-rw-r--r--tests/patmat/patmat-indent.scala2
-rw-r--r--tests/patmat/planets.scala26
-rw-r--r--tests/patmat/t6420.check2
-rw-r--r--tests/patmat/t7285.check2
-rw-r--r--tests/patmat/t7466.check2
-rw-r--r--tests/run/enum-Color.scala4
10 files changed, 98 insertions, 5 deletions
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/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/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)}")
+ }
+}
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)
diff --git a/tests/run/enum-Color.scala b/tests/run/enum-Color.scala
index 683d18d9e..f4f6aaef8 100644
--- a/tests/run/enum-Color.scala
+++ b/tests/run/enum-Color.scala
@@ -7,5 +7,9 @@ object Test {
for (color <- Color.enumValues) {
println(s"$color: ${color.enumTag}")
assert(Color.enumValue(color.enumTag) eq color)
+ import Color._
+ color match {
+ case Red | Green | Blue =>
+ }
}
}