git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 03/14] Add test cases for NB.encode and NB.decode family of routines

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 18, 2008, 23:53 UTC
Message-ID
<1219103602-32222-4-git-send-email-spearce@spearce.org>
In-Reply-To
<1219103602-32222-3-git-send-email-spearce@spearce.org>

We really need to ensure these methods work correctly, and since we just suffered from a bug in NB.decodeUInt16 we now have a set of test cases for the corner conditions of each encode and decode method pair we support.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../tst/org/spearce/jgit/util/NBTest.java          |  328 ++++++++++++++++++++
 1 files changed, 328 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/util/NBTest.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/util/NBTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/util/NBTest.java
new file mode 100644
index 0000000..217db7f
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/util/NBTest.java
@@ -0,0 +1,328 @@
+/*
+ * 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 junit.framework.TestCase;
+
+public class NBTest extends TestCase {
+	public void testCompareUInt32() {
+		assertTrue(NB.compareUInt32(0, 0) == 0);
+		assertTrue(NB.compareUInt32(1, 0) > 0);
+		assertTrue(NB.compareUInt32(0, 1) < 0);
+		assertTrue(NB.compareUInt32(-1, 0) > 0);
+		assertTrue(NB.compareUInt32(0, -1) < 0);
+		assertTrue(NB.compareUInt32(-1, 1) > 0);
+		assertTrue(NB.compareUInt32(1, -1) < 0);
+	}
+
+	public void testDecodeUInt16() {
+		assertEquals(0, NB.decodeUInt16(b(0, 0), 0));
+		assertEquals(0, NB.decodeUInt16(padb(3, 0, 0), 3));
+
+		assertEquals(3, NB.decodeUInt16(b(0, 3), 0));
+		assertEquals(3, NB.decodeUInt16(padb(3, 0, 3), 3));
+
+		assertEquals(0xde03, NB.decodeUInt16(b(0xde, 3), 0));
+		assertEquals(0xde03, NB.decodeUInt16(padb(3, 0xde, 3), 3));
+
+		assertEquals(0x03de, NB.decodeUInt16(b(3, 0xde), 0));
+		assertEquals(0x03de, NB.decodeUInt16(padb(3, 3, 0xde), 3));
+
+		assertEquals(0xffff, NB.decodeUInt16(b(0xff, 0xff), 0));
+		assertEquals(0xffff, NB.decodeUInt16(padb(3, 0xff, 0xff), 3));
+	}
+
+	public void testDecodeInt32() {
+		assertEquals(0, NB.decodeInt32(b(0, 0, 0, 0), 0));
+		assertEquals(0, NB.decodeInt32(padb(3, 0, 0, 0, 0), 3));
+
+		assertEquals(3, NB.decodeInt32(b(0, 0, 0, 3), 0));
+		assertEquals(3, NB.decodeInt32(padb(3, 0, 0, 0, 3), 3));
+
+		assertEquals(0xdeadbeef, NB.decodeInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
+		assertEquals(0xdeadbeef, NB.decodeInt32(
+				padb(3, 0xde, 0xad, 0xbe, 0xef), 3));
+
+		assertEquals(0x0310adef, NB.decodeInt32(b(0x03, 0x10, 0xad, 0xef), 0));
+		assertEquals(0x0310adef, NB.decodeInt32(
+				padb(3, 0x03, 0x10, 0xad, 0xef), 3));
+
+		assertEquals(0xffffffff, NB.decodeInt32(b(0xff, 0xff, 0xff, 0xff), 0));
+		assertEquals(0xffffffff, NB.decodeInt32(
+				padb(3, 0xff, 0xff, 0xff, 0xff), 3));
+	}
+
+	public void testDecodeUInt32() {
+		assertEquals(0L, NB.decodeUInt32(b(0, 0, 0, 0), 0));
+		assertEquals(0L, NB.decodeUInt32(padb(3, 0, 0, 0, 0), 3));
+
+		assertEquals(3L, NB.decodeUInt32(b(0, 0, 0, 3), 0));
+		assertEquals(3L, NB.decodeUInt32(padb(3, 0, 0, 0, 3), 3));
+
+		assertEquals(0xdeadbeefL, NB.decodeUInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
+		assertEquals(0xdeadbeefL, NB.decodeUInt32(padb(3, 0xde, 0xad, 0xbe,
+				0xef), 3));
+
+		assertEquals(0x0310adefL, NB.decodeUInt32(b(0x03, 0x10, 0xad, 0xef), 0));
+		assertEquals(0x0310adefL, NB.decodeUInt32(padb(3, 0x03, 0x10, 0xad,
+				0xef), 3));
+
+		assertEquals(0xffffffffL, NB.decodeUInt32(b(0xff, 0xff, 0xff, 0xff), 0));
+		assertEquals(0xffffffffL, NB.decodeUInt32(padb(3, 0xff, 0xff, 0xff,
+				0xff), 3));
+	}
+
+	public void testDecodeUInt64() {
+		assertEquals(0L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 0), 0));
+		assertEquals(0L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 0), 3));
+
+		assertEquals(3L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 3), 0));
+		assertEquals(3L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 3), 3));
+
+		assertEquals(0xdeadbeefL, NB.decodeUInt64(b(0, 0, 0, 0, 0xde, 0xad,
+				0xbe, 0xef), 0));
+		assertEquals(0xdeadbeefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0xde,
+				0xad, 0xbe, 0xef), 3));
+
+		assertEquals(0x0310adefL, NB.decodeUInt64(b(0, 0, 0, 0, 0x03, 0x10,
+				0xad, 0xef), 0));
+		assertEquals(0x0310adefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0x03,
+				0x10, 0xad, 0xef), 3));
+
+		assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(b(0xc0, 0xff, 0xee,
+				0x78, 0xde, 0xad, 0xbe, 0xef), 0));
+		assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(padb(3, 0xc0, 0xff,
+				0xee, 0x78, 0xde, 0xad, 0xbe, 0xef), 3));
+
+		assertEquals(0x00000000ffffffffL, NB.decodeUInt64(b(0, 0, 0, 0, 0xff,
+				0xff, 0xff, 0xff), 0));
+		assertEquals(0x00000000ffffffffL, NB.decodeUInt64(padb(3, 0, 0, 0, 0,
+				0xff, 0xff, 0xff, 0xff), 3));
+		assertEquals(0xffffffffffffffffL, NB.decodeUInt64(b(0xff, 0xff, 0xff,
+				0xff, 0xff, 0xff, 0xff, 0xff), 0));
+		assertEquals(0xffffffffffffffffL, NB.decodeUInt64(padb(3, 0xff, 0xff,
+				0xff, 0xff, 0xff, 0xff, 0xff, 0xff), 3));
+	}
+
+	public void testEncodeInt16() {
+		final byte[] out = new byte[16];
+
+		prepareOutput(out);
+		NB.encodeInt16(out, 0, 0);
+		assertOutput(b(0, 0), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt16(out, 3, 0);
+		assertOutput(b(0, 0), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt16(out, 0, 3);
+		assertOutput(b(0, 3), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt16(out, 3, 3);
+		assertOutput(b(0, 3), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt16(out, 0, 0xdeac);
+		assertOutput(b(0xde, 0xac), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt16(out, 3, 0xdeac);
+		assertOutput(b(0xde, 0xac), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt16(out, 3, -1);
+		assertOutput(b(0xff, 0xff), out, 3);
+	}
+
+	public void testEncodeInt32() {
+		final byte[] out = new byte[16];
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 0, 0);
+		assertOutput(b(0, 0, 0, 0), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 3, 0);
+		assertOutput(b(0, 0, 0, 0), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 0, 3);
+		assertOutput(b(0, 0, 0, 3), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 3, 3);
+		assertOutput(b(0, 0, 0, 3), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 0, 0xdeac);
+		assertOutput(b(0, 0, 0xde, 0xac), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 3, 0xdeac);
+		assertOutput(b(0, 0, 0xde, 0xac), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 0, 0xdeac9853);
+		assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 3, 0xdeac9853);
+		assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt32(out, 3, -1);
+		assertOutput(b(0xff, 0xff, 0xff, 0xff), out, 3);
+	}
+
+	public void testEncodeInt64() {
+		final byte[] out = new byte[16];
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 0, 0L);
+		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 3, 0L);
+		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 0, 3L);
+		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 3, 3L);
+		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 0, 0xdeacL);
+		assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 3, 0xdeacL);
+		assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 0, 0xdeac9853L);
+		assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 3, 0xdeac9853L);
+		assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 0, 0xac431242deac9853L);
+		assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 0);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 3, 0xac431242deac9853L);
+		assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 3);
+
+		prepareOutput(out);
+		NB.encodeInt64(out, 3, -1L);
+		assertOutput(b(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), out, 3);
+	}
+
+	private static void prepareOutput(final byte[] buf) {
+		for (int i = 0; i < buf.length; i++)
+			buf[i] = (byte) (0x77 + i);
+	}
+
+	private static void assertOutput(final byte[] expect, final byte[] buf,
+			final int offset) {
+		for (int i = 0; i < offset; i++)
+			assertEquals((byte) (0x77 + i), buf[i]);
+		for (int i = 0; i < expect.length; i++)
+			assertEquals(expect[i], buf[offset + i]);
+		for (int i = offset + expect.length; i < buf.length; i++)
+			assertEquals((byte) (0x77 + i), buf[i]);
+	}
+
+	private static byte[] b(final int a, final int b) {
+		return new byte[] { (byte) a, (byte) b };
+	}
+
+	private static byte[] padb(final int len, final int a, final int b) {
+		final byte[] r = new byte[len + 2];
+		for (int i = 0; i < len; i++)
+			r[i] = (byte) 0xaf;
+		r[len] = (byte) a;
+		r[len + 1] = (byte) b;
+		return r;
+	}
+
+	private static byte[] b(final int a, final int b, final int c, final int d) {
+		return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d };
+	}
+
+	private static byte[] padb(final int len, final int a, final int b,
+			final int c, final int d) {
+		final byte[] r = new byte[len + 4];
+		for (int i = 0; i < len; i++)
+			r[i] = (byte) 0xaf;
+		r[len] = (byte) a;
+		r[len + 1] = (byte) b;
+		r[len + 2] = (byte) c;
+		r[len + 3] = (byte) d;
+		return r;
+	}
+
+	private static byte[] b(final int a, final int b, final int c, final int d,
+			final int e, final int f, final int g, final int h) {
+		return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d, (byte) e,
+				(byte) f, (byte) g, (byte) h };
+	}
+
+	private static byte[] padb(final int len, final int a, final int b,
+			final int c, final int d, final int e, final int f, final int g,
+			final int h) {
+		final byte[] r = new byte[len + 8];
+		for (int i = 0; i < len; i++)
+			r[i] = (byte) 0xaf;
+		r[len] = (byte) a;
+		r[len + 1] = (byte) b;
+		r[len + 2] = (byte) c;
+		r[len + 3] = (byte) d;
+		r[len + 4] = (byte) e;
+		r[len + 5] = (byte) f;
+		r[len + 6] = (byte) g;
+		r[len + 7] = (byte) h;
+		return r;
+	}
+}
-- 
1.6.0.87.g2858d
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 4 of 20 in “TreeWalk D/F conflict detection”
  1. 00/14 TreeWalk D/F conflict detectionShawn O. Pearce, Aug 18, 2008
  2. 01/14 Detect path names which overflow the name length field in the indexShawn O. Pearce, Aug 18, 2008
  3. 02/14 Fix NB.decodeUInt16 to correctly handle the high byteShawn O. Pearce, Aug 18, 2008
  4. 03/14 Add test cases for NB.encode and NB.decode family of routinesShawn O. Pearce, Aug 18, 2008
  5. 04/14 Fix DirCache's skip over null byte padding when reading a DIRC fileShawn O. Pearce, Aug 18, 2008
  6. 05/14 Fix usage of assertEquals in DirCacheIteratorTestShawn O. Pearce, Aug 18, 2008
  7. 06/14 Refactor AbstractTreeIterator.pathCompare to force another modeShawn O. Pearce, Aug 18, 2008
  8. 07/14 Micro-optimize AbstractTreeIterator.pathCompareShawn O. Pearce, Aug 18, 2008
  9. 08/14 Optimize path comparsion within subtrees during TreeWalkShawn O. Pearce, Aug 18, 2008
  10. 09/14 Refactor AbstractTreeIterator semantics to start on first entryShawn O. Pearce, Aug 18, 2008
  11. 10/14 Make all AbstractTreeIterator implementations bi-directionalShawn O. Pearce, Aug 18, 2008
  12. 11/14 Expose beginning of iterator indication from AbstractTreeIteratorShawn O. Pearce, Aug 18, 2008
  13. 12/14 Allow application code to set ObjectIds in DirCacheEntryShawn O. Pearce, Aug 18, 2008
  14. 13/14 Create NameConflictTreeWalk to transparently detect D/F conflictsShawn O. Pearce, Aug 18, 2008
  15. 14/14 Add test case for NameConflictTreeWalkShawn O. Pearce, Aug 18, 2008
  16. Junio C HamanoAug 19, 2008
  17. Robin RosenbergAug 19, 2008
  18. Shawn O. PearceAug 19, 2008
  19. David WoodhouseAug 19, 2008
  20. Shawn O. PearceAug 19, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.