link more documentation to test cases and filled out the test cases a little

This commit is contained in:
James Strachan
2012-03-27 12:26:20 +01:00
parent 31fd665913
commit 133afa8327
9 changed files with 106 additions and 58 deletions
+11
View File
@@ -4,6 +4,8 @@ import java.io.ByteArrayInputStream
import java.util.Arrays
import java.nio.charset.Charset
import java.util.List
import java.util.ArrayList
// Array "constructor"
inline fun <T> array(vararg t : T) : Array<T> = t
@@ -121,3 +123,12 @@ inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
/** Returns the array if its not null or else returns an empty array */
inline fun <T> Array<T>?.orEmpty() : Array<T> = if (this != null) this else array<T>()
inline fun CharArray.toList(): List<Character> {
val list = ArrayList<Character>(this.size)
for (c in this) {
if (c != null) {
list.add(Character(c))
}
}
return list
}
@@ -2,7 +2,11 @@ package kotlin
import java.util.*
/** Returns a new List containing the results of applying the given function to each element in this collection */
/**
* Returns a new List containing the results of applying the given function to each element in this collection
*
* @includeFunction ../../test/CollectionTest.kt map
*/
inline fun <T, R> java.util.Collection<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -9,19 +9,33 @@ import java.util.*
// See [[GenerateStandardLib.kt]] for more details
//
/** Returns a new List containing all elements in this collection which match the given predicate */
/**
* Returns a new List containing all elements in this collection which match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt filter
*/
inline fun <T> java.lang.Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Returns a List containing all the non null elements in this collection */
/**
* Returns a List containing all the non null elements in this collection
*
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
/**
* Returns a new collection containing all elements in this collection which do not match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt filterNot
*/
inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/**
* Returns the result of transforming each item in the collection to a one or more values which
* are concatenated together into a single collection
*/
* Returns the result of transforming each item in the collection to a one or more values which
* are concatenated together into a single collection
*
* @includeFunction ../../test/CollectionTest.kt flatMap
*/
inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
return flatMapTo(ArrayList<R>(), transform)
}
@@ -28,8 +28,8 @@ fun <T> java.lang.Iterable<T>.count() : Int {
* Get the first element in the collection.
*
* Will throw an exception if there are no elements
* TODO: Specify type of the exception
*/
// TODO: Specify type of the exception
inline fun <T> java.lang.Iterable<T>.first() : T {
if (this is AbstractList<T>) {
return this.get(0)
@@ -46,8 +46,10 @@ inline fun <T> java.lang.Iterable<T>.first() : T {
* last item by iterating through the all items.
*
* Will throw an exception if there are no elements.
* TODO: Specify type of the exception
*
* @includeFunction ../../test/CollectionTest.kt last
*/
// TODO: Specify type of the exception
fun <T> java.lang.Iterable<T>.last() : T {
if (this is List<T>) {
return this.get(this.size() - 1);
@@ -71,7 +73,6 @@ fun <T> java.lang.Iterable<T>.last() : T {
* method will be used.
*/
fun <T> java.lang.Iterable<T>.contains(item : T) : Boolean {
kotlin.io.println("!!!!!")
if (this is java.util.AbstractCollection<T>) {
return this.contains(item);
}
+11 -3
View File
@@ -20,7 +20,7 @@ val JMap<*,*>.empty : Boolean
/** Provides [] access to maps */
fun <K, V> JMap<K, V>.set(key : K, value : V) = this.put(key, value)
/** Returns the Mao if its not null otherwise it returns the empty Map */
/** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */
inline fun <K,V> java.util.Map<K,V>?.orEmpty() : java.util.Map<K,V>
= if (this != null) this else Collections.EMPTY_MAP as java.util.Map<K,V>
@@ -35,7 +35,11 @@ inline fun <K,V> java.util.Map<K,V>?.orEmpty() : java.util.Map<K,V>
//val <K,V> JEntry<K,V>.value : V
// get() = getValue().sure()
/** Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key */
/**
* Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key
*
* @includeFunction ../../test/MapTest.kt getOrElse
*/
inline fun <K,V> java.util.Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
@@ -45,7 +49,11 @@ inline fun <K,V> java.util.Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V
}
}
/** Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned */
/**
* Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned
*
* @includeFunction ../../test/MapTest.kt getOrElse
*/
inline fun <K,V> java.util.Map<K,V>.getOrPut(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
+4
View File
@@ -1,6 +1,8 @@
package kotlin
import java.io.StringReader
import java.util.List
import java.util.ArrayList
inline fun String.lastIndexOf(str: String) = (this as java.lang.String).lastIndexOf(str)
@@ -52,6 +54,8 @@ inline fun String.getBytes() = (this as java.lang.String).getBytes().sure()
inline fun String.toCharArray() = (this as java.lang.String).toCharArray().sure()
inline fun String.toCharList(): List<Character> = toCharArray().toList()
inline fun String.format(format : String, vararg args : Any?) = java.lang.String.format(format, args).sure()
inline fun String.split(regex : String) = (this as java.lang.String).split(regex)
+33 -27
View File
@@ -100,6 +100,18 @@ class CollectionTest {
}
}
Test fun filterNotNull() {
val data = arrayList(null, "foo", null, "bar")
val foo = data.filterNotNull()
assertEquals(2, foo.size)
assertEquals(linkedList("foo", "bar"), foo)
assertTrue {
foo is List<String>
}
}
Test fun filterIntoSet() {
val data = arrayList("foo", "bar")
// TODO would be nice to avoid the <String>
@@ -141,19 +153,12 @@ class CollectionTest {
}
Test fun flatMap() {
val data = arrayList("foo", "bar")
val characters = arrayList('f', 'o', 'o', 'b', 'a', 'r')
// TODO figure out how to get a line like this to compile :)
/*
val characters = data.flatMap<String,Character>{
it.toCharArray().toList() as Collection<Character>
}
*/
todo {
println("Got list of characters ${characters}")
val text = characters.join("")
assertEquals("foobar", text)
}
val data = arrayList("", "foo", "bar", "x", "")
val characters = data.flatMap<String,Character>{ it.toCharList() }
println("Got list of characters ${characters}")
assertEquals(7, characters.size())
val text = characters.join("")
assertEquals("foobarx", text)
}
Test fun forEach() {
@@ -165,13 +170,13 @@ class CollectionTest {
/*
// TODO would be nice to be able to write this as this
//numbers.fold(0){it + it2}
numbers.fold(0){(it, it2) -> it + it2}
// TODO would be nice to be able to write this as this
//numbers.fold(0){it + it2}
numbers.fold(0){(it, it2) -> it + it2}
// TODO would be nice to be able to write this as this
// numbers.map{it.toString()}.fold(""){it + it2}
numbers.map<Int, String>{it.toString()}.fold(""){(it, it2) -> it + it2}
// TODO would be nice to be able to write this as this
// numbers.map{it.toString()}.fold(""){it + it2}
numbers.map<Int, String>{it.toString()}.fold(""){(it, it2) -> it + it2}
*/
Test fun fold() {
// lets calculate the sum of some numbers
@@ -204,7 +209,6 @@ class CollectionTest {
}
}
/*
TODO inference engine should not need this type info?
val byLength = words.groupBy<String, Int>{it.length}
@@ -225,14 +229,14 @@ class CollectionTest {
assertEquals("<foo-bar>", text)
}
/*
TODO compiler bug
we should be able to remove the explicit type <String,Int> on the map function
http://youtrack.jetbrains.net/issue/KT-1145
*/
Test fun map() {
val data = arrayList("foo", "bar")
/**
TODO compiler bug
we should be able to remove the explicit type on the function
http://youtrack.jetbrains.net/issue/KT-1145
*/
val lengths = data.map<String, Int>{s -> s.length}
val lengths = data.map<String, Int>{ it.length }
assertTrue {
lengths.all{it == 3}
}
@@ -281,9 +285,11 @@ class CollectionTest {
val data = arrayList("foo", "bar")
assertEquals("bar", data.last())
assertEquals(25, arrayList(15, 19, 20, 25).last())
// assertEquals(19, TreeSet(arrayList(90, 47, 19)).first())
assertEquals('a', linkedList('a').last())
}
// TODO
// assertEquals(19, TreeSet(arrayList(90, 47, 19)).first())
Test fun lastException() {
fails { arrayList<Int>().last() }
+12 -16
View File
@@ -2,18 +2,14 @@ package test.collections
import kotlin.test.*
// TODO can we avoid importing all this stuff by default I wonder?
// e.g. making println and the collection builder methods public by default?
import kotlin.*
import kotlin.io.*
import kotlin.util.*
import java.util.*
import junit.framework.TestCase
import org.junit.Test
class MapTest() : TestCase() {
val data: java.util.Map<String, Int> = java.util.HashMap<String, Int>()
class MapTest {
fun testGetOrElse() {
Test fun getOrElse() {
val data = HashMap<String, Int>()
val a = data.getOrElse("foo"){2}
assertEquals(2, a)
@@ -22,7 +18,8 @@ class MapTest() : TestCase() {
assertEquals(0, data.size())
}
fun testGetOrPut() {
Test fun getOrPut() {
val data = HashMap<String, Int>()
val a = data.getOrPut("foo"){2}
assertEquals(2, a)
@@ -32,17 +29,16 @@ class MapTest() : TestCase() {
assertEquals(1, data.size())
}
fun testSizeAndEmpty() {
Test fun sizeAndEmpty() {
val data = HashMap<String, Int>()
assertTrue{ data.empty }
// TODO using size breaks a test case
assertEquals(data.size(), 0)
assertEquals(data.size, 0)
}
fun testSetViaIndexOperators() {
val map = java.util.HashMap<String, String>()
Test fun setViaIndexOperators() {
val map = HashMap<String, String>()
assertTrue{ map.empty }
// TODO using size breaks a test case
assertEquals(map.size(), 0)
assertEquals(map.size, 0)
map["name"] = "James"