From 929d8dfb458403dc776e19c84828e8acaa1f693e Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Mon, 28 Mar 2016 13:02:32 -0700 Subject: [PATCH] Consolidated similar tests. Change-Id: If8161fdd23956f3cb61660f45d1b1407bb102835 --- .../core/parser/tests/CharArrayUtilsTest.java | 33 +-- .../cdt/core/parser/tests/ObjectMapTest.java | 251 +++++++++--------- 2 files changed, 141 insertions(+), 143 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java index 9ad5715b8ba..7cffecb4f5a 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/CharArrayUtilsTest.java @@ -11,7 +11,6 @@ *******************************************************************************/ package org.eclipse.cdt.core.parser.tests; -import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayUtils; import junit.framework.TestCase; @@ -21,32 +20,11 @@ import junit.framework.TestCase; */ public class CharArrayUtilsTest extends TestCase { - public void testMapAdd() { - CharArrayObjectMap map = new CharArrayObjectMap(4); - char[] key1 = "key1".toCharArray(); - Object value1 = new Integer(43); - map.put(key1, value1); - - char[] key2 = "key1".toCharArray(); - Object value2 = map.get(key2); - assertEquals(value1, value2); - - for (int i = 0; i < 5; ++i) { - map.put(("ikey" + i).toCharArray(), new Integer(i)); - } - - Object ivalue1 = map.get("ikey1".toCharArray()); - assertEquals(ivalue1, new Integer(1)); - - Object ivalue4 = map.get("ikey4".toCharArray()); - assertEquals(ivalue4, new Integer(4)); - } - public void testEquals_Bug289852() { assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 3, "abc".toCharArray(), false)); assertFalse(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 4, "abcd".toCharArray(), false)); assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 2, "ab".toCharArray(), false)); - + assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 3, "ABC".toCharArray(), true)); assertFalse(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 4, "abcd".toCharArray(), true)); assertTrue(CharArrayUtils.equals("pre_abc".toCharArray(), 4, 2, "AB".toCharArray(), true)); @@ -74,5 +52,14 @@ public class CharArrayUtilsTest extends TestCase { assertEquals(6, CharArrayUtils.lastIndexOf("".toCharArray(), "123456".toCharArray())); assertEquals(4, CharArrayUtils.lastIndexOf("56".toCharArray(), "123456".toCharArray())); assertEquals(-1, CharArrayUtils.lastIndexOf("123".toCharArray(), "".toCharArray())); + + char[] buffer = "A::B::C".toCharArray(); + + assertEquals(CharArrayUtils.lastIndexOf("::".toCharArray(), buffer), 4); + assertTrue(CharArrayUtils.equals(CharArrayUtils.lastSegment(buffer, "::".toCharArray()), "C".toCharArray())); + + buffer = "A::B::C:foo".toCharArray(); + assertEquals(CharArrayUtils.lastIndexOf("::".toCharArray(), buffer), 4); + assertTrue(CharArrayUtils.equals(CharArrayUtils.lastSegment(buffer, "::".toCharArray()), "C:foo".toCharArray())); } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ObjectMapTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ObjectMapTest.java index 49a7318d385..455ce1db048 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ObjectMapTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ObjectMapTest.java @@ -14,150 +14,161 @@ */ package org.eclipse.cdt.core.parser.tests; -import junit.framework.TestCase; - -import org.eclipse.cdt.core.parser.util.CharArrayUtils; +import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.ObjectMap; +import junit.framework.TestCase; + /** * @author aniefer */ public class ObjectMapTest extends TestCase { - static public class HashObject{ - HashObject( int h ){ + static public class HashObject { + HashObject(int h) { hash = h; } + @Override - public int hashCode(){ + public int hashCode() { return hash; } final public int hash; } - public void insertContents( ObjectMap map, Object[][] contents ) throws Exception { - for( int i = 0; i < contents.length; i++ ) - map.put( contents[i][0], contents[i][1] ); - } - - public void assertContents( ObjectMap map, Object[][] contents ) throws Exception { - for( int i = 0; i < contents.length; i++ ){ - assertEquals( map.keyAt(i), contents[i][0] ); - assertEquals( map.getAt(i), contents[i][1] ); - assertEquals( map.get(contents[i][0]), contents[i][1] ); + public void insertContents(ObjectMap map, Object[][] contents) throws Exception { + for (int i = 0; i < contents.length; i++) { + map.put(contents[i][0], contents[i][1]); } - assertEquals( map.size(), contents.length ); } - + + public void assertContents(ObjectMap map, Object[][] contents) throws Exception { + for (int i = 0; i < contents.length; i++) { + assertEquals(map.keyAt(i), contents[i][0]); + assertEquals(map.getAt(i), contents[i][1]); + assertEquals(map.get(contents[i][0]), contents[i][1]); + } + assertEquals(map.size(), contents.length); + } + public void testSimpleAdd() throws Exception{ - ObjectMap map = new ObjectMap( 2 ); - - Object [][] contents = new Object[][] { {"1", "ob" } }; //$NON-NLS-1$//$NON-NLS-2$ + ObjectMap map = new ObjectMap(2); - insertContents( map, contents ); - assertContents( map, contents ); - - assertEquals( map.size(), 1 ); - assertEquals( map.capacity(), 2 ); + Object[][] contents = new Object[][] { {"1", "ob" } }; + + insertContents(map, contents); + assertContents(map, contents); + + assertEquals(map.size(), 1); + assertEquals(map.capacity(), 2); } - + public void testSimpleCollision() throws Exception{ - ObjectMap map = new ObjectMap( 2 ); - - HashObject key1 = new HashObject( 1 ); - HashObject key2 = new HashObject( 1 ); - - Object [][] contents = new Object[][] { {key1, "1" }, //$NON-NLS-1$ - {key2, "2" } }; //$NON-NLS-1$ - - insertContents( map, contents ); - - assertEquals( map.size(), 2 ); - assertEquals( map.capacity(), 2 ); + ObjectMap map = new ObjectMap(2); - assertContents( map, contents ); + HashObject key1 = new HashObject(1); + HashObject key2 = new HashObject(1); + + Object[][] contents = new Object[][] { {key1, "1" }, + {key2, "2" } }; + + insertContents(map, contents); + + assertEquals(map.size(), 2); + assertEquals(map.capacity(), 2); + + assertContents(map, contents); } - + public void testResize() throws Exception{ - ObjectMap map = new ObjectMap( 1 ); - - assertEquals( map.size(), 0 ); - assertEquals( map.capacity(), 2 ); - - Object [][] res = new Object [][] { { "0", "o0" }, //$NON-NLS-1$//$NON-NLS-2$ - { "1", "o1" }, //$NON-NLS-1$//$NON-NLS-2$ - { "2", "o2" }, //$NON-NLS-1$//$NON-NLS-2$ - { "3", "o3" }, //$NON-NLS-1$//$NON-NLS-2$ - { "4", "o4" } }; //$NON-NLS-1$//$NON-NLS-2$ - - insertContents( map, res ); - assertEquals( map.capacity(), 8 ); - assertContents( map, res ); - } - - public void testCollisionResize() throws Exception{ - ObjectMap map = new ObjectMap( 1 ); - - assertEquals( map.size(), 0 ); - assertEquals( map.capacity(), 2 ); - - Object [][] res = new Object [][] { { new HashObject(0), "o0" }, //$NON-NLS-1$ - { new HashObject(1), "o1" }, //$NON-NLS-1$ - { new HashObject(0), "o2" }, //$NON-NLS-1$ - { new HashObject(1), "o3" }, //$NON-NLS-1$ - { new HashObject(0), "o4" } }; //$NON-NLS-1$ - - insertContents( map, res ); - assertEquals( map.capacity(), 8 ); - assertContents( map, res ); - } - - public void testReAdd() throws Exception{ - ObjectMap map = new ObjectMap( 1 ); - - assertEquals( map.size(), 0 ); - assertEquals( map.capacity(), 2 ); - - Object [][] res = new Object [][] { { "0", "o0" }, //$NON-NLS-1$ //$NON-NLS-2$ - { "1", "o1" } }; //$NON-NLS-1$ //$NON-NLS-2$ - - insertContents( map, res ); - assertEquals( map.capacity(), 2 ); - assertContents( map, res ); - - res = new Object [][]{ { "0", "o00" }, //$NON-NLS-1$ //$NON-NLS-2$ - { "1", "o01" }, //$NON-NLS-1$ //$NON-NLS-2$ - { "10", "o10" }, //$NON-NLS-1$ //$NON-NLS-2$ - { "11", "o11" } }; //$NON-NLS-1$ //$NON-NLS-2$ - - insertContents( map, res ); - assertContents( map, res ); - } - - public void testResizeResolvesCollision() throws Exception{ - ObjectMap map = new ObjectMap( 2 ); - - Object k1 = new HashObject( 0 ); - Object k2 = new HashObject( 1 ); - Object k3 = new HashObject( 4 ); //collision with 0 in a table capacity 2, but ok in table capacity 4 - - Object [][] con = new Object[][] { { k1, "1" }, //$NON-NLS-1$ - { k2, "2" }, //$NON-NLS-1$ - { k3, "3" } } ; //$NON-NLS-1$ - - insertContents( map, con ); - assertContents( map, con ); - } - - public void testCharArrayUtils() throws Exception{ - char [] buffer = "A::B::C".toCharArray(); //$NON-NLS-1$ - - assertEquals( CharArrayUtils.lastIndexOf( "::".toCharArray(), buffer ), 4 ); //$NON-NLS-1$ - assertTrue( CharArrayUtils.equals( CharArrayUtils.lastSegment( buffer, "::".toCharArray()), "C".toCharArray() ) ); //$NON-NLS-1$ //$NON-NLS-2$ - - buffer = "A::B::C:foo".toCharArray(); //$NON-NLS-1$ - assertEquals( CharArrayUtils.lastIndexOf( "::".toCharArray(), buffer ), 4 ); //$NON-NLS-1$ - assertTrue( CharArrayUtils.equals( CharArrayUtils.lastSegment( buffer, "::".toCharArray()), "C:foo".toCharArray() ) ); //$NON-NLS-1$ //$NON-NLS-2$ + ObjectMap map = new ObjectMap(1); + + assertEquals(map.size(), 0); + assertEquals(map.capacity(), 2); + + Object[][] res = new Object[][] { { "0", "o0" }, + { "1", "o1" }, + { "2", "o2" }, + { "3", "o3" }, + { "4", "o4" } }; + + insertContents(map, res); + assertEquals(map.capacity(), 8); + assertContents(map, res); } + public void testCollisionResize() throws Exception{ + ObjectMap map = new ObjectMap(1); + + assertEquals(map.size(), 0); + assertEquals(map.capacity(), 2); + + Object[][] res = new Object[][] { { new HashObject(0), "o0" }, + { new HashObject(1), "o1" }, + { new HashObject(0), "o2" }, + { new HashObject(1), "o3" }, + { new HashObject(0), "o4" } }; + + insertContents(map, res); + assertEquals(map.capacity(), 8); + assertContents(map, res); + } + + public void testReAdd() throws Exception{ + ObjectMap map = new ObjectMap(1); + + assertEquals(map.size(), 0); + assertEquals(map.capacity(), 2); + + Object[][] res = new Object[][] { { "0", "o0" }, + { "1", "o1" } }; + + insertContents(map, res); + assertEquals(map.capacity(), 2); + assertContents(map, res); + + res = new Object[][]{ { "0", "o00" }, + { "1", "o01" }, + { "10", "o10" }, + { "11", "o11" } }; + + insertContents(map, res); + assertContents(map, res); + } + + public void testResizeResolvesCollision() throws Exception{ + ObjectMap map = new ObjectMap(2); + + Object k1 = new HashObject(0); + Object k2 = new HashObject(1); + Object k3 = new HashObject(4); // Collision with 0 in a table capacity 2, but ok in table capacity 4 + + Object[][] con = new Object[][] { { k1, "1" }, + { k2, "2" }, + { k3, "3" } }; + + insertContents(map, con); + assertContents(map, con); + } + + public void testMapAdd() { + CharArrayObjectMap map = new CharArrayObjectMap(4); + char[] key1 = "key1".toCharArray(); + Object value1 = new Integer(43); + map.put(key1, value1); + + char[] key2 = "key1".toCharArray(); + Object value2 = map.get(key2); + assertEquals(value1, value2); + + for (int i = 0; i < 5; ++i) { + map.put(("ikey" + i).toCharArray(), new Integer(i)); + } + + Object ivalue1 = map.get("ikey1".toCharArray()); + assertEquals(ivalue1, new Integer(1)); + + Object ivalue4 = map.get("ikey4".toCharArray()); + assertEquals(ivalue4, new Integer(4)); + } }