Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.util; |
| 18 | |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 19 | import static com.android.internal.util.ArrayUtils.concatElements; |
| 20 | |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 21 | import static org.junit.Assert.assertArrayEquals; |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 22 | |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 23 | import junit.framework.TestCase; |
| 24 | |
| 25 | /** |
| 26 | * Tests for {@link ArrayUtils} |
| 27 | */ |
| 28 | public 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 | } |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 84 | |
| 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 Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 96 | assertArrayEquals(new int[] { 1 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 97 | ArrayUtils.appendInt(null, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 98 | assertArrayEquals(new int[] { 1 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 99 | ArrayUtils.appendInt(new int[] { }, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 100 | assertArrayEquals(new int[] { 1, 2 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 101 | ArrayUtils.appendInt(new int[] { 1 }, 2)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 102 | assertArrayEquals(new int[] { 1, 2 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 103 | ArrayUtils.appendInt(new int[] { 1, 2 }, 1)); |
| 104 | } |
| 105 | |
| 106 | public void testRemoveInt() throws Exception { |
| 107 | assertNull(ArrayUtils.removeInt(null, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 108 | assertArrayEquals(new int[] { }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 109 | ArrayUtils.removeInt(new int[] { }, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 110 | assertArrayEquals(new int[] { 1, 2, 3, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 111 | ArrayUtils.removeInt(new int[] { 1, 2, 3}, 4)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 112 | assertArrayEquals(new int[] { 2, 3, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 113 | ArrayUtils.removeInt(new int[] { 1, 2, 3}, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 114 | assertArrayEquals(new int[] { 1, 3, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 115 | ArrayUtils.removeInt(new int[] { 1, 2, 3}, 2)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 116 | assertArrayEquals(new int[] { 1, 2, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 117 | ArrayUtils.removeInt(new int[] { 1, 2, 3}, 3)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 118 | assertArrayEquals(new int[] { 2, 3, 1 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 119 | 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 Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 133 | assertArrayEquals(new long[] { 1 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 134 | ArrayUtils.appendLong(null, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 135 | assertArrayEquals(new long[] { 1 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 136 | ArrayUtils.appendLong(new long[] { }, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 137 | assertArrayEquals(new long[] { 1, 2 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 138 | ArrayUtils.appendLong(new long[] { 1 }, 2)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 139 | assertArrayEquals(new long[] { 1, 2 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 140 | ArrayUtils.appendLong(new long[] { 1, 2 }, 1)); |
| 141 | } |
| 142 | |
| 143 | public void testRemoveLong() throws Exception { |
| 144 | assertNull(ArrayUtils.removeLong(null, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 145 | assertArrayEquals(new long[] { }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 146 | ArrayUtils.removeLong(new long[] { }, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 147 | assertArrayEquals(new long[] { 1, 2, 3, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 148 | ArrayUtils.removeLong(new long[] { 1, 2, 3}, 4)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 149 | assertArrayEquals(new long[] { 2, 3, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 150 | ArrayUtils.removeLong(new long[] { 1, 2, 3}, 1)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 151 | assertArrayEquals(new long[] { 1, 3, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 152 | ArrayUtils.removeLong(new long[] { 1, 2, 3}, 2)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 153 | assertArrayEquals(new long[] { 1, 2, }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 154 | ArrayUtils.removeLong(new long[] { 1, 2, 3}, 3)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 155 | assertArrayEquals(new long[] { 2, 3, 1 }, |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 156 | ArrayUtils.removeLong(new long[] { 1, 2, 3, 1 }, 1)); |
| 157 | } |
| 158 | |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 159 | public void testConcatEmpty() throws Exception { |
| 160 | assertArrayEquals(new Long[] {}, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 161 | concatElements(Long.class, null, null)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 162 | assertArrayEquals(new Long[] {}, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 163 | concatElements(Long.class, new Long[] {}, null)); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 164 | assertArrayEquals(new Long[] {}, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 165 | concatElements(Long.class, null, new Long[] {})); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 166 | assertArrayEquals(new Long[] {}, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 167 | concatElements(Long.class, new Long[] {}, new Long[] {})); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 168 | } |
| 169 | |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 170 | public void testconcatElements() throws Exception { |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 171 | assertArrayEquals(new Long[] { 1L }, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 172 | concatElements(Long.class, new Long[] { 1L }, new Long[] {})); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 173 | assertArrayEquals(new Long[] { 1L }, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 174 | concatElements(Long.class, new Long[] {}, new Long[] { 1L })); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 175 | assertArrayEquals(new Long[] { 1L, 2L }, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 176 | concatElements(Long.class, new Long[] { 1L }, new Long[] { 2L })); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 177 | assertArrayEquals(new Long[] { 1L, 2L, 3L, 4L }, |
Jeff Sharkey | 0da0483 | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 178 | concatElements(Long.class, new Long[] { 1L, 2L }, new Long[] { 3L, 4L })); |
Jeff Sharkey | 3e26b7d | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 179 | } |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 180 | } |