1cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath/*
2cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  Licensed to the Apache Software Foundation (ASF) under one or more
3cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  contributor license agreements.  See the NOTICE file distributed with
4cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  this work for additional information regarding copyright ownership.
5cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  The ASF licenses this file to You under the Apache License, Version 2.0
6cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  (the "License"); you may not use this file except in compliance with
7cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  the License.  You may obtain a copy of the License at
8cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
9cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *     http://www.apache.org/licenses/LICENSE-2.0
10cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
11cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  Unless required by applicable law or agreed to in writing, software
12cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  distributed under the License is distributed on an "AS IS" BASIS,
13cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  See the License for the specific language governing permissions and
15cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  limitations under the License.
16cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */
17cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
18ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamathpackage org.apache.harmony.tests.java.util;
19cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
20cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.Arrays;
21cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.Comparator;
22cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.HashSet;
23cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.Iterator;
24cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.Set;
25cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.SortedSet;
26cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.TreeSet;
27cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
28cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathpublic class TreeSetTest extends junit.framework.TestCase {
29cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
30cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public static class ReversedIntegerComparator implements Comparator {
31cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        public int compare(Object o1, Object o2) {
32cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            return -(((Integer) o1).compareTo((Integer) o2));
33cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
34cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
35cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        public boolean equals(Object o1, Object o2) {
36cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            return ((Integer) o1).compareTo((Integer) o2) == 0;
37cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
38cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
39cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
40cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    TreeSet ts;
41cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
42cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    Object objArray[] = new Object[1000];
43cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
44cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
45cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#TreeSet()
46cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
47cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_Constructor() {
48cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.TreeSet()
49cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Did not construct correct TreeSet", new TreeSet().isEmpty());
50cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
51cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
52cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
53cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#TreeSet(java.util.Collection)
54cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
55cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_ConstructorLjava_util_Collection() {
56cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.TreeSet(java.util.Collection)
57cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet myTreeSet = new TreeSet(Arrays.asList(objArray));
58cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("TreeSet incorrect size",
59cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                myTreeSet.size() == objArray.length);
60cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        for (int counter = 0; counter < objArray.length; counter++)
61cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            assertTrue("TreeSet does not contain correct elements", myTreeSet
62cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                    .contains(objArray[counter]));
63cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
64cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
65cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
66cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#TreeSet(java.util.Comparator)
67cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
68cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_ConstructorLjava_util_Comparator() {
69cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.TreeSet(java.util.Comparator)
70cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet myTreeSet = new TreeSet(new ReversedIntegerComparator());
71cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Did not construct correct TreeSet", myTreeSet.isEmpty());
72cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        myTreeSet.add(new Integer(1));
73cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        myTreeSet.add(new Integer(2));
74cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(
75cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "Answered incorrect first element--did not use custom comparator ",
76cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                myTreeSet.first().equals(new Integer(2)));
77cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(
78cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "Answered incorrect last element--did not use custom comparator ",
79cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                myTreeSet.last().equals(new Integer(1)));
80cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
81cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
82cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
83cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#TreeSet(java.util.SortedSet)
84cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
85cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_ConstructorLjava_util_SortedSet() {
86cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.TreeSet(java.util.SortedSet)
87cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ReversedIntegerComparator comp = new ReversedIntegerComparator();
88cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet myTreeSet = new TreeSet(comp);
89cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        for (int i = 0; i < objArray.length; i++)
90cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            myTreeSet.add(objArray[i]);
91cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet anotherTreeSet = new TreeSet(myTreeSet);
92cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("TreeSet is not correct size",
93cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                anotherTreeSet.size() == objArray.length);
94cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        for (int counter = 0; counter < objArray.length; counter++)
95cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            assertTrue("TreeSet does not contain correct elements",
96cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                    anotherTreeSet.contains(objArray[counter]));
97cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("TreeSet does not answer correct comparator", anotherTreeSet
98cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .comparator() == comp);
99cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("TreeSet does not use comparator",
100cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                anotherTreeSet.first() == objArray[objArray.length - 1]);
101cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
102cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
103cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
104cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#add(java.lang.Object)
105cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
106cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_addLjava_lang_Object() {
107cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method boolean java.util.TreeSet.add(java.lang.Object)
108cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ts.add(new Integer(-8));
109cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Failed to add Object", ts.contains(new Integer(-8)));
110cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ts.add(objArray[0]);
111cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Added existing element", ts.size() == objArray.length + 1);
112cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
113cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
114cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
115cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
116cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#addAll(java.util.Collection)
117cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
118cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_addAllLjava_util_Collection() {
119cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method boolean
120cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // java.util.TreeSet.addAll(java.util.Collection)
121cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet s = new TreeSet();
122cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s.addAll(ts);
123cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Incorrect size after add", s.size() == ts.size());
124cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Iterator i = ts.iterator();
125cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        while (i.hasNext())
126cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            assertTrue("Returned incorrect set", s.contains(i.next()));
127cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
128cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
129cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
130cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
131cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#clear()
132cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
133cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_clear() {
134cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method void java.util.TreeSet.clear()
135cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ts.clear();
136cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned non-zero size after clear", 0, ts.size());
137cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Found element in cleared set", !ts.contains(objArray[0]));
138cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
139cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
140cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
141cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#clone()
142cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
143cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_clone() {
144cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.lang.Object java.util.TreeSet.clone()
145cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet s = (TreeSet) ts.clone();
146cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Iterator i = ts.iterator();
147cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        while (i.hasNext())
148cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            assertTrue("Clone failed to copy all elements", s
149cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                    .contains(i.next()));
150cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
151cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
152cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
153cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#comparator()
154cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
155cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_comparator() {
156cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.Comparator java.util.TreeSet.comparator()
157cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ReversedIntegerComparator comp = new ReversedIntegerComparator();
158cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet myTreeSet = new TreeSet(comp);
159cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Answered incorrect comparator",
160cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                myTreeSet.comparator() == comp);
161cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
162cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
163cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
164cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#contains(java.lang.Object)
165cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
166cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_containsLjava_lang_Object() {
167cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method boolean java.util.TreeSet.contains(java.lang.Object)
168cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Returned false for valid Object", ts
169cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .contains(objArray[objArray.length / 2]));
170cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Returned true for invalid Object", !ts
171cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .contains(new Integer(-9)));
172cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
173cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            ts.contains(new Object());
174cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (ClassCastException e) {
175cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // Correct
176cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            return;
177cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
178cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        fail("Failed to throw exception when passed invalid element");
179cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
180cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
181cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
182cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
183cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#first()
184cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
185cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_first() {
186cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.lang.Object java.util.TreeSet.first()
187cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Returned incorrect first element",
188cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                ts.first() == objArray[0]);
189cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
190cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
191cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
192cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#headSet(java.lang.Object)
193cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
194cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_headSetLjava_lang_Object() {
195cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.SortedSet
196cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // java.util.TreeSet.headSet(java.lang.Object)
197cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Set s = ts.headSet(new Integer(100));
198cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned set of incorrect size", 100, s.size());
199cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        for (int i = 0; i < 100; i++)
200cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            assertTrue("Returned incorrect set", s.contains(objArray[i]));
201cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
202cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
203cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
204cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#isEmpty()
205cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
206cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_isEmpty() {
207cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method boolean java.util.TreeSet.isEmpty()
208cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Empty set returned false", new TreeSet().isEmpty());
209cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Non-Empty returned true", !ts.isEmpty());
210cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
211cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
212cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
213cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#iterator()
214cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
215cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_iterator() {
216cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.Iterator java.util.TreeSet.iterator()
217cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        TreeSet s = new TreeSet();
218cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s.addAll(ts);
219cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Iterator i = ts.iterator();
220cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Set as = new HashSet(Arrays.asList(objArray));
221cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        while (i.hasNext())
222cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            as.remove(i.next());
223cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect iterator", 0, as.size());
224cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
225cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
226cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
227cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
228cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#last()
229cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
230cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_last() {
231cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.lang.Object java.util.TreeSet.last()
232cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Returned incorrect last element",
233cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                ts.last() == objArray[objArray.length - 1]);
234cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
235cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
236cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
237cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#remove(java.lang.Object)
238cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
239cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_removeLjava_lang_Object() {
240cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method boolean java.util.TreeSet.remove(java.lang.Object)
241cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ts.remove(objArray[0]);
242cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Failed to remove object", !ts.contains(objArray[0]));
243cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Failed to change size after remove",
244cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                ts.size() == objArray.length - 1);
245cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
246cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            ts.remove(new Object());
247cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (ClassCastException e) {
248cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // Correct
249cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            return;
250cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
251cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        fail("Failed to throw exception when past uncomparable value");
252cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
253cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
254cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
255cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#size()
256cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
257cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_size() {
258cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method int java.util.TreeSet.size()
259cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Returned incorrect size", ts.size() == objArray.length);
260cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
261cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
262cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
263cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#subSet(java.lang.Object, java.lang.Object)
264cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
265cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_subSetLjava_lang_ObjectLjava_lang_Object() {
266cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.SortedSet
267cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // java.util.TreeSet.subSet(java.lang.Object, java.lang.Object)
268cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        final int startPos = objArray.length / 4;
269cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        final int endPos = 3 * objArray.length / 4;
270cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        SortedSet aSubSet = ts.subSet(objArray[startPos], objArray[endPos]);
271cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("Subset has wrong number of elements",
272cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                aSubSet.size() == (endPos - startPos));
273cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        for (int counter = startPos; counter < endPos; counter++)
274cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            assertTrue("Subset does not contain all the elements it should",
275cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                    aSubSet.contains(objArray[counter]));
276cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
277cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        int result;
278cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
279cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            ts.subSet(objArray[3], objArray[0]);
280cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            result = 0;
281cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (IllegalArgumentException e) {
282cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            result = 1;
283cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
284cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("end less than start should throw", 1, result);
285cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
286cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
287cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
288cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.util.TreeSet#tailSet(java.lang.Object)
289cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
290cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_tailSetLjava_lang_Object() {
291cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Test for method java.util.SortedSet
292cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // java.util.TreeSet.tailSet(java.lang.Object)
293cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Set s = ts.tailSet(new Integer(900));
294cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned set of incorrect size", 100, s.size());
295cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        for (int i = 900; i < objArray.length; i++)
296cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            assertTrue("Returned incorrect set", s.contains(objArray[i]));
297cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
298cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
299cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
300cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Tests equals() method.
301cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Tests that no ClassCastException will be thrown in all cases.
302cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Regression test for HARMONY-1639.
303cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
304cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_equals() throws Exception {
305cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // comparing TreeSets with different object types
306cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Set s1 = new TreeSet();
307cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Set s2 = new TreeSet();
308cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s1.add("key1");
309cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s1.add("key2");
310cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s2.add(new Integer(1));
311cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s2.add(new Integer(2));
312cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse("Sets should not be equal 1", s1.equals(s2));
313cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse("Sets should not be equal 2", s2.equals(s1));
314cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
315cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // comparing TreeSet with HashSet
316cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s1 = new TreeSet();
317cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s2 = new HashSet();
318cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s1.add("key");
319cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        s2.add(new Object());
320cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse("Sets should not be equal 3", s1.equals(s2));
321cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse("Sets should not be equal 4", s2.equals(s1));
322cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
323cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
324cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
325cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Sets up the fixture, for example, open a network connection. This method
326cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * is called before a test is executed.
327cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
328cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    protected void setUp() {
329cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ts = new TreeSet();
330cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        for (int i = 0; i < objArray.length; i++) {
331cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            Object x = objArray[i] = new Integer(i);
332cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            ts.add(x);
333cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
334cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
335cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
336cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
337cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Tears down the fixture, for example, close a network connection. This
338cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * method is called after a test is executed.
339cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
340cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    protected void tearDown() {
341cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
342cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath}
343