Skip to content

Commit ffe758b

Browse files
committed
Add comprehensive tests for Pair class
Add 15 unit tests covering getKey(), getValue(), equals(), hashCode(), and toString() methods. Tests include edge cases with null values and different generic types. https://claude.ai/code/session_01T7864vnS6vc9W1PbHRzCK4
1 parent 8737e23 commit ffe758b

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package de.kherud.llama;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class PairTest {
8+
9+
@Test
10+
public void testGetKey() {
11+
Pair<String, Integer> pair = new Pair<>("key1", 42);
12+
assertEquals("key1", pair.getKey());
13+
}
14+
15+
@Test
16+
public void testGetValue() {
17+
Pair<String, Integer> pair = new Pair<>("key1", 42);
18+
assertEquals(Integer.valueOf(42), pair.getValue());
19+
}
20+
21+
@Test
22+
public void testGetKeyWithNullValue() {
23+
Pair<String, Integer> pair = new Pair<>("key1", null);
24+
assertEquals("key1", pair.getKey());
25+
assertNull(pair.getValue());
26+
}
27+
28+
@Test
29+
public void testGetKeyWithNullKey() {
30+
Pair<String, Integer> pair = new Pair<>(null, 42);
31+
assertNull(pair.getKey());
32+
assertEquals(Integer.valueOf(42), pair.getValue());
33+
}
34+
35+
@Test
36+
public void testEqualsWithSamePair() {
37+
Pair<String, Integer> pair1 = new Pair<>("key", 123);
38+
Pair<String, Integer> pair2 = new Pair<>("key", 123);
39+
assertEquals(pair1, pair2);
40+
}
41+
42+
@Test
43+
public void testEqualsWithDifferentKey() {
44+
Pair<String, Integer> pair1 = new Pair<>("key1", 123);
45+
Pair<String, Integer> pair2 = new Pair<>("key2", 123);
46+
assertNotEquals(pair1, pair2);
47+
}
48+
49+
@Test
50+
public void testEqualsWithDifferentValue() {
51+
Pair<String, Integer> pair1 = new Pair<>("key", 123);
52+
Pair<String, Integer> pair2 = new Pair<>("key", 456);
53+
assertNotEquals(pair1, pair2);
54+
}
55+
56+
@Test
57+
public void testEqualsWithNull() {
58+
Pair<String, Integer> pair = new Pair<>("key", 123);
59+
assertNotEquals(pair, null);
60+
}
61+
62+
@Test
63+
public void testEqualsWithDifferentClass() {
64+
Pair<String, Integer> pair = new Pair<>("key", 123);
65+
assertNotEquals(pair, "not a pair");
66+
}
67+
68+
@Test
69+
public void testEqualsSameInstance() {
70+
Pair<String, Integer> pair = new Pair<>("key", 123);
71+
assertEquals(pair, pair);
72+
}
73+
74+
@Test
75+
public void testEqualsWithBothNullKeyAndValue() {
76+
Pair<String, Integer> pair1 = new Pair<>(null, null);
77+
Pair<String, Integer> pair2 = new Pair<>(null, null);
78+
assertEquals(pair1, pair2);
79+
}
80+
81+
@Test
82+
public void testHashCodeSamePair() {
83+
Pair<String, Integer> pair1 = new Pair<>("key", 123);
84+
Pair<String, Integer> pair2 = new Pair<>("key", 123);
85+
assertEquals(pair1.hashCode(), pair2.hashCode());
86+
}
87+
88+
@Test
89+
public void testHashCodeDifferentPairs() {
90+
Pair<String, Integer> pair1 = new Pair<>("key1", 123);
91+
Pair<String, Integer> pair2 = new Pair<>("key2", 456);
92+
// Different pairs may have different hash codes (not guaranteed, but likely)
93+
// We mostly check that hashCode() doesn't throw
94+
assertNotNull(pair1.hashCode());
95+
assertNotNull(pair2.hashCode());
96+
}
97+
98+
@Test
99+
public void testHashCodeWithNull() {
100+
Pair<String, Integer> pair = new Pair<>(null, null);
101+
// Should not throw when hashing null values
102+
assertNotNull(pair.hashCode());
103+
}
104+
105+
@Test
106+
public void testToString() {
107+
Pair<String, Integer> pair = new Pair<>("testKey", 42);
108+
String result = pair.toString();
109+
assertTrue(result.contains("Pair"));
110+
assertTrue(result.contains("testKey"));
111+
assertTrue(result.contains("42"));
112+
}
113+
114+
@Test
115+
public void testToStringWithNull() {
116+
Pair<String, Integer> pair = new Pair<>(null, 42);
117+
String result = pair.toString();
118+
assertTrue(result.contains("Pair"));
119+
assertTrue(result.contains("null"));
120+
}
121+
122+
@Test
123+
public void testPairWithDifferentTypes() {
124+
Pair<Integer, Double> pair = new Pair<>(10, 3.14);
125+
assertEquals(Integer.valueOf(10), pair.getKey());
126+
assertEquals(Double.valueOf(3.14), pair.getValue());
127+
}
128+
129+
@Test
130+
public void testPairWithComplexTypes() {
131+
Pair<String[], Integer[]> pair = new Pair<>(
132+
new String[]{"a", "b"},
133+
new Integer[]{1, 2}
134+
);
135+
assertArrayEquals(new String[]{"a", "b"}, pair.getKey());
136+
assertArrayEquals(new Integer[]{1, 2}, pair.getValue());
137+
}
138+
}

0 commit comments

Comments
 (0)