From 4f2866ff5b24863a87d00d1059b47945bc959ad2 Mon Sep 17 00:00:00 2001 From: "Mark S. Petrovic" Date: Sat, 17 Mar 2012 19:00:06 -0700 Subject: [PATCH] Add sortedSet and unit test --- libraries/stdlib/src/JavaUtil.kt | 3 +++ libraries/stdlib/test/CollectionTest.kt | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libraries/stdlib/src/JavaUtil.kt b/libraries/stdlib/src/JavaUtil.kt index b683b334b93..1195328195a 100644 --- a/libraries/stdlib/src/JavaUtil.kt +++ b/libraries/stdlib/src/JavaUtil.kt @@ -20,6 +20,9 @@ inline fun linkedList(vararg values: T) : LinkedList = values.to(LinkedLi /** Returns a new HashSet with a variable number of initial elements */ inline fun hashSet(vararg values: T) : HashSet = values.to(HashSet(values.size)) +/** Returns a new SortedSet with a variable number of initial elements */ +inline fun sortedSet(vararg values: T) : TreeSet = values.to(TreeSet()) + inline fun hashMap(): HashMap = HashMap() inline fun sortedMap(): SortedMap = TreeMap() diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index 5b8eec239eb..d4a48566a14 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -81,7 +81,7 @@ class CollectionTest() : TestCase() { } } - fun testFilterIntoSortedSet() { + fun testFilterIntoSet() { // TODO would be nice to avoid the val foo = data.filterTo(hashSet()){it.startsWith("f")} @@ -96,6 +96,16 @@ class CollectionTest() : TestCase() { } } + fun testFilterIntoSortedSet() { + // TODO would be nice to avoid the + val sorted = data.filterTo(sortedSet()){it.length == 3} + assertEquals(2, sorted.size) + assertEquals(sortedSet("bar", "foo"), sorted) + assertTrue { + sorted is TreeSet + } + } + fun testFind() { val x = data.find{it.startsWith("x")} assertNull(x)