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 BitIntSetTest extends TestCase { test_basic()23 public void test_basic() { 24 BitIntSet set = new BitIntSet(32); 25 26 assertEquals(0, set.elements()); 27 28 set.add(0); 29 set.add(1); 30 set.add(31); 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 test_iterator()43 public void test_iterator() { 44 BitIntSet set = new BitIntSet(32); 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 test_remove()72 public void test_remove() { 73 BitIntSet set = new BitIntSet(32); 74 75 set.add(0); 76 set.add(1); 77 set.add(31); 78 79 assertTrue(set.has(0)); 80 assertTrue(set.has(1)); 81 assertTrue(set.has(31)); 82 83 assertFalse(set.has(2)); 84 assertFalse(set.has(7)); 85 assertFalse(set.has(30)); 86 87 set.remove(0); 88 89 assertFalse(set.has(0)); 90 91 assertTrue(set.has(1)); 92 assertTrue(set.has(31)); 93 } 94 95 /** 96 * Tests the auto-expansion of the set 97 */ test_expand()98 public void test_expand() { 99 BitIntSet set = new BitIntSet(32); 100 int[] values = {0, 1, 31, 32, 128}; 101 102 for (int i = 0; i < values.length; i++) { 103 set.add(values[i]); 104 } 105 106 IntIterator iter = set.iterator(); 107 108 for (int i = 0; i < values.length; i++) { 109 assertTrue(iter.hasNext()); 110 assertEquals(values[i], iter.next()); 111 } 112 assertFalse(iter.hasNext()); 113 } 114 test_merge()115 public void test_merge() { 116 BitIntSet setA = new BitIntSet(32); 117 int[] valuesA = {0, 1, 31}; 118 119 for (int i = 0; i < valuesA.length; i++) { 120 setA.add(valuesA[i]); 121 } 122 123 BitIntSet setB = new BitIntSet(32); 124 int[] valuesB = {0, 5, 6, 8, 31}; 125 126 for (int i = 0; i < valuesB.length; i++) { 127 setB.add(valuesB[i]); 128 } 129 130 setA.merge(setB); 131 132 for (int i = 0; i < valuesA.length; i++) { 133 assertTrue(setA.has(valuesA[i])); 134 } 135 136 for (int i = 0; i < valuesB.length; i++) { 137 assertTrue(setA.has(valuesB[i])); 138 } 139 } 140 test_mergeWithListIntSet()141 public void test_mergeWithListIntSet() { 142 BitIntSet setA = new BitIntSet(32); 143 int[] valuesA = {0, 1, 31}; 144 145 for (int i = 0; i < valuesA.length; i++) { 146 setA.add(valuesA[i]); 147 } 148 149 ListIntSet setB = new ListIntSet(); 150 int[] valuesB = {0, 5, 6, 8, 31}; 151 152 for (int i = 0; i < valuesB.length; i++) { 153 setB.add(valuesB[i]); 154 } 155 156 setA.merge(setB); 157 158 for (int i = 0; i < valuesA.length; i++) { 159 assertTrue(setA.has(valuesA[i])); 160 } 161 162 for (int i = 0; i < valuesB.length; i++) { 163 assertTrue(setA.has(valuesB[i])); 164 } 165 } 166 test_mergeAndExpand()167 public void test_mergeAndExpand() { 168 BitIntSet setA = new BitIntSet(32); 169 int[] valuesA = {0, 1, 31}; 170 171 for (int i = 0; i < valuesA.length; i++) { 172 setA.add(valuesA[i]); 173 } 174 175 BitIntSet setB = new BitIntSet(32); 176 int[] valuesB = {0, 5, 6, 32, 127}; 177 178 for (int i = 0; i < valuesB.length; i++) { 179 setB.add(valuesB[i]); 180 } 181 182 setA.merge(setB); 183 184 for (int i = 0; i < valuesA.length; i++) { 185 assertTrue(setA.has(valuesA[i])); 186 } 187 188 for (int i = 0; i < valuesB.length; i++) { 189 assertTrue(setA.has(valuesB[i])); 190 } 191 } 192 test_toString()193 public void test_toString() { 194 BitIntSet set = new BitIntSet(32); 195 196 assertEquals(set.toString(), "{}"); 197 198 set.add(1); 199 200 assertEquals(set.toString(), "{1}"); 201 202 set.add(2); 203 204 assertEquals(set.toString(), "{1, 2}"); 205 } 206 } 207