diff --git a/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java b/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java index 377701e0..4574f402 100644 --- a/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java +++ b/src/main/java/com/github/pedrovgs/linkedlist/ListNode.java @@ -15,6 +15,8 @@ */ package com.github.pedrovgs.linkedlist; +import java.util.Objects; + /** * Main class for linked list data structure. A linked list is a data structure consisting of a * group of nodes which together represent a sequence. Under the simplest form, each node is @@ -57,7 +59,7 @@ public void setNext(ListNode next) { if (!data.equals(listNode.data)) return false; - return true; + return Objects.equals(next, listNode.next); } @Override public int hashCode() { diff --git a/src/test/java/com/github/pedrovgs/binarytree/BinaryNodeTest.java b/src/test/java/com/github/pedrovgs/binarytree/BinaryNodeTest.java new file mode 100644 index 00000000..0e37d50b --- /dev/null +++ b/src/test/java/com/github/pedrovgs/binarytree/BinaryNodeTest.java @@ -0,0 +1,131 @@ +package com.github.pedrovgs.binarytree; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BinaryNodeTest { + + @Test + public void testEqualsSameObject() { + BinaryNode node = new BinaryNode<>(1); + assertEquals(node, node); // this == o + } + + @Test + public void testEqualsEqualObjectsWithoutChildren() { + BinaryNode node1 = new BinaryNode<>(1); + BinaryNode node2 = new BinaryNode<>(1); + assertEquals(node1, node2); + } + + @Test + public void testEqualsEqualObjectsWithChildren() { + BinaryNode node1 = new BinaryNode<>(1); + node1.setLeft(new BinaryNode<>(2)); + node1.setRight(new BinaryNode<>(3)); + + BinaryNode node2 = new BinaryNode<>(1); + node2.setLeft(new BinaryNode<>(2)); + node2.setRight(new BinaryNode<>(3)); + + assertEquals(node1, node2); + } + + @Test + public void testEqualsNull() { + BinaryNode node = new BinaryNode<>(1); + assertNotEquals(node, null); + } + + @Test + public void testEqualsDifferentClass() { + BinaryNode node = new BinaryNode<>(1); + String other = "not a node"; + assertNotEquals(node, other); + } + + @Test + public void testNotEqualsDifferentData() { + BinaryNode node1 = new BinaryNode<>(1); + BinaryNode node2 = new BinaryNode<>(2); + assertNotEquals(node1, node2); + } + + @Test + public void testNotEqualsDifferentLeft() { + BinaryNode node1 = new BinaryNode<>(1); + node1.setLeft(new BinaryNode<>(2)); + + BinaryNode node2 = new BinaryNode<>(1); + node2.setLeft(new BinaryNode<>(3)); + + assertNotEquals(node1, node2); + } + + @Test + public void testNotEqualsDifferentRight() { + BinaryNode node1 = new BinaryNode<>(1); + node1.setRight(new BinaryNode<>(2)); + + BinaryNode node2 = new BinaryNode<>(1); + node2.setRight(new BinaryNode<>(3)); + + assertNotEquals(node1, node2); + } + + @Test + public void testHashCodeEqualObjectsWithoutChildren() { + BinaryNode node1 = new BinaryNode<>(1); + BinaryNode node2 = new BinaryNode<>(1); + assertEquals(node1.hashCode(), node2.hashCode()); + } + + @Test + public void testHashCodeEqualObjectsWithChildren() { + BinaryNode node1 = new BinaryNode<>(1); + node1.setLeft(new BinaryNode<>(2)); + node1.setRight(new BinaryNode<>(3)); + + BinaryNode node2 = new BinaryNode<>(1); + node2.setLeft(new BinaryNode<>(2)); + node2.setRight(new BinaryNode<>(3)); + + assertEquals(node1.hashCode(), node2.hashCode()); + } + + @Test + public void testHashCodeConsistency() { + BinaryNode node = new BinaryNode<>(42); + int h1 = node.hashCode(); + int h2 = node.hashCode(); + assertEquals(h1, h2); + } + + @Test + public void testBinaryNodeSetAndGetData() { + BinaryNode node = new BinaryNode<>(1); + node.setRight(new BinaryNode<>(2)); + node.setLeft(new BinaryNode<>(3)); + + assertEquals(1, (int) node.getData()); + assertEquals(2, (int) node.getRight().getData()); + assertEquals(3, (int) node.getLeft().getData()); + } + + @Test + public void testBinaryNodeToString() { + BinaryNode node = new BinaryNode<>("Hello World"); + String expected = "BinaryNode{data=Hello World}"; + assertEquals(expected, node.toString()); + } + + @Test + public void testHasChildren() { + BinaryNode node = new BinaryNode<>(1); + node.setRight(new BinaryNode<>(2)); + + assertTrue(node.hasRight()); + assertFalse(node.hasLeft()); + } +} diff --git a/src/test/java/com/github/pedrovgs/linkedlist/ListNodeTest.java b/src/test/java/com/github/pedrovgs/linkedlist/ListNodeTest.java new file mode 100644 index 00000000..8c8050dd --- /dev/null +++ b/src/test/java/com/github/pedrovgs/linkedlist/ListNodeTest.java @@ -0,0 +1,102 @@ +package com.github.pedrovgs.linkedlist; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ListNodeTest { + + @Test + public void testEqualsSameObject() { + ListNode node = new ListNode<>(1); + assertEquals(node, node); + } + + @Test + public void testEqualsEqualObjects() { + ListNode node1 = new ListNode<>(1); + ListNode node2 = new ListNode<>(1); + assertEquals(node1, node2); + } + + @Test + public void testEqualsWithNextEqual() { + ListNode node1 = new ListNode<>(1); + node1.setNext(new ListNode<>(2)); + + ListNode node2 = new ListNode<>(1); + node2.setNext(new ListNode<>(2)); + + assertEquals(node1, node2); + } + + @Test + public void testNotEqualsDifferentData() { + ListNode node1 = new ListNode<>(1); + ListNode node2 = new ListNode<>(2); + assertNotEquals(node1, node2); + } + + @Test + public void testNotEqualsDifferentNext() { + ListNode node1 = new ListNode<>(1); + node1.setNext(new ListNode<>(2)); + + ListNode node2 = new ListNode<>(1); + node2.setNext(new ListNode<>(3)); + + assertNotEquals(node1, node2); + } + + @Test + public void testNotEqualsNull() { + ListNode node = new ListNode<>(1); + assertNotEquals(node, null); + } + + @Test + public void testNotEqualsDifferentClass() { + ListNode node = new ListNode<>(1); + String other = "not a node"; + assertNotEquals(node, other); + } + + @Test + public void testHashCodeEqualObjects() { + ListNode node1 = new ListNode<>(1); + ListNode node2 = new ListNode<>(1); + assertEquals(node1.hashCode(), node2.hashCode()); + } + + @Test + public void testHashCodeConsistency() { + ListNode node = new ListNode<>(42); + int h1 = node.hashCode(); + int h2 = node.hashCode(); + assertEquals(h1, h2); + } + + @Test + public void testToStringSingleNode() { + ListNode node = new ListNode<>(1); + String expected = "ListNode{data=1, next=null}"; + assertEquals(expected, node.toString()); + } + + @Test + public void testToStringTwoNodes() { + ListNode node = new ListNode<>(1); + node.setNext(new ListNode<>(2)); + String expected = "ListNode{data=1, next=ListNode{data=2, next=null}}"; + assertEquals(expected, node.toString()); + } + + @Test + public void testToStringChain() { + ListNode node = new ListNode<>(1); + node.setNext(new ListNode<>(2)); + node.getNext().setNext(new ListNode<>(3)); + String expected = "ListNode{data=1, next=ListNode{data=2, next=ListNode{data=3, next=null}}}"; + assertEquals(expected, node.toString()); + } +} diff --git a/src/test/java/com/github/pedrovgs/pair/PairTest.java b/src/test/java/com/github/pedrovgs/pair/PairTest.java new file mode 100644 index 00000000..743f6c4d --- /dev/null +++ b/src/test/java/com/github/pedrovgs/pair/PairTest.java @@ -0,0 +1,57 @@ +package com.github.pedrovgs.pair; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PairTest { + + @Test + public void testEqualsSameObject() { + Pair pair = new Pair<>(1, "one"); + assertEquals(pair, pair); + } + + @Test + public void testEqualsEqualObjects() { + Pair p1 = new Pair<>(1, "one"); + Pair p2 = new Pair<>(1, "one"); + assertEquals(p1, p2); + assertEquals(p1.hashCode(), p2.hashCode()); + } + + @Test + public void testNotEqualsDifferentA() { + Pair p1 = new Pair<>(1, "one"); + Pair p2 = new Pair<>(2, "one"); + assertNotEquals(p1, p2); + } + + @Test + public void testNotEqualsDifferentB() { + Pair p1 = new Pair<>(1, "one"); + Pair p2 = new Pair<>(1, "two"); + assertNotEquals(p1, p2); + } + + @Test + public void testNotEqualsNull() { + Pair p1 = new Pair<>(1, "one"); + assertNotEquals(p1, null); + } + + @Test + public void testNotEqualsDifferentClass() { + Pair p1 = new Pair<>(1, "one"); + String other = "something"; + assertNotEquals(p1, other); + } + + @Test + public void testHashCodeConsistency() { + Pair pair = new Pair<>(42, "answer"); + int h1 = pair.hashCode(); + int h2 = pair.hashCode(); + assertEquals(h1, h2); + } +}