• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.inputmethod.latin.utils;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.inputmethod.latin.common.CollectionUtils;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 
39 /**
40  * Tests for {@link CollectionUtils}.
41  */
42 @SmallTest
43 @RunWith(AndroidJUnit4.class)
44 public class CollectionUtilsTests {
45     /**
46      * Tests that {@link CollectionUtils#arrayAsList(Object[],int,int)} fails as expected
47      * with some invalid inputs.
48      */
49     @Test
testArrayAsListFailure()50     public void testArrayAsListFailure() {
51         final String[] array = { "0", "1" };
52         // Negative start
53         try {
54             CollectionUtils.arrayAsList(array, -1, 1);
55             fail("Failed to catch start < 0");
56         } catch (final IllegalArgumentException e) {
57             assertEquals("Invalid start: -1 end: 1 with array.length: 2", e.getMessage());
58         }
59         // start > end
60         try {
61             CollectionUtils.arrayAsList(array, 1, -1);
62             fail("Failed to catch start > end");
63         } catch (final IllegalArgumentException e) {
64             assertEquals("Invalid start: 1 end: -1 with array.length: 2", e.getMessage());
65         }
66         // end > array.length
67         try {
68             CollectionUtils.arrayAsList(array, 1, 3);
69             fail("Failed to catch end > array.length");
70         } catch (final IllegalArgumentException e) {
71             assertEquals("Invalid start: 1 end: 3 with array.length: 2", e.getMessage());
72         }
73     }
74 
75     /**
76      * Tests that {@link CollectionUtils#arrayAsList(Object[],int,int)} gives the expected
77      * results for a few valid inputs.
78      */
79     @Test
testArrayAsList()80     public void testArrayAsList() {
81         final ArrayList<String> empty = new ArrayList<>();
82         assertEquals(empty, CollectionUtils.arrayAsList(new String[] {}, 0, 0));
83         final String[] array = { "0", "1", "2", "3", "4" };
84         assertEquals(empty, CollectionUtils.arrayAsList(array, 0, 0));
85         assertEquals(empty, CollectionUtils.arrayAsList(array, 1, 1));
86         assertEquals(empty, CollectionUtils.arrayAsList(array, array.length, array.length));
87         final ArrayList<String> expected123 = new ArrayList<>(Arrays.asList("1", "2", "3"));
88         assertEquals(expected123, CollectionUtils.arrayAsList(array, 1, 4));
89     }
90 
91     /**
92      * Tests that {@link CollectionUtils#isNullOrEmpty(java.util.Collection)} gives the expected
93      * results for a few cases.
94      */
95     @Test
testIsNullOrEmpty()96     public void testIsNullOrEmpty() {
97         assertTrue(CollectionUtils.isNullOrEmpty((List<String>) null));
98         assertTrue(CollectionUtils.isNullOrEmpty((Map<String, String>) null));
99         assertTrue(CollectionUtils.isNullOrEmpty(new ArrayList<String>()));
100         assertTrue(CollectionUtils.isNullOrEmpty(new HashMap<String, String>()));
101         assertFalse(CollectionUtils.isNullOrEmpty(Collections.singletonList("Not empty")));
102         assertFalse(CollectionUtils.isNullOrEmpty(Collections.singletonMap("Not", "empty")));
103     }
104 }
105