package jetbrains.mps.internalCollections.test.closures;

/*Generated by MPS */

import org.junit.jupiter.api.Test;
import jetbrains.mps.internal.collections.runtime.ISequence;
import jetbrains.mps.internal.collections.runtime.Sequence;
import java.util.Arrays;
import jetbrains.mps.internal.collections.runtime.ISelector;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.baseLanguage.closures.runtime.YieldingIterator;
import org.junit.Assert;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import jetbrains.mps.internal.collections.runtime.IterableUtils;

public class SelectTest_Test extends Util_Test {
  @Test
  public void test_selectMethod() throws Exception {
    ISequence<Integer> seq = Sequence.fromIterable(this.input5());
    ISequence<Integer> test = seq.select((it) -> (Integer) ((it % 2 == 1 ? it * 2 : it * 3)));
    this.assertIterableEquals(Arrays.asList(2, 6, 6, 12, 10), test.toIterable());
  }
  @Test
  public void test_selectMethod2() throws Exception {
    int a = Character.valueOf('a').charValue();
    Iterable<Integer> input = Arrays.asList(a, a + 1, a + 2, a + 3, a + 4);
    ISequence<Integer> seq = Sequence.fromIterable(input);
    ISequence<String> test = seq.select((it) -> Character.valueOf((char) it.intValue()).toString());
    this.assertIterableEquals(Arrays.asList("a", "b", "c", "d", "e"), test.toIterable());
  }
  @Test
  public void test_selectorVar() throws Exception {
    ISequence<Integer> seq = Sequence.fromIterable(this.input5());
    ISelector<Integer, Integer> selector = new ISelector<Integer, Integer>() {
      public Integer select(Integer it) {
        return (it % 2 == 1 ? it * 2 : it * 3);
      }
    };
    ISequence<Integer> test = seq.select(selector);
    this.assertIterableEquals(Arrays.asList(2, 6, 6, 12, 10), test.toIterable());
  }
  @Test
  public void test_selectOperation() throws Exception {
    this.assertIterableEquals(this.expectEven10(), Sequence.fromIterable(this.input5()).select((it) -> it * 2));
    _FunctionTypes._return_P1_E0<? extends Integer, ? super Integer> cl = (Integer it) -> it * 2;
    this.assertIterableEquals(this.expectEven10(), Sequence.fromIterable(this.input5()).select(cl));
  }
  @Test
  public void test_legacySelector() throws Exception {
    Iterable<Integer> test = Sequence.fromIterable(this.input5()).select((it) -> it * 2);
    this.assertIterableEquals(this.expectEven10(), test);
  }
  @Test
  public void test_selectMany() throws Exception {
    final List<Integer> list = ListSequence.fromListAndArray(new ArrayList<Integer>(), 1, 2, 3);
    Iterable<Integer> test = Sequence.fromClosure(() -> {
      return (Iterable<Integer>) () -> {
        return new YieldingIterator<Integer>() {
          private int __CP__ = 0;
          protected boolean moveToNext() {
__loop__:
            do {
__switch__:
              switch (this.__CP__) {
                case -1:
                  assert false : "Internal error";
                  return false;
                case 2:
                  this.__CP__ = 1;
                  this.yield(0);
                  return true;
                case 0:
                  this.__CP__ = 2;
                  break;
                default:
                  break __loop__;
              }
            } while (true);
            return false;
          }
        };
      };
    });
    Sequence.fromIterable(test).select((it) -> list);
    Iterable<List<Integer>> result = Sequence.fromIterable(test).select((it) -> list);
    Assert.assertEquals(list, Sequence.fromIterable(result).first());
  }
  @Test
  public void test_nextWithoutHasNext() throws Exception {
    this.assertIteratorYields(ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<Integer>(), 1, 2, 3)).select((i) -> i * 2).iterator(), 2, 4, 6);
  }
  @Test
  public void test_selectAdvancesTooEarly() throws Exception {
    final List<Integer> test = ListSequence.fromListAndArray(new ArrayList<Integer>(), 1, 2, 3, 4, 5, 6, 7, 8, 9);
    final List<Integer> plusten = ListSequence.fromList(new ArrayList<Integer>());
    final Wrappers._int idx = new Wrappers._int(0);
    ListSequence.fromList(test).select((i) -> i).visitAll((i) -> {
      if (++idx.value < ListSequence.fromList(test).count()) {
        ListSequence.fromList(test).setElement(idx.value, ListSequence.fromList(test).getElement(idx.value) - idx.value);
      }
      ListSequence.fromList(plusten).addElement(i + 10);
    });
    Assert.assertEquals(ListSequence.fromListAndArray(new ArrayList<Integer>(), 11, 11, 11, 11, 11, 11, 11, 11, 11), plusten);
  }
  @Test
  public void test_ofType() throws Exception {
    List<Object> lo = ListSequence.fromListAndArray(new ArrayList<Object>(), 1, "foo", Boolean.TRUE, -1L, "bar", ListSequence.fromListAndArray(new ArrayList<String>(), "baz"));
    Iterable<String> seqs = ListSequence.fromList(lo).ofType(String.class);
    Assert.assertEquals("foo bar", IterableUtils.join(Sequence.fromIterable(seqs), " "));
    Iterable<Long> seql = ListSequence.fromList(lo).ofType(Long.class);
    Assert.assertTrue(Sequence.fromIterable(seql).count() == 1 && (long) Sequence.fromIterable(seql).first() == -1L);
    Assert.assertTrue(ListSequence.fromList(lo).ofType(Boolean.class).count() == 1);
    Assert.assertTrue(ListSequence.fromList(lo).ofType(Float.class).isEmpty());
  }
  @Test
  public void test_mps19134() throws Exception {
    // testing compilation
    ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<Integer>(), 111, 112, 113)).translate((it) -> {
      return ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<String>(), "foo", "bar")).select(new _FunctionTypes._return_P1_E0<String, String>() {
        public String invoke(String it) {
          return ((true ? "" : "")) + "";
        }
      });
    });
  }
}
