Skip to content

Commit 4ef5c41

Browse files
committed
More tests
1 parent 006752e commit 4ef5c41

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

tests/neg/OpaqueEscape.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object OpaqueEscape{
2+
opaque type Wrapped = Int
3+
abstract class EscaperBase {
4+
def unwrap(i:Wrapped):Int
5+
def wrap(i:Int):Wrapped
6+
}
7+
class Escaper extends EscaperBase{ // error: needs to be abstract
8+
override def unwrap(i:Int):Int = i // error overriding method unwrap
9+
override def wrap(i:Int):Int = i // error overriding method wrap
10+
}
11+
12+
val e = new Escaper:EscaperBase
13+
val w:Wrapped = e.wrap(1)
14+
val u:Int = e.unwrap(w)
15+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
object ia {
2+
3+
import java.util.Arrays
4+
5+
opaque type IArray[A1] = Array[A1]
6+
7+
object IArray {
8+
def initialize[A](body: => Array[A]): IArray[A] = body
9+
def size[A](ia: IArray[A]): Int = ia.length
10+
def get[A](ia: IArray[A], i: Int): A = ia(i)
11+
12+
// return a sorted copy of the array
13+
def sorted[A <: AnyRef : math.Ordering](ia: IArray[A]): IArray[A] = {
14+
val arr = Arrays.copyOf(ia, ia.length)
15+
scala.util.Sorting.quickSort(arr)
16+
arr
17+
}
18+
19+
// use a standard java method to search a sorted IArray.
20+
// (note that this doesn't mutate the array).
21+
def binarySearch(ia: IArray[Long], elem: Long): Int =
22+
Arrays.binarySearch(ia, elem)
23+
}
24+
25+
// same as IArray.binarySearch but implemented by-hand.
26+
//
27+
// given a sorted IArray, returns index of `elem`,
28+
// or a negative value if not found.
29+
def binaryIndexOf(ia: IArray[Long], elem: Long): Int = {
30+
var lower: Int = 0
31+
var upper: Int = IArray.size(ia)
32+
while (lower <= upper) {
33+
val middle = (lower + upper) >>> 1
34+
val n = IArray.get(ia, middle)
35+
36+
if (n == elem) return middle
37+
else if (n < elem) lower = middle + 1
38+
else upper = middle - 1
39+
}
40+
-lower - 1
41+
}
42+
43+
def xs: IArray[Long] = ???
44+
45+
xs.length // error: not a member
46+
xs.apply(2) // error: not a member
47+
xs(1) = 0 // error: not a member
48+
}

0 commit comments

Comments
 (0)