CE-938 update memoization to say if it should store null values or not

This commit is contained in:
2024-05-19 20:21:03 -05:00
parent be69836b5b
commit 485bc618e0
2 changed files with 127 additions and 2 deletions

View File

@ -119,6 +119,57 @@ class MemoizationTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testMayNotStoreNull()
{
Memoization<String, String> memoization = new Memoization<>();
memoization.setMayStoreNullValues(false);
AtomicInteger callCounter = new AtomicInteger();
callCounter.set(0);
UnsafeFunction<String, String, Exception> supplier = name ->
{
callCounter.getAndIncrement();
if(name.equals("throw"))
{
throw (new Exception("You asked me to throw"));
}
else if(name.equals("null"))
{
return (null);
}
else
{
return (name);
}
};
assertThat(memoization.getResult("null", supplier)).isEmpty();
assertEquals(1, callCounter.get());
assertThat(memoization.getResult("null", supplier)).isEmpty();
assertEquals(2, callCounter.get()); // should re-run the supplier, incrementing the counter
assertThat(memoization.getResult("throw", supplier)).isEmpty();
assertEquals(3, callCounter.get());
assertThat(memoization.getResult("throw", supplier)).isEmpty();
assertEquals(4, callCounter.get()); // should re-run the supplier, incrementing the counter
//noinspection AssertBetweenInconvertibleTypes
assertThat(memoization.getResult("foo", supplier)).isPresent().get().isEqualTo("foo");
assertEquals(5, callCounter.get());
//noinspection AssertBetweenInconvertibleTypes
assertThat(memoization.getResult("foo", supplier)).isPresent().get().isEqualTo("foo");
assertEquals(5, callCounter.get()); // should NOT re-run the supplier, NOT incrementing the counter
}
/*******************************************************************************
**
*******************************************************************************/