mirror of
https://github.com/cincheo/jsweet.git
synced 2026-02-07 14:06:35 +00:00
add test on iterators (see #394)
This commit is contained in:
parent
c5975a1803
commit
d72053ab25
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@ -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() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user