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.assertNotEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  
23  /**
24   * Test for {@link Pair}.
25   */
26  class PairTest {
27  
28      @Test
29      void testAccessor() {
30          final Pair<Integer, Double> p = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
31          assertEquals(Integer.valueOf(1), p.getKey());
32          assertEquals(2, p.getValue().doubleValue(), Math.ulp(1d));
33      }
34  
35      @Test
36      void testAccessor2() {
37          final Pair<Integer, Double> p = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
38  
39          // Check that both APIs refer to the same data.
40  
41          assertSame(p.getFirst(), p.getKey());
42          assertSame(p.getSecond(), p.getValue());
43      }
44  
45      @Test
46      void testEquals() {
47          Pair<Integer, Double> p1 = new Pair<>(null, null);
48          assertNotEquals(null, p1);
49  
50          Pair<Integer, Double> p2 = new Pair<>(null, null);
51          assertEquals(p1, p2);
52  
53          p1 = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
54          assertNotEquals(p1, p2);
55  
56          p2 = new Pair<>(Integer.valueOf(1), Double.valueOf(2));
57          assertEquals(p1, p2);
58  
59          Pair<Integer, Float> p3 = new Pair<>(Integer.valueOf(1), Float.valueOf(2));
60          assertNotEquals(p1, p3);
61      }
62  
63      @Test
64      void testHashCode() {
65          final MyInteger m1 = new MyInteger(1);
66          final MyInteger m2 = new MyInteger(1);
67  
68          final Pair<MyInteger, MyInteger> p1 = new Pair<>(m1, m1);
69          final Pair<MyInteger, MyInteger> p2 = new Pair<>(m2, m2);
70          // Same contents, same hash code.
71          assertEquals(p1.hashCode(), p2.hashCode());
72  
73          // Different contents, different hash codes.
74          m2.set(2);
75          assertNotEquals(p1.hashCode(), p2.hashCode());
76      }
77  
78      @Test
79      void testToString() {
80          assertEquals("[null, null]", new Pair<>(null, null).toString());
81          assertEquals("[foo, 3]", new Pair<>("foo", 3).toString());
82      }
83  
84      @Test
85      void testCreate() {
86          final Pair<String, Integer> p1 = Pair.create("foo", 3);
87          assertNotNull(p1);
88          final Pair<String, Integer> p2 = new Pair<>("foo", 3);
89          assertEquals(p2, p1);
90      }
91  
92      /**
93       * A mutable integer.
94       */
95      private static class MyInteger {
96          private int i;
97  
98          public MyInteger(int i) {
99              this.i = i;
100         }
101 
102         public void set(int i) {
103             this.i = i;
104         }
105 
106         @Override
107         public boolean equals(Object o) {
108             if (!(o instanceof MyInteger)) {
109                 return false;
110             } else {
111                 return i == ((MyInteger) o).i;
112             }
113         }
114 
115         @Override
116         public int hashCode() {
117             return i;
118         }
119     }
120 }