diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java index fc2ec113233..e3d9517582d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java @@ -181,7 +181,7 @@ public class CompileTimeConstantResolver { if (text.length() == 1) { return new CharValue(text.charAt(0)); } - return new ErrorValue("Too many characters in a character literal" + text); + return new ErrorValue("Too many characters in a character literal '" + text + "'"); } return escapedStringToCharValue(text); } diff --git a/stdlib/ktSrc/JavaIterablesSpecial.kt b/stdlib/ktSrc/JavaIterablesSpecial.kt new file mode 100644 index 00000000000..2e30749f216 --- /dev/null +++ b/stdlib/ktSrc/JavaIterablesSpecial.kt @@ -0,0 +1,85 @@ +package std.util +// Number of extension function for ava.lang.Iterable that shouldn't participate in auto generation + +import java.util.Collection +import java.util.List +import java.util.AbstractList + +/** + * Count the number of elements in collection. + * + * If base collection implements Collection interface method Collection#size() will be used. + * Otherwise, this method determines the count by iterating through the all items. + */ +fun java.lang.Iterable.count() : Int { + if (this is Collection) { + return this.size() + } + + var number : Int = 0 + for (elem in this) { + ++number + } + return number +} + +/** + * Get the first element in the collection. + * + * Will throw an exception if there are no elements + * TODO: Specify type of the exception + */ +inline fun java.lang.Iterable.first() : T { + if (this is AbstractList) { + return this.get(0) + } + + return this.iterator().sure().next() +} + +/** + * Get the last element in the collection. + * + * If base collection implements List interface, the combination of size() and get() + * methods will be used for getting last element. Otherwise, this method determines the + * last item by iterating through the all items. + * + * Will throw an exception if there are no elements. + * TODO: Specify type of the exception + */ +fun java.lang.Iterable.last() : T { + if (this is List) { + return this.get(this.size() - 1); + } + + val iterator = this.iterator().sure() + var last : T = iterator.next() + + while (iterator.hasNext()) { + last = iterator.next() + } + + return last; +} + +/** + * Checks if collection contains given item. + * + * Method checks equality of the objects with T.equals method. + * If collection implements java.util.AbstractCollection an overridden implementation of the contains + * method will be used. + */ +fun java.lang.Iterable.contains(item : T) : Boolean { + std.io.println("!!!!!") + if (this is java.util.AbstractCollection) { + return this.contains(item); + } + + for (var elem in this) { + if (elem.equals(item)) { + return true + } + } + + return false +} \ No newline at end of file diff --git a/testlib/src/Test.kt b/testlib/src/Test.kt index 7694865d2a1..d6e0a26e9c3 100644 --- a/testlib/src/Test.kt +++ b/testlib/src/Test.kt @@ -81,6 +81,14 @@ fun assert(actual: Boolean, message: String) { println("Answer: ${actual} for ${message}") } +fun assertTrue(actual: Boolean, message: String = "") { + return assertEquals(true, actual, message) +} + +fun assertFalse(actual: Boolean, message: String = "") { + return assertEquals(false, actual, message) +} + fun assertEquals(expected: Any, actual: Any?, message: String = "") { Assert.assertEquals(message, expected, actual) } diff --git a/testlib/test/CollectionTest.kt b/testlib/test/CollectionTest.kt index 5fc1a980b4a..3cfd2f7b6fe 100644 --- a/testlib/test/CollectionTest.kt +++ b/testlib/test/CollectionTest.kt @@ -9,6 +9,16 @@ import std.test.* import java.util.* class CollectionTest() : TestSupport() { + + class IterableWrapper(collection : java.lang.Iterable) : java.lang.Iterable { + private val collection = collection + + override fun iterator(): java.util.Iterator { + return collection.iterator().sure() + } + } + + val data = arrayList("foo", "bar") fun testAny() { @@ -177,4 +187,39 @@ class CollectionTest() : TestSupport() { } } } + + fun testSimpleCount() { + assertEquals(2, data.count()) + assertEquals(3, hashSet(12, 14, 15).count()) + assertEquals(0, ArrayList().count()) + } + + fun testLast() { + 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()) + } + + fun testLastException() { + fails { arrayList().last() } + fails { linkedList().last() } + fails { hashSet().last() } + } + + fun testContains() { + assertTrue(data.contains("foo")) + assertTrue(data.contains("bar")) + assertFalse(data.contains("some")) + + // TODO: Problems with generation +// assertTrue(IterableWrapper(data).contains("bar")) +// assertFalse(IterableWrapper(data).contains("some")) + + assertFalse(hashSet().contains(12)) + assertTrue(linkedList(15, 19, 20).contains(15)) + +// assertTrue(IterableWrapper(hashSet(45, 14, 13)).contains(14)) +// assertFalse(IterableWrapper(linkedList()).contains(15)) + } } \ No newline at end of file