|
| 1 | +import org.testng.Assert; |
| 2 | +import org.testng.annotations.Test; |
| 3 | + |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.Stack; |
| 6 | + |
| 7 | +/** |
| 8 | + * Unit tests for the {@link Couple} class. |
| 9 | + * <p> |
| 10 | + * This class validates all functionalities of the {@code Couple} class including construction, getters/setters, |
| 11 | + * equality, comparison, hash code generation, string representation, and utility methods like {@code swap} and |
| 12 | + * {@code makeCouple}. It ensures correct behavior across different data types and edge cases (nulls, identity, etc.). |
| 13 | + * </p> |
| 14 | + * |
| 15 | + * @author Adarsh Sharma |
| 16 | + * @since 1.0 |
| 17 | + */ |
| 18 | +public class CoupleTest { |
| 19 | + |
| 20 | + /** |
| 21 | + * Verifies that the constructor initializes the first and second elements correctly. |
| 22 | + */ |
| 23 | + @Test |
| 24 | + public void testConstructor() { |
| 25 | + Couple<Integer, String> Couple = new Couple<>(1, "one"); |
| 26 | + Assert.assertEquals(Couple.getFirst(), Integer.valueOf(1)); |
| 27 | + Assert.assertEquals(Couple.getSecond(), "one"); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Verifies that the getter and setter methods update and retrieve values as expected. |
| 32 | + */ |
| 33 | + @Test |
| 34 | + public void testGettersAndSetters() { |
| 35 | + Couple<Integer, String> Couple = new Couple<>(1, "one"); |
| 36 | + Couple.setFirst(2); |
| 37 | + Couple.setSecond("two"); |
| 38 | + Assert.assertEquals(Couple.getFirst(), Integer.valueOf(2)); |
| 39 | + Assert.assertEquals(Couple.getSecond(), "two"); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Verifies that the {@code equals} method correctly evaluates object equality. |
| 44 | + */ |
| 45 | + @Test |
| 46 | + public void testEquals() { |
| 47 | + Couple<Integer, String> Couple1 = new Couple<>(1, "one"); |
| 48 | + Couple<Integer, String> Couple2 = new Couple<>(1, "one"); |
| 49 | + Couple<Integer, String> Couple3 = new Couple<>(2, "two"); |
| 50 | + |
| 51 | + Assert.assertTrue(Couple1.equals(Couple2)); |
| 52 | + Assert.assertFalse(Couple1.equals(Couple3)); |
| 53 | + Assert.assertFalse(Couple1.equals(null)); |
| 54 | + Assert.assertFalse(Couple1.equals("not a Couple")); |
| 55 | + Assert.assertTrue(Couple1.equals(Couple1)); // Self-comparison |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Verifies equality behavior when one or both elements are {@code null}. |
| 60 | + */ |
| 61 | + @Test |
| 62 | + public void testEqualsWithNullElements() { |
| 63 | + Couple<String, String> Couple1 = new Couple<>(null, "one"); |
| 64 | + Couple<String, String> Couple2 = new Couple<>(null, "one"); |
| 65 | + Assert.assertTrue(Couple1.equals(Couple2)); |
| 66 | + Couple<String, String> Couple3 = new Couple<>(null, null); |
| 67 | + Couple<String, String> Couple4 = new Couple<>(null, null); |
| 68 | + Assert.assertTrue(Couple1.equals(Couple2)); |
| 69 | + Assert.assertTrue(Couple2.equals(Couple1)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Validates that {@code hashCode} is consistent for equal Couples. |
| 74 | + */ |
| 75 | + @Test |
| 76 | + public void testHashCode() { |
| 77 | + Couple<Integer, String> Couple1 = new Couple<>(1, "one"); |
| 78 | + Couple<Integer, String> Couple2 = new Couple<>(1, "one"); |
| 79 | + Assert.assertEquals(Couple1.hashCode(), Couple2.hashCode()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Ensures {@code hashCode} handles {@code null} elements correctly. |
| 84 | + */ |
| 85 | + @Test |
| 86 | + public void testHashCodeWithNulls() { |
| 87 | + Couple<String, String> Couple = new Couple<>(null, null); |
| 88 | + Assert.assertEquals(Couple.hashCode(), Objects.hash(null, null)); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Validates the static factory method {@code makeCouple} creates an equivalent {@code Couple}. |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void testMakeCouple() { |
| 96 | + Couple<Integer, String> Couple = Couple.makeCouple(1, "one"); |
| 97 | + Assert.assertEquals(Couple.getFirst(), Integer.valueOf(1)); |
| 98 | + Assert.assertEquals(Couple.getSecond(), "one"); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Tests usage of {@code Couple} with custom objects such as {@code Stack}. |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void testCustomObjects() { |
| 106 | + Couple<Stack<Integer>, String> Couple = new Couple<>(new Stack<>(), "test"); |
| 107 | + Couple.getFirst().push(1); |
| 108 | + Couple.getFirst().push(2); |
| 109 | + Assert.assertEquals(Couple.getFirst().size(), 2); |
| 110 | + Assert.assertEquals(Couple.getSecond(), "test"); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Validates the {@code toString} representation for non-null elements. |
| 115 | + */ |
| 116 | + @Test |
| 117 | + public void testToString() { |
| 118 | + Couple<Integer, String> Couple = new Couple<>(1, "one"); |
| 119 | + Assert.assertEquals(Couple.toString(), "(1, one)"); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Validates the {@code toString} representation when both elements are {@code null}. |
| 124 | + */ |
| 125 | + @Test |
| 126 | + public void testToStringWithNulls() { |
| 127 | + Couple<String, String> Couple = new Couple<>(null, null); |
| 128 | + Assert.assertEquals(Couple.toString(), "(null, null)"); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Validates that {@code compareTo} returns zero for equal Couples. |
| 133 | + */ |
| 134 | + @Test |
| 135 | + public void testCompareToEqual() { |
| 136 | + Couple<String, String> Couple1 = new Couple<>("a", "b"); |
| 137 | + Couple<String, String> Couple2 = new Couple<>("a", "b"); |
| 138 | + Assert.assertEquals(Couple1.compareTo(Couple2), 0); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Validates that {@code compareTo} returns a negative or positive value |
| 143 | + * when the first elements differ. |
| 144 | + */ |
| 145 | + @Test |
| 146 | + public void testCompareToFirstDifferent() { |
| 147 | + Couple<String, String> Couple1 = new Couple<>("a", "b"); |
| 148 | + Couple<String, String> Couple2 = new Couple<>("b", "b"); |
| 149 | + Assert.assertTrue(Couple1.compareTo(Couple2) < 0); |
| 150 | + Assert.assertTrue(Couple2.compareTo(Couple1) > 0); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Validates that {@code compareTo} evaluates second elements when first are equal. |
| 155 | + */ |
| 156 | + @Test |
| 157 | + public void testCompareToSecondDifferent() { |
| 158 | + Couple<String, String> Couple1 = new Couple<>("a", "b"); |
| 159 | + Couple<String, String> Couple2 = new Couple<>("a", "c"); |
| 160 | + Assert.assertTrue(Couple1.compareTo(Couple2) < 0); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Tests {@code compareTo} behavior when one or both elements are {@code null}. |
| 165 | + */ |
| 166 | + @Test |
| 167 | + public void testCompareToWithNulls() { |
| 168 | + Couple<String, String> Couple1 = new Couple<>(null, "b"); |
| 169 | + Couple<String, String> Couple2 = new Couple<>("a", "b"); |
| 170 | + Assert.assertTrue(Couple1.compareTo(Couple2) < 0); |
| 171 | + |
| 172 | + Couple<String, String> Couple3 = new Couple<>("a", null); |
| 173 | + Couple<String, String> Couple4 = new Couple<>("a", "b"); |
| 174 | + Assert.assertTrue(Couple3.compareTo(Couple4) < 0); |
| 175 | + |
| 176 | + Couple<String, String> Couple5 = new Couple<>(null, null); |
| 177 | + Couple<String, String> Couple6 = new Couple<>(null, null); |
| 178 | + Assert.assertEquals(Couple5.compareTo(Couple6), 0); |
| 179 | + // null is considered less than non-null for comparison |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Verifies that passing {@code null} to {@code compareTo} throws {@code NullPointerException}. |
| 184 | + */ |
| 185 | + @Test(expectedExceptions = NullPointerException.class) |
| 186 | + public void testCompareToNull() { |
| 187 | + Couple<String, String> Couple = new Couple<>("a", "b"); |
| 188 | + Couple.compareTo(null); |
| 189 | + } |
| 190 | +} |
0 commit comments