|
| 1 | +package org.baeldung.guava; |
| 2 | + |
| 3 | +import com.google.common.base.Suppliers; |
| 4 | +import org.baeldung.guava.memoizer.CostlySupplier; |
| 5 | +import org.baeldung.guava.memoizer.Factorial; |
| 6 | +import org.baeldung.guava.memoizer.FibonacciSequence; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.math.BigInteger; |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.Instant; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | +import java.util.function.Supplier; |
| 14 | + |
| 15 | +import static org.hamcrest.core.Is.is; |
| 16 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 17 | +import static org.hamcrest.number.IsCloseTo.closeTo; |
| 18 | +import static org.junit.Assert.assertThat; |
| 19 | + |
| 20 | +public class GuavaMemoizerUnitTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenInteger_whenGetFibonacciNumber_thenShouldCalculateFibonacciNumber() { |
| 24 | + // given |
| 25 | + int n = 95; |
| 26 | + |
| 27 | + // when |
| 28 | + BigInteger fibonacciNumber = FibonacciSequence.getFibonacciNumber(n); |
| 29 | + |
| 30 | + // then |
| 31 | + BigInteger expectedFibonacciNumber = new BigInteger("31940434634990099905"); |
| 32 | + assertThat(fibonacciNumber, is(equalTo(expectedFibonacciNumber))); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenInteger_whenGetFactorial_thenShouldCalculateFactorial() { |
| 37 | + // given |
| 38 | + int n = 95; |
| 39 | + |
| 40 | + // when |
| 41 | + BigInteger factorial = new Factorial().getFactorial(n); |
| 42 | + |
| 43 | + // then |
| 44 | + BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000"); |
| 45 | + assertThat(factorial, is(equalTo(expectedFactorial))); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() { |
| 50 | + Supplier<BigInteger> memoizedSupplier; |
| 51 | + memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber); |
| 52 | + |
| 53 | + Instant start = Instant.now(); |
| 54 | + BigInteger bigNumber = memoizedSupplier.get(); |
| 55 | + Long durationInMs = Duration.between(start, Instant.now()).toMillis(); |
| 56 | + assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); |
| 57 | + assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); |
| 58 | + |
| 59 | + start = Instant.now(); |
| 60 | + bigNumber = memoizedSupplier.get().add(BigInteger.ONE); |
| 61 | + durationInMs = Duration.between(start, Instant.now()).toMillis(); |
| 62 | + assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); |
| 63 | + assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); |
| 64 | + |
| 65 | + start = Instant.now(); |
| 66 | + bigNumber = memoizedSupplier.get().add(BigInteger.TEN); |
| 67 | + durationInMs = Duration.between(start, Instant.now()).toMillis(); |
| 68 | + assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); |
| 69 | + assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException { |
| 74 | + Supplier<BigInteger> memoizedSupplier; |
| 75 | + memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS); |
| 76 | + |
| 77 | + Instant start = Instant.now(); |
| 78 | + BigInteger bigNumber = memoizedSupplier.get(); |
| 79 | + Long durationInMs = Duration.between(start, Instant.now()).toMillis(); |
| 80 | + assertThat(bigNumber, is(equalTo(new BigInteger("12345")))); |
| 81 | + assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); |
| 82 | + |
| 83 | + start = Instant.now(); |
| 84 | + bigNumber = memoizedSupplier.get().add(BigInteger.ONE); |
| 85 | + durationInMs = Duration.between(start, Instant.now()).toMillis(); |
| 86 | + assertThat(bigNumber, is(equalTo(new BigInteger("12346")))); |
| 87 | + assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D))); |
| 88 | + |
| 89 | + TimeUnit.SECONDS.sleep(1); |
| 90 | + |
| 91 | + start = Instant.now(); |
| 92 | + bigNumber = memoizedSupplier.get().add(BigInteger.TEN); |
| 93 | + durationInMs = Duration.between(start, Instant.now()).toMillis(); |
| 94 | + assertThat(bigNumber, is(equalTo(new BigInteger("12355")))); |
| 95 | + assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D))); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments