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.Assert;
17  import org.junit.Test;
18  
19  /**
20   * Test for {@link Pair}.
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          // Check that both APIs refer to the same data.
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          // Same contents, same hash code.
67          Assert.assertTrue(p1.hashCode() == p2.hashCode());
68  
69          // Different contents, different hash codes.
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       * A mutable integer.
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 }