View Javadoc
1   /*
2    *    Copyright 2009-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.reflection;
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.assertTrue;
21  
22  import java.util.Arrays;
23  
24  import org.junit.jupiter.api.Test;
25  
26  class ArrayUtilTest {
27  
28    @Test
29    void testHashCode() {
30      Object arr = new long[] { 1 };
31      assertEquals(Arrays.hashCode((long[]) arr), ArrayUtil.hashCode(arr));
32      arr = new int[] { 1 };
33      assertEquals(Arrays.hashCode((int[]) arr), ArrayUtil.hashCode(arr));
34      arr = new short[] { 1 };
35      assertEquals(Arrays.hashCode((short[]) arr), ArrayUtil.hashCode(arr));
36      arr = new char[] { 1 };
37      assertEquals(Arrays.hashCode((char[]) arr), ArrayUtil.hashCode(arr));
38      arr = new byte[] { 1 };
39      assertEquals(Arrays.hashCode((byte[]) arr), ArrayUtil.hashCode(arr));
40      arr = new boolean[] { true };
41      assertEquals(Arrays.hashCode((boolean[]) arr), ArrayUtil.hashCode(arr));
42      arr = new float[] { 1f };
43      assertEquals(Arrays.hashCode((float[]) arr), ArrayUtil.hashCode(arr));
44      arr = new double[] { 1d };
45      assertEquals(Arrays.hashCode((double[]) arr), ArrayUtil.hashCode(arr));
46      arr = new Object[] { "str" };
47      assertEquals(Arrays.hashCode((Object[]) arr), ArrayUtil.hashCode(arr));
48  
49      assertEquals(0, ArrayUtil.hashCode(null));
50      assertEquals("str".hashCode(), ArrayUtil.hashCode("str"));
51      assertEquals(Integer.valueOf(1).hashCode(), ArrayUtil.hashCode(1));
52    }
53  
54    @Test
55    void testequals() {
56      assertTrue(ArrayUtil.equals(new long[] { 1 }, new long[] { 1 }));
57      assertTrue(ArrayUtil.equals(new int[] { 1 }, new int[] { 1 }));
58      assertTrue(ArrayUtil.equals(new short[] { 1 }, new short[] { 1 }));
59      assertTrue(ArrayUtil.equals(new char[] { 1 }, new char[] { 1 }));
60      assertTrue(ArrayUtil.equals(new byte[] { 1 }, new byte[] { 1 }));
61      assertTrue(ArrayUtil.equals(new boolean[] { true }, new boolean[] { true }));
62      assertTrue(ArrayUtil.equals(new float[] { 1f }, new float[] { 1f }));
63      assertTrue(ArrayUtil.equals(new double[] { 1d }, new double[] { 1d }));
64      assertTrue(ArrayUtil.equals(new Object[] { "str" }, new Object[] { "str" }));
65  
66      assertFalse(ArrayUtil.equals(new long[] { 1 }, new long[] { 2 }));
67      assertFalse(ArrayUtil.equals(new int[] { 1 }, new int[] { 2 }));
68      assertFalse(ArrayUtil.equals(new short[] { 1 }, new short[] { 2 }));
69      assertFalse(ArrayUtil.equals(new char[] { 1 }, new char[] { 2 }));
70      assertFalse(ArrayUtil.equals(new byte[] { 1 }, new byte[] { 2 }));
71      assertFalse(ArrayUtil.equals(new boolean[] { true }, new boolean[] { false }));
72      assertFalse(ArrayUtil.equals(new float[] { 1f }, new float[] { 2f }));
73      assertFalse(ArrayUtil.equals(new double[] { 1d }, new double[] { 2d }));
74      assertFalse(ArrayUtil.equals(new Object[] { "str" }, new Object[] { "rts" }));
75  
76      assertTrue(ArrayUtil.equals(null, null));
77      assertFalse(ArrayUtil.equals(new long[] { 1 }, null));
78      assertFalse(ArrayUtil.equals(null, new long[] { 1 }));
79  
80      assertTrue(ArrayUtil.equals(1, 1));
81      assertTrue(ArrayUtil.equals("str", "str"));
82    }
83  
84    @Test
85    void testToString() {
86      Object arr = new long[] { 1 };
87      assertEquals(Arrays.toString((long[]) arr), ArrayUtil.toString(arr));
88      arr = new int[] { 1 };
89      assertEquals(Arrays.toString((int[]) arr), ArrayUtil.toString(arr));
90      arr = new short[] { 1 };
91      assertEquals(Arrays.toString((short[]) arr), ArrayUtil.toString(arr));
92      arr = new char[] { 1 };
93      assertEquals(Arrays.toString((char[]) arr), ArrayUtil.toString(arr));
94      arr = new byte[] { 1 };
95      assertEquals(Arrays.toString((byte[]) arr), ArrayUtil.toString(arr));
96      arr = new boolean[] { true };
97      assertEquals(Arrays.toString((boolean[]) arr), ArrayUtil.toString(arr));
98      arr = new float[] { 1f };
99      assertEquals(Arrays.toString((float[]) arr), ArrayUtil.toString(arr));
100     arr = new double[] { 1d };
101     assertEquals(Arrays.toString((double[]) arr), ArrayUtil.toString(arr));
102     arr = new Object[] { "str" };
103     assertEquals(Arrays.toString((Object[]) arr), ArrayUtil.toString(arr));
104 
105     assertEquals(Integer.toString(1), ArrayUtil.toString(1));
106     assertEquals("null", ArrayUtil.toString(null));
107   }
108 
109 }