/
MatchlessTests.scala
176 lines (150 loc) · 5.15 KB
/
MatchlessTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.bykn.bosatsu
import cats.data.NonEmptyList
import org.scalacheck.{Arbitrary, Gen}
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks.{
forAll,
PropertyCheckConfiguration
}
import Identifier.{Bindable, Constructor}
import rankn.DataRepr
import cats.implicits._
import org.scalatest.funsuite.AnyFunSuite
class MatchlessTest extends AnyFunSuite {
implicit val generatorDrivenConfig: PropertyCheckConfiguration =
PropertyCheckConfiguration(minSuccessful =
if (Platform.isScalaJvm) 5000 else 20
)
type Fn = (PackageName, Constructor) => Option[DataRepr]
def fnFromTypeEnv[A](te: rankn.TypeEnv[A]): Fn = {
// the list constructors *have* to be in scope or matching will generate
// bad code
case (PackageName.PredefName, Constructor("EmptyList")) =>
Some(DataRepr.Enum(0, 0, List(0, 1)))
case (PackageName.PredefName, Constructor("NonEmptyList")) =>
Some(DataRepr.Enum(1, 2, List(0, 1)))
case (pn, cons) =>
te.getConstructor(pn, cons)
.map(_._1.dataRepr(cons))
.orElse(Some(DataRepr.Struct(0)))
}
lazy val genInputs: Gen[(Bindable, RecursionKind, TypedExpr[Unit], Fn)] =
Generators
.genPackage(Gen.const(()), 5)
.flatMap { (m: Map[PackageName, Package.Typed[Unit]]) =>
val candidates = m.filter { case (_, t) => t.program.lets.nonEmpty }
if (candidates.isEmpty) genInputs
else
for {
packName <- Gen.oneOf(candidates.keys.toSeq)
prog = m(packName).program
(b, r, t) <- Gen.oneOf(prog.lets)
fn = fnFromTypeEnv(prog.types)
} yield (b, r, t, fn)
}
test("matchless.fromLet is pure: f(x) == f(x)") {
forAll(genInputs) { case (b, r, t, fn) =>
def run(): Matchless.Expr =
Matchless.fromLet(b, r, t)(fn)
assert(run() == run())
}
}
val genMatchlessExpr: Gen[Matchless.Expr] =
genInputs.map { case (b, r, t, fn) =>
Matchless.fromLet(b, r, t)(fn)
}
test("regressions") {
// this is illegal code, but it shouldn't throw a match error:
val name = Identifier.Name("foo")
val te = TypedExpr.Local(name, rankn.Type.IntType, ())
// this should not throw
val me = Matchless.fromLet(name, RecursionKind.Recursive, te)(
fnFromTypeEnv(rankn.TypeEnv.empty)
)
assert(me != null)
}
def genNE[A](max: Int, ga: Gen[A]): Gen[NonEmptyList[A]] =
for {
h <- ga
// this can get out of control with long lists
cnt <- Gen.choose(0, max)
tail <- Gen.listOfN(cnt, ga)
} yield NonEmptyList(h, tail)
test("Matchless.product works") {
forAll(genNE(5, genNE(5, Gen.choose(-10, 10)))) { ne2 =>
val p1 = Matchless.product(ne2)(_ * _).toList.reduce(_ + _)
val p0 = ne2.map(_.toList.reduce(_ + _)).toList.reduce(_ * _)
assert(p1 == p0)
}
}
test("Matchless.stopAt works") {
forAll(
genNE(100, Gen.choose(-100, 100)),
Arbitrary.arbitrary[Int => Boolean]
) { (nel, fn) =>
val stopped = Matchless.stopAt(nel)(fn)
if (fn(stopped.last)) {
// none of the items before the last are true:
assert(stopped.init.exists(fn) == false)
} else {
// none of them were true
assert(stopped == nel)
assert(nel.exists(fn) == false)
}
}
}
test("Mathless.matchList works like SeqPattern") {
val genArgs: Gen[(List[Byte], List[Option[Byte => Option[Int]]])] = {
val bytes = Gen.choose(Byte.MinValue, Byte.MaxValue)
val size = Gen.choose(0, 20)
for {
s <- size
left <- Gen.listOfN(s, bytes)
sright <- Gen.choose(0, 2 * s)
pat <- Gen.listOfN(
sright,
Arbitrary.arbitrary[Option[Byte => Option[Int]]]
)
} yield (left, pat)
}
import pattern.{SeqPattern, SeqPart, Splitter, Matcher}
def toSeqPat[A, B](
pat: List[Option[A => Option[B]]]
): SeqPattern[A => Option[B]] =
SeqPattern.fromList(pat.map {
case None => SeqPart.Wildcard
case Some(fn) => SeqPart.Lit(fn)
})
val matcher = SeqPattern.matcher(
Splitter.listSplitter(new Matcher[Byte => Option[Int], Byte, Int] {
def apply(fn: Byte => Option[Int]) = fn
})
)
forAll(genArgs) { case (targ, pat) =>
val seqPat = toSeqPat(pat)
val matchRes = matcher(seqPat)(targ)
val matchlessRes = Matchless.matchList(
targ,
pat.map {
case None => Left((_: List[Byte]) => 0)
case Some(fn) => Right(fn)
}
)
assert(matchlessRes == matchRes)
}
}
test("If.flatten can be unflattened") {
forAll(genMatchlessExpr) {
case ifexpr @ Matchless.If(_, _, _) =>
val (chain, rest) = ifexpr.flatten
def unflatten(ifs: NonEmptyList[(Matchless.BoolExpr, Matchless.Expr)], elseX: Matchless.Expr): Matchless.If =
ifs.tail match {
case Nil => Matchless.If(ifs.head._1, ifs.head._2, elseX)
case head :: next =>
val end = unflatten(NonEmptyList(head, next), elseX)
Matchless.If(ifs.head._1, ifs.head._2, end)
}
assert(unflatten(chain, rest) == ifexpr)
case _ => ()
}
}
}