View Javadoc
1   /*
2    *    Copyright 2009-2023 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;
31      arr = new long[] { 1 };
32      assertEquals(Arrays.hashCode((long[]) arr), ArrayUtil.hashCode(arr));
33      arr = new int[] { 1 };
34      assertEquals(Arrays.hashCode((int[]) arr), ArrayUtil.hashCode(arr));
35      arr = new short[] { 1 };
36      assertEquals(Arrays.hashCode((short[]) arr), ArrayUtil.hashCode(arr));
37      arr = new char[] { 1 };
38      assertEquals(Arrays.hashCode((char[]) arr), ArrayUtil.hashCode(arr));
39      arr = new byte[] { 1 };
40      assertEquals(Arrays.hashCode((byte[]) arr), ArrayUtil.hashCode(arr));
41      arr = new boolean[] { true };
42      assertEquals(Arrays.hashCode((boolean[]) arr), ArrayUtil.hashCode(arr));
43      arr = new float[] { 1f };
44      assertEquals(Arrays.hashCode((float[]) arr), ArrayUtil.hashCode(arr));
45      arr = new double[] { 1d };
46      assertEquals(Arrays.hashCode((double[]) arr), ArrayUtil.hashCode(arr));
47      arr = new Object[] { "str" };
48      assertEquals(Arrays.hashCode((Object[]) arr), ArrayUtil.hashCode(arr));
49  
50      assertEquals(0, ArrayUtil.hashCode(null));
51      assertEquals("str".hashCode(), ArrayUtil.hashCode("str"));
52      assertEquals(Integer.valueOf(1).hashCode(), ArrayUtil.hashCode(1));
53    }
54  
55    @Test
56    void testequals() {
57      assertTrue(ArrayUtil.equals(new long[] { 1 }, new long[] { 1 }));
58      assertTrue(ArrayUtil.equals(new int[] { 1 }, new int[] { 1 }));
59      assertTrue(ArrayUtil.equals(new short[] { 1 }, new short[] { 1 }));
60      assertTrue(ArrayUtil.equals(new char[] { 1 }, new char[] { 1 }));
61      assertTrue(ArrayUtil.equals(new byte[] { 1 }, new byte[] { 1 }));
62      assertTrue(ArrayUtil.equals(new boolean[] { true }, new boolean[] { true }));
63      assertTrue(ArrayUtil.equals(new float[] { 1f }, new float[] { 1f }));
64      assertTrue(ArrayUtil.equals(new double[] { 1d }, new double[] { 1d }));
65      assertTrue(ArrayUtil.equals(new Object[] { "str" }, new Object[] { "str" }));
66  
67      assertFalse(ArrayUtil.equals(new long[] { 1 }, new long[] { 2 }));
68      assertFalse(ArrayUtil.equals(new int[] { 1 }, new int[] { 2 }));
69      assertFalse(ArrayUtil.equals(new short[] { 1 }, new short[] { 2 }));
70      assertFalse(ArrayUtil.equals(new char[] { 1 }, new char[] { 2 }));
71      assertFalse(ArrayUtil.equals(new byte[] { 1 }, new byte[] { 2 }));
72      assertFalse(ArrayUtil.equals(new boolean[] { true }, new boolean[] { false }));
73      assertFalse(ArrayUtil.equals(new float[] { 1f }, new float[] { 2f }));
74      assertFalse(ArrayUtil.equals(new double[] { 1d }, new double[] { 2d }));
75      assertFalse(ArrayUtil.equals(new Object[] { "str" }, new Object[] { "rts" }));
76  
77      assertTrue(ArrayUtil.equals(null, null));
78      assertFalse(ArrayUtil.equals(new long[] { 1 }, null));
79      assertFalse(ArrayUtil.equals(null, new long[] { 1 }));
80  
81      assertTrue(ArrayUtil.equals(1, 1));
82      assertTrue(ArrayUtil.equals("str", "str"));
83    }
84  
85    @Test
86    void testToString() {
87      Object arr;
88      arr = new long[] { 1 };
89      assertEquals(Arrays.toString((long[]) arr), ArrayUtil.toString(arr));
90      arr = new int[] { 1 };
91      assertEquals(Arrays.toString((int[]) arr), ArrayUtil.toString(arr));
92      arr = new short[] { 1 };
93      assertEquals(Arrays.toString((short[]) arr), ArrayUtil.toString(arr));
94      arr = new char[] { 1 };
95      assertEquals(Arrays.toString((char[]) arr), ArrayUtil.toString(arr));
96      arr = new byte[] { 1 };
97      assertEquals(Arrays.toString((byte[]) arr), ArrayUtil.toString(arr));
98      arr = new boolean[] { true };
99      assertEquals(Arrays.toString((boolean[]) arr), ArrayUtil.toString(arr));
100     arr = new float[] { 1f };
101     assertEquals(Arrays.toString((float[]) arr), ArrayUtil.toString(arr));
102     arr = new double[] { 1d };
103     assertEquals(Arrays.toString((double[]) arr), ArrayUtil.toString(arr));
104     arr = new Object[] { "str" };
105     assertEquals(Arrays.toString((Object[]) arr), ArrayUtil.toString(arr));
106 
107     assertEquals(Integer.toString(1), ArrayUtil.toString(1));
108     assertEquals("null", ArrayUtil.toString(null));
109   }
110 
111 }