OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / libcore / java / util / OldTreeSetTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package libcore.java.util;
19
20 import dalvik.annotation.TestLevel;
21 import dalvik.annotation.TestTargetClass;
22 import dalvik.annotation.TestTargetNew;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.NoSuchElementException;
28 import java.util.Set;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31
32 @TestTargetClass(TreeSet.class)
33 public class OldTreeSetTest extends junit.framework.TestCase {
34
35     TreeSet ts;
36
37     Object objArray[] = new Object[1000];
38
39     /**
40      * @tests java.util.TreeSet#TreeSet(java.util.Collection)
41      */
42     @TestTargetNew(
43         level = TestLevel.COMPLETE,
44         notes = "",
45         method = "TreeSet",
46         args = {java.util.Collection.class}
47     )
48     public void test_ConstructorLjava_util_Collection() {
49         // Test for method java.util.TreeSet(java.util.Collection)
50         TreeSet myTreeSet = new TreeSet(Arrays.asList(objArray));
51         assertTrue("TreeSet incorrect size",
52                 myTreeSet.size() == objArray.length);
53         for (int counter = 0; counter < objArray.length; counter++)
54             assertTrue("TreeSet does not contain correct elements", myTreeSet
55                     .contains(objArray[counter]));
56
57         HashMap hm = new HashMap();
58         hm.put("First", new Integer(1));
59         hm.put(new Integer(2), "two");
60
61         try {
62             new TreeSet(hm.values());
63             fail("ClassCastException expected");
64         } catch (ClassCastException e) {
65             //expected
66         }
67
68         try {
69             new TreeSet((Collection)null);
70             fail("NullPointerException expected");
71         } catch (NullPointerException e) {
72             //expected
73         }
74     }
75
76     /**
77      * @tests java.util.TreeSet#TreeSet(java.util.SortedSet)
78      */
79     @TestTargetNew(
80         level = TestLevel.COMPLETE,
81         notes = "",
82         method = "TreeSet",
83         args = {java.util.SortedSet.class}
84     )
85     public void test_ConstructorLjava_util_SortedSet() {
86         try {
87             new TreeSet((SortedSet)null);
88             fail("NullPointerException expected");
89         } catch (NullPointerException e) {
90             //expected
91         }
92     }
93
94     /**
95      * @tests java.util.TreeSet#add(java.lang.Object)
96      */
97     @TestTargetNew(
98         level = TestLevel.COMPLETE,
99         notes = "",
100         method = "add",
101         args = {java.lang.Object.class}
102     )
103     public void test_addLjava_lang_Object() {
104         // Test for method boolean java.util.TreeSet.add(java.lang.Object)
105         ts.add(new Integer(-8));
106         assertTrue("Failed to add Object", ts.contains(new Integer(-8)));
107         ts.add(objArray[0]);
108         assertTrue("Added existing element", ts.size() == objArray.length + 1);
109
110         HashMap hm = new HashMap();
111         hm.put("First", new Integer(1));
112         hm.put(new Integer(2), "two");
113
114         try {
115             ts.add("Wrong element");
116             fail("ClassCastException expected");
117         } catch (ClassCastException e) {
118             //expected
119         }
120     }
121
122     /**
123      * @tests java.util.TreeSet#addAll(java.util.Collection)
124      */
125     @TestTargetNew(
126         level = TestLevel.COMPLETE,
127         notes = "",
128         method = "addAll",
129         args = {java.util.Collection.class}
130     )
131     public void test_addAllLjava_util_Collection() {
132         // Test for method boolean
133         // java.util.TreeSet.addAll(java.util.Collection)
134         TreeSet s = new TreeSet();
135         s.addAll(ts);
136         assertTrue("Incorrect size after add", s.size() == ts.size());
137         Iterator i = ts.iterator();
138         while (i.hasNext())
139             assertTrue("Returned incorrect set", s.contains(i.next()));
140
141         HashMap hm = new HashMap();
142         hm.put("First", new Integer(1));
143         hm.put(new Integer(2), "two");
144
145         try {
146             s.addAll(hm.values());
147             fail("ClassCastException expected");
148         } catch (ClassCastException e) {
149             //expected
150         }
151
152         try {
153             s.addAll(null);
154             fail("NullPointerException expected");
155         } catch (NullPointerException e) {
156             //expected
157         }
158     }
159
160     /**
161      * @tests java.util.TreeSet#first()
162      */
163     @TestTargetNew(
164         level = TestLevel.COMPLETE,
165         notes = "",
166         method = "first",
167         args = {}
168     )
169     public void test_first() {
170         // Test for method java.lang.Object java.util.TreeSet.first()
171         assertTrue("Returned incorrect first element",
172                 ts.first() == objArray[0]);
173
174         ts = new TreeSet();
175         try {
176             ts.first();
177             fail("NoSuchElementException expected");
178         } catch (NoSuchElementException e) {
179             //expected
180         }
181     }
182
183     /**
184      * @tests java.util.TreeSet#headSet(java.lang.Object)
185      */
186     @TestTargetNew(
187         level = TestLevel.COMPLETE,
188         notes = "",
189         method = "headSet",
190         args = {java.lang.Object.class}
191     )
192     public void test_headSetLjava_lang_Object() {
193         // Test for method java.util.SortedSet
194         // java.util.TreeSet.headSet(java.lang.Object)
195         Set s = ts.headSet(new Integer(100));
196         assertEquals("Returned set of incorrect size", 100, s.size());
197         for (int i = 0; i < 100; i++)
198             assertTrue("Returned incorrect set", s.contains(objArray[i]));
199
200         SortedSet sort = ts.headSet(new Integer(100));
201         try {
202             sort.headSet(new Integer(101));
203             fail("IllegalArgumentException expected");
204         } catch (IllegalArgumentException e) {
205             //expected
206         }
207
208         try {
209             ts.headSet(this);
210             fail("ClassCastException expected");
211         } catch (ClassCastException e) {
212             //expected
213         }
214
215         try {
216             ts.headSet(null);
217             fail("NullPointerException expected");
218         } catch (NullPointerException e) {
219             //expected
220         }
221     }
222
223     /**
224      * @tests java.util.TreeSet#last()
225      */
226     @TestTargetNew(
227         level = TestLevel.COMPLETE,
228         notes = "",
229         method = "last",
230         args = {}
231     )
232     public void test_last() {
233         // Test for method java.lang.Object java.util.TreeSet.last()
234         assertTrue("Returned incorrect last element",
235                 ts.last() == objArray[objArray.length - 1]);
236
237         ts = new TreeSet();
238         try {
239             ts.last();
240             fail("NoSuchElementException expected");
241         } catch (NoSuchElementException e) {
242             //expected
243         }
244     }
245
246     /**
247      * @tests java.util.TreeSet#subSet(java.lang.Object, java.lang.Object)
248      */
249     @TestTargetNew(
250         level = TestLevel.COMPLETE,
251         notes = "",
252         method = "subSet",
253         args = {java.lang.Object.class, java.lang.Object.class}
254     )
255     public void test_subSetLjava_lang_ObjectLjava_lang_Object() {
256         // Test for method java.util.SortedSet
257         // java.util.TreeSet.subSet(java.lang.Object, java.lang.Object)
258         final int startPos = objArray.length / 4;
259         final int endPos = 3 * objArray.length / 4;
260         SortedSet aSubSet = ts.subSet(objArray[startPos], objArray[endPos]);
261         assertTrue("Subset has wrong number of elements",
262                 aSubSet.size() == (endPos - startPos));
263         for (int counter = startPos; counter < endPos; counter++)
264             assertTrue("Subset does not contain all the elements it should",
265                     aSubSet.contains(objArray[counter]));
266
267         try {
268             ts.subSet(objArray[3], objArray[0]);
269             fail("IllegalArgumentException expected");
270         } catch (IllegalArgumentException e) {
271             //expected
272         }
273
274         try {
275             ts.subSet(null, objArray[3]);
276             fail("NullPointerException expected");
277         } catch (NullPointerException e) {
278             //expected
279         }
280
281         try {
282             ts.subSet(objArray[3], null);
283             fail("NullPointerException expected");
284         } catch (NullPointerException e) {
285             //expected
286         }
287
288         try {
289             ts.subSet(objArray[3], this);
290             fail("ClassCastException expected");
291         } catch (ClassCastException e) {
292             //expected
293         }
294     }
295
296     /**
297      * @tests java.util.TreeSet#tailSet(java.lang.Object)
298      */
299     @TestTargetNew(
300         level = TestLevel.COMPLETE,
301         notes = "",
302         method = "tailSet",
303         args = {java.lang.Object.class}
304     )
305     public void test_tailSetLjava_lang_Object() {
306         // Test for method java.util.SortedSet
307         // java.util.TreeSet.tailSet(java.lang.Object)
308         Set s = ts.tailSet(new Integer(900));
309         assertEquals("Returned set of incorrect size", 100, s.size());
310         for (int i = 900; i < objArray.length; i++)
311             assertTrue("Returned incorrect set", s.contains(objArray[i]));
312
313         SortedSet sort = ts.tailSet(new Integer(101));
314
315         try {
316             sort.tailSet(new Integer(100));
317             fail("IllegalArgumentException expected");
318         } catch (IllegalArgumentException e) {
319             //expected
320         }
321
322         try {
323             ts.tailSet(this);
324             fail("ClassCastException expected");
325         } catch (ClassCastException e) {
326             //expected
327         }
328
329         try {
330             ts.tailSet(null);
331             fail("NullPointerException expected");
332         } catch (NullPointerException e) {
333             //expected
334         }
335     }
336
337     /**
338      * Sets up the fixture, for example, open a network connection. This method
339      * is called before a test is executed.
340      */
341     protected void setUp() {
342         ts = new TreeSet();
343         for (int i = 0; i < objArray.length; i++) {
344             Object x = objArray[i] = new Integer(i);
345             ts.add(x);
346         }
347     }
348
349     /**
350      * Tears down the fixture, for example, close a network connection. This
351      * method is called after a test is executed.
352      */
353     protected void tearDown() {
354     }
355 }