Skip to content

Replace Hamcrest matchers with AssertJ matchers. Fix #24 #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'net.bytebuddy:byte-buddy:1.14.12'
testImplementation 'org.assertj:assertj-core:3.27.3'
testImplementation 'org.hamcrest:hamcrest:3.0'
testImplementation 'org.mockito:mockito-core:5.17.0'
testImplementation 'org.awaitility:awaitility:4.3.0'
testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.19.3'
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/by/andd3dfx/numeric/Fibonacci.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public class Fibonacci {
/**
* Top-down approach (recursive, better)
*/
public static int calculate(int n) {
public static int calculateTopDown(int n) {
if (n < 0) throw new IllegalArgumentException("Number should be not less than 0!");

if (!map.containsKey(n)) {
map.put(n, calculate(n - 1) + calculate(n - 2));
map.put(n, calculateTopDown(n - 1) + calculateTopDown(n - 2));
}

return map.get(n);
Expand All @@ -32,7 +32,7 @@ public static int calculate(int n) {
/**
* Down-top approach (loop, just as example)
*/
public static int calculate2(int n) {
public static int calculateDownTop(int n) {
if (n < 0) throw new IllegalArgumentException("Number should be not less than 0!");

for (int i = 2; i <= n; i++) {
Expand Down
66 changes: 32 additions & 34 deletions src/test/java/by/andd3dfx/cache/LFUCacheUsingLinkedHashSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,36 @@

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

public class LFUCacheUsingLinkedHashSetTest {

@Test
public void testCacheWithZeroCapacity() {
LFUCacheUsingLinkedHashSet<Integer, Integer> cache = new LFUCacheUsingLinkedHashSet<>(0);

assertThat(cache.get(2), nullValue());
assertThat(cache.get(2)).isNull();
cache.put(2, 67);
assertThat(cache.get(2), nullValue());
assertThat(cache.get(2)).isNull();
}

@Test
public void testCache() {
LFUCacheUsingLinkedHashSet<Integer, Integer> cache = new LFUCacheUsingLinkedHashSet<>(2);
cache.put(1, 10);
cache.put(2, 20);
assertThat(cache.get(1), is(10));
assertThat(cache.get(1)).isEqualTo(10);

cache.put(3, 30); //evicts key 2

assertThat(cache.get(2), nullValue());
assertThat(cache.get(3), is(30));
assertThat(cache.get(2)).isNull();
assertThat(cache.get(3)).isEqualTo(30);

cache.put(4, 40); //evicts key 1

assertThat(cache.get(1), nullValue());
assertThat(cache.get(3), is(30));
assertThat(cache.get(4), is(40));
assertThat(cache.get(1)).isNull();
assertThat(cache.get(3)).isEqualTo(30);
assertThat(cache.get(4)).isEqualTo(40);
}

@Test
Expand All @@ -42,29 +40,29 @@ public void testCacheComplex() {
cache.put(1, 10);
cache.put(2, 20);
cache.put(3, 30);
assertThat(cache.get(1), is(10));
assertThat(cache.get(3), is(30));
assertThat(cache.get(1), is(10));
assertThat(cache.get(1), is(10)); // 1 -> 3 times used
assertThat(cache.get(3), is(30)); // 3 -> 2 times used
assertThat(cache.get(2), is(20)); // 2 -> 1 times used
assertThat(cache.get(1)).isEqualTo(10);
assertThat(cache.get(3)).isEqualTo(30);
assertThat(cache.get(1)).isEqualTo(10);
assertThat(cache.get(1)).isEqualTo(10); // 1 -> 3 times used
assertThat(cache.get(3)).isEqualTo(30); // 3 -> 2 times used
assertThat(cache.get(2)).isEqualTo(20); // 2 -> 1 times used

cache.put(4, 40); // evicts key 2 (less used)

assertThat(cache.get(1), is(10)); // 1 -> 4 times used
assertThat(cache.get(2), nullValue());
assertThat(cache.get(4), is(40));
assertThat(cache.get(4), is(40));
assertThat(cache.get(4), is(40)); // 4 -> 3 times used
assertThat(cache.get(3), is(30)); // 3 -> 3 times used
assertThat(cache.get(1)).isEqualTo(10); // 1 -> 4 times used
assertThat(cache.get(2)).isNull();
assertThat(cache.get(4)).isEqualTo(40);
assertThat(cache.get(4)).isEqualTo(40);
assertThat(cache.get(4)).isEqualTo(40); // 4 -> 3 times used
assertThat(cache.get(3)).isEqualTo(30); // 3 -> 3 times used

cache.put(5, 50); // evicts key 4 (same used as 3, but 3 is last used)

assertThat(cache.get(1), is(10));
assertThat(cache.get(2), nullValue());
assertThat(cache.get(3), is(30));
assertThat(cache.get(4), nullValue());
assertThat(cache.get(5), is(50));
assertThat(cache.get(1)).isEqualTo(10);
assertThat(cache.get(2)).isNull();
assertThat(cache.get(3)).isEqualTo(30);
assertThat(cache.get(4)).isNull();
assertThat(cache.get(5)).isEqualTo(50);
}

@Test
Expand All @@ -74,23 +72,23 @@ public void testCacheLeetCode_updateValue() {
cache.put(2, 1);
cache.put(2, 2); // overwrites value for key 2
cache.put(4, 4); // evicts key 3
assertThat(cache.get(2), is(2));
assertThat(cache.get(2)).isEqualTo(2);
}

@Test
public void determineKeyToDelete() {
var cache = new LFUCacheUsingLinkedHashSet<Integer, Integer>(2);
cache.put(2, 100);
cache.put(3, 200);
assertThat(cache.determineKeyToDelete(), is(2));
assertThat(cache.determineKeyToDelete()).isEqualTo(2);

cache.get(2);
assertThat(cache.determineKeyToDelete(), is(3));
assertThat(cache.determineKeyToDelete()).isEqualTo(3);
cache.get(3);
assertThat(cache.determineKeyToDelete(), is(2));
assertThat(cache.determineKeyToDelete()).isEqualTo(2);
cache.put(4, 500);
assertThat(cache.determineKeyToDelete(), is(4));
assertThat(cache.determineKeyToDelete()).isEqualTo(4);
cache.get(4);
assertThat(cache.determineKeyToDelete(), is(3));
assertThat(cache.determineKeyToDelete()).isEqualTo(3);
}
}
66 changes: 32 additions & 34 deletions src/test/java/by/andd3dfx/cache/LFUCacheUsingTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,36 @@

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

public class LFUCacheUsingTimeTest {

@Test
public void testCacheWithZeroCapacity() {
LFUCacheUsingTime<Integer, Integer> cache = new LFUCacheUsingTime<>(0);

assertThat(cache.get(2), nullValue());
assertThat(cache.get(2)).isNull();
cache.put(2, 67);
assertThat(cache.get(2), nullValue());
assertThat(cache.get(2)).isNull();
}

@Test
public void testCache() {
LFUCacheUsingTime<Integer, Integer> cache = new LFUCacheUsingTime<>(2);
cache.put(1, 10);
cache.put(2, 20);
assertThat(cache.get(1), is(10));
assertThat(cache.get(1)).isEqualTo(10);

cache.put(3, 30); //evicts key 2

assertThat(cache.get(2), nullValue());
assertThat(cache.get(3), is(30));
assertThat(cache.get(2)).isNull();
assertThat(cache.get(3)).isEqualTo(30);

cache.put(4, 40); //evicts key 1

assertThat(cache.get(1), nullValue());
assertThat(cache.get(3), is(30));
assertThat(cache.get(4), is(40));
assertThat(cache.get(1)).isNull();
assertThat(cache.get(3)).isEqualTo(30);
assertThat(cache.get(4)).isEqualTo(40);
}

@Test
Expand All @@ -42,29 +40,29 @@ public void testCacheComplex() {
cache.put(1, 10);
cache.put(2, 20);
cache.put(3, 30);
assertThat(cache.get(1), is(10));
assertThat(cache.get(3), is(30));
assertThat(cache.get(1), is(10));
assertThat(cache.get(1), is(10)); // 1 -> 3 times used
assertThat(cache.get(3), is(30)); // 3 -> 2 times used
assertThat(cache.get(2), is(20)); // 2 -> 1 times used
assertThat(cache.get(1)).isEqualTo(10);
assertThat(cache.get(3)).isEqualTo(30);
assertThat(cache.get(1)).isEqualTo(10);
assertThat(cache.get(1)).isEqualTo(10); // 1 -> 3 times used
assertThat(cache.get(3)).isEqualTo(30); // 3 -> 2 times used
assertThat(cache.get(2)).isEqualTo(20); // 2 -> 1 times used

cache.put(4, 40); // evicts key 2 (less used)

assertThat(cache.get(1), is(10)); // 1 -> 4 times used
assertThat(cache.get(2), nullValue());
assertThat(cache.get(4), is(40));
assertThat(cache.get(4), is(40));
assertThat(cache.get(4), is(40)); // 4 -> 3 times used
assertThat(cache.get(3), is(30)); // 3 -> 3 times used
assertThat(cache.get(1)).isEqualTo(10); // 1 -> 4 times used
assertThat(cache.get(2)).isNull();
assertThat(cache.get(4)).isEqualTo(40);
assertThat(cache.get(4)).isEqualTo(40);
assertThat(cache.get(4)).isEqualTo(40); // 4 -> 3 times used
assertThat(cache.get(3)).isEqualTo(30); // 3 -> 3 times used

cache.put(5, 50); // evicts key 4 (same used as 3, but 3 is last used)

assertThat(cache.get(1), is(10));
assertThat(cache.get(2), nullValue());
assertThat(cache.get(3), is(30));
assertThat(cache.get(4), nullValue());
assertThat(cache.get(5), is(50));
assertThat(cache.get(1)).isEqualTo(10);
assertThat(cache.get(2)).isNull();
assertThat(cache.get(3)).isEqualTo(30);
assertThat(cache.get(4)).isNull();
assertThat(cache.get(5)).isEqualTo(50);
}

@Test
Expand All @@ -74,23 +72,23 @@ public void testCacheLeetcode_updateValue() {
cache.put(2, 1);
cache.put(2, 2); // overwrites value for key 2
cache.put(4, 4); // evicts key 3
assertThat(cache.get(2), is(2));
assertThat(cache.get(2)).isEqualTo(2);
}

@Test
public void determineKeyToDelete() {
var cache = new LFUCacheUsingTime<Integer, Integer>(2);
cache.put(2, 100);
cache.put(3, 200);
assertThat(cache.determineKeyToDelete(), is(2));
assertThat(cache.determineKeyToDelete()).isEqualTo(2);

cache.get(2);
assertThat(cache.determineKeyToDelete(), is(3));
assertThat(cache.determineKeyToDelete()).isEqualTo(3);
cache.get(3);
assertThat(cache.determineKeyToDelete(), is(2));
assertThat(cache.determineKeyToDelete()).isEqualTo(2);
cache.put(4, 500);
assertThat(cache.determineKeyToDelete(), is(4));
assertThat(cache.determineKeyToDelete()).isEqualTo(4);
cache.get(4);
assertThat(cache.determineKeyToDelete(), is(3));
assertThat(cache.determineKeyToDelete()).isEqualTo(3);
}
}
18 changes: 8 additions & 10 deletions src/test/java/by/andd3dfx/cache/LRUCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

public class LRUCacheTest {

Expand All @@ -13,20 +11,20 @@ public void testCache() {
LRUCache<Integer, Integer> cache = new LRUCache<>(2);
cache.put(1, 1);
cache.put(2, 4);
assertThat(cache.get(1), is(1));
assertThat(cache.get(1)).isEqualTo(1);
cache.put(3, 9); // evicts key 2
assertThat(cache.get(2), nullValue());
assertThat(cache.get(2)).isNull();
cache.put(4, 16); // evicts key 1
assertThat(cache.get(1), nullValue());
assertThat(cache.get(3), is(9));
assertThat(cache.get(4), is(16));
assertThat(cache.get(1)).isNull();
assertThat(cache.get(3)).isEqualTo(9);
assertThat(cache.get(4)).isEqualTo(16);
}

@Test
public void testCacheForZeroCapacity() {
LRUCache<Integer, Integer> cache = new LRUCache<>(0);
cache.put(2, 4);
assertThat(cache.get(2), nullValue());
assertThat(cache.get(1), nullValue());
assertThat(cache.get(2)).isNull();
assertThat(cache.get(1)).isNull();
}
}
22 changes: 13 additions & 9 deletions src/test/java/by/andd3dfx/collections/ReverseLinkedListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import static by.andd3dfx.collections.ReverseLinkedList.reverseUsingLoop;
import static by.andd3dfx.collections.ReverseLinkedList.reverseUsingRecursion;
import static by.andd3dfx.collections.ReverseLinkedList.reverseUsingStack;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;

public class ReverseLinkedListTest {
Expand Down Expand Up @@ -51,15 +50,20 @@ public void testReverseUsingRecursionForNull() {
assertNull(reverseUsingRecursion(null));
}

private Node buildLinkedList() {
return new Node(3, new Node(7, new Node(12, new Node(10, null))));
private Node<Integer> buildLinkedList() {
return new Node<>(3, new Node<>(7, new Node<>(12, new Node<>(10, null))));
}

private void checkReversedList(Node head) {
assertThat("Wrong 0 item of reversed list", head.getValue(), is(10));
assertThat("Wrong 1 item of reversed list", head.getNext().getValue(), is(12));
assertThat("Wrong 2 item of reversed list", head.getNext().getNext().getValue(), is(7));
assertThat("Wrong 3 item of reversed list", head.getNext().getNext().getNext().getValue(), is(3));
assertNull("Wrong next of 3 item of reversed list", head.getNext().getNext().getNext().getNext());
assertThat(head.getValue())
.as("Wrong 0 item of reversed list").isEqualTo(10);
assertThat(head.getNext().getValue())
.as("Wrong 1 item of reversed list").isEqualTo(12);
assertThat(head.getNext().getNext().getValue())
.as("Wrong 2 item of reversed list").isEqualTo(7);
assertThat(head.getNext().getNext().getNext().getValue())
.as("Wrong 3 item of reversed list").isEqualTo(3);
assertThat(head.getNext().getNext().getNext().getNext())
.as("Wrong next of 3 item of reversed list").isNull();
}
}
Loading