1+ /*
2+ * Copyright 2018 dc-square and the HiveMQ MQTT Client Project
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+ * http://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+ */
17+
18+ package com .hivemq .client .internal .util ;
19+
20+ import org .junit .jupiter .api .Test ;
21+
22+ import java .util .Arrays ;
23+ import java .util .Random ;
24+
25+ import static org .junit .jupiter .api .Assertions .*;
26+
27+ /**
28+ * @author Silvio Giebl
29+ */
30+ class ByteArrayUtilTest {
31+
32+ @ Test
33+ void equals () {
34+ final byte [] bytes = new byte [100 ];
35+ final Random random = new Random ();
36+ random .nextBytes (bytes );
37+ final byte [] clone = bytes .clone ();
38+ assertTrue (ByteArrayUtil .equals (bytes , 0 , bytes .length , bytes , 0 , bytes .length ));
39+ assertTrue (ByteArrayUtil .equals (bytes , 0 , bytes .length , clone , 0 , clone .length ));
40+ assertTrue (ByteArrayUtil .equals (bytes , 10 , bytes .length , clone , 10 , clone .length ));
41+ assertTrue (ByteArrayUtil .equals (bytes , 0 , 20 , clone , 0 , 20 ));
42+ assertTrue (ByteArrayUtil .equals (bytes , 10 , 20 , clone , 10 , 20 ));
43+ }
44+
45+ @ Test
46+ void equals_differentOffset () {
47+ final byte [] bytes = new byte [100 ];
48+ final Random random = new Random ();
49+ random .nextBytes (bytes );
50+ final byte [] clone = Arrays .copyOfRange (bytes , 50 , 100 );
51+ assertTrue (ByteArrayUtil .equals (bytes , 50 , 100 , clone , 0 , 50 ));
52+ assertTrue (ByteArrayUtil .equals (bytes , 60 , 90 , clone , 10 , 40 ));
53+ assertFalse (ByteArrayUtil .equals (bytes , 0 , 50 , clone , 0 , 50 ));
54+ assertFalse (ByteArrayUtil .equals (bytes , 10 , 40 , clone , 10 , 40 ));
55+ }
56+
57+ @ Test
58+ void equals_differentLength () {
59+ final byte [] bytes = new byte [100 ];
60+ final Random random = new Random ();
61+ random .nextBytes (bytes );
62+ final byte [] clone = bytes .clone ();
63+ assertFalse (ByteArrayUtil .equals (bytes , 0 , bytes .length , bytes , 0 , bytes .length - 1 ));
64+ assertFalse (ByteArrayUtil .equals (bytes , 0 , bytes .length - 1 , bytes , 0 , bytes .length ));
65+ assertFalse (ByteArrayUtil .equals (bytes , 0 , bytes .length , clone , 0 , clone .length - 1 ));
66+ assertFalse (ByteArrayUtil .equals (bytes , 0 , bytes .length - 1 , clone , 0 , clone .length ));
67+ assertFalse (ByteArrayUtil .equals (bytes , 10 , bytes .length , clone , 10 , clone .length - 1 ));
68+ assertFalse (ByteArrayUtil .equals (bytes , 10 , bytes .length - 1 , clone , 10 , clone .length ));
69+ assertFalse (ByteArrayUtil .equals (bytes , 0 , 20 , clone , 0 , 20 - 1 ));
70+ assertFalse (ByteArrayUtil .equals (bytes , 0 , 20 - 1 , clone , 0 , 20 ));
71+ assertFalse (ByteArrayUtil .equals (bytes , 10 , 20 , clone , 10 , 20 - 1 ));
72+ assertFalse (ByteArrayUtil .equals (bytes , 10 , 20 - 1 , clone , 10 , 20 ));
73+ }
74+
75+ @ Test
76+ void hashCode_sameAsArrays () {
77+ final byte [] bytes = new byte [100 ];
78+ final Random random = new Random ();
79+ random .nextBytes (bytes );
80+ assertEquals (
81+ Arrays .hashCode (Arrays .copyOfRange (bytes , 0 , bytes .length )),
82+ ByteArrayUtil .hashCode (bytes , 0 , bytes .length ));
83+ assertEquals (
84+ Arrays .hashCode (Arrays .copyOfRange (bytes , 10 , bytes .length )),
85+ ByteArrayUtil .hashCode (bytes , 10 , bytes .length ));
86+ assertEquals (Arrays .hashCode (Arrays .copyOfRange (bytes , 0 , 20 )), ByteArrayUtil .hashCode (bytes , 0 , 20 ));
87+ assertEquals (Arrays .hashCode (Arrays .copyOfRange (bytes , 10 , 20 )), ByteArrayUtil .hashCode (bytes , 10 , 20 ));
88+ }
89+
90+ @ Test
91+ void indexOf () {
92+ final byte [] bytes = {0 , 1 , 2 , 3 , 4 , 5 };
93+ for (byte b = 0 ; b < 6 ; b ++) {
94+ assertEquals (b , ByteArrayUtil .indexOf (bytes , 0 , b ));
95+ }
96+ assertEquals (bytes .length , ByteArrayUtil .indexOf (bytes , 0 , (byte ) 123 ));
97+ }
98+
99+ @ Test
100+ void indexOf_offset () {
101+ final byte [] bytes = {12 , 12 , 12 , 0 , 1 , 2 , 3 , 4 , 5 };
102+ for (byte b = 0 ; b < 6 ; b ++) {
103+ assertEquals (3 + b , ByteArrayUtil .indexOf (bytes , 3 , b ));
104+ }
105+ assertEquals (bytes .length , ByteArrayUtil .indexOf (bytes , 0 , (byte ) 123 ));
106+ }
107+ }
0 commit comments