All Downloads are FREE. Search and download functionalities are using the official Maven repository.

generator.server.javatool.memoizers.test.MemoizersTest.mustache Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package {{packageName}}.shared.memoizer.domain;

import static org.assertj.core.api.Assertions.*;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
import {{packageName}}.UnitTest;

@UnitTest
class MemoizersTest {

  @Test
  void shouldGetFunctionResult() {
    Function memoizer = Memoizers.of(d -> d * d);

    assertThat(memoizer.apply(2D)).isEqualTo(4D);
  }

  @Test
  void shouldMemoizeFunctionResult() {
    AtomicInteger result = new AtomicInteger();

    Function memoizer = Memoizers.of(d -> result.incrementAndGet());

    assertThat(memoizer.apply(1)).isEqualTo(memoizer.apply(1));
    assertThat(memoizer.apply(1)).isNotEqualTo(memoizer.apply(2));
  }

  @Test
  void shouldMemoizeNullResult() {
    NullFactory factory = new NullFactory();
    Function memoizer = Memoizers.of(factory);

    memoizer.apply(1);
    memoizer.apply(1);

    assertThat(factory.callsCount()).isEqualTo(1);
    assertThat(memoizer.apply(1)).isNull();
  }

  @Test
  void shouldMemoizeSupplier() {
    Supplier supplier = Memoizers.of(() -> "Pouet");

    assertThat(supplier.get()).isEqualTo("Pouet");
  }

  private static final class NullFactory implements Function {

    private final AtomicInteger callsCount = new AtomicInteger();

    public int callsCount() {
      return callsCount.get();
    }

    @Override
    public String apply(Object input) {
      callsCount.incrementAndGet();

      return null;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy