aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2006-09-23 18:45:11 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2006-09-23 18:45:11 +0000
commit23423a0a369af9187e74615f09299c39fd337e60 (patch)
treef47a43153de355342269a4482012b56522cf14a1
parent78c26cb7fe9a4e20b7e6dfca3e375a3c0c927134 (diff)
downloadjackcess-23423a0a369af9187e74615f09299c39fd337e60.tar.gz
jackcess-23423a0a369af9187e74615f09299c39fd337e60.zip
fix ordering of byte code comparator
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@120 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r--src/java/com/healthmarketscience/jackcess/ByteUtil.java15
-rw-r--r--src/java/com/healthmarketscience/jackcess/Index.java5
-rw-r--r--test/src/java/com/healthmarketscience/jackcess/IndexTest.java96
3 files changed, 110 insertions, 6 deletions
diff --git a/src/java/com/healthmarketscience/jackcess/ByteUtil.java b/src/java/com/healthmarketscience/jackcess/ByteUtil.java
index c1b260e..d6a7f67 100644
--- a/src/java/com/healthmarketscience/jackcess/ByteUtil.java
+++ b/src/java/com/healthmarketscience/jackcess/ByteUtil.java
@@ -126,9 +126,9 @@ public final class ByteUtil {
offset += 2;
}
- int rtn = buffer.get(offset) & 0xFF;
- rtn += ((((int) buffer.get(offset + (1 * offInc))) & 0xFF) << 8);
- rtn += ((((int) buffer.get(offset + (2 * offInc))) & 0xFF) << 16);
+ int rtn = toUnsignedInt(buffer.get(offset));
+ rtn += (toUnsignedInt(buffer.get(offset + (1 * offInc))) << 8);
+ rtn += (toUnsignedInt(buffer.get(offset + (2 * offInc))) << 16);
rtn &= 0xFFFFFF;
return rtn;
}
@@ -230,5 +230,12 @@ public final class ByteUtil {
writer.close();
}
}
-
+
+ /**
+ * @return the byte value converted to an unsigned int value
+ */
+ public static int toUnsignedInt(byte b) {
+ return (int)b & 0xFF;
+ }
+
}
diff --git a/src/java/com/healthmarketscience/jackcess/Index.java b/src/java/com/healthmarketscience/jackcess/Index.java
index 7c51191..1b3febe 100644
--- a/src/java/com/healthmarketscience/jackcess/Index.java
+++ b/src/java/com/healthmarketscience/jackcess/Index.java
@@ -60,7 +60,7 @@ public class Index implements Comparable<Index> {
private static final byte REVERSE_ORDER_FLAG = (byte)0x01;
- private static final Comparator<byte[]> BYTE_CODE_COMPARATOR =
+ static final Comparator<byte[]> BYTE_CODE_COMPARATOR =
new Comparator<byte[]>() {
public int compare(byte[] left, byte[] right) {
if(left == right) {
@@ -79,7 +79,8 @@ public class Index implements Comparable<Index> {
++pos;
}
if(pos < len) {
- return ((left[pos] < right[pos]) ? -1 : 1);
+ return ((ByteUtil.toUnsignedInt(left[pos]) <
+ ByteUtil.toUnsignedInt(right[pos])) ? -1 : 1);
}
return ((left.length < right.length) ? -1 :
((left.length > right.length) ? 1 : 0));
diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
new file mode 100644
index 0000000..0adeb10
--- /dev/null
+++ b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java
@@ -0,0 +1,96 @@
+// Copyright (c) 2006 Health Market Science, Inc.
+
+package com.healthmarketscience.jackcess;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import junit.framework.TestCase;
+
+import static com.healthmarketscience.jackcess.DatabaseTest.*;
+
+/**
+ * @author James Ahlborn
+ */
+public class IndexTest extends TestCase {
+
+ /**
+ * Creates a new <code>IndexTest</code> instance.
+ *
+ */
+ public IndexTest(String name) {
+ super(name);
+ }
+
+ public void testByteOrder() throws Exception {
+ byte b1 = (byte)0x00;
+ byte b2 = (byte)0x01;
+ byte b3 = (byte)0x7F;
+ byte b4 = (byte)0x80;
+ byte b5 = (byte)0xFF;
+
+ assertTrue(ByteUtil.toUnsignedInt(b1) < ByteUtil.toUnsignedInt(b2));
+ assertTrue(ByteUtil.toUnsignedInt(b2) < ByteUtil.toUnsignedInt(b3));
+ assertTrue(ByteUtil.toUnsignedInt(b3) < ByteUtil.toUnsignedInt(b4));
+ assertTrue(ByteUtil.toUnsignedInt(b4) < ByteUtil.toUnsignedInt(b5));
+ }
+
+ public void testByteCodeComparator() {
+ // FIXME, writeme
+ byte[] b0 = null;
+ byte[] b1 = new byte[]{(byte)0x00};
+ byte[] b2 = new byte[]{(byte)0x00, (byte)0x00};
+ byte[] b3 = new byte[]{(byte)0x00, (byte)0x01};
+ byte[] b4 = new byte[]{(byte)0x01};
+ byte[] b5 = new byte[]{(byte)0x80};
+ byte[] b6 = new byte[]{(byte)0xFF};
+ byte[] b7 = new byte[]{(byte)0xFF, (byte)0x00};
+ byte[] b8 = new byte[]{(byte)0xFF, (byte)0x01};
+
+ List<byte[]> expectedList = Arrays.<byte[]>asList(b0, b1, b2, b3, b4,
+ b5, b6, b7, b8);
+ SortedSet<byte[]> sortedSet = new TreeSet<byte[]>(
+ Index.BYTE_CODE_COMPARATOR);
+ sortedSet.addAll(expectedList);
+ assertEquals(expectedList, new ArrayList<byte[]>(sortedSet));
+
+ }
+
+ public void testPrimaryKey() throws Exception {
+ Table table = open().getTable("Table1");
+ Map<String, Boolean> foundPKs = new HashMap<String, Boolean>();
+ for(Index index : table.getIndexes()) {
+ foundPKs.put(index.getColumns().iterator().next().getName(),
+ index.isPrimaryKey());
+ }
+ Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>();
+ expectedPKs.put("A", Boolean.TRUE);
+ expectedPKs.put("B", Boolean.FALSE);
+ assertEquals(expectedPKs, foundPKs);
+ }
+
+ public void testIndexSlots() throws Exception
+ {
+ Database mdb = Database.open(new File("test/data/indexTest.mdb"));
+
+ Table table = mdb.getTable("Table1");
+ assertEquals(4, table.getIndexes().size());
+ assertEquals(4, table.getIndexSlotCount());
+
+ table = mdb.getTable("Table2");
+ assertEquals(2, table.getIndexes().size());
+ assertEquals(3, table.getIndexSlotCount());
+
+ table = mdb.getTable("Table3");
+ assertEquals(2, table.getIndexes().size());
+ assertEquals(3, table.getIndexSlotCount());
+ }
+
+
+}