wangzhibo
2026-08-16 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package nodomain.freeyourgadget.gadgetbridge.test;
 
import org.junit.Test;
 
import nodomain.freeyourgadget.gadgetbridge.util.ArrayUtils;
 
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
 
/**
 * Tests ArrayUtils
 */
public class ArrayUtilsTest extends TestBase {
 
    private static final byte[] EMPTY = new byte[0];
    private static final byte[] DATA_5 = new byte[] { 1, 2, 3, 4, 5};
 
    public ArrayUtilsTest() throws Exception {
    }
 
    @Test
    public void testEqualsException1() throws Exception {
        try {
            ArrayUtils.equals(null, EMPTY, 0);
            fail("equals should throw on bad argument");
        } catch (IllegalArgumentException ex) {
            // expected
        }
    }
 
    @Test
    public void testEqualsException2() throws Exception {
        try {
            ArrayUtils.equals(EMPTY, null, 0);
            fail("equals should throw on bad argument");
        } catch (IllegalArgumentException ex) {
            // expected
        }
    }
 
    @Test
    public void testEqualsException5() throws Exception {
        try {
            ArrayUtils.equals(EMPTY, EMPTY, -1);
            fail("equals should throw on bad argument");
        } catch (IllegalArgumentException ex) {
            // expected
        }
    }
 
    @Test
    public void testEqualsEmptyData() throws Exception {
        assertFalse(ArrayUtils.equals(EMPTY, DATA_5, 0));
    }
 
    @Test
    public void testEqualsEmptyTest() throws Exception {
        // testing for 0 equal bytes may be senseless, but always true
        assertTrue(ArrayUtils.equals(EMPTY, EMPTY, 0));
        assertTrue(ArrayUtils.equals(DATA_5, EMPTY, 0));
    }
 
    @Test
    public void testEquals1() throws Exception {
        assertTrue(ArrayUtils.equals(DATA_5, b(1), 0));
    }
 
    @Test
    public void testEquals2() throws Exception {
        assertTrue(ArrayUtils.equals(DATA_5, b(2), 1));
    }
 
    @Test
    public void testEquals5() throws Exception {
        assertTrue(ArrayUtils.equals(DATA_5, b(5), 4));
    }
 
    @Test
    public void testEqualsOutOfRange() throws Exception {
        assertFalse(ArrayUtils.equals(DATA_5, b(5), 5));
    }
 
    @Test
    public void testEquals123() throws Exception {
        assertTrue(ArrayUtils.equals(DATA_5, new byte[] {1, 2, 3}, 0));
    }
 
    @Test
    public void testEquals234() throws Exception {
        assertTrue(ArrayUtils.equals(DATA_5, new byte[] {2, 3, 4}, 1));
    }
 
    @Test
    public void testEquals345() throws Exception {
        assertTrue(ArrayUtils.equals(DATA_5, new byte[] {3, 4, 5}, 2));
    }
 
    @Test
    public void testEquals12345() throws Exception {
        assertTrue(ArrayUtils.equals(DATA_5, DATA_5, 0));
    }
 
    @Test
    public void testEqualsWrongStart() throws Exception {
        assertFalse(ArrayUtils.equals(DATA_5, new byte[] {0, 2, 3}, 0));
    }
 
    @Test
    public void testEqualsWrongEnd() throws Exception {
        assertFalse(ArrayUtils.equals(DATA_5, new byte[] {3, 4, 6}, 2));
    }
 
    @Test
    public void testStartsWith1() throws Exception {
        assertTrue(ArrayUtils.startsWith(DATA_5, new byte[] {1}));
    }
 
    @Test
    public void testStartsWith2() throws Exception {
        assertTrue(ArrayUtils.startsWith(DATA_5, new byte[] {1, 2}));
    }
 
    @Test
    public void testStartsWithAll() throws Exception {
        assertTrue(ArrayUtils.startsWith(DATA_5, DATA_5.clone()));
    }
 
    @Test
    public void testStartsWithEmpty() throws Exception {
        assertTrue(ArrayUtils.startsWith(DATA_5, EMPTY));
    }
 
    @Test
    public void testStartsWithFail1() throws Exception {
        try {
            ArrayUtils.startsWith(DATA_5, null);
            fail("should have thrown an exception");
        } catch (IllegalArgumentException ex) {
            // expected
        }
    }
 
    @Test
    public void testStartsWithFail3() throws Exception {
        assertFalse(ArrayUtils.startsWith(DATA_5, new byte[] {2, 3}));
    }
 
    @Test
    public void testStartsWithFail4() throws Exception {
        assertFalse(ArrayUtils.startsWith(DATA_5, new byte[] {1, 2, 3, 4, 5, 6}));
    }
 
    private byte[] b(int b) {
        return new byte[] {(byte) b};
    }
}