Use mock JDK in compiler tests where full JDK is not needed

- move some of boxWithStdlib tests under fullJdk/ directory, where they will be
  compiled against the full JDK
- introduce FULL_JDK in-text directive for the reflection test as only 4 tests
  out of 654 needed the full JDK
This commit is contained in:
Alexander Udalov
2015-04-02 03:14:46 +03:00
parent 150e0a4ef2
commit 72aa3d1465
31 changed files with 189 additions and 161 deletions
@@ -1,148 +0,0 @@
package collections
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import java.security.KeyPair
fun <T> testCollectionSize(c: Collection<T>) = assertEquals(0, c.size())
fun <T> testCollectionIsEmpty(c: Collection<T>) = assertTrue(c.isEmpty())
fun <T> testCollectionContains(c: Collection<T>) = assertTrue(c.contains(1))
fun <T> testCollectionIterator(c: Collection<T>) {
val it = c.iterator()
while (it.hasNext()) {
assertEquals(1, it.next())
}
}
fun <T> testCollectionContainsAll(c: Collection<T>) = assertTrue(c.containsAll(c))
fun <T> testMutableCollectionAdd(c: MutableCollection<T>, t: T) {
c.add(t)
assertEquals(1, c.size())
assertTrue(c.contains(t))
}
fun <T> testMutableCollectionRemove(c: MutableCollection<T>, t: T) {
c.remove(t)
assertEquals(0, c.size())
assertFalse(c.contains(t))
}
fun <T> testMutableCollectionIterator(c: MutableCollection<T>, t: T) {
c.add(t)
val it = c.iterator()
while (it.hasNext()) {
it.next()
it.remove()
}
assertEquals(0, c.size())
}
fun <T> testMutableCollectionAddAll(c: MutableCollection<T>, t1: T, t2: T) {
c.addAll(arrayList(t1, t2))
assertEquals(arrayList(t1, t2), c)
}
fun <T> testMutableCollectionRemoveAll(c: MutableCollection<T>, t1: T, t2: T) {
c.addAll(arrayList(t1, t2))
c.removeAll(arrayList(t1))
assertEquals(arrayList(t2), c)
}
fun <T> testMutableCollectionRetainAll(c: MutableCollection<T>, t1: T, t2: T) {
c.addAll(arrayList(t1, t2))
c.retainAll(arrayList(t1))
assertEquals(arrayList(t1), c)
}
fun <T> testMutableCollectionClear(c: MutableCollection<T>) {
c.clear()
assertTrue(c.isEmpty())
}
fun testCollection() {
testCollectionSize(arrayList<Int>())
testCollectionIsEmpty(arrayList<String>())
testCollectionContains(arrayList(1))
testCollectionIterator(arrayList(1))
testCollectionContainsAll(arrayList("a", "b"))
testMutableCollectionAdd(arrayList(), "")
testMutableCollectionRemove(arrayList("a"), "a")
testMutableCollectionIterator(arrayList(), 1)
testMutableCollectionAddAll(arrayList(), 1, 2)
testMutableCollectionRemoveAll(arrayList(), 1, 2)
testMutableCollectionRetainAll(arrayList(), 1, 2)
testMutableCollectionClear(arrayList(1, 2))
}
fun <T> testListGet(l: List<T>, t: T) = assertEquals(t, l.get(0))
fun <T> testListIndexOf(l: List<T>, t: T) = assertEquals(0, l.indexOf(t))
fun <T> testListIterator(l: List<T>, t1 : T, t2 : T) {
val indexes = arrayList<Int>()
val result = arrayList<T>()
val it = l.listIterator()
while (it.hasNext()) {
indexes.add(it.nextIndex())
result.add(it.next())
}
while (it.hasPrevious()) {
indexes.add(it.previousIndex())
result.add(it.previous())
}
assertEquals(arrayList(0, 1, 1, 0), indexes)
assertEquals(arrayList(t1, t2, t2, t1), result)
}
fun <T> testListSublist(l: List<T>, t: T) = assertEquals(arrayList(t), l.subList(0, 1))
fun <T> testMutableListSet(l: MutableList<T>, t: T) {
l.set(0, t)
assertEquals(arrayList(t), l)
}
fun <T> testMutableListIterator(l: MutableList<T>, t1: T, t2: T, t3: T) {
val it = l.listIterator()
while (it.hasNext()) {
it.next()
it.add(t3)
}
assertEquals(arrayList(t1, t3, t2, t3), l)
}
fun testList() {
testListGet(arrayList(1), 1)
testListIndexOf(arrayList(1), 1)
testListIterator(arrayList("a", "b"), "a", "b")
testListSublist(arrayList(1, 2), 1)
testMutableListSet(arrayList(2), 4)
testMutableListIterator(arrayList(1, 2), 1, 2, 3)
}
fun <K, V> testMapContainsKey(map: Map<K, V>, k: K) = assertTrue(map.containsKey(k))
fun <K, V> testMapKeys(map: Map<K, V>, k1: K, k2: K) = assertEqualCollections(hashSet(k1, k2), map.keySet())
fun <K, V> testMapValues(map: Map<K, V>, v1: V, v2: V) = assertEqualCollections(hashSet(v1, v2), map.values())
fun <K, V> testMapEntrySet(map: Map<K, V>, k : K, v: V) {
for (entry in map.entrySet()) {
assertEquals(k, entry.key)
assertEquals(v, entry.value)
}
}
fun <K, V> testMutableMapEntry(map: MutableMap<K, V>, k1 : K, v: V) {
for (entry in map.entrySet()) {
entry.setValue(v)
}
assertEquals(hashMap(k1 to v), map)
}
fun testMap() {
testMapContainsKey(hashMap(1 to 'a', 2 to 'b'), 2)
testMapKeys(hashMap(1 to 'a', 2 to 'b'), 1, 2)
testMapValues(hashMap(1 to 'a', 2 to 'b'), 'a', 'b')
testMapEntrySet(hashMap(1 to 'a'), 1, 'a')
testMutableMapEntry(hashMap(1 to 'a'), 1, 'b')
}
fun box() : String {
testCollection()
testList()
testMap()
return "OK"
}
fun <T> assertEqualCollections(c1: Collection<T>, c2: Collection<T>) = assertEquals(c1.toCollection(hashSet<T>()), c2.toCollection(hashSet<T>()))
@@ -1,116 +0,0 @@
package testeval
import java.util.*
trait Expression
class Num(val value : Int) : Expression
class Sum(val left : Expression, val right : Expression) : Expression
class Mult(val left : Expression, val right : Expression) : Expression
fun eval(e : Expression) : Int {
return when (e) {
is Num -> e.value
is Sum -> eval(e.left) + eval (e.right)
is Mult -> eval(e.left) * eval (e.right)
else -> throw AssertionError("Unknown expression")
}
}
trait ParseResult<out T> {
val success : Boolean
val value : T
}
class Success<T>(override val value : T) : ParseResult<T> {
public override val success : Boolean = true
}
class Failure(val message : String) : ParseResult<Nothing> {
override val success = false
override val value : Nothing = throw UnsupportedOperationException("Don't call value on a Failure")
}
open class Token(val text : String) {
override fun toString() = text
}
object LPAR : Token("(")
object RPAR : Token(")")
object PLUS : Token("+")
object TIMES : Token("*")
object EOF : Token("EOF")
class Number(text : String) : Token(text)
class Error(text : String) : Token("[Error: $text]")
fun tokenize(text : String) : Deque<Token> {
val result = LinkedList<Token>()
for (c in text) {
result add when (c) {
'(' -> LPAR
')' -> RPAR
'+' -> PLUS
'*' -> TIMES
in '0'..'9' -> Number(c.toString())
else -> Error(c.toString())
}
}
result.add(EOF)
return result
}
fun parseSum(tokens : Deque<Token>) : ParseResult<Expression> {
val left = parseMult(tokens)
if (!left.success) return left
if (tokens.peek() == PLUS) {
tokens.pop()
val right = parseSum(tokens)
if (!right.success) return right
return Success(Sum(left.value, right.value))
}
return left
}
fun parseMult(tokens : Deque<Token>) : ParseResult<Expression> {
val left = parseAtomic(tokens)
if (!left.success) return left
if (tokens.peek() == PLUS) {
tokens.pop()
val right = parseMult(tokens)
if (!right.success) return right
return Success(Mult(left.value, right.value))
}
return left
}
fun parseAtomic(tokens : Deque<Token>) : ParseResult<Expression> {
val token = tokens.poll()
return when (token) {
LPAR -> {
val result = parseSum(tokens)
val rpar = tokens.poll()
if (rpar == RPAR)
result
else
Failure("Expecting ')'")
}
is Number -> Success(Num(Integer.parseInt((token as Token).text)))
else -> Failure("Unexpected EOF")
}
}
fun parse(text : String) : ParseResult<Expression> = parseSum(tokenize(text))
fun box(): String {
if (1 != eval(Num(1))) return "fail 1"
if (2 != eval(Sum(Num(1), Num(1)))) return "fail 2"
if (3 != eval(Mult(Num(3), Num(1)))) return "fail 3"
if (6 != eval(Mult(Num(3), Sum(Num(1), Num(1))))) return "fail 4"
if (1 != eval(parse("1").value)) return "fail 5"
return "OK"
}
@@ -1,27 +0,0 @@
package pack
import java.util.ArrayList
import java.util.regex.Pattern
import kotlin.util.*
class C{
public fun foo(){
val items : Collection<Item> = java.util.Collections.singleton(Item())!!
val result = ArrayList<Item>()
val pattern: Pattern? = Pattern.compile("...")
items.filterTo(result) {
pattern!!.matcher(it.name())!!.matches()
}
}
private fun Item.name() : String = ""
}
class Item{
}
fun box() : String {
C().foo()
return "OK"
}
@@ -1,17 +0,0 @@
import org.w3c.dom.Element
import org.xml.sax.InputSource
import javax.xml.parsers.DocumentBuilderFactory
import java.io.StringReader
class MyElement(e: Element): Element by e {
fun bar() = "OK"
}
fun box() : String {
val factory = DocumentBuilderFactory.newInstance()!!;
val builder = factory.newDocumentBuilder()!!;
val source = InputSource(StringReader("<OK></OK>"));
val doc = builder.parse(source)!!;
val myElement = MyElement(doc.getDocumentElement()!!)
return myElement.getTagName()!!
}
@@ -1,14 +0,0 @@
import kotlin.*
fun box() : String {
val vector = java.util.Vector<Int>()
vector.add(1)
vector.add(2)
vector.add(3)
var sum = 0
for(e in vector.elements()) {
sum += e
}
return if(sum == 6) "OK" else "fail"
}
@@ -1,126 +0,0 @@
package mask
import java.io.*
import java.util.*
fun box() : String {
val input = StringReader("/aaa/bbb/ccc/ddd")
val luhny = Luhny()
input.forEachChar {
luhny.charIn(it)
}
luhny.printAll()
return "OK"
}
class Luhny() {
val buffer = LinkedList<Char>()
val digits = LinkedList<Int>()
var toBeMasked = 0
fun charIn(it : Char) {
buffer.addLast(it)
// Commented for KT-621
// when (it) {
// .isDigit() => digits.addLast(it.toInt() - '0'.toInt())
// ' ', '-' => {}
// else => {
// printAll()
// digits.clear()
// }
// }
if (it.isDigit()) {
digits.addLast(it.toInt() - '0'.toInt())
} else if (it == ' ' || it == '-') {
} else {
printAll()
digits.clear()
}
if (digits.size() > 16)
printOneDigit()
check()
}
fun check() {
if (digits.size() < 14) return
print("check")
val sum = digits.sum { i, d ->
// println("$i -> $d")
if (i % 2 == digits.size()) {
val f = d * 2 / 10
val s = d * 2 % 10
// println("$d: f = $f, s = $s")
(f + s).pr {
// println("to be doubled: $i -> $d : $it")
}
} else d
}
// println(sum)
if (sum % 10 == 0) {print("s"); toBeMasked = digits.size()}
}
fun printOneDigit() {
while (!buffer.isEmpty()) {
val c = buffer.removeFirst()
out(c)
if (c.isDigit()) {
digits.removeFirst()
return
}
}
}
fun printAll() {
while (!buffer.isEmpty())
out(buffer.removeFirst())
}
fun out(c : Char) {
if (toBeMasked > 0) {
print('X')
toBeMasked--
}
else {
// print(c)
}
}
}
fun <T> T.pr(f : (T) -> Unit) : T {
f(this)
return this
}
fun LinkedList<Int>.sum(f : (Int, Int )-> Int): Int {
var sum = 0
for (i in 1..size()) {
val j = size() - i
sum += f(j, get(j))
}
return sum
}
//fun <T> List<T>.backwards() : Itl<T> = object : Itl<T> {
// override fun iterator() : It<T> =
// object : It<T> {
// var current = size()
// override fun next() : T = get(--current)
// override fun hasNext() : Boolean = current > 0
// override fun remove() {throw UnsupportedOperationException()}
// }
//}
// fun Char.isDigit() = Character.isDigit(this)
fun Reader.forEachChar(body : (Char) -> Unit) {
do {
var i = read();
if (i == -1) break
body(i.toChar())
} while(true)
}
@@ -1,107 +0,0 @@
package mask
import java.io.*
import java.util.*
fun box() : String {
val input = StringReader("/aaa/bbb/ccc/ddd")
val luhny = Luhny()
input.forEachChar {
luhny.process(it)
}
luhny.printAll()
return "OK"
}
class Luhny() {
private val buffer = ArrayDeque<Char>()
private val digits = ArrayDeque<Int>(16)
private var toBeMasked = 0
fun process(it : Char) {
buffer.addLast(it)
// Commented for KT-621
// when (it) {
// .isDigit() => digits.addLast(it.toInt() - '0'.toInt())
// ' ', '-' => {}
// else => printAll()
// }
if (it.isDigit()) {
digits.addLast(it.toInt() - '0'.toInt())
} else if (it == ' ' || it == '-') {
} else {
printAll()
}
if (digits.size() > 16)
printOneDigit()
check()
}
private fun check() {
val size = digits.size()
if (size < 14) return
val sum = digits.sum {i, d ->
if (i % 2 == size % 2) double(d) else d
}
// var sum = 0
// var i = 0
// for (d in digits) {
// sum += if (i % 2 == size % 2) double(d) else d
// i++
// }
if (sum % 10 == 0) toBeMasked = digits.size()
}
private fun double(d : Int) = d * 2 / 10 + d * 2 % 10
private fun printOneDigit() {
while (!buffer.isEmpty()) {
val c = buffer.removeFirst()!!
print(c)
if (c.isDigit()) {
digits.removeFirst()
return
}
}
}
fun printAll() {
while (!buffer.isEmpty())
print(buffer.removeFirst())
digits.clear()
}
private fun print(c : Char) {
if (c.isDigit() && toBeMasked > 0) {
kotlin.io.print('X')
toBeMasked--
} else {
// kotlin.io.print(c)
}
}
}
// fun Char.isDigit() = Character.isDigit(this)
fun Iterable<Int>.sum(f : (index : Int, value : Int) -> Int) : Int {
var sum = 0
var i = 0
for (d in this) {
sum += f(i, d)
i++
}
return sum
}
fun Reader.forEachChar(body : (Char) -> Unit) {
do {
var i = read();
if (i == -1) break
body(i.toChar())
} while(true)
}
@@ -1,144 +0,0 @@
package mask
import java.io.*
import java.util.*
fun box() : String {
val input = StringReader("/aaa/bbb/ccc/ddd")
val luhny = Luhny()
input.forEachChar {
luhny.charIn(it)
}
luhny.printAll()
return "OK"
}
class Luhny() {
val buffer = LinkedList<Char>()
val digits = LinkedList<Int>()
var toBeMasked = 0
fun charIn(it : Char) {
buffer.push(it)
// Commented for KT-621
// when (it) {
// .isDigit() => digits.push(it.toInt() - '0'.toInt())
// ' ', '-' => {}
// else => {
// printAll()
// digits.clear()
// }
// }
if (it.isDigit()) {
digits.push(it.toInt() - '0'.toInt())
} else if (it == ' ' || it == '-') {
} else {
printAll()
digits.clear()
}
if (digits.size() > 16)
printOneDigit()
check()
}
fun check() {
if (digits.size() < 14) return
val sum = digits.sum { i, d ->
if (i % 2 != 0)
d * 2 / 10 + d * 2 % 10
else d
}
if (sum % 10 == 0) toBeMasked = digits.size()
}
fun printOneDigit() {
while (!buffer.isEmpty()) {
val c = buffer.pop()
out(c)
if (c.isDigit()) {
digits.pop()
return
}
}
}
fun printAll() {
while (!buffer.isEmpty())
out(buffer.pop())
}
fun out(c : Char) {
if (toBeMasked > 0) {
print('X')
toBeMasked--
}
else {
// print(c)
}
}
}
fun LinkedList<Int>.sum(f : (Int, Int) -> Int) : Int {
var sum = 0
var i = 0
for (d in backwards()) {
sum += f(i, d)
i++
}
return sum
}
fun <T> List<T>.backwards() : Iterable<T> = object : Iterable<T> {
override fun iterator() : Iterator<T> =
object : Iterator<T> {
var current = size()
override fun next() : T = get(--current)
override fun hasNext() : Boolean = current > 0
}
}
// fun Char.isDigit() = Character.isDigit(this)
//class Queue<T>(initialBufSize : Int) {
//
// private var bufSize = initialBufSize
// private val buf = Array<T>(initialBufSize)
// private var head = 0
// private var tail = 0
// private var size = 0
//
// val empty : Boolean get() = size == 0
//
// private fun prev(i : Int) = (bufSize + i - 1) % bufSize
// private fun next(i : Int) = (i + 1) % bufSize
//
// fun push(c : T) {
// buf[tail] = c
// tail = prev(tail)
// size++
// }
//
// fun pop() : T {
// if (size == 0) throw IllegalStateException()
// size--
// val result = buf[head]
// head = prev(head)
// return result
// }
//
// fun clear() {}
//}
fun Reader.forEachChar(body : (Char) -> Unit) {
do {
var i = read();
if (i == -1) break
body(i.toChar())
} while(true)
}
@@ -1,19 +0,0 @@
import kotlin.util.*
import java.io.*
import java.util.*
fun sample() : Reader {
return StringReader("""Hello
World""");
}
fun box() : String {
// TODO compiler error
// both these expressions causes java.lang.NoClassDefFoundError: collections/CollectionPackage
val list1 = sample().useLines{it.toArrayList()}
val list2 = sample().useLines<ArrayList<String>>{it.toArrayList()}
if(arrayList("Hello", "World") != list1) return "fail"
if(arrayList("Hello", "World") != list2) return "fail"
return "OK"
}