add test on iterators (see #394)

This commit is contained in:
Renaud Pawlak 2017-10-29 07:33:03 +01:00
parent c5975a1803
commit d72053ab25
2 changed files with 59 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import source.nativestructures.ExtendsJDKAnonymous;
import source.nativestructures.ExtendsJDKInterface;
import source.nativestructures.ExtendsJDKRegular;
import source.nativestructures.Input;
import source.nativestructures.Iterators;
import source.nativestructures.Maps;
import source.nativestructures.NativeArrays;
import source.nativestructures.NativeStringBuilder;
@ -198,5 +199,11 @@ public class NativeStructuresTests extends AbstractTest {
}, getSourceFile(ExtendsJDKAnonymous.class));
}
@Test
public void testIterators() {
eval(ModuleKind.none, (logHandler, result) -> {
logHandler.assertNoProblems();
}, getSourceFile(Iterators.class));
}
}

View File

@ -0,0 +1,52 @@
package source.nativestructures;
import java.util.Iterator;
public class Iterators {
public static void main(String[] args) {
Range r = new Range(10, 20);
Iterator<Integer> it = r.iterator();
int j = 10;
while (it.hasNext()) {
int i = it.next();
assert i == j++;
}
assert j == 20;
}
}
class Range implements Iterable<Integer> {
private int start;
private int end;
private int at;
public Range(int start, int end) {
this.start = start;
this.end = end;
at = start;
}
public Iterator<Integer> iterator() {
return new RangeIterator();
}
private class RangeIterator implements Iterator<Integer> {
public boolean hasNext() {
return at < end;
}
public Integer next() {
int next = at;
at += 1;
return next;
}
public void remove() {
}
}
}