package jetbrains.mps.internalCollections.test.closures;
/*Generated by MPS */
import junit.framework.Assert;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.ILeftCombinator;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import java.util.Collections;
import jetbrains.mps.internal.collections.runtime.IRightCombinator;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.internal.collections.runtime.ISelector;
public class ReduceFold_Test extends Util_Test {
public void test_reduceLeft() throws Exception {
Iterable<Integer> in = this.input10();
Assert.assertSame(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, Sequence.fromIterable(in).reduceLeft(new ILeftCombinator<Integer, Integer>() {
public Integer combine(Integer a, Integer b) {
return a + b;
}
}));
_FunctionTypes._return_P2_E0<? extends Integer, ? super Integer, ? super Integer> cl = new _FunctionTypes._return_P2_E0<Integer, Integer, Integer>() {
public Integer invoke(Integer a, Integer b) {
return a + b;
}
};
Assert.assertSame(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, Sequence.fromIterable(in).reduceLeft(cl));
Assert.assertSame(1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10, Sequence.fromIterable(in).reduceLeft(new ILeftCombinator<Integer, Integer>() {
public Integer combine(Integer a, Integer b) {
return a - b;
}
}));
Iterable<Integer> single = Sequence.<Integer>singleton(42);
Assert.assertSame(42, Sequence.fromIterable(single).reduceLeft(new ILeftCombinator<Integer, Integer>() {
public Integer combine(Integer a, Integer b) {
return a + b;
}
}));
Iterable<Integer> empty = Sequence.fromIterable(Collections.<Integer>emptyList());
Assert.assertNull(Sequence.fromIterable(empty).reduceLeft(new ILeftCombinator<Integer, Integer>() {
public Integer combine(Integer a, Integer b) {
int i = 0;
if (i == 0) {
throw new RuntimeException();
}
return i;
}
}));
}
public void test_reduceRight() throws Exception {
Iterable<Integer> in = this.input10();
Assert.assertSame(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, Sequence.fromIterable(in).reduceRight(new IRightCombinator<Integer, Integer>() {
public Integer combine(Integer a, Integer b) {
return a + b;
}
}));
_FunctionTypes._return_P2_E0<? extends Integer, ? super Integer, ? super Integer> cl = new _FunctionTypes._return_P2_E0<Integer, Integer, Integer>() {
public Integer invoke(Integer a, Integer b) {
return a + b;
}
};
Assert.assertSame(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10, Sequence.fromIterable(in).reduceRight(cl));
Assert.assertSame(1 - (2 - (3 - (4 - (5 - (6 - (7 - (8 - (9 - 10)))))))), Sequence.fromIterable(in).reduceRight(new IRightCombinator<Integer, Integer>() {
public Integer combine(Integer a, Integer b) {
return a - b;
}
}));
Iterable<Integer> single = Sequence.<Integer>singleton(42);
Assert.assertSame(42, Sequence.fromIterable(single).reduceRight(new IRightCombinator<Integer, Integer>() {
public Integer combine(Integer b, Integer a) {
return b + a;
}
}));
Iterable<Integer> empty = Sequence.fromIterable(Collections.<Integer>emptyList());
Assert.assertNull(Sequence.fromIterable(empty).reduceRight(new IRightCombinator<Integer, Integer>() {
public Integer combine(Integer b, Integer a) {
int i = 0;
if (i == 0) {
throw new RuntimeException();
}
return i;
}
}));
}
public void test_foldLeft() throws Exception {
Iterable<Integer> in = this.input10();
Assert.assertEquals("12345678910", Sequence.fromIterable(in).foldLeft("", new ILeftCombinator<Integer, String>() {
public String combine(String s, Integer it) {
return "" + s + it;
}
}));
_FunctionTypes._return_P2_E0<? extends String, ? super String, ? super Integer> cl = new _FunctionTypes._return_P2_E0<String, String, Integer>() {
public String invoke(String s, Integer it) {
return "" + s + it;
}
};
Assert.assertEquals("12345678910", Sequence.fromIterable(in).foldLeft("", cl));
Iterable<Integer> single = Sequence.<Integer>singleton(42);
Assert.assertEquals("bar42", Sequence.fromIterable(single).foldLeft("bar", new ILeftCombinator<Integer, String>() {
public String combine(String s, Integer it) {
return "" + s + it;
}
}));
Iterable<Integer> empty = Sequence.fromIterable(Collections.<Integer>emptyList());
Assert.assertEquals("ack", Sequence.fromIterable(empty).foldLeft("ack", new ILeftCombinator<Integer, String>() {
public String combine(String s, Integer it) {
int i = 0;
if (i == 0) {
throw new RuntimeException();
}
return "foo";
}
}));
}
public void test_foldRight() throws Exception {
Iterable<Integer> in = this.input10();
Assert.assertEquals("10987654321", Sequence.fromIterable(in).foldRight("", new IRightCombinator<Integer, String>() {
public String combine(Integer it, String s) {
return "" + s + it;
}
}));
_FunctionTypes._return_P2_E0<? extends String, ? super Integer, ? super String> cl = new _FunctionTypes._return_P2_E0<String, Integer, String>() {
public String invoke(Integer it, String s) {
return "" + s + it;
}
};
Assert.assertEquals("10987654321", Sequence.fromIterable(in).foldRight("", cl));
Iterable<Integer> single = Sequence.<Integer>singleton(42);
Assert.assertEquals("bar42", Sequence.fromIterable(single).foldRight("bar", new IRightCombinator<Integer, String>() {
public String combine(Integer it, String s) {
return s + it;
}
}));
Iterable<Integer> empty = Sequence.fromIterable(Collections.<Integer>emptyList());
Assert.assertEquals("ack", Sequence.fromIterable(empty).foldRight("ack", new IRightCombinator<Integer, String>() {
public String combine(Integer it, String s) {
int i = 0;
if (i == 0) {
throw new RuntimeException();
}
return "foo";
}
}));
}
public void test_mps10786() throws Exception {
List<IntHolder> input = ListSequence.fromListAndArray(new ArrayList<IntHolder>(), new IntHolder(3), new IntHolder(5));
int res = ListSequence.fromList(input).select(new ISelector<IntHolder, Integer>() {
public Integer select(IntHolder it) {
return it.getInt();
}
}).reduceLeft(new ILeftCombinator<Integer, Integer>() {
public Integer combine(Integer a, Integer b) {
return a + b;
}
}) + 1;
Assert.assertSame(9, res);
}
public ReduceFold_Test() {
}
}