1 /* 2 * Copyright (C) 2007 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._tests; 18 19 import com.android.dx.util.Bits; 20 21 import junit.framework.TestCase; 22 23 /** 24 * Test the class {@code com.android.dx.util.Bits}. 25 */ 26 public class _Bits 27 extends TestCase { test_makeBitSet()28 public void test_makeBitSet() { 29 assertEquals(label(0), 0, Bits.makeBitSet(0).length); 30 31 for (int i = 1; i <= 32; i++) { 32 assertEquals(label(i), 1, Bits.makeBitSet(i).length); 33 } 34 35 for (int i = 33; i <= 64; i++) { 36 assertEquals(label(i), 2, Bits.makeBitSet(i).length); 37 } 38 39 for (int i = 65; i < 4000; i += 101) { 40 int expect = i >> 5; 41 if ((expect * 32) < i) { 42 expect++; 43 } 44 assertEquals(label(i), expect, Bits.makeBitSet(i).length); 45 } 46 } 47 test_getMax()48 public void test_getMax() { 49 for (int i = 0; i < 4000; i += 59) { 50 int expect = i >> 5; 51 if ((expect * 32) < i) { 52 expect++; 53 } 54 assertEquals(label(i), expect * 32, 55 Bits.getMax(new int[expect])); 56 } 57 } 58 test1_get()59 public void test1_get() { 60 int[] bits = Bits.makeBitSet(100); 61 62 for (int i = 0; i < 100; i++) { 63 assertFalse(label(i), Bits.get(bits, i)); 64 } 65 } 66 test2_get()67 public void test2_get() { 68 int[] bits = Bits.makeBitSet(100); 69 for (int i = 0; i < bits.length; i++) { 70 bits[i] = -1; 71 } 72 73 for (int i = 0; i < 100; i++) { 74 assertTrue(label(i), Bits.get(bits, i)); 75 } 76 } 77 test3_get()78 public void test3_get() { 79 int[] bits = Bits.makeBitSet(100); 80 81 for (int i = 0; i < 100; i++) { 82 Bits.set(bits, i, (i % 5) == 0); 83 } 84 85 for (int i = 0; i < 100; i++) { 86 boolean expect = (i % 5) == 0; 87 assertTrue(label(i), Bits.get(bits, i) == expect); 88 } 89 } 90 test1_set1()91 public void test1_set1() { 92 int[] bits = Bits.makeBitSet(50); 93 bits[1] = -1; 94 95 Bits.set(bits, 0, true); 96 Bits.set(bits, 3, true); 97 Bits.set(bits, 6, true); 98 Bits.set(bits, 3, false); 99 Bits.set(bits, 35, false); 100 Bits.set(bits, 38, false); 101 Bits.set(bits, 42, false); 102 Bits.set(bits, 38, true); 103 104 assertEquals(label(1), 0x41, bits[0]); 105 assertEquals(label(2), 0xfffffbf7, bits[1]); 106 } 107 test2_set1()108 public void test2_set1() { 109 int[] bits = Bits.makeBitSet(100); 110 111 for (int i = 0; i < 100; i++) { 112 if ((i % 3) == 0) { 113 Bits.set(bits, i, true); 114 } 115 } 116 117 for (int i = 0; i < 100; i++) { 118 if ((i % 5) == 0) { 119 Bits.set(bits, i, false); 120 } 121 } 122 123 for (int i = 0; i < 100; i++) { 124 if ((i % 7) == 0) { 125 Bits.set(bits, i, true); 126 } 127 } 128 129 for (int i = 0; i < 100; i++) { 130 boolean expect = ((i % 7) == 0) || 131 (((i % 3) == 0) && ((i % 5) != 0)); 132 assertTrue(label(i), Bits.get(bits, i) == expect); 133 } 134 } 135 test_set2()136 public void test_set2() { 137 int[] bits = Bits.makeBitSet(100); 138 139 for (int i = 0; i < 100; i++) { 140 if ((i % 11) == 0) { 141 Bits.set(bits, i); 142 } 143 } 144 145 for (int i = 0; i < 100; i++) { 146 boolean expect = (i % 11) == 0; 147 assertTrue(label(i), Bits.get(bits, i) == expect); 148 } 149 } 150 test_clear()151 public void test_clear() { 152 int[] bits = Bits.makeBitSet(100); 153 for (int i = 0; i < bits.length; i++) { 154 bits[i] = -1; 155 } 156 157 for (int i = 0; i < 100; i++) { 158 if ((i % 5) == 0) { 159 Bits.clear(bits, i); 160 } 161 } 162 163 for (int i = 0; i < 100; i++) { 164 boolean expect = (i % 5) != 0; 165 assertTrue(label(i), Bits.get(bits, i) == expect); 166 } 167 } 168 test1_isEmpty()169 public void test1_isEmpty() { 170 for (int i = 0; i < 10; i++) { 171 assertTrue(label(i), Bits.isEmpty(new int[i])); 172 } 173 } 174 test2_isEmpty()175 public void test2_isEmpty() { 176 for (int i = 1; i < 1000; i += 11) { 177 int[] bits = Bits.makeBitSet(i); 178 for (int j = i % 11; j >= 0; j--) { 179 int x = i - 1 - (j * 13); 180 if (x >= 0) { 181 Bits.set(bits, x); 182 } 183 } 184 assertFalse(label(i), Bits.isEmpty(bits)); 185 } 186 } 187 test1_bitCount()188 public void test1_bitCount() { 189 for (int i = 0; i < 10; i++) { 190 assertEquals(label(i), 0, Bits.bitCount(new int[i])); 191 } 192 } 193 test2_bitCount()194 public void test2_bitCount() { 195 for (int i = 1; i < 1000; i += 13) { 196 int[] bits = Bits.makeBitSet(i); 197 int count = 0; 198 for (int j = 0; j < i; j += 20) { 199 Bits.set(bits, j); 200 count++; 201 } 202 for (int j = 7; j < i; j += 11) { 203 if (!Bits.get(bits, j)) { 204 Bits.set(bits, j); 205 count++; 206 } 207 } 208 for (int j = 3; j < i; j += 17) { 209 if (!Bits.get(bits, j)) { 210 Bits.set(bits, j); 211 count++; 212 } 213 } 214 assertEquals(label(i), count, Bits.bitCount(bits)); 215 } 216 } 217 test1_anyInRange()218 public void test1_anyInRange() { 219 int[] bits = new int[100]; 220 221 for (int i = 0; i < 100; i += 11) { 222 assertFalse(label(i), Bits.anyInRange(bits, 0, i)); 223 } 224 } 225 test2_anyInRange()226 public void test2_anyInRange() { 227 int[] bits = new int[100]; 228 229 for (int i = 0; i < 100; i += 11) { 230 assertFalse(label(i), Bits.anyInRange(bits, i, 100)); 231 } 232 } 233 test3_anyInRange()234 public void test3_anyInRange() { 235 int[] bits = new int[100]; 236 237 for (int i = 0; i < 50; i += 7) { 238 assertFalse(label(i), Bits.anyInRange(bits, i, 100 - i)); 239 } 240 } 241 test4_anyInRange()242 public void test4_anyInRange() { 243 int[] bits = new int[100]; 244 for (int i = 0; i < bits.length; i++) { 245 bits[i] = -1; 246 } 247 248 for (int i = 1; i < 100; i += 11) { 249 assertTrue(label(i), Bits.anyInRange(bits, 0, i)); 250 } 251 } 252 test5_anyInRange()253 public void test5_anyInRange() { 254 int[] bits = new int[100]; 255 for (int i = 0; i < bits.length; i++) { 256 bits[i] = -1; 257 } 258 259 for (int i = 1; i < 100; i += 11) { 260 assertTrue(label(i), Bits.anyInRange(bits, i, 100)); 261 } 262 } 263 test6_anyInRange()264 public void test6_anyInRange() { 265 int[] bits = new int[100]; 266 for (int i = 0; i < bits.length; i++) { 267 bits[i] = -1; 268 } 269 270 for (int i = 0; i < 50; i += 7) { 271 assertTrue(label(i), Bits.anyInRange(bits, i, 100 - i)); 272 } 273 } 274 test1_findFirst1()275 public void test1_findFirst1() { 276 int[] bits = new int[100]; 277 278 for (int i = 0; i < 100; i++) { 279 assertEquals(label(i), -1, Bits.findFirst(bits, i)); 280 } 281 } 282 test2_findFirst1()283 public void test2_findFirst1() { 284 int[] bits = new int[100]; 285 for (int i = 0; i < bits.length; i++) { 286 bits[i] = -1; 287 } 288 289 for (int i = 0; i < 100; i++) { 290 assertEquals(label(i), i, Bits.findFirst(bits, i)); 291 } 292 } 293 test3_findFirst1()294 public void test3_findFirst1() { 295 int[] bits = new int[100]; 296 297 for (int i = 25; i < 80; i++) { 298 for (int j = 0; j < bits.length; j++) { 299 bits[j] = 0; 300 } 301 302 Bits.set(bits, i - 5); 303 Bits.set(bits, i + 5); 304 Bits.set(bits, i + 10); 305 Bits.set(bits, i + 20); 306 assertEquals(label(i), i + 5, Bits.findFirst(bits, i)); 307 } 308 } 309 test1_findFirst2()310 public void test1_findFirst2() { 311 for (int i = 0; i < 32; i++) { 312 assertEquals(label(i), -1, Bits.findFirst(0, i)); 313 } 314 } 315 test2_findFirst2()316 public void test2_findFirst2() { 317 for (int i = 0; i < 32; i++) { 318 assertEquals(label(i), i, Bits.findFirst(-1, i)); 319 } 320 } 321 test3_findFirst2()322 public void test3_findFirst2() { 323 for (int i = 0; i < 32; i++) { 324 assertEquals(label(i), -1, Bits.findFirst((1 << i) >>> 1, i)); 325 } 326 } 327 test4_findFirst2()328 public void test4_findFirst2() { 329 for (int i = 0; i < 32; i++) { 330 assertEquals(label(i), i, Bits.findFirst(1 << i, i)); 331 } 332 } 333 test5_findFirst2()334 public void test5_findFirst2() { 335 for (int i = 0; i < 31; i++) { 336 assertEquals(label(i), i + 1, Bits.findFirst(1 << (i + 1), i)); 337 } 338 } 339 test6_findFirst2()340 public void test6_findFirst2() { 341 for (int i = 0; i < 32; i++) { 342 int value = (1 << i); 343 value |= (value >>> 1); 344 assertEquals(label(i), i, Bits.findFirst(value, i)); 345 } 346 } 347 label(int n)348 private static String label(int n) { 349 return "(" + n + ")"; 350 } 351 } 352