more tests for for-

This commit is contained in:
Alex Tkachman
2011-09-03 17:09:49 +02:00
parent c20a39dbd8
commit 07b2ab347d
3 changed files with 70 additions and 16 deletions
@@ -1,25 +1,70 @@
fun box() : String {
var sum : Int = 0
var i = 0
val c6 = MyCollection4()
sum = 0
for (el in c6) {
System.out?.println(el)
sum = sum + el
}
if(sum != 15) return "c6 failed"
val c5 = MyCollection3()
sum = 0
for (el in c5) {
System.out?.println(el)
sum = sum + (el ?: 0)
}
if(sum != 15) return "c5 failed"
val c1: java.lang.Iterable<Int> = MyCollection1()
for (el in c1) {}
sum = 0
for (el in c1) {
sum = sum + el
}
if(sum != 15) return "c1 failed"
val c2 = MyCollection1()
for (el in c2) {}
sum = 0
for (el in c2) {
sum = sum + el
}
if(sum != 15) return "c2 failed"
val c3: Iterable<Int> = MyCollection2()
for (el in c3) {}
sum = 0
for (el in c3) {
sum = sum + el
}
if(sum != 15) return "c3 failed"
val c4 = MyCollection2()
for (el in c4) {}
sum = 0
for (el in c4) {
sum = sum + el
}
if(sum != 15) return "c4 failed"
val a : Array<Int> = Array<Int> (5)
for(el in 0..4) {
a[i] = i++
}
sum = 0
for (el in a) {
sum = sum + el
}
if(sum != 10) return "a failed"
val b : Array<Int?> = Array<Int?> (5)
i = 0
while(i < 5) {
b[i] = i++
}
sum = 0
for (el in b) {
sum = sum + (el ?: 0)
}
System.out?.println(sum)
if(sum != 10) return "b failed"
return "OK"
}
@@ -28,8 +73,10 @@ class MyCollection1(): java.lang.Iterable<Int> {
fun iterator(): java.util.Iterator<Int> = MyIterator()
class MyIterator(): java.util.Iterator<Int> {
fun next() = 0
fun hasNext() = false
var k : Int = 5
fun next() : Int = k--
fun hasNext() = k > 0
}
}
@@ -37,8 +84,10 @@ class MyCollection2(): Iterable<Int> {
fun iterator(): Iterator<Int> = MyIterator()
class MyIterator(): Iterator<Int> {
fun next() = 0
fun hasNext() = false
var k : Int = 5
fun next() : Int = k--
fun hasNext() = k > 0
}
}