|
| 1 | +package dotty.tools |
| 2 | +package dotc |
| 3 | +package core |
| 4 | + |
| 5 | +import Contexts.*, Decorators.*, Denotations.*, SymDenotations.*, Symbols.*, Types.* |
| 6 | +import printing.Formatting.Show |
| 7 | + |
| 8 | +import org.junit.Test |
| 9 | +import org.junit.Assert.* |
| 10 | + |
| 11 | +class TypeComparerTest extends DottyTest: |
| 12 | + val LongType = defn.LongType |
| 13 | + |
| 14 | + // Ensure glb and lub give lower and upper bounds when one of the inputs is WildcardType |
| 15 | + // and that glb and lub honours left identity and right identity, and thus is commutative with WildcardType |
| 16 | + @Test def glbWildcardL = identityL("glb", glb)(LongType, id = WildcardType) |
| 17 | + @Test def glbWildcardR = identityR("glb", glb)(LongType, id = WildcardType) |
| 18 | + @Test def lubWildcardL = identityL("lub", lub)(LongType, id = WildcardType) |
| 19 | + @Test def lubWildcardR = identityR("lub", lub)(LongType, id = WildcardType) |
| 20 | + |
| 21 | + def identityL[A: Show](op: String, fn: (A, A) => A)(a: A, id: A) = |
| 22 | + val x = fn(id, a) |
| 23 | + assertEquals(i"$op(id=$id, $a) = $x, expected $a (left identity)", a, x) |
| 24 | + |
| 25 | + def identityR[A: Show](op: String, fn: (A, A) => A)(a: A, id: A) = |
| 26 | + val x = fn(a, id) |
| 27 | + assertEquals(i"$op($a, id=$id) = $x, expected $a (right identity)", a, x) |
| 28 | + |
| 29 | + // glb(a, b) = x such that x <: a, x <: b, & forAll y, y <: a, y <: b ==> y <: x |
| 30 | + def glb(a: Type, b: Type) = |
| 31 | + val x = TypeComparer.glb(a, b) |
| 32 | + assertTrue(i"glb($a, $b) = $x, but $x !<: $a", x <:< a) |
| 33 | + assertTrue(i"glb($a, $b) = $x, but $x !<: $b", x <:< b) |
| 34 | + x |
| 35 | + |
| 36 | + // lub(a, b) = x such that a <: x, b <: x, & forAll y, a <: y, b <: y ==> x <: y |
| 37 | + def lub(a: Type, b: Type) = |
| 38 | + val x = TypeComparer.lub(a, b) |
| 39 | + assertTrue(i"lub($a, $b) = $x, but $a !<: $x", a <:< x) |
| 40 | + assertTrue(i"lub($a, $b) = $x, but $b !<: $x", b <:< x) |
| 41 | + x |
| 42 | +end TypeComparerTest |
0 commit comments