migrated most of the test cases over to the new -> syntax for functions; still can't compile or run the tests yet though :(, needs more work...
This commit is contained in:
+114
-112
@@ -1,136 +1,138 @@
|
|||||||
package std
|
package std.io
|
||||||
|
|
||||||
package io {
|
import java.io.*
|
||||||
import java.io.*
|
import java.nio.charset.*
|
||||||
import java.nio.charset.*
|
|
||||||
|
|
||||||
inline fun print(message : Any?) { System.out?.print(message) }
|
inline fun print(message : Any?) { System.out?.print(message) }
|
||||||
inline fun print(message : Int) { System.out?.print(message) }
|
inline fun print(message : Int) { System.out?.print(message) }
|
||||||
inline fun print(message : Long) { System.out?.print(message) }
|
inline fun print(message : Long) { System.out?.print(message) }
|
||||||
inline fun print(message : Byte) { System.out?.print(message) }
|
inline fun print(message : Byte) { System.out?.print(message) }
|
||||||
inline fun print(message : Short) { System.out?.print(message) }
|
inline fun print(message : Short) { System.out?.print(message) }
|
||||||
inline fun print(message : Char) { System.out?.print(message) }
|
inline fun print(message : Char) { System.out?.print(message) }
|
||||||
inline fun print(message : Boolean) { System.out?.print(message) }
|
inline fun print(message : Boolean) { System.out?.print(message) }
|
||||||
inline fun print(message : Float) { System.out?.print(message) }
|
inline fun print(message : Float) { System.out?.print(message) }
|
||||||
inline fun print(message : Double) { System.out?.print(message) }
|
inline fun print(message : Double) { System.out?.print(message) }
|
||||||
inline fun print(message : CharArray) { System.out?.print(message) }
|
inline fun print(message : CharArray) { System.out?.print(message) }
|
||||||
|
|
||||||
inline fun println(message : Any?) { System.out?.println(message) }
|
inline fun println(message : Any?) { System.out?.println(message) }
|
||||||
inline fun println(message : Int) { System.out?.println(message) }
|
inline fun println(message : Int) { System.out?.println(message) }
|
||||||
inline fun println(message : Long) { System.out?.println(message) }
|
inline fun println(message : Long) { System.out?.println(message) }
|
||||||
inline fun println(message : Byte) { System.out?.println(message) }
|
inline fun println(message : Byte) { System.out?.println(message) }
|
||||||
inline fun println(message : Short) { System.out?.println(message) }
|
inline fun println(message : Short) { System.out?.println(message) }
|
||||||
inline fun println(message : Char) { System.out?.println(message) }
|
inline fun println(message : Char) { System.out?.println(message) }
|
||||||
inline fun println(message : Boolean) { System.out?.println(message) }
|
inline fun println(message : Boolean) { System.out?.println(message) }
|
||||||
inline fun println(message : Float) { System.out?.println(message) }
|
inline fun println(message : Float) { System.out?.println(message) }
|
||||||
inline fun println(message : Double) { System.out?.println(message) }
|
inline fun println(message : Double) { System.out?.println(message) }
|
||||||
inline fun println(message : CharArray) { System.out?.println(message) }
|
inline fun println(message : CharArray) { System.out?.println(message) }
|
||||||
inline fun println() { System.out?.println() }
|
inline fun println() { System.out?.println() }
|
||||||
|
|
||||||
private val stdin : BufferedReader = BufferedReader(InputStreamReader(object : InputStream() {
|
private val stdin : BufferedReader = BufferedReader(InputStreamReader(object : InputStream() {
|
||||||
override fun read() : Int {
|
override fun read() : Int {
|
||||||
return System.`in`?.read() ?: -1
|
return System.`in`?.read() ?: -1
|
||||||
}
|
|
||||||
|
|
||||||
override fun reset() {
|
|
||||||
System.`in`?.reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun read(b: ByteArray?): Int {
|
|
||||||
return System.`in`?.read(b) ?: -1
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun close() {
|
|
||||||
System.`in`?.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun mark(readlimit: Int) {
|
|
||||||
System.`in`?.mark(readlimit)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun skip(n: Long): Long {
|
|
||||||
return System.`in`?.skip(n) ?: -1.lng
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun available(): Int {
|
|
||||||
return System.`in`?.available() ?: 0
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun markSupported(): Boolean {
|
|
||||||
return System.`in`?.markSupported() ?: false
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun read(b: ByteArray?, off: Int, len: Int): Int {
|
|
||||||
return System.`in`?.read(b, off, len) ?: -1
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
|
|
||||||
inline fun readLine() : String? = stdin.readLine()
|
|
||||||
|
|
||||||
/** Uses the given resource then closes it to ensure its closed again */
|
|
||||||
inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
|
|
||||||
var closed = false
|
|
||||||
try {
|
|
||||||
return block(this)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
try {
|
|
||||||
this.close()
|
|
||||||
} catch (closeException: Exception) {
|
|
||||||
// eat the closeException as we are already throwing the original cause
|
|
||||||
// and we don't want to mask the real exception
|
|
||||||
}
|
|
||||||
throw e
|
|
||||||
} finally {
|
|
||||||
if (!closed) {
|
|
||||||
this.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun InputStream.iterator() : ByteIterator =
|
override fun reset() {
|
||||||
object: ByteIterator() {
|
System.`in`?.reset()
|
||||||
override val hasNext : Boolean
|
}
|
||||||
get() = available() > 0
|
|
||||||
|
|
||||||
override fun nextByte() = read().byt
|
override fun read(b: ByteArray?): Int {
|
||||||
}
|
return System.`in`?.read(b) ?: -1
|
||||||
|
}
|
||||||
|
|
||||||
inline fun InputStream.buffered(bufferSize: Int) = BufferedInputStream(this, bufferSize)
|
override fun close() {
|
||||||
|
System.`in`?.close()
|
||||||
|
}
|
||||||
|
|
||||||
inline val InputStream.reader : InputStreamReader
|
override fun mark(readlimit: Int) {
|
||||||
get() = InputStreamReader(this)
|
System.`in`?.mark(readlimit)
|
||||||
|
}
|
||||||
|
|
||||||
inline val InputStream.bufferedReader : BufferedReader
|
override fun skip(n: Long): Long {
|
||||||
get() = BufferedReader(reader)
|
return System.`in`?.skip(n) ?: -1.lng
|
||||||
|
}
|
||||||
|
|
||||||
inline fun InputStream.reader(charset: Charset) : InputStreamReader = InputStreamReader(this, charset)
|
override fun available(): Int {
|
||||||
|
return System.`in`?.available() ?: 0
|
||||||
|
}
|
||||||
|
|
||||||
inline fun InputStream.reader(charsetName: String) = InputStreamReader(this, charsetName)
|
override fun markSupported(): Boolean {
|
||||||
|
return System.`in`?.markSupported() ?: false
|
||||||
|
}
|
||||||
|
|
||||||
inline fun InputStream.reader(charsetDecoder: CharsetDecoder) = InputStreamReader(this, charsetDecoder)
|
override fun read(b: ByteArray?, off: Int, len: Int): Int {
|
||||||
|
return System.`in`?.read(b, off, len) ?: -1
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
inline val InputStream.buffered : BufferedInputStream
|
inline fun readLine() : String? = stdin.readLine()
|
||||||
get() = if(this is BufferedInputStream) this else BufferedInputStream(this)
|
|
||||||
|
/** Uses the given resource then closes it to ensure its closed again */
|
||||||
|
/*
|
||||||
|
inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
|
||||||
|
var closed = false
|
||||||
|
try {
|
||||||
|
return block(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
try {
|
||||||
|
this.close()
|
||||||
|
} catch (closeException: Exception) {
|
||||||
|
// eat the closeException as we are already throwing the original cause
|
||||||
|
// and we don't want to mask the real exception
|
||||||
|
}
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
|
if (!closed) {
|
||||||
|
this.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
fun InputStream.iterator() : ByteIterator =
|
||||||
|
object: ByteIterator() {
|
||||||
|
override val hasNext : Boolean
|
||||||
|
get() = available() > 0
|
||||||
|
|
||||||
|
override fun nextByte() = read().byt
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun InputStream.buffered(bufferSize: Int) = BufferedInputStream(this, bufferSize)
|
||||||
|
|
||||||
|
inline val InputStream.reader : InputStreamReader
|
||||||
|
get() = InputStreamReader(this)
|
||||||
|
|
||||||
|
inline val InputStream.bufferedReader : BufferedReader
|
||||||
|
get() = BufferedReader(reader)
|
||||||
|
|
||||||
|
inline fun InputStream.reader(charset: Charset) : InputStreamReader = InputStreamReader(this, charset)
|
||||||
|
|
||||||
|
inline fun InputStream.reader(charsetName: String) = InputStreamReader(this, charsetName)
|
||||||
|
|
||||||
|
inline fun InputStream.reader(charsetDecoder: CharsetDecoder) = InputStreamReader(this, charsetDecoder)
|
||||||
|
|
||||||
|
inline val InputStream.buffered : BufferedInputStream
|
||||||
|
get() = if(this is BufferedInputStream) this else BufferedInputStream(this)
|
||||||
|
|
||||||
// inline val Reader.buffered : BufferedReader
|
// inline val Reader.buffered : BufferedReader
|
||||||
// get() = if(this is BufferedReader) this else BufferedReader(this)
|
// get() = if(this is BufferedReader) this else BufferedReader(this)
|
||||||
|
|
||||||
inline fun Reader.buffered() = BufferedReader(this)
|
inline fun Reader.buffered() = BufferedReader(this)
|
||||||
|
|
||||||
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
|
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
|
||||||
|
|
||||||
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
/*
|
||||||
|
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
||||||
|
|
||||||
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
|
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
|
||||||
var nextLine: String? = null
|
var nextLine: String? = null
|
||||||
|
|
||||||
override val hasNext: Boolean
|
override val hasNext: Boolean
|
||||||
get() {
|
get() {
|
||||||
nextLine = reader.readLine()
|
nextLine = reader.readLine()
|
||||||
return nextLine != null
|
return nextLine != null
|
||||||
}
|
|
||||||
|
|
||||||
override fun next(): String = nextLine.sure()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
override fun next(): String = nextLine.sure()
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|||||||
@@ -190,10 +190,10 @@ val <T> List<T>.tail : T?
|
|||||||
get() {
|
get() {
|
||||||
val s = this.size
|
val s = this.size
|
||||||
return if (s > 0) this.get(s - 1) else null
|
return if (s > 0) this.get(s - 1) else null
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val <T> List<T>.last : T?
|
val <T> List<T>.last : T?
|
||||||
get() = this.tail
|
get() = this.tail
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -9,18 +9,18 @@ import org.junit.runner.*
|
|||||||
import org.junit.runner.notification.*
|
import org.junit.runner.notification.*
|
||||||
import junit.framework.*
|
import junit.framework.*
|
||||||
|
|
||||||
fun assert(message: String, block: fun() : Boolean) {
|
fun assert(message: String, block: ()-> Boolean) {
|
||||||
val actual = block()
|
val actual = block()
|
||||||
Assert.assertTrue(message, actual)
|
Assert.assertTrue(message, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun assert(block: fun() : Boolean) = assert(block.toString(), block)
|
fun assert(block: ()-> Boolean) = assert(block.toString(), block)
|
||||||
|
|
||||||
fun assertNot(message: String, block: fun() : Boolean) {
|
fun assertNot(message: String, block: ()-> Boolean) {
|
||||||
assert(message){ !block() }
|
assert(message){ !block() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun assertNot(block: fun() : Boolean) = assertNot(block.toString(), block)
|
fun assertNot(block: ()-> Boolean) = assertNot(block.toString(), block)
|
||||||
|
|
||||||
fun assert(actual: Boolean, message: String) {
|
fun assert(actual: Boolean, message: String) {
|
||||||
println("Answer: ${actual} for ${message}")
|
println("Answer: ${actual} for ${message}")
|
||||||
@@ -34,7 +34,7 @@ fun assertNull(actual: Any?, message: String = "") {
|
|||||||
Assert.assertNull(message, actual)
|
Assert.assertNull(message, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fails(block: fun() : Any) {
|
fun fails(block: ()-> Any) {
|
||||||
try {
|
try {
|
||||||
block()
|
block()
|
||||||
Assert.fail("Expected an exception to be thrown")
|
Assert.fail("Expected an exception to be thrown")
|
||||||
@@ -43,7 +43,7 @@ fun fails(block: fun() : Any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun todo(block: fun(): Any) {
|
fun todo(block: ()-> Any) {
|
||||||
println("TODO at " + Exception().getStackTrace()?.get(1) + " for " + block)
|
println("TODO at " + Exception().getStackTrace()?.get(1) + " for " + block)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,24 +6,24 @@ import java.util.*
|
|||||||
trait Traversable<T> {
|
trait Traversable<T> {
|
||||||
|
|
||||||
/** Returns true if any elements in the collection match the given predicate */
|
/** Returns true if any elements in the collection match the given predicate */
|
||||||
fun any(predicate: fun(T): Boolean) : Boolean
|
fun any(predicate: (T)-> Boolean) : Boolean
|
||||||
|
|
||||||
/** Returns true if all elements in the collection match the given predicate */
|
/** Returns true if all elements in the collection match the given predicate */
|
||||||
fun all(predicate: fun(T): Boolean) : Boolean
|
fun all(predicate: (T)-> Boolean) : Boolean
|
||||||
|
|
||||||
/** Returns the first item in the collection which matches the given predicate or null if none matched */
|
/** Returns the first item in the collection which matches the given predicate or null if none matched */
|
||||||
fun find(predicate: fun(T): Boolean) : T?
|
fun find(predicate: (T)-> Boolean) : T?
|
||||||
|
|
||||||
/** Returns a new collection containing all elements in this collection which match the given predicate */
|
/** Returns a new collection containing all elements in this collection which match the given predicate */
|
||||||
// TODO using: Collection<T> for the return type - I wonder if this exact type could be
|
// TODO using: Collection<T> for the return type - I wonder if this exact type could be
|
||||||
// deduced somewhat from the This.Type; e.g. returning Set on a Set, Array on Array etc
|
// deduced somewhat from the This.Type; e.g. returning Set on a Set, Array on Array etc
|
||||||
fun filter(predicate: fun(T): Boolean) : Collection<T>
|
fun filter(predicate: (T)-> Boolean) : Collection<T>
|
||||||
|
|
||||||
/** Performs the given operation on each element inside the collection */
|
/** Performs the given operation on each element inside the collection */
|
||||||
fun foreach(operation: fun(element: T) : Unit)
|
fun foreach(operation: (element: T)-> Unit)
|
||||||
|
|
||||||
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
/** Returns a new collection containing the results of applying the given function to each element in this collection */
|
||||||
fun <T, R> java.lang.Iterable<T>.map(transform : fun(T) : R) : Collection<R>
|
fun <T, R> java.lang.Iterable<T>.map(transform : (T)-> R) : Collection<R>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class CollectionTest() : TestSupport() {
|
|||||||
data.all{it.length == 3}
|
data.all{it.length == 3}
|
||||||
}
|
}
|
||||||
assertNot {
|
assertNot {
|
||||||
data.all{s => s.startsWith("b")}
|
data.all{s -> s.startsWith("b")}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ class CollectionTest() : TestSupport() {
|
|||||||
we should be able to remove the explicit type on the function
|
we should be able to remove the explicit type on the function
|
||||||
http://youtrack.jetbrains.net/issue/KT-849
|
http://youtrack.jetbrains.net/issue/KT-849
|
||||||
*/
|
*/
|
||||||
val lengths = data.map<String,Int>{s => s.length}
|
val lengths = data.map<String,Int>{s -> s.length}
|
||||||
assert {
|
assert {
|
||||||
lengths.all{it == 3}
|
lengths.all{it == 3}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,13 @@ import std.test.*
|
|||||||
import std.io.*
|
import std.io.*
|
||||||
import std.util.*
|
import std.util.*
|
||||||
import java.io.*
|
import java.io.*
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class IoTest() : TestSupport() {
|
class IoTest() : TestSupport() {
|
||||||
val file = File("test/HelloWorld.txt")
|
val file = File("test/HelloWorld.txt")
|
||||||
|
|
||||||
fun testUse() {
|
fun testUse() {
|
||||||
val list = arrayList<String>()
|
val list = ArrayList<String>()
|
||||||
|
|
||||||
val reader = FileReader(file).buffered()
|
val reader = FileReader(file).buffered()
|
||||||
reader.close()
|
reader.close()
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import std.util.*
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class MapTest() : TestSupport() {
|
class MapTest() : TestSupport() {
|
||||||
val data = HashMap<String, Int>()
|
val data: Map<String,Int> = HashMap<String, Int>()
|
||||||
|
|
||||||
fun testGetOrElse() {
|
fun testGetOrElse() {
|
||||||
val a = data.getOrElse("foo"){2}
|
val a = data.getOrElse("foo"){2}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class SetTest() : TestSupport() {
|
|||||||
data.all{it.length == 3}
|
data.all{it.length == 3}
|
||||||
}
|
}
|
||||||
assertNot {
|
assertNot {
|
||||||
data.all{s => s.startsWith("b")}
|
data.all{(s: String) -> s.startsWith("b")}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ class SetTest() : TestSupport() {
|
|||||||
we should be able to remove the explicit type on the function
|
we should be able to remove the explicit type on the function
|
||||||
http://youtrack.jetbrains.net/issue/KT-849
|
http://youtrack.jetbrains.net/issue/KT-849
|
||||||
*/
|
*/
|
||||||
val lengths = data.map<String,Int>{s => s.length}
|
val lengths = data.map<String,Int>{(s: String) -> s.length}
|
||||||
assert {
|
assert {
|
||||||
lengths.all{it == 3}
|
lengths.all{it == 3}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user