blob: 39bb84a20d7a56bb1e6e006d993d6ae80f57a03f [file] [log] [blame]
Jeff Sharkey94c91dc2013-03-06 16:25:50 -08001/*
2 * Copyright (C) 2013 The Android Open Source 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
17package com.android.internal.util;
18
Jeff Sharkey0da04832018-07-26 14:36:59 -060019import static com.android.internal.util.ArrayUtils.concatElements;
20
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -060021import static org.junit.Assert.assertArrayEquals;
dcashman874d0d42014-05-30 09:34:04 -070022
Jeff Sharkey94c91dc2013-03-06 16:25:50 -080023import junit.framework.TestCase;
24
25/**
26 * Tests for {@link ArrayUtils}
27 */
28public class ArrayUtilsTest extends TestCase {
29 public void testContains() throws Exception {
30 final Object A = new Object();
31 final Object B = new Object();
32 final Object C = new Object();
33 final Object D = new Object();
34
35 assertTrue(ArrayUtils.contains(new Object[] { A, B, C }, A));
36 assertTrue(ArrayUtils.contains(new Object[] { A, B, C }, B));
37 assertTrue(ArrayUtils.contains(new Object[] { A, B, C }, C));
38 assertTrue(ArrayUtils.contains(new Object[] { A, null, C }, null));
39
40 assertFalse(ArrayUtils.contains(new Object[] { A, B, C }, null));
41 assertFalse(ArrayUtils.contains(new Object[] { }, null));
42 assertFalse(ArrayUtils.contains(new Object[] { null }, A));
43 }
44
45 public void testIndexOf() throws Exception {
46 final Object A = new Object();
47 final Object B = new Object();
48 final Object C = new Object();
49 final Object D = new Object();
50
51 assertEquals(0, ArrayUtils.indexOf(new Object[] { A, B, C }, A));
52 assertEquals(1, ArrayUtils.indexOf(new Object[] { A, B, C }, B));
53 assertEquals(2, ArrayUtils.indexOf(new Object[] { A, B, C }, C));
54 assertEquals(-1, ArrayUtils.indexOf(new Object[] { A, B, C }, D));
55
56 assertEquals(-1, ArrayUtils.indexOf(new Object[] { A, B, C }, null));
57 assertEquals(-1, ArrayUtils.indexOf(new Object[] { }, A));
58 assertEquals(-1, ArrayUtils.indexOf(new Object[] { }, null));
59
60 assertEquals(0, ArrayUtils.indexOf(new Object[] { null, null }, null));
61 assertEquals(1, ArrayUtils.indexOf(new Object[] { A, null, B }, null));
62 assertEquals(2, ArrayUtils.indexOf(new Object[] { A, null, B }, B));
63 }
64
65 public void testContainsAll() throws Exception {
66 final Object A = new Object();
67 final Object B = new Object();
68 final Object C = new Object();
69
70 assertTrue(ArrayUtils.containsAll(new Object[] { C, B, A }, new Object[] { A, B, C }));
71 assertTrue(ArrayUtils.containsAll(new Object[] { A, B }, new Object[] { A }));
72 assertTrue(ArrayUtils.containsAll(new Object[] { A }, new Object[] { A }));
73 assertTrue(ArrayUtils.containsAll(new Object[] { A }, new Object[] { }));
74 assertTrue(ArrayUtils.containsAll(new Object[] { }, new Object[] { }));
75 assertTrue(ArrayUtils.containsAll(new Object[] { null }, new Object[] { }));
76 assertTrue(ArrayUtils.containsAll(new Object[] { null }, new Object[] { null }));
77 assertTrue(ArrayUtils.containsAll(new Object[] { A, null, C }, new Object[] { C, null }));
78
79 assertFalse(ArrayUtils.containsAll(new Object[] { }, new Object[] { A }));
80 assertFalse(ArrayUtils.containsAll(new Object[] { B }, new Object[] { A }));
81 assertFalse(ArrayUtils.containsAll(new Object[] { }, new Object[] { null }));
82 assertFalse(ArrayUtils.containsAll(new Object[] { A }, new Object[] { null }));
83 }
dcashman874d0d42014-05-30 09:34:04 -070084
85 public void testContainsInt() throws Exception {
86 assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 1));
87 assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 2));
88 assertTrue(ArrayUtils.contains(new int[] { 1, 2, 3 }, 3));
89
90 assertFalse(ArrayUtils.contains(new int[] { 1, 2, 3 }, 0));
91 assertFalse(ArrayUtils.contains(new int[] { 1, 2, 3 }, 4));
92 assertFalse(ArrayUtils.contains(new int[] { }, 2));
93 }
94
95 public void testAppendInt() throws Exception {
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -060096 assertArrayEquals(new int[] { 1 },
dcashman874d0d42014-05-30 09:34:04 -070097 ArrayUtils.appendInt(null, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -060098 assertArrayEquals(new int[] { 1 },
dcashman874d0d42014-05-30 09:34:04 -070099 ArrayUtils.appendInt(new int[] { }, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600100 assertArrayEquals(new int[] { 1, 2 },
dcashman874d0d42014-05-30 09:34:04 -0700101 ArrayUtils.appendInt(new int[] { 1 }, 2));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600102 assertArrayEquals(new int[] { 1, 2 },
dcashman874d0d42014-05-30 09:34:04 -0700103 ArrayUtils.appendInt(new int[] { 1, 2 }, 1));
104 }
105
106 public void testRemoveInt() throws Exception {
107 assertNull(ArrayUtils.removeInt(null, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600108 assertArrayEquals(new int[] { },
dcashman874d0d42014-05-30 09:34:04 -0700109 ArrayUtils.removeInt(new int[] { }, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600110 assertArrayEquals(new int[] { 1, 2, 3, },
dcashman874d0d42014-05-30 09:34:04 -0700111 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 4));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600112 assertArrayEquals(new int[] { 2, 3, },
dcashman874d0d42014-05-30 09:34:04 -0700113 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600114 assertArrayEquals(new int[] { 1, 3, },
dcashman874d0d42014-05-30 09:34:04 -0700115 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 2));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600116 assertArrayEquals(new int[] { 1, 2, },
dcashman874d0d42014-05-30 09:34:04 -0700117 ArrayUtils.removeInt(new int[] { 1, 2, 3}, 3));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600118 assertArrayEquals(new int[] { 2, 3, 1 },
dcashman874d0d42014-05-30 09:34:04 -0700119 ArrayUtils.removeInt(new int[] { 1, 2, 3, 1 }, 1));
120 }
121
122 public void testContainsLong() throws Exception {
123 assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 1));
124 assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 2));
125 assertTrue(ArrayUtils.contains(new long[] { 1, 2, 3 }, 3));
126
127 assertFalse(ArrayUtils.contains(new long[] { 1, 2, 3 }, 0));
128 assertFalse(ArrayUtils.contains(new long[] { 1, 2, 3 }, 4));
129 assertFalse(ArrayUtils.contains(new long[] { }, 2));
130 }
131
132 public void testAppendLong() throws Exception {
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600133 assertArrayEquals(new long[] { 1 },
dcashman874d0d42014-05-30 09:34:04 -0700134 ArrayUtils.appendLong(null, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600135 assertArrayEquals(new long[] { 1 },
dcashman874d0d42014-05-30 09:34:04 -0700136 ArrayUtils.appendLong(new long[] { }, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600137 assertArrayEquals(new long[] { 1, 2 },
dcashman874d0d42014-05-30 09:34:04 -0700138 ArrayUtils.appendLong(new long[] { 1 }, 2));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600139 assertArrayEquals(new long[] { 1, 2 },
dcashman874d0d42014-05-30 09:34:04 -0700140 ArrayUtils.appendLong(new long[] { 1, 2 }, 1));
141 }
142
143 public void testRemoveLong() throws Exception {
144 assertNull(ArrayUtils.removeLong(null, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600145 assertArrayEquals(new long[] { },
dcashman874d0d42014-05-30 09:34:04 -0700146 ArrayUtils.removeLong(new long[] { }, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600147 assertArrayEquals(new long[] { 1, 2, 3, },
dcashman874d0d42014-05-30 09:34:04 -0700148 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 4));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600149 assertArrayEquals(new long[] { 2, 3, },
dcashman874d0d42014-05-30 09:34:04 -0700150 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 1));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600151 assertArrayEquals(new long[] { 1, 3, },
dcashman874d0d42014-05-30 09:34:04 -0700152 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 2));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600153 assertArrayEquals(new long[] { 1, 2, },
dcashman874d0d42014-05-30 09:34:04 -0700154 ArrayUtils.removeLong(new long[] { 1, 2, 3}, 3));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600155 assertArrayEquals(new long[] { 2, 3, 1 },
dcashman874d0d42014-05-30 09:34:04 -0700156 ArrayUtils.removeLong(new long[] { 1, 2, 3, 1 }, 1));
157 }
158
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600159 public void testConcatEmpty() throws Exception {
160 assertArrayEquals(new Long[] {},
Jeff Sharkey0da04832018-07-26 14:36:59 -0600161 concatElements(Long.class, null, null));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600162 assertArrayEquals(new Long[] {},
Jeff Sharkey0da04832018-07-26 14:36:59 -0600163 concatElements(Long.class, new Long[] {}, null));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600164 assertArrayEquals(new Long[] {},
Jeff Sharkey0da04832018-07-26 14:36:59 -0600165 concatElements(Long.class, null, new Long[] {}));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600166 assertArrayEquals(new Long[] {},
Jeff Sharkey0da04832018-07-26 14:36:59 -0600167 concatElements(Long.class, new Long[] {}, new Long[] {}));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600168 }
169
Jeff Sharkey0da04832018-07-26 14:36:59 -0600170 public void testconcatElements() throws Exception {
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600171 assertArrayEquals(new Long[] { 1L },
Jeff Sharkey0da04832018-07-26 14:36:59 -0600172 concatElements(Long.class, new Long[] { 1L }, new Long[] {}));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600173 assertArrayEquals(new Long[] { 1L },
Jeff Sharkey0da04832018-07-26 14:36:59 -0600174 concatElements(Long.class, new Long[] {}, new Long[] { 1L }));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600175 assertArrayEquals(new Long[] { 1L, 2L },
Jeff Sharkey0da04832018-07-26 14:36:59 -0600176 concatElements(Long.class, new Long[] { 1L }, new Long[] { 2L }));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600177 assertArrayEquals(new Long[] { 1L, 2L, 3L, 4L },
Jeff Sharkey0da04832018-07-26 14:36:59 -0600178 concatElements(Long.class, new Long[] { 1L, 2L }, new Long[] { 3L, 4L }));
Jeff Sharkey3e26b7d2018-07-12 19:47:49 -0600179 }
Jeff Sharkey94c91dc2013-03-06 16:25:50 -0800180}