diff options
author | Jesse Wilson <jessewilson@google.com> | 2011-12-13 13:33:35 -0500 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2011-12-13 13:33:35 -0500 |
commit | 0647bfed6eda99ad77b2dfe8e3696e3fabfaf3cc (patch) | |
tree | 6cb7ceab117a5928e7be992748fef054dfc562a7 /luni | |
parent | 56de8e10ca290d46c26831d298d5cc7673a168ff (diff) | |
download | libcore-0647bfed6eda99ad77b2dfe8e3696e3fabfaf3cc.zip libcore-0647bfed6eda99ad77b2dfe8e3696e3fabfaf3cc.tar.gz libcore-0647bfed6eda99ad77b2dfe8e3696e3fabfaf3cc.tar.bz2 |
Bring more tests from frameworks/base/tests/CoreTests into libcore.
Bug: http://b/3073226
Change-Id: I66f88f2bfaadd39e53b2977606731e8579d880b6
Diffstat (limited to 'luni')
7 files changed, 615 insertions, 5 deletions
diff --git a/luni/src/test/java/libcore/java/io/CharArrayWriterTest.java b/luni/src/test/java/libcore/java/io/CharArrayWriterTest.java new file mode 100644 index 0000000..7e4dd42 --- /dev/null +++ b/luni/src/test/java/libcore/java/io/CharArrayWriterTest.java @@ -0,0 +1,41 @@ +/* + * 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 libcore.java.io; + +import java.io.CharArrayWriter; +import junit.framework.TestCase; + +public final class CharArrayWriterTest extends TestCase { + public void testCharArrayWriter() throws Exception { + String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; + CharArrayWriter a = new CharArrayWriter(); + CharArrayWriter b = new CharArrayWriter(); + + a.write(str, 0, 26); + a.write('X'); + a.writeTo(b); + + assertEquals(27, a.size()); + assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzX", a.toString()); + + b.write("alphabravodelta", 5, 5); + b.append('X'); + assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoX", b.toString()); + b.append("omega"); + assertEquals("AbCdEfGhIjKlMnOpQrStUvWxYzXbravoXomega", b.toString()); + } +} diff --git a/luni/src/test/java/libcore/java/lang/ThreadTest.java b/luni/src/test/java/libcore/java/lang/ThreadTest.java index 42a18ef..998afdb 100644 --- a/luni/src/test/java/libcore/java/lang/ThreadTest.java +++ b/luni/src/test/java/libcore/java/lang/ThreadTest.java @@ -17,11 +17,21 @@ package libcore.java.lang; import java.util.concurrent.atomic.AtomicInteger; +import junit.framework.Assert; import junit.framework.TestCase; import libcore.java.lang.ref.FinalizationTester; public final class ThreadTest extends TestCase { + /** + * getContextClassLoader returned a non-application class loader. + * http://code.google.com/p/android/issues/detail?id=5697 + */ + public void testJavaContextClassLoader() throws Exception { + Assert.assertNotNull("Must have a Java context ClassLoader", + Thread.currentThread().getContextClassLoader()); + } + public void testLeakingStartedThreads() { final AtomicInteger finalizedThreadsCount = new AtomicInteger(); for (int i = 0; true; i++) { @@ -57,4 +67,46 @@ public final class ThreadTest extends TestCase { } }; } + + /** + * Thread.getStackTrace() is broken. http://b/1252043 + */ + public void testGetStackTrace() throws Exception { + Thread t1 = new Thread("t1") { + @Override public void run() { + doSomething(); + } + public void doSomething() { + for (int i = 0; i < 20;) { + try { + Thread.sleep(100); + } catch (InterruptedException ignored) { + } + } + } + }; + t1.start(); + Thread.sleep(1000); + StackTraceElement[] traces = t1.getStackTrace(); + StackTraceElement trace = traces[traces.length - 2]; + + // Expect to find MyThread.doSomething in the trace + assertTrue(trace.getClassName().contains("ThreadTest") + && trace.getMethodName().equals("doSomething")); + } + + public void testGetAllStackTracesIncludesAllGroups() throws Exception { + final AtomicInteger visibleTraces = new AtomicInteger(); + ThreadGroup group = new ThreadGroup("1"); + Thread t2 = new Thread(group, "t2") { + @Override public void run() { + visibleTraces.set(Thread.getAllStackTraces().size()); + } + }; + t2.start(); + t2.join(); + + // Expect to see the traces of all threads (not just t2) + assertTrue("Must have traces for all threads", visibleTraces.get() > 1); + } } diff --git a/luni/src/test/java/libcore/java/lang/reflect/FieldTest.java b/luni/src/test/java/libcore/java/lang/reflect/FieldTest.java new file mode 100644 index 0000000..eb3d034 --- /dev/null +++ b/luni/src/test/java/libcore/java/lang/reflect/FieldTest.java @@ -0,0 +1,30 @@ +/* + * 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 libcore.java.lang.reflect; + +import java.lang.reflect.Field; +import junit.framework.TestCase; + +public final class FieldTest extends TestCase { + private static final long MY_LONG = 5073258162644648461L; + + // Reflection for static long fields was broken http://b/1120750 + public void testLongFieldReflection() throws Exception { + Field field = getClass().getDeclaredField("MY_LONG"); + assertEquals(5073258162644648461L, field.getLong(null)); + } +} diff --git a/luni/src/test/java/libcore/java/lang/reflect/ReflectionTest.java b/luni/src/test/java/libcore/java/lang/reflect/ReflectionTest.java index 3b092e5..ffdd569 100644 --- a/luni/src/test/java/libcore/java/lang/reflect/ReflectionTest.java +++ b/luni/src/test/java/libcore/java/lang/reflect/ReflectionTest.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; import java.util.RandomAccess; import java.util.Set; +import junit.framework.Assert; import junit.framework.TestCase; public final class ReflectionTest extends TestCase { @@ -254,6 +255,28 @@ public final class ReflectionTest extends TestCase { assertEquals(1, count(names(fields), "field")); } + /** + * Class.isEnum() erroneously returned true for indirect descendants of + * Enum. http://b/1062200. + */ + public void testClassIsEnum() { + Class<?> trafficClass = TrafficLights.class; + Class<?> redClass = TrafficLights.RED.getClass(); + Class<?> yellowClass = TrafficLights.YELLOW.getClass(); + Class<?> greenClass = TrafficLights.GREEN.getClass(); + assertSame(trafficClass, redClass); + assertNotSame(trafficClass, yellowClass); + assertNotSame(trafficClass, greenClass); + assertNotSame(yellowClass, greenClass); + assertTrue(trafficClass.isEnum()); + assertTrue(redClass.isEnum()); + assertFalse(yellowClass.isEnum()); + assertFalse(greenClass.isEnum()); + assertNotNull(trafficClass.getEnumConstants()); + assertNull(yellowClass.getEnumConstants()); + assertNull(greenClass.getEnumConstants()); + } + static class A {} static class AList extends ArrayList<A> {} @@ -319,4 +342,15 @@ public final class ReflectionTest extends TestCase { } return result; } + + enum TrafficLights { + RED, + YELLOW {}, + GREEN { + @SuppressWarnings("unused") + int i; + @SuppressWarnings("unused") + void foobar() {} + } + } } diff --git a/luni/src/test/java/libcore/java/security/KeyStoreTest.java b/luni/src/test/java/libcore/java/security/KeyStoreTest.java index 0a4d966..65250de 100644 --- a/luni/src/test/java/libcore/java/security/KeyStoreTest.java +++ b/luni/src/test/java/libcore/java/security/KeyStoreTest.java @@ -1208,7 +1208,7 @@ public class KeyStoreTest extends TestCase { assertTrue(keyStore.aliases().hasMoreElements()); } else { assertEquals(Collections.EMPTY_SET, - new HashSet(Collections.list(keyStore.aliases()))); + new HashSet(Collections.list(keyStore.aliases()))); } } @@ -1396,14 +1396,14 @@ public class KeyStoreTest extends TestCase { assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE)); assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET)); assertEquals(isCertificateEnabled(keyStore) && !isReadOnly(keyStore), - keyStore.isCertificateEntry(ALIAS_CERTIFICATE)); + keyStore.isCertificateEntry(ALIAS_CERTIFICATE)); assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE)); assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET)); assertEquals(!isCaseSensitive(keyStore) - && isCertificateEnabled(keyStore) - && !isReadOnly(keyStore), - keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE)); + && isCertificateEnabled(keyStore) + && !isReadOnly(keyStore), + keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE)); } } @@ -2183,4 +2183,24 @@ public class KeyStoreTest extends TestCase { } } } + + // http://b/857840: want JKS key store + public void testDefaultKeystore() { + String type = KeyStore.getDefaultType(); + assertEquals("Default keystore type must be Bouncy Castle", "BKS", type); + + try { + KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType()); + assertNotNull("Keystore must not be null", store); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + + try { + KeyStore store = KeyStore.getInstance("BKS"); + assertNotNull("Keystore must not be null", store); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } } diff --git a/luni/src/test/java/libcore/java/util/regex/OldAndroidRegexTest.java b/luni/src/test/java/libcore/java/util/regex/OldAndroidRegexTest.java new file mode 100644 index 0000000..9d4c22d --- /dev/null +++ b/luni/src/test/java/libcore/java/util/regex/OldAndroidRegexTest.java @@ -0,0 +1,265 @@ +/* + * 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 libcore.java.util.regex; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import junit.framework.TestCase; + +public final class OldAndroidRegexTest extends TestCase { + public void testMatches() throws Exception { + /* Tests class Matcher */ + + Pattern p = Pattern.compile("bcd"); + Matcher m = p.matcher("bcd"); + assertTrue("Should match.", m.matches()); + + /* Pattern in the middle */ + p = Pattern.compile("bcd"); + m = p.matcher("abcdefg"); + assertFalse("Should not match.", m.matches()); + + /* Pattern at the head */ + m = p.matcher("bcdefg"); + assertFalse("Should not match.", m.matches()); + + /* Pattern at the tail */ + m = p.matcher("abcd"); + assertFalse("Should not match.", m.matches()); + + /* Make sure matches() doesn't change after calls to find() */ + p = Pattern.compile(".*"); + m = p.matcher("abc"); + assertTrue(m.matches()); + assertTrue(m.find()); + assertTrue(m.matches()); + + p = Pattern.compile("."); + m = p.matcher("abc"); + assertFalse(m.matches()); + assertTrue(m.find()); + assertFalse(m.matches()); + + /* Make sure matches() agrees after a reset() */ + m.reset("z"); + assertTrue(m.matches()); + + m.reset("xyz"); + assertFalse(m.matches()); + + /* Tests class Pattern */ + + assertFalse("Erroneously matched partial string. " + + "See http://b/issue?id=754601", Pattern.matches("er", "xer")); + assertFalse("Erroneously matched partial string. " + + "See http://b/issue?id=754601", Pattern.matches("xe", "xer")); + assertTrue("Generic regex should match.", + Pattern.matches(".*", "bcd")); + assertTrue("Grouped regex should match.", + Pattern.matches("(b(c(d)))", "bcd")); + assertTrue("Grouped regex should match.", + Pattern.matches("(b)(c)(d)", "bcd")); + } + + public void testGroupCount() throws Exception { + Pattern p = Pattern.compile( + "\\b(?:\\+?1)?" + + "(?:[ -\\.])?" + + "\\(?(\\d{3})?\\)?" + + "(?:[ -\\.\\/])?" + + "(\\d{3})" + + "(?:[ -\\.])?" + + "(\\d{4})\\b" + ); + + Matcher m = p.matcher("1 (919) 555-1212"); + + assertEquals("groupCount is incorrect, see http://b/issue?id=759412", + 3, m.groupCount()); + } + + public void testGroups() throws Exception { + Pattern p = Pattern.compile("(b)([c|d])(z*)"); + Matcher m = p.matcher("abcdefg"); + + /* Must call find() first, otherwise group*() are undefined. */ + assertTrue(m.find()); + + assertEquals(3, m.groupCount()); + + assertEquals("bc", m.group(0)); + assertEquals("b", m.group(1)); + assertEquals("c", m.group(2)); + assertEquals("", m.group(3)); + } + + public void testFind() throws Exception { + Pattern p = Pattern.compile("."); + Matcher m = p.matcher("abc"); + + assertTrue(m.find()); + assertEquals("a", m.group(0)); + + assertTrue(m.find()); + assertEquals("b", m.group(0)); + + assertTrue(m.find()); + assertEquals("c", m.group(0)); + + assertFalse(m.find()); + } + + public void testReplaceAll() throws Exception { + // Begins with non-matching text, ends with matching text + Pattern p = Pattern.compile("a*b"); + Matcher m = p.matcher("fooaabfooaabfooabfoob"); + + String r = m.replaceAll("-"); + assertEquals("foo-foo-foo-foo-", r); + + // Begins with matching text, ends with non-matching text + p = Pattern.compile("a*b"); + m = p.matcher("aabfooaabfooabfoobfoo"); + + r = m.replaceAll("-"); + assertEquals("-foo-foo-foo-foo", r); + } + + public void testReplaceFirst() throws Exception { + // Begins with non-matching text, ends with matching text + Pattern p = Pattern.compile("a*b"); + Matcher m = p.matcher("fooaabfooaabfooabfoob"); + + String r = m.replaceFirst("-"); + assertEquals("foo-fooaabfooabfoob", r); + + // Begins with matching text, ends with non-matching text + p = Pattern.compile("a*b"); + m = p.matcher("aabfooaabfooabfoobfoo"); + + r = m.replaceFirst("-"); + assertEquals("-fooaabfooabfoobfoo", r); + } + + public void testSplit() throws Exception { + Pattern p = Pattern.compile(":"); + String[] strings; + + strings = p.split("boo:and:foo"); + assertEquals(3, strings.length); + assertEquals("boo", strings[0]); + assertEquals("and", strings[1]); + assertEquals("foo", strings[2]); + + strings = p.split("boo:and:foo", 2); + assertEquals(2, strings.length); + assertEquals("boo", strings[0]); + assertEquals("and:foo", strings[1]); + + strings = p.split("boo:and:foo", 5); + assertEquals(3, strings.length); + assertEquals("boo", strings[0]); + assertEquals("and", strings[1]); + assertEquals("foo", strings[2]); + + strings = p.split("boo:and:foo", -2); + assertEquals(3, strings.length); + assertEquals("boo", strings[0]); + assertEquals("and", strings[1]); + assertEquals("foo", strings[2]); + + p = Pattern.compile("o"); + + strings = p.split("boo:and:foo"); + assertEquals(3, strings.length); + assertEquals("b", strings[0]); + assertEquals("", strings[1]); + assertEquals(":and:f", strings[2]); + + strings = p.split("boo:and:foo", 5); + assertEquals(5, strings.length); + assertEquals("b", strings[0]); + assertEquals("", strings[1]); + assertEquals(":and:f", strings[2]); + assertEquals("", strings[3]); + assertEquals("", strings[4]); + + strings = p.split("boo:and:foo", -2); + assertEquals(5, strings.length); + assertEquals("b", strings[0]); + assertEquals("", strings[1]); + assertEquals(":and:f", strings[2]); + assertEquals("", strings[3]); + assertEquals("", strings[4]); + + strings = p.split("boo:and:foo", 0); + assertEquals(3, strings.length); + assertEquals("b", strings[0]); + assertEquals("", strings[1]); + assertEquals(":and:f", strings[2]); + } + + // ------------------------------------------------------------------- + // Regression test for #1172774: Bug in Regex.java + // Regression test for #1216887: Regular expression match is very slow + public static final Pattern TOP_LEVEL_DOMAIN_PATTERN = Pattern.compile( + "((aero|arpa|asia|a[cdefgilmnoqrstuwxz])" + + "|(biz|b[abdefghijmnorstvwyz])" + + "|(cat|com|coop|c[acdfghiklmnoruvxyz])" + + "|d[ejkmoz]" + + "|(edu|e[cegrstu])" + + "|f[ijkmor]" + + "|(gov|g[abdefghilmnpqrstuwy])" + + "|h[kmnrtu]" + + "|(info|int|i[delmnoqrst])" + + "|(jobs|j[emop])" + + "|k[eghimnrwyz]" + + "|l[abcikrstuvy]" + + "|(mil|mobi|museum|m[acdghklmnopqrstuvwxyz])" + + "|(name|net|n[acefgilopruz])" + + "|(org|om)" + + "|(pro|p[aefghklmnrstwy])" + + "|qa" + + "|r[eouw]" + + "|s[abcdeghijklmnortuvyz]" + + "|(tel|travel|t[cdfghjklmnoprtvwz])" + + "|u[agkmsyz]" + + "|v[aceginu]" + + "|w[fs]" + + "|y[etu]" + + "|z[amw])"); + + public static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile( + "[\\+a-zA-Z0-9\\.\\_\\%\\-]+\\@" + + "((" + + "[a-zA-Z0-9]\\.|" + + "([a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]\\.)+)" + + TOP_LEVEL_DOMAIN_PATTERN + + ")"); + + public void testMonsterRegexCorrectness() { + assertTrue(EMAIL_ADDRESS_PATTERN.matcher("a+b@gmail.com").matches()); + } + + public void testMonsterRegexPerformance() { + long t0 = System.currentTimeMillis(); + Matcher m = EMAIL_ADDRESS_PATTERN.matcher("donot repeate@RC8jjjjjjjjjjjjjjj"); + assertFalse(m.find()); + long t1 = System.currentTimeMillis(); + System.out.println("RegEx performance test finished, took " + (t1 - t0) + " ms."); + } +} diff --git a/luni/src/test/java/libcore/java/util/zip/OldAndroidZipStressTest.java b/luni/src/test/java/libcore/java/util/zip/OldAndroidZipStressTest.java new file mode 100644 index 0000000..9ffcadd --- /dev/null +++ b/luni/src/test/java/libcore/java/util/zip/OldAndroidZipStressTest.java @@ -0,0 +1,168 @@ +/* + * 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 libcore.java.util.zip; + +import java.io.File; +import java.io.InputStream; +import java.security.cert.Certificate; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.Random; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.zip.Deflater; +import java.util.zip.Inflater; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import junit.framework.TestCase; + +public final class OldAndroidZipStressTest extends TestCase { + /** + * JarEntry.getCertificates() is really slow. http://b/1046174 + */ + public void checkJarCertificates(File file) throws Exception { + JarFile jarFile = new JarFile(file); + JarEntry je = jarFile.getJarEntry("AndroidManifest.xml"); + byte[] readBuffer = new byte[1024]; + + long t0 = System.currentTimeMillis(); + + // We must read the stream for the JarEntry to retrieve its certificates. + InputStream is = jarFile.getInputStream(je); + while (is.read(readBuffer, 0, readBuffer.length) != -1) { + } + is.close(); + Certificate[] certs = je != null ? je.getCertificates() : null; + + long t1 = System.currentTimeMillis(); + System.out.println("loadCertificates() took " + (t1 - t0) + " ms"); + if (certs == null) { + System.out.println("We have no certificates"); + } else { + System.out.println("We have " + certs.length + " certificates"); + } + } + + private File[] getFiles() { + File[] result = new File("/system/app").listFiles(); + return result != null ? result : new File[0]; + } + + public void testJarCertificates() throws Exception { + for (File file : getFiles()) { + checkJarCertificates(file); + } + } + + // Boot-time package scan is slow. Not expected to fail. Please see log if + // you are interested in the results. http://b/1212257 + public void testZipStressManifest() throws Exception { + long time0 = System.currentTimeMillis(); + byte[] buffer = new byte[512]; + for (File file : getFiles()) { + System.out.println("ZIP stress test processing " + file + "..."); + + ZipFile zip = new ZipFile(file); + ZipEntry entry = zip.getEntry("AndroidManifest.xml"); + InputStream stream = zip.getInputStream(entry); + int j = stream.read(buffer); + while (j != -1) { + j = stream.read(buffer); + } + stream.close(); + } + long time1 = System.currentTimeMillis(); + System.out.println("ZIP stress test finished, time was " + (time1- time0) + "ms"); + } + + public void testZipStressAllFiles() throws Exception { + long time0 = System.currentTimeMillis(); + byte[] buffer = new byte[512]; + for (File file : getFiles()) { + System.out.println("ZIP stress test processing " + file + "..."); + ZipFile zip = new ZipFile(file); + Enumeration<? extends ZipEntry> entries = zip.entries(); + while (entries.hasMoreElements()) { + InputStream stream = zip.getInputStream(entries.nextElement()); + int j = stream.read(buffer); + while (j != -1) { + j = stream.read(buffer); + } + stream.close(); + } + } + long time1 = System.currentTimeMillis(); + System.out.println("ZIP stress test finished, time was " + (time1- time0) + "ms"); + } + + private void assertEquals(byte[] a, byte[] b) { + assertTrue(Arrays.equals(a, b)); + } + + /** + * Native memory allocated by Deflater in system_server. The fix reduced + * some internal ZLIB buffers in size, so this test is trying to execute a + * lot of deflating to ensure that things are still working properly. + * http://b/1185084 + */ + public void testZipDeflateInflateStress() throws Exception { + final int DATA_SIZE = 16384; + Random random = new Random(42); // Seed makes test reproducible + // Outer loop selects "mode" of test. + for (int j = 1; j <= 2; j++) { + byte[] input = new byte[DATA_SIZE]; + + if (j == 1) { + // Totally random content + random.nextBytes(input); + } else { + // Random contents with longer repetitions + int pos = 0; + while (pos < input.length) { + byte what = (byte)random.nextInt(256); + int howMany = random.nextInt(32); + if (pos + howMany >= input.length) { + howMany = input.length - pos; + } + Arrays.fill(input, pos, pos + howMany, what); + pos += howMany; + } + } + + // Inner loop tries all 9 compression levels. + for (int i = 1; i <= 9; i++) { + System.out.println("ZipDeflateInflateStress test (" + j + "," + i + ")..."); + byte[] zipped = new byte[2 * DATA_SIZE]; // Just to make sure... + + Deflater deflater = new Deflater(i); + deflater.setInput(input); + deflater.finish(); + deflater.deflate(zipped); + deflater.end(); + + byte[] output = new byte[DATA_SIZE]; + + Inflater inflater = new Inflater(); + inflater.setInput(zipped); + inflater.finished(); + inflater.inflate(output); + inflater.end(); + assertEquals(input, output); + } + } + } +} |