View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9    * or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package org.hipparchus.util;
15  
16  import org.junit.jupiter.api.Test;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertNotEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  /**
25   * Test for {@link Pair}.
26   */
27  class PairTest {
28  
29      @Test
30      void testAccessor() {
31          final Pair<Integer, Double> p = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
32          assertEquals(Integer.valueOf(1), p.getKey());
33          assertEquals(2, p.getValue().doubleValue(), Math.ulp(1d));
34      }
35  
36      @Test
37      void testAccessor2() {
38          final Pair<Integer, Double> p = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
39  
40          // Check that both APIs refer to the same data.
41  
42          assertTrue(p.getFirst() == p.getKey());
43          assertTrue(p.getSecond() == p.getValue());
44      }
45  
46      @Test
47      void testEquals() {
48          Pair<Integer, Double> p1 = new Pair<>(null, null);
49          assertNotEquals(null, p1);
50  
51          Pair<Integer, Double> p2 = new Pair<>(null, null);
52          assertEquals(p1, p2);
53  
54          p1 = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
55          assertNotEquals(p1, p2);
56  
57          p2 = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
58          assertEquals(p1, p2);
59  
60          Pair<Integer, Float> p3 = new Pair<>(Integer.valueOf(1), Float.valueOf(2));
61          assertNotEquals(p1, p3);
62      }
63  
64      @Test
65      void testHashCode() {
66          final MyInteger m1 = new MyInteger(1);
67          final MyInteger m2 = new MyInteger(1);
68  
69          final Pair<MyInteger, MyInteger> p1 = new Pair<>(m1, m1);
70          final Pair<MyInteger, MyInteger> p2 = new Pair<>(m2, m2);
71          // Same contents, same hash code.
72          assertEquals(p1.hashCode(), p2.hashCode());
73  
74          // Different contents, different hash codes.
75          m2.set(2);
76          assertFalse(p1.hashCode() == p2.hashCode());
77      }
78  
79      @Test
80      void testToString() {
81          assertEquals("[null, null]", new Pair<>(null, null).toString());
82          assertEquals("[foo, 3]", new Pair<>("foo", 3).toString());
83      }
84  
85      @Test
86      void testCreate() {
87          final Pair<String, Integer> p1 = Pair.create("foo", 3);
88          assertNotNull(p1);
89          final Pair<String, Integer> p2 = new Pair<>("foo", 3);
90          assertEquals(p2, p1);
91      }
92  
93      /**
94       * A mutable integer.
95       */
96      private static class MyInteger {
97          private int i;
98  
99          public MyInteger(int i) {
100             this.i = i;
101         }
102 
103         public void set(int i) {
104             this.i = i;
105         }
106 
107         @Override
108         public boolean equals(Object o) {
109             if (!(o instanceof MyInteger)) {
110                 return false;
111             } else {
112                 return i == ((MyInteger) o).i;
113             }
114         }
115 
116         @Override
117         public int hashCode() {
118             return i;
119         }
120     }
121 }