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