package foo import java.util.ArrayList data class Holder(val v: T) data class Dat(val start: String, val end: String) class Obj(val start: String, val end: String) fun assertSomeNotEqual(c: Iterable) { val it = c.iterator() val first = it.next() while (it.hasNext()) { val item: T = it.next() if (item != first) { return; } } throw Exception("All elements are the same: $first") } fun assertAllEqual(c: Iterable) { val it = c.iterator() val first = it.next() while (it.hasNext()) { val item: T = it.next() assertEquals(first, item) } } val hashCoder: (o: Any) -> Int = { o -> o.hashCode() } val wrapInH = { (t: T) -> Holder(t) } fun box(): String { // Check that same Dat's have the same hashcode. val sameDs = listOf(Dat("a", "b"), Dat("a", "b")) assertAllEqual(map(sameDs, hashCoder)) // Check that different Dat's have different hashcodes (at least some of them). val differentDs = listOf(Dat("a", "b"), Dat("a", "c"), Dat("a", "d")) assertSomeNotEqual(map(differentDs, hashCoder)) // Check the same on Obj's, which should be always different and with different hashcodes. val sameOs = listOf(Obj("a", "b"), Obj("a", "b"), Obj("a", "b")) val differentOs = listOf(Obj("a", "b"), Obj("a", "b"), Obj("a", "b")) // Obj's are always different. assertSomeNotEqual(map(sameOs, hashCoder)) assertSomeNotEqual(map(differentOs, hashCoder)) // Both Dat's and Obj's wrapped as Holder should retain their hashcode relations. val sameHDs = map(sameDs, wrapInH) assertAllEqual(map(sameHDs, hashCoder)) val differentHDs = map(differentDs, wrapInH) assertSomeNotEqual(map(differentHDs, hashCoder)) val sameHOs = map(sameOs, wrapInH) assertSomeNotEqual(map(sameHOs, hashCoder)) val differentHOs = map(differentOs, wrapInH) assertSomeNotEqual(map(differentHOs, hashCoder)) return "OK" } fun listOf(vararg a: T): List { val list = ArrayList() for (e in a) { list.add(e) } return list } fun map(c : Iterable, transform : (T) -> S) : List { val list = ArrayList() for (t in c) { list.add(transform(t)) } return list }