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 18package libcore.java.util; 19 20import java.util.Arrays; 21import java.util.Collection; 22import java.util.HashMap; 23import java.util.Iterator; 24import java.util.NoSuchElementException; 25import java.util.Set; 26import java.util.SortedSet; 27import java.util.TreeSet; 28 29public class OldTreeSetTest extends junit.framework.TestCase { 30 31 TreeSet ts; 32 33 Object objArray[] = new Object[1000]; 34 35 public void test_ConstructorLjava_util_Collection() { 36 // Test for method java.util.TreeSet(java.util.Collection) 37 TreeSet myTreeSet = new TreeSet(Arrays.asList(objArray)); 38 assertTrue("TreeSet incorrect size", 39 myTreeSet.size() == objArray.length); 40 for (int counter = 0; counter < objArray.length; counter++) 41 assertTrue("TreeSet does not contain correct elements", myTreeSet 42 .contains(objArray[counter])); 43 44 HashMap hm = new HashMap(); 45 hm.put("First", new Integer(1)); 46 hm.put(new Integer(2), "two"); 47 48 try { 49 new TreeSet(hm.values()); 50 fail("ClassCastException expected"); 51 } catch (ClassCastException e) { 52 //expected 53 } 54 55 try { 56 new TreeSet((Collection)null); 57 fail("NullPointerException expected"); 58 } catch (NullPointerException e) { 59 //expected 60 } 61 } 62 63 public void test_ConstructorLjava_util_SortedSet() { 64 try { 65 new TreeSet((SortedSet)null); 66 fail("NullPointerException expected"); 67 } catch (NullPointerException e) { 68 //expected 69 } 70 } 71 72 public void test_addLjava_lang_Object() { 73 // Test for method boolean java.util.TreeSet.add(java.lang.Object) 74 ts.add(new Integer(-8)); 75 assertTrue("Failed to add Object", ts.contains(new Integer(-8))); 76 ts.add(objArray[0]); 77 assertTrue("Added existing element", ts.size() == objArray.length + 1); 78 79 HashMap hm = new HashMap(); 80 hm.put("First", new Integer(1)); 81 hm.put(new Integer(2), "two"); 82 83 try { 84 ts.add("Wrong element"); 85 fail("ClassCastException expected"); 86 } catch (ClassCastException e) { 87 //expected 88 } 89 } 90 91 public void test_addAllLjava_util_Collection() { 92 // Test for method boolean 93 // java.util.TreeSet.addAll(java.util.Collection) 94 TreeSet s = new TreeSet(); 95 s.addAll(ts); 96 assertTrue("Incorrect size after add", s.size() == ts.size()); 97 Iterator i = ts.iterator(); 98 while (i.hasNext()) 99 assertTrue("Returned incorrect set", s.contains(i.next())); 100 101 HashMap hm = new HashMap(); 102 hm.put("First", new Integer(1)); 103 hm.put(new Integer(2), "two"); 104 105 try { 106 s.addAll(hm.values()); 107 fail("ClassCastException expected"); 108 } catch (ClassCastException e) { 109 //expected 110 } 111 112 try { 113 s.addAll(null); 114 fail("NullPointerException expected"); 115 } catch (NullPointerException e) { 116 //expected 117 } 118 } 119 120 public void test_first() { 121 // Test for method java.lang.Object java.util.TreeSet.first() 122 assertTrue("Returned incorrect first element", 123 ts.first() == objArray[0]); 124 125 ts = new TreeSet(); 126 try { 127 ts.first(); 128 fail("NoSuchElementException expected"); 129 } catch (NoSuchElementException e) { 130 //expected 131 } 132 } 133 134 public void test_headSetLjava_lang_Object() { 135 // Test for method java.util.SortedSet 136 // java.util.TreeSet.headSet(java.lang.Object) 137 Set s = ts.headSet(new Integer(100)); 138 assertEquals("Returned set of incorrect size", 100, s.size()); 139 for (int i = 0; i < 100; i++) 140 assertTrue("Returned incorrect set", s.contains(objArray[i])); 141 142 SortedSet sort = ts.headSet(new Integer(100)); 143 try { 144 sort.headSet(new Integer(101)); 145 fail("IllegalArgumentException expected"); 146 } catch (IllegalArgumentException e) { 147 //expected 148 } 149 150 try { 151 ts.headSet(this); 152 fail("ClassCastException expected"); 153 } catch (ClassCastException e) { 154 //expected 155 } 156 157 try { 158 ts.headSet(null); 159 fail("NullPointerException expected"); 160 } catch (NullPointerException e) { 161 //expected 162 } 163 } 164 165 public void test_last() { 166 // Test for method java.lang.Object java.util.TreeSet.last() 167 assertTrue("Returned incorrect last element", 168 ts.last() == objArray[objArray.length - 1]); 169 170 ts = new TreeSet(); 171 try { 172 ts.last(); 173 fail("NoSuchElementException expected"); 174 } catch (NoSuchElementException e) { 175 //expected 176 } 177 } 178 179 public void test_subSetLjava_lang_ObjectLjava_lang_Object() { 180 // Test for method java.util.SortedSet 181 // java.util.TreeSet.subSet(java.lang.Object, java.lang.Object) 182 final int startPos = objArray.length / 4; 183 final int endPos = 3 * objArray.length / 4; 184 SortedSet aSubSet = ts.subSet(objArray[startPos], objArray[endPos]); 185 assertTrue("Subset has wrong number of elements", 186 aSubSet.size() == (endPos - startPos)); 187 for (int counter = startPos; counter < endPos; counter++) 188 assertTrue("Subset does not contain all the elements it should", 189 aSubSet.contains(objArray[counter])); 190 191 try { 192 ts.subSet(objArray[3], objArray[0]); 193 fail("IllegalArgumentException expected"); 194 } catch (IllegalArgumentException e) { 195 //expected 196 } 197 198 try { 199 ts.subSet(null, objArray[3]); 200 fail("NullPointerException expected"); 201 } catch (NullPointerException e) { 202 //expected 203 } 204 205 try { 206 ts.subSet(objArray[3], null); 207 fail("NullPointerException expected"); 208 } catch (NullPointerException e) { 209 //expected 210 } 211 212 try { 213 ts.subSet(objArray[3], this); 214 fail("ClassCastException expected"); 215 } catch (ClassCastException e) { 216 //expected 217 } 218 } 219 220 public void test_tailSetLjava_lang_Object() { 221 // Test for method java.util.SortedSet 222 // java.util.TreeSet.tailSet(java.lang.Object) 223 Set s = ts.tailSet(new Integer(900)); 224 assertEquals("Returned set of incorrect size", 100, s.size()); 225 for (int i = 900; i < objArray.length; i++) 226 assertTrue("Returned incorrect set", s.contains(objArray[i])); 227 228 SortedSet sort = ts.tailSet(new Integer(101)); 229 230 try { 231 sort.tailSet(new Integer(100)); 232 fail("IllegalArgumentException expected"); 233 } catch (IllegalArgumentException e) { 234 //expected 235 } 236 237 try { 238 ts.tailSet(this); 239 fail("ClassCastException expected"); 240 } catch (ClassCastException e) { 241 //expected 242 } 243 244 try { 245 ts.tailSet(null); 246 fail("NullPointerException expected"); 247 } catch (NullPointerException e) { 248 //expected 249 } 250 } 251 252 /** 253 * Sets up the fixture, for example, open a network connection. This method 254 * is called before a test is executed. 255 */ 256 protected void setUp() { 257 ts = new TreeSet(); 258 for (int i = 0; i < objArray.length; i++) { 259 Object x = objArray[i] = new Integer(i); 260 ts.add(x); 261 } 262 } 263 264 /** 265 * Tears down the fixture, for example, close a network connection. This 266 * method is called after a test is executed. 267 */ 268 protected void tearDown() { 269 } 270} 271