upload android base code part3

This commit is contained in:
August 2018-08-08 16:48:17 +08:00
parent 71b83c22f1
commit b9e30e05b1
15122 changed files with 2089659 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# Copyright 2011 The Android Open Source Project
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_JAVA_LIBRARIES := dx junit-host
LOCAL_MODULE_TAGS := tests
LOCAL_MODULE:= dx-tests
include $(BUILD_HOST_JAVA_LIBRARY)

View file

@ -0,0 +1,127 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dex;
import com.android.dex.util.ByteArrayByteInput;
import junit.framework.TestCase;
public final class EncodedValueReaderTest extends TestCase {
public void testReadByte() {
assertEquals((byte) 0x80, readerOf(0, 0x80).readByte());
assertEquals((byte) 0xff, readerOf(0, 0xff).readByte());
assertEquals((byte) 0x00, readerOf(0, 0x00).readByte());
assertEquals((byte) 0x01, readerOf(0, 0x01).readByte());
assertEquals((byte) 0x7f, readerOf(0, 0x7f).readByte());
}
public void testReadShort() {
assertEquals((short) 0x8000, readerOf(34, 0x00, 0x80).readShort());
assertEquals((short) 0, readerOf( 2, 0x00).readShort());
assertEquals((short) 0xab, readerOf(34, 0xab, 0x00).readShort());
assertEquals((short) 0xabcd, readerOf(34, 0xcd, 0xab).readShort());
assertEquals((short) 0x7FFF, readerOf(34, 0xff, 0x7f).readShort());
}
public void testReadInt() {
assertEquals(0x80000000, readerOf(100, 0x00, 0x00, 0x00, 0x80).readInt());
assertEquals( 0x00, readerOf( 4, 0x00).readInt());
assertEquals( 0xab, readerOf( 36, 0xab, 0x00).readInt());
assertEquals( 0xabcd, readerOf( 68, 0xcd, 0xab, 0x00).readInt());
assertEquals( 0xabcdef, readerOf(100, 0xef, 0xcd, 0xab, 0x00).readInt());
assertEquals(0xabcdef01, readerOf(100, 0x01, 0xef, 0xcd, 0xab).readInt());
assertEquals(0x7fffffff, readerOf(100, 0xff, 0xff, 0xff, 127).readInt());
}
public void testReadLong() {
assertEquals(0x8000000000000000L, readerOf( -26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80).readLong());
assertEquals( 0x00L, readerOf( 6, 0x00).readLong());
assertEquals( 0xabL, readerOf( 38, 0xab, 0x00).readLong());
assertEquals( 0xabcdL, readerOf( 70, 0xcd, 0xab, 0x00).readLong());
assertEquals( 0xabcdefL, readerOf( 102, 0xef, 0xcd, 0xab, 0x00).readLong());
assertEquals( 0xabcdef01L, readerOf(-122, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
assertEquals( 0xabcdef0123L, readerOf( -90, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
assertEquals( 0xabcdef012345L, readerOf( -58, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
assertEquals( 0xabcdef01234567L, readerOf( -26, 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x00).readLong());
assertEquals(0xabcdef0123456789L, readerOf( -26, 0x89, 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab).readLong());
assertEquals(0x7fffffffffffffffL, readerOf( -26, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f).readLong());
}
public void testReadFloat() {
assertEquals(Float.NEGATIVE_INFINITY, readerOf(48, -128, -1).readFloat());
assertEquals(Float.POSITIVE_INFINITY, readerOf(48, -128, 127).readFloat());
assertEquals(Float.NaN, readerOf(48, -64, 127).readFloat());
assertEquals(-0.0f, readerOf(16, -128).readFloat());
assertEquals(0.0f, readerOf(16, 0).readFloat());
assertEquals(0.5f, readerOf(16, 63).readFloat());
assertEquals(1f, readerOf(48, -128, 63).readFloat());
assertEquals(1.0E06f, readerOf(80, 36, 116, 73).readFloat());
assertEquals(1.0E12f, readerOf(112, -91, -44, 104, 83).readFloat());
}
public void testReadDouble() {
assertEquals(Double.NEGATIVE_INFINITY, readerOf(49, -16, -1).readDouble());
assertEquals(Double.POSITIVE_INFINITY, readerOf(49, -16, 127).readDouble());
assertEquals(Double.NaN, readerOf(49, -8, 127).readDouble());
assertEquals(-0.0, readerOf(17, -128).readDouble());
assertEquals(0.0, readerOf(17, 0).readDouble());
assertEquals(0.5, readerOf(49, -32, 63).readDouble());
assertEquals(1.0, readerOf(49, -16, 63).readDouble());
assertEquals(1.0E06, readerOf(113, -128, -124, 46, 65).readDouble());
assertEquals(1.0E12, readerOf(-111, -94, -108, 26, 109, 66).readDouble());
assertEquals(1.0E24, readerOf(-15, -76, -99, -39, 121, 67, 120, -22, 68).readDouble());
}
public void testReadChar() {
assertEquals('\u0000', readerOf( 3, 0x00).readChar());
assertEquals('\u00ab', readerOf( 3, 0xab).readChar());
assertEquals('\uabcd', readerOf(35, 0xcd, 0xab).readChar());
assertEquals('\uffff', readerOf(35, 0xff, 0xff).readChar());
}
public void testReadBoolean() {
assertEquals(true, readerOf(63).readBoolean());
assertEquals(false, readerOf(31).readBoolean());
}
public void testReadNull() {
readerOf(30).readNull();
}
public void testReadReference() {
assertEquals( 0xab, readerOf(0x17, 0xab).readString());
assertEquals( 0xabcd, readerOf(0x37, 0xcd, 0xab).readString());
assertEquals( 0xabcdef, readerOf(0x57, 0xef, 0xcd, 0xab).readString());
assertEquals(0xabcdef01, readerOf(0x77, 0x01, 0xef, 0xcd, 0xab).readString());
}
public void testReadWrongType() {
try {
readerOf(0x17, 0xab).readField();
fail();
} catch (IllegalStateException expected) {
}
}
private EncodedValueReader readerOf(int... bytes) {
byte[] data = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
data[i] = (byte) bytes[i];
}
return new EncodedValueReader(new ByteArrayByteInput(data));
}
}

View file

@ -0,0 +1,93 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.merge;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.android.dex.Dex;
import com.android.dx.command.Main;
import com.android.dx.command.dexer.DxContext;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class DexMergerTest {
static class NoFieldsClassA {
}
static class NoFieldsClassB {
}
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void test_merge_dexesWithEmptyFieldsSection() throws IOException {
List<Dex> outputDexes = new ArrayList<>();
outputDexes.add(getDexForClass(NoFieldsClassA.class));
outputDexes.add(getDexForClass(NoFieldsClassB.class));
Dex merged =
new DexMerger(
outputDexes.toArray(new Dex[outputDexes.size()]),
CollisionPolicy.FAIL,
new DxContext())
.merge();
assertNotNull(merged);
assertNotNull(merged.getTableOfContents());
assertEquals(0, merged.getTableOfContents().fieldIds.off);
}
private Dex getDexForClass(Class<?> clazz) throws IOException {
String path = clazz.getName().replace('.', '/') + ".class";
Path classesJar = temporaryFolder.newFile(clazz.getName() + ".jar").toPath();
try (InputStream in = getClass().getClassLoader().getResourceAsStream(path);
ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(classesJar))) {
ZipEntry entry = new ZipEntry(path);
zip.putNextEntry(entry);
zip.write(readEntireStream(in));
zip.closeEntry();
}
Path output = temporaryFolder.newFolder().toPath();
Main.main(new String[]{"--dex", "--output=" + output.toString(), classesJar.toString()});
return new Dex(Files.readAllBytes(output.resolve("classes.dex")));
}
private static byte[] readEntireStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int count;
while ((count = inputStream.read(buffer)) != -1) {
bytesOut.write(buffer, 0, count);
}
return bytesOut.toByteArray();
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.rop.cst;
import com.android.dx.rop.type.Type;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class CstTypeTest {
@Test
public void checkClearInternTable() {
CstType boolArray = CstType.BOOLEAN_ARRAY;
assertTrue(boolArray == CstType.intern(Type.BOOLEAN_ARRAY));
CstType myClass = CstType.intern(Type.intern("Lcom/example/Foo;"));
CstType.clearInternTable();
Type.clearInternTable();
assertTrue(boolArray == CstType.intern(Type.BOOLEAN_ARRAY));
CstType myClass2 = CstType.intern(Type.intern("Lcom/example/Foo;"));
assertEquals(myClass.getClassType(), myClass2.getClassType());
assertFalse(myClass == myClass2);
}
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.rop.type;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class TypeTest {
@Test
public void testClearInternTable() {
Type boolArray = Type.BOOLEAN.getArrayType();
assertTrue(boolArray == Type.BOOLEAN_ARRAY);
assertTrue(boolArray == Type.intern("[Z"));
Type myClass = Type.intern("Lcom/example/Foo;");
Type.clearInternTable();
assertTrue(boolArray == Type.intern("[Z"));
Type myClass2 = Type.intern("Lcom/example/Foo;");
assertEquals(myClass, myClass2);
assertFalse(myClass == myClass2);
}
}

View file

@ -0,0 +1,206 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.util;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
public final class BitIntSetTest extends TestCase {
public void test_basic() {
BitIntSet set = new BitIntSet(32);
assertEquals(0, set.elements());
set.add(0);
set.add(1);
set.add(31);
assertTrue(set.has(0));
assertTrue(set.has(1));
assertTrue(set.has(31));
assertEquals(3, set.elements());
assertFalse(set.has(2));
assertFalse(set.has(7));
assertFalse(set.has(30));
}
public void test_iterator() {
BitIntSet set = new BitIntSet(32);
set.add(0);
set.add(0);
set.add(1);
set.add(1);
set.add(31);
set.add(31);
IntIterator iter = set.iterator();
assertTrue(iter.hasNext());
assertEquals(iter.next(), 0);
assertTrue(iter.hasNext());
assertEquals(iter.next(), 1);
assertTrue(iter.hasNext());
assertEquals(iter.next(), 31);
assertFalse(iter.hasNext());
try {
iter.next();
fail();
} catch (NoSuchElementException ex) {
// exception excepted
}
}
public void test_remove() {
BitIntSet set = new BitIntSet(32);
set.add(0);
set.add(1);
set.add(31);
assertTrue(set.has(0));
assertTrue(set.has(1));
assertTrue(set.has(31));
assertFalse(set.has(2));
assertFalse(set.has(7));
assertFalse(set.has(30));
set.remove(0);
assertFalse(set.has(0));
assertTrue(set.has(1));
assertTrue(set.has(31));
}
/**
* Tests the auto-expansion of the set
*/
public void test_expand() {
BitIntSet set = new BitIntSet(32);
int[] values = {0, 1, 31, 32, 128};
for (int i = 0; i < values.length; i++) {
set.add(values[i]);
}
IntIterator iter = set.iterator();
for (int i = 0; i < values.length; i++) {
assertTrue(iter.hasNext());
assertEquals(values[i], iter.next());
}
assertFalse(iter.hasNext());
}
public void test_merge() {
BitIntSet setA = new BitIntSet(32);
int[] valuesA = {0, 1, 31};
for (int i = 0; i < valuesA.length; i++) {
setA.add(valuesA[i]);
}
BitIntSet setB = new BitIntSet(32);
int[] valuesB = {0, 5, 6, 8, 31};
for (int i = 0; i < valuesB.length; i++) {
setB.add(valuesB[i]);
}
setA.merge(setB);
for (int i = 0; i < valuesA.length; i++) {
assertTrue(setA.has(valuesA[i]));
}
for (int i = 0; i < valuesB.length; i++) {
assertTrue(setA.has(valuesB[i]));
}
}
public void test_mergeWithListIntSet() {
BitIntSet setA = new BitIntSet(32);
int[] valuesA = {0, 1, 31};
for (int i = 0; i < valuesA.length; i++) {
setA.add(valuesA[i]);
}
ListIntSet setB = new ListIntSet();
int[] valuesB = {0, 5, 6, 8, 31};
for (int i = 0; i < valuesB.length; i++) {
setB.add(valuesB[i]);
}
setA.merge(setB);
for (int i = 0; i < valuesA.length; i++) {
assertTrue(setA.has(valuesA[i]));
}
for (int i = 0; i < valuesB.length; i++) {
assertTrue(setA.has(valuesB[i]));
}
}
public void test_mergeAndExpand() {
BitIntSet setA = new BitIntSet(32);
int[] valuesA = {0, 1, 31};
for (int i = 0; i < valuesA.length; i++) {
setA.add(valuesA[i]);
}
BitIntSet setB = new BitIntSet(32);
int[] valuesB = {0, 5, 6, 32, 127};
for (int i = 0; i < valuesB.length; i++) {
setB.add(valuesB[i]);
}
setA.merge(setB);
for (int i = 0; i < valuesA.length; i++) {
assertTrue(setA.has(valuesA[i]));
}
for (int i = 0; i < valuesB.length; i++) {
assertTrue(setA.has(valuesB[i]));
}
}
public void test_toString() {
BitIntSet set = new BitIntSet(32);
assertEquals(set.toString(), "{}");
set.add(1);
assertEquals(set.toString(), "{1}");
set.add(2);
assertEquals(set.toString(), "{1, 2}");
}
}

View file

@ -0,0 +1,345 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.util;
import junit.framework.TestCase;
public final class BitsTest extends TestCase {
public void test_makeBitSet() {
assertEquals(label(0), 0, Bits.makeBitSet(0).length);
for (int i = 1; i <= 32; i++) {
assertEquals(label(i), 1, Bits.makeBitSet(i).length);
}
for (int i = 33; i <= 64; i++) {
assertEquals(label(i), 2, Bits.makeBitSet(i).length);
}
for (int i = 65; i < 4000; i += 101) {
int expect = i >> 5;
if ((expect * 32) < i) {
expect++;
}
assertEquals(label(i), expect, Bits.makeBitSet(i).length);
}
}
public void test_getMax() {
for (int i = 0; i < 4000; i += 59) {
int expect = i >> 5;
if ((expect * 32) < i) {
expect++;
}
assertEquals(label(i), expect * 32,
Bits.getMax(new int[expect]));
}
}
public void test1_get() {
int[] bits = Bits.makeBitSet(100);
for (int i = 0; i < 100; i++) {
assertFalse(label(i), Bits.get(bits, i));
}
}
public void test2_get() {
int[] bits = Bits.makeBitSet(100);
for (int i = 0; i < bits.length; i++) {
bits[i] = -1;
}
for (int i = 0; i < 100; i++) {
assertTrue(label(i), Bits.get(bits, i));
}
}
public void test3_get() {
int[] bits = Bits.makeBitSet(100);
for (int i = 0; i < 100; i++) {
Bits.set(bits, i, (i % 5) == 0);
}
for (int i = 0; i < 100; i++) {
boolean expect = (i % 5) == 0;
assertTrue(label(i), Bits.get(bits, i) == expect);
}
}
public void test1_set1() {
int[] bits = Bits.makeBitSet(50);
bits[1] = -1;
Bits.set(bits, 0, true);
Bits.set(bits, 3, true);
Bits.set(bits, 6, true);
Bits.set(bits, 3, false);
Bits.set(bits, 35, false);
Bits.set(bits, 38, false);
Bits.set(bits, 42, false);
Bits.set(bits, 38, true);
assertEquals(label(1), 0x41, bits[0]);
assertEquals(label(2), 0xfffffbf7, bits[1]);
}
public void test2_set1() {
int[] bits = Bits.makeBitSet(100);
for (int i = 0; i < 100; i++) {
if ((i % 3) == 0) {
Bits.set(bits, i, true);
}
}
for (int i = 0; i < 100; i++) {
if ((i % 5) == 0) {
Bits.set(bits, i, false);
}
}
for (int i = 0; i < 100; i++) {
if ((i % 7) == 0) {
Bits.set(bits, i, true);
}
}
for (int i = 0; i < 100; i++) {
boolean expect = ((i % 7) == 0) ||
(((i % 3) == 0) && ((i % 5) != 0));
assertTrue(label(i), Bits.get(bits, i) == expect);
}
}
public void test_set2() {
int[] bits = Bits.makeBitSet(100);
for (int i = 0; i < 100; i++) {
if ((i % 11) == 0) {
Bits.set(bits, i);
}
}
for (int i = 0; i < 100; i++) {
boolean expect = (i % 11) == 0;
assertTrue(label(i), Bits.get(bits, i) == expect);
}
}
public void test_clear() {
int[] bits = Bits.makeBitSet(100);
for (int i = 0; i < bits.length; i++) {
bits[i] = -1;
}
for (int i = 0; i < 100; i++) {
if ((i % 5) == 0) {
Bits.clear(bits, i);
}
}
for (int i = 0; i < 100; i++) {
boolean expect = (i % 5) != 0;
assertTrue(label(i), Bits.get(bits, i) == expect);
}
}
public void test1_isEmpty() {
for (int i = 0; i < 10; i++) {
assertTrue(label(i), Bits.isEmpty(new int[i]));
}
}
public void test2_isEmpty() {
for (int i = 1; i < 1000; i += 11) {
int[] bits = Bits.makeBitSet(i);
for (int j = i % 11; j >= 0; j--) {
int x = i - 1 - (j * 13);
if (x >= 0) {
Bits.set(bits, x);
}
}
assertFalse(label(i), Bits.isEmpty(bits));
}
}
public void test1_bitCount() {
for (int i = 0; i < 10; i++) {
assertEquals(label(i), 0, Bits.bitCount(new int[i]));
}
}
public void test2_bitCount() {
for (int i = 1; i < 1000; i += 13) {
int[] bits = Bits.makeBitSet(i);
int count = 0;
for (int j = 0; j < i; j += 20) {
Bits.set(bits, j);
count++;
}
for (int j = 7; j < i; j += 11) {
if (!Bits.get(bits, j)) {
Bits.set(bits, j);
count++;
}
}
for (int j = 3; j < i; j += 17) {
if (!Bits.get(bits, j)) {
Bits.set(bits, j);
count++;
}
}
assertEquals(label(i), count, Bits.bitCount(bits));
}
}
public void test1_anyInRange() {
int[] bits = new int[100];
for (int i = 0; i < 100; i += 11) {
assertFalse(label(i), Bits.anyInRange(bits, 0, i));
}
}
public void test2_anyInRange() {
int[] bits = new int[100];
for (int i = 0; i < 100; i += 11) {
assertFalse(label(i), Bits.anyInRange(bits, i, 100));
}
}
public void test3_anyInRange() {
int[] bits = new int[100];
for (int i = 0; i < 50; i += 7) {
assertFalse(label(i), Bits.anyInRange(bits, i, 100 - i));
}
}
public void test4_anyInRange() {
int[] bits = new int[100];
for (int i = 0; i < bits.length; i++) {
bits[i] = -1;
}
for (int i = 1; i < 100; i += 11) {
assertTrue(label(i), Bits.anyInRange(bits, 0, i));
}
}
public void test5_anyInRange() {
int[] bits = new int[100];
for (int i = 0; i < bits.length; i++) {
bits[i] = -1;
}
for (int i = 1; i < 100; i += 11) {
assertTrue(label(i), Bits.anyInRange(bits, i, 100));
}
}
public void test6_anyInRange() {
int[] bits = new int[100];
for (int i = 0; i < bits.length; i++) {
bits[i] = -1;
}
for (int i = 0; i < 50; i += 7) {
assertTrue(label(i), Bits.anyInRange(bits, i, 100 - i));
}
}
public void test1_findFirst1() {
int[] bits = new int[100];
for (int i = 0; i < 100; i++) {
assertEquals(label(i), -1, Bits.findFirst(bits, i));
}
}
public void test2_findFirst1() {
int[] bits = new int[100];
for (int i = 0; i < bits.length; i++) {
bits[i] = -1;
}
for (int i = 0; i < 100; i++) {
assertEquals(label(i), i, Bits.findFirst(bits, i));
}
}
public void test3_findFirst1() {
int[] bits = new int[100];
for (int i = 25; i < 80; i++) {
for (int j = 0; j < bits.length; j++) {
bits[j] = 0;
}
Bits.set(bits, i - 5);
Bits.set(bits, i + 5);
Bits.set(bits, i + 10);
Bits.set(bits, i + 20);
assertEquals(label(i), i + 5, Bits.findFirst(bits, i));
}
}
public void test1_findFirst2() {
for (int i = 0; i < 32; i++) {
assertEquals(label(i), -1, Bits.findFirst(0, i));
}
}
public void test2_findFirst2() {
for (int i = 0; i < 32; i++) {
assertEquals(label(i), i, Bits.findFirst(-1, i));
}
}
public void test3_findFirst2() {
for (int i = 0; i < 32; i++) {
assertEquals(label(i), -1, Bits.findFirst((1 << i) >>> 1, i));
}
}
public void test4_findFirst2() {
for (int i = 0; i < 32; i++) {
assertEquals(label(i), i, Bits.findFirst(1 << i, i));
}
}
public void test5_findFirst2() {
for (int i = 0; i < 31; i++) {
assertEquals(label(i), i + 1, Bits.findFirst(1 << (i + 1), i));
}
}
public void test6_findFirst2() {
for (int i = 0; i < 32; i++) {
int value = (1 << i);
value |= (value >>> 1);
assertEquals(label(i), i, Bits.findFirst(value, i));
}
}
private static String label(int n) {
return "(" + n + ")";
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.util;
import junit.framework.TestCase;
public final class IntListTest extends TestCase {
public void test_contains() {
for (int sz = 0; sz < 100; sz++) {
IntList list = new IntList(sz);
for (int i = 0; i < sz; i++) {
list.add(i * 2);
}
for (int i = (sz * 2) - 1; i >= 0; i--) {
boolean contains = list.contains(i);
if ((i & 1) == 0) {
assertTrue(label(sz, i), contains);
} else {
assertFalse(label(sz, i), contains);
}
}
assertFalse(label(sz, -1), list.contains(-1));
assertFalse(label(sz, sz * 2), list.contains(sz * 2));
}
}
public void test_addSorted() {
IntList list = new IntList(2);
list.add(9);
list.add(12);
assertTrue(list.contains(9));
assertTrue(list.contains(12));
}
public void test_addUnsorted() {
IntList list = new IntList(2);
list.add(12);
list.add(9);
assertTrue(list.contains(12));
assertTrue(list.contains(9));
}
private static String label(int n, int m) {
return "(" + n + "/" + m + ")";
}
}

View file

@ -0,0 +1,198 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dx.util;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
public final class ListIntSetTest extends TestCase {
public void test_basic() {
ListIntSet set = new ListIntSet();
assertEquals(0, set.elements());
set.add(31);
set.add(0);
set.add(1);
assertTrue(set.has(0));
assertTrue(set.has(1));
assertTrue(set.has(31));
assertEquals(3, set.elements());
assertFalse(set.has(2));
assertFalse(set.has(7));
assertFalse(set.has(30));
}
public void test_iterator() {
ListIntSet set = new ListIntSet();
set.add(0);
set.add(0);
set.add(1);
set.add(1);
set.add(31);
set.add(31);
IntIterator iter = set.iterator();
assertTrue(iter.hasNext());
assertEquals(iter.next(), 0);
assertTrue(iter.hasNext());
assertEquals(iter.next(), 1);
assertTrue(iter.hasNext());
assertEquals(iter.next(), 31);
assertFalse(iter.hasNext());
try {
iter.next();
fail();
} catch (NoSuchElementException ex) {
// exception excepted
}
}
public void test_empty() {
ListIntSet set = new ListIntSet();
IntIterator iter = set.iterator();
assertFalse(iter.hasNext());
}
public void test_remove() {
ListIntSet set = new ListIntSet();
set.add(0);
set.add(1);
set.add(31);
assertTrue(set.has(0));
assertTrue(set.has(1));
assertTrue(set.has(31));
assertFalse(set.has(2));
assertFalse(set.has(7));
assertFalse(set.has(30));
set.remove(0);
assertFalse(set.has(0));
assertTrue(set.has(1));
assertTrue(set.has(31));
}
public void test_mergeA() {
ListIntSet setA = new ListIntSet();
int[] valuesA = {0, 1, 31};
for (int i = 0; i < valuesA.length; i++) {
setA.add(valuesA[i]);
}
ListIntSet setB = new ListIntSet();
int[] valuesB = {0, 5, 6, 32, 127, 128};
for (int i = 0; i < valuesB.length; i++) {
setB.add(valuesB[i]);
}
setA.merge(setB);
for (int i = 0; i < valuesA.length; i++) {
assertTrue(setA.has(valuesA[i]));
}
for (int i = 0; i < valuesB.length; i++) {
assertTrue(setA.has(valuesB[i]));
}
}
public void test_mergeB() {
ListIntSet setA = new ListIntSet();
int[] valuesA = {0, 1, 31, 129, 130};
for (int i = 0; i < valuesA.length; i++) {
setA.add(valuesA[i]);
}
ListIntSet setB = new ListIntSet();
int[] valuesB = {0, 5, 6, 32, 127,128};
for (int i = 0; i < valuesB.length; i++) {
setB.add(valuesB[i]);
}
setA.merge(setB);
for (int i = 0; i < valuesA.length; i++) {
assertTrue(setA.has(valuesA[i]));
}
for (int i = 0; i < valuesB.length; i++) {
assertTrue(setA.has(valuesB[i]));
}
}
public void test_mergeWithBitIntSet() {
ListIntSet setA = new ListIntSet();
int[] valuesA = {0, 1, 31, 129, 130};
for (int i = 0; i < valuesA.length; i++) {
setA.add(valuesA[i]);
}
BitIntSet setB = new BitIntSet(129);
int[] valuesB = {0, 5, 6, 32, 127,128};
for (int i = 0; i < valuesB.length; i++) {
setB.add(valuesB[i]);
}
setA.merge(setB);
for (int i = 0; i < valuesA.length; i++) {
assertTrue(setA.has(valuesA[i]));
}
for (int i = 0; i < valuesB.length; i++) {
assertTrue(setA.has(valuesB[i]));
}
}
public void test_toString() {
ListIntSet set = new ListIntSet();
assertEquals(set.toString(), "{}");
set.add(1);
assertEquals(set.toString(), "{1}");
set.add(2);
assertEquals(set.toString(), "{1, 2}");
}
}