1/* 2 * Copyright (C) 2010 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 libcore.libcore.util; 18 19import java.lang.ref.Reference; 20import java.lang.ref.WeakReference; 21import java.util.ArrayList; 22import java.util.Arrays; 23import java.util.Collections; 24import java.util.Iterator; 25import java.util.List; 26import junit.framework.TestCase; 27 28import libcore.util.CollectionUtils; 29 30public final class CollectionUtilsTest extends TestCase { 31 32 public void testDereferenceIterable() { 33 List<Reference<String>> refs = new ArrayList<Reference<String>>(); 34 refs.add(newLiteralReference("a")); 35 refs.add(newLiteralReference("b")); 36 refs.add(newLiteralReference("c")); 37 refs.add(newLiteralReference("d")); 38 refs.add(newLiteralReference("e")); 39 40 Iterable<String> strings = CollectionUtils.dereferenceIterable(refs, true); 41 assertEquals(Arrays.<String>asList("a", "b", "c", "d", "e"), toList(strings)); 42 43 refs.get(1).clear(); // b 44 assertEquals(Arrays.<String>asList("a", "c", "d", "e"), toList(strings)); 45 assertEquals(4, refs.size()); 46 47 Iterator<String> i = strings.iterator(); 48 assertEquals("a", i.next()); 49 i.remove(); 50 assertEquals(3, refs.size()); 51 assertEquals("c", i.next()); 52 assertEquals("d", i.next()); 53 assertTrue(i.hasNext()); 54 try { 55 i.remove(); 56 fail("Expected hasNext() to make remove() impossible."); 57 } catch (IllegalStateException expected) { 58 } 59 assertEquals("e", i.next()); 60 i.remove(); 61 assertEquals(2, refs.size()); 62 assertFalse(i.hasNext()); 63 64 refs.get(0).clear(); // c 65 refs.get(1).clear(); // d 66 assertEquals(Arrays.<String>asList(), toList(strings)); 67 } 68 69 private <T> List<T> toList(Iterable<T> iterable) { 70 List<T> result = new ArrayList<T>(); 71 for (T t : iterable) { 72 result.add(t); 73 } 74 return result; 75 } 76 77 public void testRemoveDuplicatesOnEmptyCollection() { 78 List<String> list = new ArrayList<String>(); 79 CollectionUtils.removeDuplicates(list, String.CASE_INSENSITIVE_ORDER); 80 assertTrue(list.isEmpty()); 81 } 82 83 public void testRemoveDuplicatesOnSingletonCollection() { 84 List<String> list = Arrays.asList("A"); 85 CollectionUtils.removeDuplicates(list, String.CASE_INSENSITIVE_ORDER); 86 assertEquals(Collections.singletonList("A"), list); 87 } 88 89 public void testRemoveDuplicates() { 90 List<String> list = new ArrayList<String>(); 91 list.add("A"); 92 list.add("A"); 93 list.add("A"); 94 list.add("B"); 95 list.add("C"); 96 list.add("C"); 97 list.add("C"); 98 CollectionUtils.removeDuplicates(list, String.CASE_INSENSITIVE_ORDER); 99 assertEquals(Arrays.asList("A", "B", "C"), list); 100 } 101 102 /** 103 * A reference that must be manually cleared. 104 */ 105 public Reference<String> newLiteralReference(String s) { 106 return new WeakReference<String>(s); 107 } 108} 109