# HG changeset patch # User Artem Tikhomirov # Date 1314653497 -7200 # Node ID 55fad5e0e98b200513c2b8ec302694f815595510 # Parent 74e7493a042a6fdb28b5d7214065f5fe99437710 Ensure capacity grows regardless of initial map size. Separate unit test diff -r 74e7493a042a -r 55fad5e0e98b src/org/tmatesoft/hg/internal/IntMap.java --- 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 m = new IntMap(-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)); - } - } - } } diff -r 74e7493a042a -r 55fad5e0e98b test/org/tmatesoft/hg/test/TestIntMap.java --- /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 m = new IntMap(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); + } +}