package jetbrains.mps.internalCollections.test.closures; /*Generated by MPS */ import java.util.ArrayList; import jetbrains.mps.internal.collections.runtime.Sequence; import junit.framework.Assert; import java.util.List; import jetbrains.mps.internal.collections.runtime.ListSequence; import jetbrains.mps.internal.collections.runtime.ArrayUtils; import jetbrains.mps.internal.collections.runtime.ILeftCombinator; public class ForEach_Test extends Util_Test { public void test_legacyForeach() throws Exception { ArrayList<Integer> res = new ArrayList<Integer>(); for (Integer foo : Sequence.fromIterable(this.input5())) { res.add(foo * 2); } this.assertIterableEquals(this.expectEven10(), res); } public void test_iterateArray() throws Exception { int[] arr = new int[]{1, 2, 3, 4, 5}; Iterable<Integer> exp = this.input5(); for (int i : arr) { Assert.assertTrue(Sequence.fromIterable(exp).contains(i)); } String[] arr2 = new String[]{"A", "B", "C"}; Iterable<String> exp2 = this.inputABC(); for (String s : arr2) { Assert.assertTrue(Sequence.fromIterable(exp2).contains(s)); } } public void test_noWrapperForGNE() throws Exception { Iterable<Integer> exp = this.input5(); for (int i : new int[]{1, 2, 3, 4, 5}) { Assert.assertTrue(Sequence.fromIterable(exp).contains(i)); this.accept(i); } } public void test_noWrapperForArray() throws Exception { Iterable<Integer> exp = this.input5(); int[] arr = new int[]{1, 2, 3, 4, 5}; for (int i : arr) { Assert.assertTrue(Sequence.fromIterable(exp).contains(i)); this.accept(i); } } public void test_mps14282() throws Exception { List<int[]> lints = ListSequence.fromListAndArray(new ArrayList<int[]>(), new int[]{1001}, new int[]{1001, 1002}, new int[]{1001, 1002, 1003}); int sum = 0; for (int[] ints : lints) { sum += Sequence.fromIterable(ArrayUtils.fromIntegerArray(ints)).reduceLeft(new ILeftCombinator<Integer, Integer>() { public Integer combine(Integer a, Integer b) { return a + b; } }); } Assert.assertEquals(6010, sum); } public void test_mps10737() throws Exception { // testing compilation only IFoo foo = new IFoo() { @Override public Iterable<? extends IBar> get() { return null; } }; for (IBar bars : Sequence.fromIterable(foo.get())) { } for (IBar.Bar bars : Sequence.fromIterable(new IFoo.Foo().get())) { } } private void accept(int valid) { } private void accept(Object invalid) { Assert.assertTrue(false); } }