1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.hipparchus.util;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18
19
20
21
22 public class PairTest {
23
24 @Test
25 public void testAccessor() {
26 final Pair<Integer, Double> p = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
27 Assert.assertEquals(Integer.valueOf(1), p.getKey());
28 Assert.assertEquals(2, p.getValue().doubleValue(), Math.ulp(1d));
29 }
30
31 @Test
32 public void testAccessor2() {
33 final Pair<Integer, Double> p = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
34
35
36
37 Assert.assertTrue(p.getFirst() == p.getKey());
38 Assert.assertTrue(p.getSecond() == p.getValue());
39 }
40
41 @Test
42 public void testEquals() {
43 Pair<Integer, Double> p1 = new Pair<>(null, null);
44 Assert.assertFalse(p1.equals(null));
45
46 Pair<Integer, Double> p2 = new Pair<>(null, null);
47 Assert.assertTrue(p1.equals(p2));
48
49 p1 = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
50 Assert.assertFalse(p1.equals(p2));
51
52 p2 = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
53 Assert.assertTrue(p1.equals(p2));
54
55 Pair<Integer, Float> p3 = new Pair<>(Integer.valueOf(1), Float.valueOf(2));
56 Assert.assertFalse(p1.equals(p3));
57 }
58
59 @Test
60 public void testHashCode() {
61 final MyInteger m1 = new MyInteger(1);
62 final MyInteger m2 = new MyInteger(1);
63
64 final Pair<MyInteger, MyInteger> p1 = new Pair<>(m1, m1);
65 final Pair<MyInteger, MyInteger> p2 = new Pair<>(m2, m2);
66
67 Assert.assertTrue(p1.hashCode() == p2.hashCode());
68
69
70 m2.set(2);
71 Assert.assertFalse(p1.hashCode() == p2.hashCode());
72 }
73
74 @Test
75 public void testToString() {
76 Assert.assertEquals("[null, null]", new Pair<>(null, null).toString());
77 Assert.assertEquals("[foo, 3]", new Pair<>("foo", 3).toString());
78 }
79
80 @Test
81 public void testCreate() {
82 final Pair<String, Integer> p1 = Pair.create("foo", 3);
83 Assert.assertNotNull(p1);
84 final Pair<String, Integer> p2 = new Pair<>("foo", 3);
85 Assert.assertEquals(p2, p1);
86 }
87
88
89
90
91 private static class MyInteger {
92 private int i;
93
94 public MyInteger(int i) {
95 this.i = i;
96 }
97
98 public void set(int i) {
99 this.i = i;
100 }
101
102 @Override
103 public boolean equals(Object o) {
104 if (!(o instanceof MyInteger)) {
105 return false;
106 } else {
107 return i == ((MyInteger) o).i;
108 }
109 }
110
111 @Override
112 public int hashCode() {
113 return i;
114 }
115 }
116 }