From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 02/15] Add tests for TemporaryBuffer
Date: Thu, 11 Dec 2008 18:46:08 -0800 [thread overview]
Message-ID: <1229049981-14152-3-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1229049981-14152-2-git-send-email-spearce@spearce.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
.../org/spearce/jgit/util/TemporaryBufferTest.java | 374 ++++++++++++++++++++
.../tst/org/spearce/jgit/util/TestRng.java | 61 ++++
.../src/org/spearce/jgit/util/TemporaryBuffer.java | 4 +-
3 files changed, 437 insertions(+), 2 deletions(-)
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/util/TemporaryBufferTest.java
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/util/TestRng.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/util/TemporaryBufferTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/util/TemporaryBufferTest.java
new file mode 100644
index 0000000..e532d98
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/util/TemporaryBufferTest.java
@@ -0,0 +1,374 @@
+/*
+ * Copyright (C) 2008, Google Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class TemporaryBufferTest extends TestCase {
+ public void testEmpty() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ try {
+ b.close();
+ assertEquals(0, b.length());
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(0, r.length);
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testOneByte() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte test = (byte) new TestRng(getName()).nextInt();
+ try {
+ b.write(test);
+ b.close();
+ assertEquals(1, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(1, r.length);
+ assertEquals(test, r[0]);
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(1, r.length);
+ assertEquals(test, r[0]);
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testOneBlock_BulkWrite() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.Block.SZ);
+ try {
+ b.write(test, 0, 2);
+ b.write(test, 2, 4);
+ b.write(test, 6, test.length - 6 - 2);
+ b.write(test, test.length - 2, 2);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testOneBlockAndHalf_BulkWrite() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
+ try {
+ b.write(test, 0, 2);
+ b.write(test, 2, 4);
+ b.write(test, 6, test.length - 6 - 2);
+ b.write(test, test.length - 2, 2);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testOneBlockAndHalf_SingleWrite() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
+ try {
+ for (int i = 0; i < test.length; i++)
+ b.write(test[i]);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testOneBlockAndHalf_Copy() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.Block.SZ * 3 / 2);
+ try {
+ final ByteArrayInputStream in = new ByteArrayInputStream(test);
+ b.write(in.read());
+ b.copy(in);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testLarge_SingleWrite() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
+ try {
+ b.write(test);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testInCoreLimit_SwitchOnAppendByte() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT + 1);
+ try {
+ b.write(test, 0, test.length - 1);
+ b.write(test[test.length - 1]);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testInCoreLimit_SwitchBeforeAppendByte() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 3);
+ try {
+ b.write(test, 0, test.length - 1);
+ b.write(test[test.length - 1]);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testInCoreLimit_SwitchOnCopy() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final byte[] test = new TestRng(getName())
+ .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2);
+ try {
+ final ByteArrayInputStream in = new ByteArrayInputStream(test,
+ TemporaryBuffer.DEFAULT_IN_CORE_LIMIT, test.length
+ - TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
+ b.write(test, 0, TemporaryBuffer.DEFAULT_IN_CORE_LIMIT);
+ b.copy(in);
+ b.close();
+ assertEquals(test.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(test.length, r.length);
+ assertTrue(Arrays.equals(test, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testDestroyWhileOpen() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ try {
+ b.write(new TestRng(getName())
+ .nextBytes(TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2));
+ } finally {
+ b.destroy();
+ }
+ }
+
+ public void testRandomWrites() throws IOException {
+ final TemporaryBuffer b = new TemporaryBuffer();
+ final TestRng rng = new TestRng(getName());
+ final int max = TemporaryBuffer.DEFAULT_IN_CORE_LIMIT * 2;
+ final byte[] expect = new byte[max];
+ try {
+ int written = 0;
+ boolean onebyte = true;
+ while (written < max) {
+ if (onebyte) {
+ final byte v = (byte) rng.nextInt();
+ b.write(v);
+ expect[written++] = v;
+ } else {
+ final int len = Math
+ .min(rng.nextInt() & 127, max - written);
+ final byte[] tmp = rng.nextBytes(len);
+ b.write(tmp, 0, len);
+ System.arraycopy(tmp, 0, expect, written, len);
+ written += len;
+ }
+ onebyte = !onebyte;
+ }
+ assertEquals(expect.length, written);
+ b.close();
+
+ assertEquals(expect.length, b.length());
+ {
+ final byte[] r = b.toByteArray();
+ assertNotNull(r);
+ assertEquals(expect.length, r.length);
+ assertTrue(Arrays.equals(expect, r));
+ }
+ {
+ final ByteArrayOutputStream o = new ByteArrayOutputStream();
+ b.writeTo(o, null);
+ o.close();
+ final byte[] r = o.toByteArray();
+ assertEquals(expect.length, r.length);
+ assertTrue(Arrays.equals(expect, r));
+ }
+ } finally {
+ b.destroy();
+ }
+ }
+
+}
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/util/TestRng.java b/org.spearce.jgit.test/tst/org/spearce/jgit/util/TestRng.java
new file mode 100644
index 0000000..d74a534
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/util/TestRng.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2008, Google Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.util;
+
+/** Toy RNG to ensure we get predictable numbers during unit tests. */
+public class TestRng {
+ private int next;
+
+ public TestRng(final String seed) {
+ next = 0;
+ for (int i = 0; i < seed.length(); i++)
+ next = next * 11 + seed.charAt(i);
+ }
+
+ public byte[] nextBytes(final int cnt) {
+ final byte[] r = new byte[cnt];
+ for (int i = 0; i < cnt; i++)
+ r[i] = (byte) nextInt();
+ return r;
+ }
+
+ public int nextInt() {
+ next = next * 1103515245 + 12345;
+ return next;
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java b/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
index 8f91246..6267fb5 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
@@ -60,7 +60,7 @@
* after this stream has been properly closed by {@link #close()}.
*/
public class TemporaryBuffer extends OutputStream {
- private static final int DEFAULT_IN_CORE_LIMIT = 1024 * 1024;
+ static final int DEFAULT_IN_CORE_LIMIT = 1024 * 1024;
/** Chain of data, if we are still completely in-core; otherwise null. */
private ArrayList<Block> blocks;
@@ -297,7 +297,7 @@ public void destroy() {
}
}
- private static class Block {
+ static class Block {
static final int SZ = 8 * 1024;
final byte[] buffer = new byte[SZ];
--
1.6.1.rc2.306.ge5d5e
next prev parent reply other threads:[~2008-12-12 2:48 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-12 2:46 [JGIT PATCH 00/15] More patch parsing support Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 01/15] Correct use of TemporaryBuffer in Patch Shawn O. Pearce
2008-12-12 2:46 ` Shawn O. Pearce [this message]
2008-12-12 2:46 ` [JGIT PATCH 03/15] Add IntList as a more efficient representation of List<Integer> Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 04/15] Add lineMap computer to RawParseUtils to index locations of line starts Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 05/15] Define FileHeader.PatchType to report the style of patch used Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 06/15] Test for non-git binary files and mark them as PatchType.BINARY Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 07/15] Set empty patches with no Git metadata to PatchType.BINARY Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 08/15] Always use the FileHeader buffer during Patch.parseHunks Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 09/15] Parse "GIT binary patch" style patch metadata Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 10/15] Record patch parsing errors for later inspection by applications Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 11/15] Fix Patch.parse to honor the end point passed in Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 12/15] Correctly handle hunk headers such as "@@ -0,0 +1 @@" Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 13/15] Patch parse test comparing "git log -p" output to "git log --numstat" Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 14/15] Abstract the hunk header testing into a method Shawn O. Pearce
2008-12-12 2:46 ` [JGIT PATCH 15/15] Treat "diff --combined" the same as "diff --cc" Shawn O. Pearce
2008-12-12 23:11 ` Robin Rosenberg
2008-12-12 23:18 ` [JGIT PATCH 15/15 v2] " Shawn O. Pearce
[not found] ` <bd6139dc0812120243y2b1a3dddu4975162114280e17@mail.gmail.com>
2008-12-12 15:15 ` [JGIT PATCH 03/15] Add IntList as a more efficient representation of List<Integer> Shawn O. Pearce
2008-12-12 15:33 ` Sverre Rabbelier
2008-12-12 15:41 ` Shawn O. Pearce
2008-12-12 15:50 ` Sverre Rabbelier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1229049981-14152-3-git-send-email-spearce@spearce.org \
--to=spearce@spearce.org \
--cc=git@vger.kernel.org \
--cc=robin.rosenberg@dewire.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).