<commit_before>package io.trane.future;

import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.Test;

public class FutureTest {

  private <T> T get(Future<T> future) throws CheckedFutureException {
    return future.get(1, TimeUnit.SECONDS);
  }

  private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  private final Exception ex = new TestException();

  @After
  public void shutdownScheduler() {
    scheduler.shutdown();
  }

  /*** never ***/

  @Test
  public void never() {
    assertTrue(Future.never() instanceof NoFuture);
  }

  /*** apply ***/

  @Test
  public void applyValue() throws CheckedFutureException {
    Integer value = 1;
    Future<Integer> future = Future.apply(() -> value);
    assertEquals(value, get(future));
  }

  @Test(expected = ArithmeticException.class)
  public void applyException() throws CheckedFutureException {
    Future<Integer> future = Future.apply(() -> 1 / 0);
    get(future);
  }

  /*** value ***/

  @Test
  public void value() throws CheckedFutureException {
    Integer value = 1;
    Future<Integer> future = Future.value(value);
    assertEquals(value, get(future));
  }

  /*** exception ***/

  @Test(expected = TestException.class)
  public void exception() throws CheckedFutureException {

    Future<Integer> future = Future.exception(ex);
    get(future);
  }

  /*** flatten ***/

  @Test
  public void flatten() throws CheckedFutureException {
    Future<Future<Integer>> future = Future.value(Future.value(1));
    assertEquals(get(Future.flatten(future)), get(future.flatMap(f -> f)));
  }

  /*** tailrec ***/

  Future<Integer> tailrecLoop(Future<Integer> f) {
    return Tailrec.apply(() -> {
      return f.flatMap(i -> {
        if (i == 0)
          return Future.value(0);
        else
          return tailrecLoop(Future.value(i - 1));
      });
    });
  }

  @Test
  public void tailrec() throws CheckedFutureException {
    assertEquals(new Integer(0), get(tailrecLoop(Future.value(20000))));
  }

  Future<Integer> tailrecLoopDelayed(Future<Integer> f) {
    return Tailrec.apply(() -> {
      return f.flatMap(i -> {
        if (i == 0)
          return Future.value(0);
        else
          return tailrecLoopDelayed(Future.value(i - 1).delayed(1, TimeUnit.NANOSECONDS, scheduler));
      });
    });
  }

  @Test
  public void tailrecDelayed() throws CheckedFutureException {
    assertEquals(new Integer(0), get(tailrecLoopDelayed(Future.value(20000))));
  }

  Future<Integer> nonTailrecLoop(Future<Integer> f) {
    return f.flatMap(i -> {
      if (i == 0)
        return Future.value(0);
      else
        return nonTailrecLoop(Future.value(i - 1));
    });
  }

  @Test(expected = StackOverflowError.class)
  public void nonTailrec() throws CheckedFutureException {<commit_msg>fix flaky test<commit_after>package io.trane.future;

import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.Test;

public class FutureTest {

  private <T> T get(Future<T> future) throws CheckedFutureException {
    return future.get(1, TimeUnit.SECONDS);
  }

  private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  private final Exception ex = new TestException();

  @After
  public void shutdownScheduler() {
    scheduler.shutdown();
  }

  /*** never ***/

  @Test
  public void never() {
    assertTrue(Future.never() instanceof NoFuture);
  }

  /*** apply ***/

  @Test
  public void applyValue() throws CheckedFutureException {
    Integer value = 1;
    Future<Integer> future = Future.apply(() -> value);
    assertEquals(value, get(future));
  }

  @Test(expected = ArithmeticException.class)
  public void applyException() throws CheckedFutureException {
    Future<Integer> future = Future.apply(() -> 1 / 0);
    get(future);
  }

  /*** value ***/

  @Test
  public void value() throws CheckedFutureException {
    Integer value = 1;
    Future<Integer> future = Future.value(value);
    assertEquals(value, get(future));
  }

  /*** exception ***/

  @Test(expected = TestException.class)
  public void exception() throws CheckedFutureException {

    Future<Integer> future = Future.exception(ex);
    get(future);
  }

  /*** flatten ***/

  @Test
  public void flatten() throws CheckedFutureException {
    Future<Future<Integer>> future = Future.value(Future.value(1));
    assertEquals(get(Future.flatten(future)), get(future.flatMap(f -> f)));
  }

  /*** tailrec ***/

  Future<Integer> tailrecLoop(Future<Integer> f) {
    return Tailrec.apply(() -> {
      return f.flatMap(i -> {
        if (i == 0)
          return Future.value(0);
        else
          return tailrecLoop(Future.value(i - 1));
      });
    });
  }

  @Test
  public void tailrec() throws CheckedFutureException {
    assertEquals(new Integer(0), get(tailrecLoop(Future.value(20000))));
  }

  Future<Integer> tailrecLoopDelayed(Future<Integer> f) {
    return Tailrec.apply(() -> {
      return f.flatMap(i -> {
        if (i == 0)
          return Future.value(0);
        else
          return tailrecLoopDelayed(Future.value(i - 1).delayed(1, TimeUnit.NANOSECONDS, scheduler));
      });
    });
  }

  @Test
  public void tailrecDelayed() throws CheckedFutureException {
    assertEquals(new Integer(0), tailrecLoopDelayed(Future.value(20000)).get(10, TimeUnit.SECONDS));
  }

  Future<Integer> nonTailrecLoop(Future<Integer> f) {
    return f.flatMap(i -> {
      if (i == 0)
        return Future.value(0);
      else
        return nonTailrecLoop(Future.value(i - 1));
    });
  }

  @Test(expected = StackOverflowError.class)
  public void nonTailrec() throws CheckedFutureException {
{'fraction_non_alphanumeric': 0.1299123551031948, 'fraction_numerical': 0.008199038733389879, 'mean_word_length': 3.704122340425532, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2557450', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2119, 'n_words': 457}