package jetbrains.mps.internalCollections.test.closures;
/*Generated by MPS */
import jetbrains.mps.internal.collections.runtime.ISequence;
import jetbrains.mps.internal.collections.runtime.Sequence;
import java.util.ArrayList;
import jetbrains.mps.internal.collections.runtime.IVisitor;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import jetbrains.mps.internal.collections.runtime.StopIteratingException;
import java.util.Arrays;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import junit.framework.Assert;
public class VisitAll_Test extends Util_Test {
public void test_visitAllMethod() throws Exception {
ISequence<Integer> seq = Sequence.fromIterable(this.input5());
final ArrayList<Integer> res = new ArrayList<Integer>();
seq.visitAll(new IVisitor<Integer>() {
public void visit(Integer i) {
res.add(i * 2);
}
});
this.assertIterableEquals(this.expectEven10(), res);
}
public void test_visitorVar() throws Exception {
ISequence<Integer> seq = Sequence.fromIterable(this.input5());
final ArrayList<Integer> res = new ArrayList<Integer>();
IVisitor<Integer> visitor = new IVisitor<Integer>() {
public void visit(Integer i) {
res.add(i * 2);
}
};
seq.visitAll(visitor);
this.assertIterableEquals(this.expectEven10(), res);
}
public void test_visitOperation() throws Exception {
final ArrayList<Integer> res = new ArrayList<Integer>();
Sequence.fromIterable(this.input5()).visitAll(new IVisitor<Integer>() {
public void visit(Integer it) {
res.add(it * 2);
}
});
this.assertIterableEquals(this.expectEven10(), res);
}
public void test_visitorVarOperation() throws Exception {
final ArrayList<Integer> res = new ArrayList<Integer>();
_FunctionTypes._void_P1_E0<? super Integer> cl = new _FunctionTypes._void_P1_E0<Integer>() {
public void invoke(Integer it) {
res.add(it * 2);
}
};
Sequence.fromIterable(this.input5()).visitAll(cl);
this.assertIterableEquals(this.expectEven10(), res);
}
public void test_legacyForEach() throws Exception {
final ArrayList<Integer> res = new ArrayList<Integer>();
Sequence.fromIterable(this.input5()).visitAll(new IVisitor<Integer>() {
public void visit(Integer it) {
res.add(it * 2);
}
});
this.assertIterableEquals(this.expectEven10(), res);
}
public void test_skipStop() throws Exception {
final ArrayList<Integer> res = new ArrayList<Integer>();
Sequence.fromIterable(this.input10()).visitAll(new IVisitor<Integer>() {
public void visit(Integer it) {
__skip__:
do {
if (it % 2 == 1) {
break __skip__;
}
res.add(it * 2);
if (it > 5) {
throw new StopIteratingException();
}
} while (false);
}
});
this.assertIterableEquals(Arrays.asList(4, 8, 12), res);
res.clear();
Sequence.fromIterable(this.input10()).visitAll(new IVisitor<Integer>() {
public void visit(Integer it) {
__skip__:
do {
if (it % 2 == 1) {
break __skip__;
}
res.add(it * 2);
if (it > 5) {
throw new StopIteratingException();
}
} while (false);
}
});
this.assertIterableEquals(Arrays.asList(4, 8, 12), res);
}
public void test_parameterizedAdaptation() throws Exception {
List<String> ls = ListSequence.fromListAndArray(new ArrayList<String>(), "A", "BB", "CCC", "DDDD");
final Wrappers._int len = new Wrappers._int(0);
this.forEach(ls, new IVisitor<String>() {
public void visit(String s) {
len.value += s.length();
}
});
Assert.assertEquals(10, len.value);
}
public void test_typing() throws Exception {
List<Integer> test = ListSequence.fromListAndArray(new ArrayList<Integer>(), 2, 4, 6);
final List<Integer> out = ListSequence.fromList(new ArrayList<Integer>());
ListSequence.fromList(test).visitAll(new IVisitor<Integer>() {
public void visit(Integer it) {
ListSequence.fromList(out).addElement(it - 1);
}
});
this.assertIterableEquals(this.expectOdd5(), out);
}
public void forEach(Iterable<String> seqs, IVisitor<String> visitor) {
Sequence.fromIterable(seqs).visitAll(visitor);
}
}