changeset 278:55fad5e0e98b

Ensure capacity grows regardless of initial map size. Separate unit test
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 29 Aug 2011 23:31:37 +0200
parents 74e7493a042a
children 23e3ea855097
files src/org/tmatesoft/hg/internal/IntMap.java test/org/tmatesoft/hg/test/TestIntMap.java
diffstat 2 files changed, 64 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/IntMap.java	Mon Aug 29 23:14:59 2011 +0200
+++ b/src/org/tmatesoft/hg/internal/IntMap.java	Mon Aug 29 23:31:37 2011 +0200
@@ -71,7 +71,8 @@
 			final int insertPoint = -ix - 1;
 			assert insertPoint <= size; // can't be greater, provided binarySearch didn't malfunction.
 			if (size == keys.length) {
-				int newCapacity = size + (size >>> 2);
+				int capInc = size >>> 2; // +25%
+				int newCapacity = size + (capInc < 2 ? 2 : capInc) ;
 				int[] newKeys = new int[newCapacity];
 				Object[] newValues = new Object[newCapacity];
 				System.arraycopy(keys, 0, newKeys, 0, insertPoint);
@@ -127,25 +128,4 @@
 		}
 		return -(low + 1); // key not found.
 	}
-
-	public static void main(String[] args) {
-		IntMap<String> m = new IntMap<String>(-1);
-		m.put(18, "18");
-		m.put(1, "1");
-		m.put(9, "9");
-		m.put(20, "20");
-		m.put(2, "2");
-		m.put(3, "3");
-		m.put(21, "21");
-		m.put(15, "15");
-		m.put(12, "12");
-		m.put(11, "11");
-		m.put(31, "31");
-		System.out.printf("%d  [%d..%d]\n", m.size(), m.firstKey(), m.lastKey());
-		for (int i = m.firstKey(); i <= m.lastKey(); i++) {
-			if (m.containsKey(i)) {
-				System.out.printf("@%02d:%s\n", i, m.get(i));
-			}
-		}
-	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/org/tmatesoft/hg/test/TestIntMap.java	Mon Aug 29 23:31:37 2011 +0200
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2011 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.test;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.tmatesoft.hg.internal.IntMap;
+
+/**
+ *
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class TestIntMap {
+
+	public static void main(String[] args) {
+		TestIntMap t = new TestIntMap();
+		t.testBasic();
+	}
+	
+	@Test
+	public void testBasic() {
+		IntMap<String> m = new IntMap<String>(2);
+		m.put(18, "18");
+		m.put(1, "1");
+		m.put(9, "9");
+		m.put(20, "20");
+		m.put(2, "2");
+		m.put(3, "3");
+		m.put(21, "21");
+		m.put(15, "15");
+		m.put(12, "12");
+		m.put(11, "11");
+		m.put(31, "31");
+		assertEquals(11, m.size());
+		assertEquals(1, m.firstKey());
+		assertEquals(31, m.lastKey());
+		int actualCount = 0;
+		for (int i = m.firstKey(); i <= m.lastKey(); i++) {
+			if (m.containsKey(i)) {
+				actualCount++;
+				assertEquals(m.get(i), Integer.toString(i));
+			}
+		}
+		assertEquals(m.size(), actualCount);
+	}
+}