got more map code compiling to JS and running as unit tests

This commit is contained in:
James Strachan
2012-07-03 07:43:15 +01:00
parent 8637373aa2
commit 541f3d4e78
12 changed files with 276 additions and 48 deletions
+1 -1
View File
@@ -105,5 +105,5 @@ val <T> List<T>.head : T?
*/
val <T> List<T>.tail : List<T>
get() {
return drop(1)
return this.drop(1)
}
+2 -40
View File
@@ -1,15 +1,9 @@
package kotlin
import java.util.Collection
import java.util.ArrayList
import java.util.LinkedList
import java.util.Collection
import java.util.HashSet
import java.util.LinkedHashSet
import java.util.TreeSet
import java.util.SortedSet
import java.util.Comparator
import java.io.PrintWriter
import java.io.PrintStream
import java.util.LinkedList
/**
Helper to make jet.Iterator usable in for
@@ -59,22 +53,6 @@ Add iterated elements to java.util.HashSet
*/
public inline fun <T> Iterator<T>.toHashSet() : HashSet<T> = toCollection(HashSet<T>())
/**
* Add iterated elements to a [[LinkedHashSet]] to preserve insertion order
*/
public inline fun <T> Iterator<T>.toLinkedSet() : LinkedHashSet<T> = toCollection(LinkedHashSet<T>())
/**
* Add iterated elements to [[SortedSet]] to ensure iteration is in the order of the default comparator
* for the type
*/
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
/**
* Add iterated elements to [[SortedSet]] with the given *comparator* to ensure iteration is in the order of the given comparator
*/
public inline fun <T> Iterator<T>.toSortedSet(comparator: Comparator<T>) : SortedSet<T> = toCollection(TreeSet<T>(comparator))
/**
* Creates a tuple of type [[#(A,B)]] from this and *that* which can be useful for creating [[Map]] literals
@@ -99,19 +77,3 @@ public inline fun runnable(action: ()-> Unit): Runnable {
}
}
}
/**
* Allows a stack trace to be printed from Kotlin's [[Throwable]]
*/
public inline fun Throwable.printStackTrace(writer: PrintWriter): Unit {
val jlt = this as java.lang.Throwable
jlt.printStackTrace(writer)
}
/**
* Allows a stack trace to be printed from Kotlin's [[Throwable]]
*/
public inline fun Throwable.printStackTrace(stream: PrintStream): Unit {
val jlt = this as java.lang.Throwable
jlt.printStackTrace(stream)
}
@@ -0,0 +1,46 @@
package kotlin
import java.util.Collection
import java.util.ArrayList
import java.util.LinkedList
import java.util.HashSet
import java.util.LinkedHashSet
import java.util.TreeSet
import java.util.SortedSet
import java.util.Comparator
import java.io.PrintWriter
import java.io.PrintStream
/**
* Add iterated elements to a [[LinkedHashSet]] to preserve insertion order
*/
public inline fun <T> Iterator<T>.toLinkedSet() : LinkedHashSet<T> = toCollection(LinkedHashSet<T>())
/**
* Add iterated elements to [[SortedSet]] to ensure iteration is in the order of the default comparator
* for the type
*/
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
/**
* Add iterated elements to [[SortedSet]] with the given *comparator* to ensure iteration is in the order of the given comparator
*/
public inline fun <T> Iterator<T>.toSortedSet(comparator: Comparator<T>) : SortedSet<T> = toCollection(TreeSet<T>(comparator))
/**
* Allows a stack trace to be printed from Kotlin's [[Throwable]]
*/
public inline fun Throwable.printStackTrace(writer: PrintWriter): Unit {
val jlt = this as java.lang.Throwable
jlt.printStackTrace(writer)
}
/**
* Allows a stack trace to be printed from Kotlin's [[Throwable]]
*/
public inline fun Throwable.printStackTrace(stream: PrintStream): Unit {
val jlt = this as java.lang.Throwable
jlt.printStackTrace(stream)
}
+1 -1
View File
@@ -3,7 +3,7 @@ package test.collections
import kotlin.test.*
import java.util.*
import org.junit.Test as test
import org.junit.Test as test
class MapTest {
+182
View File
@@ -0,0 +1,182 @@
package testPackage
import kotlin.test.*
import java.util.*
import org.junit.Test as test
class MapTest {
test fun getOrElse() {
val data = HashMap<String, Int>()
val a = data.getOrElse("foo"){2}
assertEquals(2, a)
val b = data.getOrElse("foo"){3}
assertEquals(3, b)
assertEquals(0, data.size())
}
test fun getOrPut() {
val data = HashMap<String, Int>()
val a = data.getOrPut("foo"){2}
assertEquals(2, a)
val b = data.getOrPut("foo"){3}
assertEquals(2, b)
assertEquals(1, data.size())
}
test fun sizeAndEmpty() {
val data = HashMap<String, Int>()
assertTrue{ data.empty }
assertEquals(data.size, 0)
}
/*
TODO fix bug with .set() on Map...
test fun setViaIndexOperators() {
val map = HashMap<String, String>()
assertTrue{ map.empty }
assertEquals(map.size, 0)
map["name"] = "James"
assertTrue{ !map.empty }
assertEquals(map.size(), 1)
assertEquals("James", map["name"])
}
*/
test fun createUsingTuples() {
val map = hashMap(#("a", 1), #("b", 2))
assertEquals(2, map.size)
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
}
test fun createUsingTo() {
val map = hashMap("a" to 1, "b" to 2)
assertEquals(2, map.size)
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
}
/*
test fun createLinkedMap() {
val map = linkedMap(#("c", 3), #("b", 2), #("a", 1))
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
assertEquals(3, map.get("c"))
assertEquals(arrayList("c", "b", "a"), map.keySet().toList())
}
test fun iterate() {
val map = TreeMap<String, String>()
map["beverage"] = "beer"
map["location"] = "Mells"
map["name"] = "James"
val list = arrayList<String>()
for (e in map) {
println("key = ${e.getKey()}, value = ${e.getValue()}")
list.add(e.getKey())
list.add(e.getValue())
}
assertEquals(6, list.size())
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
}
test fun iterateWithProperties() {
val map = TreeMap<String, String>()
map["beverage"] = "beer"
map["location"] = "Mells"
map["name"] = "James"
val list = arrayList<String>()
for (e in map) {
println("key = ${e.key}, value = ${e.value}")
list.add(e.key)
list.add(e.value)
}
assertEquals(6, list.size())
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
}
test fun map() {
val m1 = TreeMap<String, String>()
m1["beverage"] = "beer"
m1["location"] = "Mells"
val list = m1.map<String,String,String>{ it.value + " rocks" }
println("Got new list $list")
assertEquals(arrayList("beer rocks", "Mells rocks"), list)
}
test fun mapValues() {
val m1 = TreeMap<String, String>()
m1["beverage"] = "beer"
m1["location"] = "Mells"
val m2 = m1.mapValues<String,String,String>{ it.value + "2" }
println("Got new map $m2")
assertEquals(arrayList("beer2", "Mells2"), m2.values().toList())
}
test fun createSortedMap() {
val map = sortedMap(#("c", 3), #("b", 2), #("a", 1))
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
assertEquals(3, map.get("c"))
assertEquals(arrayList("a", "b", "c"), map.keySet()!!.toList())
}
test fun toSortedMap() {
val map = hashMap<String,Int>(#("c", 3), #("b", 2), #("a", 1))
val sorted = map.toSortedMap<String,Int>()
assertEquals(1, sorted.get("a"))
assertEquals(2, sorted.get("b"))
assertEquals(3, sorted.get("c"))
assertEquals(arrayList("a", "b", "c"), sorted.keySet()!!.toList())
}
test fun toSortedMapWithComparator() {
val map = hashMap(#("c", 3), #("bc", 2), #("bd", 4), #("abc", 1))
val c = comparator<String>{ a, b ->
val answer = a.length() - b.length()
if (answer == 0) a.compareTo(b) else answer
}
val sorted = map.toSortedMap(c)
assertEquals(arrayList("c", "bc", "bd", "abc"), sorted.keySet()!!.toList())
assertEquals(1, sorted.get("abc"))
assertEquals(2, sorted.get("bc"))
assertEquals(3, sorted.get("c"))
}
test fun compilerBug() {
val map = TreeMap<String, String>()
map["beverage"] = "beer"
map["location"] = "Mells"
map["name"] = "James"
var list = arrayList<String>()
for (e in map) {
println("key = ${e.getKey()}, value = ${e.getValue()}")
list += e.getKey()
list += e.getValue()
}
assertEquals(6, list.size())
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
println("==== worked! $list")
}
*/
}