Skip to content

Commit

Permalink
#12: Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
skapral committed Mar 23, 2022
1 parent 24bdd2c commit 823a0c8
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,72 @@


import com.pragmaticobjects.oo.memoized.core.AssertCallTimes;
import com.pragmaticobjects.oo.memoized.core.AssertDisposal;
import com.pragmaticobjects.oo.memoized.core.MemoizedCallable;
import com.pragmaticobjects.oo.tests.TestCase;
import com.pragmaticobjects.oo.tests.junit5.TestsSuite;
import io.vavr.collection.List;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

class TestCallable implements MemoizedCallable<Integer> {
private final int id;

public TestCallable(int id) {
this.id = id;
}

@Override
public final Integer call() {
return id;
}
}


class MemoryCHMTest extends TestsSuite {
public MemoryCHMTest() {
super(
new TestCase(
"Check at most once call",
new AssertCallTimes(
new MemoryCHM(
new ConcurrentHashMap<>()
),
10,
1
)
new TestCase(
"Check at most once call",
new AssertCallTimes(
new MemoryCHM(
new ConcurrentHashMap<>()
),
10,
1
)
),
new TestCase(
"Check disposal of existing value",
new AssertDisposal(
new MemoryCHM(
new ConcurrentHashMap<>()
),
List.of(
new TestCallable(1),
new TestCallable(2),
new TestCallable(3)
),
new TestCallable(1),
Optional.of(1)
)
),
new TestCase(
"Check disposal of inexisting value",
new AssertDisposal(
new MemoryCHM(
new ConcurrentHashMap<>()
),
List.of(
new TestCallable(1),
new TestCallable(2),
new TestCallable(3)
),
new TestCallable(4),
Optional.empty()
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@

import java.util.concurrent.atomic.AtomicInteger;

// NOTE: equals, hashCode and toString are implicitly generated
// by equivalence-maven-plugin
class TestCallable implements MemoizedCallable {
private final AtomicInteger counter;

public TestCallable(AtomicInteger counter) {
this.counter = counter;
}

@Override
public final Object call() {
counter.incrementAndGet();
return null;
}
}

public class AssertCallTimes implements Assertion {
private final Memory memory;
private final int callNums;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*-
* ===========================================================================
* memoized-core
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (C) 2019 - 2022 Kapralov Sergey
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ============================================================================
*/
package com.pragmaticobjects.oo.memoized.core;

import com.pragmaticobjects.oo.tests.Assertion;
import io.vavr.collection.List;
import org.assertj.core.api.Assertions;

import java.util.Optional;

public class AssertDisposal implements Assertion {
private final Memory memoryUnderTest;
private final List<MemoizedCallable<?>> preparationCallables;
private final MemoizedCallable<?> callableToDispose;
private final Optional<?> expectedDisposedValue;

public AssertDisposal(Memory memoryUnderTest, List<MemoizedCallable<?>> preparationCallables, MemoizedCallable<?> callableToDispose, Optional<?> expectedDisposedValue) {
this.memoryUnderTest = memoryUnderTest;
this.preparationCallables = preparationCallables;
this.callableToDispose = callableToDispose;
this.expectedDisposedValue = expectedDisposedValue;
}

@Override
public final void check() throws Exception {
preparationCallables.forEach(memoryUnderTest::memoized);
Optional<?> disposedValue = memoryUnderTest.dispose(callableToDispose);
Assertions.assertThat(disposedValue).isEqualTo(expectedDisposedValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*-
* ===========================================================================
* memoized-core
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (C) 2019 - 2022 Kapralov Sergey
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ============================================================================
*/
package com.pragmaticobjects.oo.memoized.core;

import java.util.concurrent.atomic.AtomicInteger;

// NOTE: equals, hashCode and toString are implicitly generated
// by equivalence-maven-plugin
class TestCallable implements MemoizedCallable {
private final AtomicInteger counter;

public TestCallable(AtomicInteger counter) {
this.counter = counter;
}

@Override
public final Object call() {
counter.incrementAndGet();
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,6 @@

import java.util.Optional;

/**
* Test {@link Memory} implementation that memoises nothing
*/
class BluntMemory implements Memory {
@Override
public final <T> T memoized(MemoizedCallable<T> callable) {
return callable.call();
}

@Override
public final <T> Optional<T> dispose(MemoizedCallable<T> callable) {
return Optional.empty();
}

@Override
public final void clean() {
// Do Nothing
}
}


public class AssertCallTimesTest extends TestsSuite {
public AssertCallTimesTest() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*-
* ===========================================================================
* memoized-core
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (C) 2019 - 2022 Kapralov Sergey
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ============================================================================
*/
package com.pragmaticobjects.oo.memoized.core;

import com.pragmaticobjects.oo.tests.AssertAssertionPasses;
import com.pragmaticobjects.oo.tests.TestCase;
import com.pragmaticobjects.oo.tests.junit5.TestsSuite;
import io.vavr.collection.List;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

public class AssertDisposalTest extends TestsSuite {
public AssertDisposalTest() {
super(
new TestCase(
"blunt disposal",
new AssertAssertionPasses(
new AssertDisposal(
new BluntMemory(),
List.empty(),
new TestCallable(
new AtomicInteger()
),
Optional.empty()
)
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*-
* ===========================================================================
* memoized-core
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Copyright (C) 2019 - 2022 Kapralov Sergey
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ============================================================================
*/
package com.pragmaticobjects.oo.memoized.core;

import java.util.Optional;

/**
* Test {@link Memory} implementation that memoises nothing
*/
class BluntMemory implements Memory {
@Override
public final <T> T memoized(MemoizedCallable<T> callable) {
return callable.call();
}

@Override
public final <T> Optional<T> dispose(MemoizedCallable<T> callable) {
return Optional.empty();
}

@Override
public final void clean() {
// Do Nothing
}
}

0 comments on commit 823a0c8

Please sign in to comment.