package fr.umlv.conc;

import static org.junit.Assert.*;

import java.util.IntSummaryStatistics;

import org.junit.Test;

public class LazyStatTest {
  @Test
  public void simpleGetStat() throws InterruptedException {
    LazyStat lazyStat = new LazyStat(1, 2, 3);
    Thread thread = new Thread(() -> {
      assertEquals(1, lazyStat.getStat().getMin());
      assertEquals(3, lazyStat.getStat().getMax());
    });
    thread.start();
    assertEquals(1, lazyStat.getStat().getMin());
    assertEquals(3, lazyStat.getStat().getMax());
    thread.join();
  }

  @Test
  public void sameObjectStat() throws InterruptedException {
    int[] array = new int[10_000_000];
    LazyStat lazyStat = new LazyStat(array);
    class Result {
      private IntSummaryStatistics stat;
    }
    Result result = new Result();
    Thread thread = new Thread(() -> {
      result.stat = lazyStat.getStat();
    });
    thread.start();
    IntSummaryStatistics stat2 = lazyStat.getStat();
    thread.join();
    assertTrue(result.stat == stat2);
  }
}
