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
+5 -1
View File
@@ -30,6 +30,11 @@ public class UnsupportedOperationException(message: String = "") : Exception() {
library
public class NumberFormatException(message: String = "") : Exception() {}
library
public trait Runnable {
open fun run() : Unit;
}
public trait Comparable<T> {
fun compareTo(that: T): Int
}
@@ -39,4 +44,3 @@ public trait Appendable {
open fun append(csq: CharSequence?, start: Int, end: Int): Appendable?
open fun append(c: Char): Appendable?
}
+5
View File
@@ -244,3 +244,8 @@ public class StringBuilder() : Appendable {
library
public class NoSuchElementException() : Exception() {}
public trait Enumeration<E> {
open fun hasMoreElements(): Boolean
open fun nextElement(): E?
}
+21 -2
View File
@@ -1,7 +1,26 @@
package kotlin
import java.util.Map as JMap
import java.util.Map
/** Provides [] access to maps */
public fun <K, V> JMap<K, V>.set(key : K, value : V): Unit {
public fun <K, V> Map<K, V>.set(key : K, value : V): Unit {
this.put(key, value)
}
/**
* Returns a new [[HashMap]] populated with the given tuple values where the first value in each tuple
* is the key and the second value is the value
*
* @includeFunctionBody ../../test/MapTest.kt createUsingTuples
*/
public inline fun <K,V> hashMap(vararg values: #(K,V)): java.util.HashMap<K,V> {
val answer = java.util.HashMap<K,V>()
/**
TODO replace by this simpler call when we can pass vararg values into other methods
answer.putAll(values)
*/
for (v in values) {
answer.put(v._1, v._2)
}
return answer
}
@@ -24,8 +24,9 @@ public class StdLibTestToJSTest extends StdLibTestSupport {
public void testGenerateTestCase() throws Exception {
generateJavaScriptFiles(EcmaVersion.all(),
"libraries/stdlib/test",
//"dom/DomTest.kt",
"js/MapTest.kt",
"ListTest.kt",
"StringTest.kt",
"js/SimpleTest.kt");
"StringTest.kt");
}
}
@@ -93,6 +93,7 @@ public abstract class Config {
@NotNull
public static final List<String> LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList(
"/stdlib/jutil.kt",
"/stdlib/JUMaps.kt",
"/stdlib/test.kt",
"/core/stringDefs.kt",
"/core/strings.kt"
@@ -114,8 +115,9 @@ public abstract class Config {
"/kotlin/JLangIterablesLazy.kt",
"/kotlin/JLangIterablesSpecial.kt",
"/kotlin/support/AbstractIterator.kt",
//"/kotlin/Ordering.kt",
"/kotlin/Standard.kt",
"/kotlin/Strings.kt",
"/kotlin/dom/Dom.kt",
"/kotlin/test/Test.kt"
);
+6
View File
@@ -220,6 +220,12 @@
}
});
Kotlin.Runnable = Kotlin.$createClass({
run:function () {
throw Kotlin.$new(Kotlin.AbstractFunctionInvocationError)();
}
});
Kotlin.ArrayIterator = Kotlin.$createClass(Kotlin.Iterator, {
initialize: function (array) {
this.array = array;
+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")
}
*/
}
@@ -77,6 +77,7 @@
<!--
<include name="Ordering.kt"/>
-->
<include name="Standard.kt"/>
<include name="Strings.kt"/>
</fileset>
</copy>