#KT-1772 Fixed

This commit is contained in:
James Strachan
2012-04-12 14:29:44 +01:00
parent c3f1e38c67
commit f9b00ad3f9
2 changed files with 61 additions and 6 deletions
+11
View File
@@ -64,3 +64,14 @@ public inline fun <K,V> java.util.Map<K,V>.getOrPut(key: K, defaultValue: ()-> V
return answer return answer
} }
} }
/**
* Returns an [[Iterator]] over the entries in the [[Map]]
*
* @includeFunctionBody ../../test/MapTest.kt iterate
*/
public inline fun <K,V> java.util.Map<K,V>.iterator(): java.util.Iterator<java.util.Map.Entry<K,V>> {
val entrySet = this.entrySet()!!
return entrySet.iterator()!!
}
+50 -6
View File
@@ -3,12 +3,11 @@ package test.collections
import kotlin.test.* import kotlin.test.*
import java.util.* import java.util.*
import junit.framework.TestCase import org.junit.Test as test
import org.junit.Test
class MapTest { class MapTest {
Test fun getOrElse() { test fun getOrElse() {
val data = HashMap<String, Int>() val data = HashMap<String, Int>()
val a = data.getOrElse("foo"){2} val a = data.getOrElse("foo"){2}
assertEquals(2, a) assertEquals(2, a)
@@ -18,7 +17,7 @@ class MapTest {
assertEquals(0, data.size()) assertEquals(0, data.size())
} }
Test fun getOrPut() { test fun getOrPut() {
val data = HashMap<String, Int>() val data = HashMap<String, Int>()
val a = data.getOrPut("foo"){2} val a = data.getOrPut("foo"){2}
assertEquals(2, a) assertEquals(2, a)
@@ -29,13 +28,13 @@ class MapTest {
assertEquals(1, data.size()) assertEquals(1, data.size())
} }
Test fun sizeAndEmpty() { test fun sizeAndEmpty() {
val data = HashMap<String, Int>() val data = HashMap<String, Int>()
assertTrue{ data.empty } assertTrue{ data.empty }
assertEquals(data.size, 0) assertEquals(data.size, 0)
} }
Test fun setViaIndexOperators() { test fun setViaIndexOperators() {
val map = HashMap<String, String>() val map = HashMap<String, String>()
assertTrue{ map.empty } assertTrue{ map.empty }
assertEquals(map.size, 0) assertEquals(map.size, 0)
@@ -46,4 +45,49 @@ class MapTest {
assertEquals(map.size(), 1) assertEquals(map.size(), 1)
assertEquals("James", map["name"]) assertEquals("James", map["name"])
} }
test fun iterate() {
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.add(e.getKey())
list.add(e.getValue())
/*
// TODO compiler bug
list += e.getKey()
list += e.getValue()
*/
}
assertEquals(6, list.size())
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
}
/*
TODO compiler bug
test fun iteratorWithProperties() {
val map = TreeMap<String, String>()
map["beverage"] = "beer"
map["location"] = "Mells"
map["name"] = "James"
var list = arrayList<String>()
for (e in map) {
println("got $e")
println("key = ${e.key}, value = ${e.value}")
list += e.key
list += e.value
}
assertEquals(6, list.size())
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
}
*/
} }