1 /* 2 * Copyright (C) 2008 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 17 package com.android.dx.util; 18 19 import java.util.NoSuchElementException; 20 import junit.framework.TestCase; 21 22 public final class ListIntSetTest extends TestCase { 23 public void test_basic() { 24 ListIntSet set = new ListIntSet(); 25 26 assertEquals(0, set.elements()); 27 28 set.add(31); 29 set.add(0); 30 set.add(1); 31 32 assertTrue(set.has(0)); 33 assertTrue(set.has(1)); 34 assertTrue(set.has(31)); 35 36 assertEquals(3, set.elements()); 37 38 assertFalse(set.has(2)); 39 assertFalse(set.has(7)); 40 assertFalse(set.has(30)); 41 } 42 43 public void test_iterator() { 44 ListIntSet set = new ListIntSet(); 45 46 set.add(0); 47 set.add(0); 48 set.add(1); 49 set.add(1); 50 set.add(31); 51 set.add(31); 52 53 IntIterator iter = set.iterator(); 54 55 assertTrue(iter.hasNext()); 56 assertEquals(iter.next(), 0); 57 assertTrue(iter.hasNext()); 58 assertEquals(iter.next(), 1); 59 assertTrue(iter.hasNext()); 60 assertEquals(iter.next(), 31); 61 62 assertFalse(iter.hasNext()); 63 64 try { 65 iter.next(); 66 fail(); 67 } catch (NoSuchElementException ex) { 68 // exception excepted 69 } 70 } 71 72 public void test_empty() { 73 ListIntSet set = new ListIntSet(); 74 75 IntIterator iter = set.iterator(); 76 77 assertFalse(iter.hasNext()); 78 } 79 80 public void test_remove() { 81 ListIntSet set = new ListIntSet(); 82 83 set.add(0); 84 set.add(1); 85 set.add(31); 86 87 assertTrue(set.has(0)); 88 assertTrue(set.has(1)); 89 assertTrue(set.has(31)); 90 91 assertFalse(set.has(2)); 92 assertFalse(set.has(7)); 93 assertFalse(set.has(30)); 94 95 set.remove(0); 96 97 assertFalse(set.has(0)); 98 99 assertTrue(set.has(1)); 100 assertTrue(set.has(31)); 101 } 102 103 public void test_mergeA() { 104 ListIntSet setA = new ListIntSet(); 105 int[] valuesA = {0, 1, 31}; 106 107 for (int i = 0; i < valuesA.length; i++) { 108 setA.add(valuesA[i]); 109 } 110 111 ListIntSet setB = new ListIntSet(); 112 int[] valuesB = {0, 5, 6, 32, 127, 128}; 113 114 for (int i = 0; i < valuesB.length; i++) { 115 setB.add(valuesB[i]); 116 } 117 118 setA.merge(setB); 119 120 for (int i = 0; i < valuesA.length; i++) { 121 assertTrue(setA.has(valuesA[i])); 122 } 123 124 for (int i = 0; i < valuesB.length; i++) { 125 assertTrue(setA.has(valuesB[i])); 126 } 127 128 } 129 130 public void test_mergeB() { 131 ListIntSet setA = new ListIntSet(); 132 int[] valuesA = {0, 1, 31, 129, 130}; 133 134 for (int i = 0; i < valuesA.length; i++) { 135 setA.add(valuesA[i]); 136 } 137 138 ListIntSet setB = new ListIntSet(); 139 int[] valuesB = {0, 5, 6, 32, 127,128}; 140 141 for (int i = 0; i < valuesB.length; i++) { 142 setB.add(valuesB[i]); 143 } 144 145 setA.merge(setB); 146 147 for (int i = 0; i < valuesA.length; i++) { 148 assertTrue(setA.has(valuesA[i])); 149 } 150 151 for (int i = 0; i < valuesB.length; i++) { 152 assertTrue(setA.has(valuesB[i])); 153 } 154 155 } 156 157 public void test_mergeWithBitIntSet() { 158 ListIntSet setA = new ListIntSet(); 159 int[] valuesA = {0, 1, 31, 129, 130}; 160 161 for (int i = 0; i < valuesA.length; i++) { 162 setA.add(valuesA[i]); 163 } 164 165 BitIntSet setB = new BitIntSet(129); 166 int[] valuesB = {0, 5, 6, 32, 127,128}; 167 168 for (int i = 0; i < valuesB.length; i++) { 169 setB.add(valuesB[i]); 170 } 171 172 setA.merge(setB); 173 174 for (int i = 0; i < valuesA.length; i++) { 175 assertTrue(setA.has(valuesA[i])); 176 } 177 178 for (int i = 0; i < valuesB.length; i++) { 179 assertTrue(setA.has(valuesB[i])); 180 } 181 182 } 183 184 public void test_toString() { 185 ListIntSet set = new ListIntSet(); 186 187 assertEquals(set.toString(), "{}"); 188 189 set.add(1); 190 191 assertEquals(set.toString(), "{1}"); 192 193 set.add(2); 194 195 assertEquals(set.toString(), "{1, 2}"); 196 } 197 198 } 199