1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Consolidated similar tests.

Change-Id: If8161fdd23956f3cb61660f45d1b1407bb102835
This commit is contained in:
Sergey Prigogin 2016-03-28 13:02:32 -07:00
parent 16562814f7
commit 929d8dfb45
2 changed files with 141 additions and 143 deletions

View file

@ -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,27 +20,6 @@ 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));
@ -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()));
}
}

View file

@ -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 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 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 );
assertEquals(map.size(), contents.length);
}
public void testSimpleAdd() throws Exception{
ObjectMap map = new ObjectMap( 2 );
ObjectMap map = new ObjectMap(2);
Object [][] contents = new Object[][] { {"1", "ob" } }; //$NON-NLS-1$//$NON-NLS-2$
Object[][] contents = new Object[][] { {"1", "ob" } };
insertContents( map, contents );
assertContents( map, contents );
insertContents(map, contents);
assertContents(map, contents);
assertEquals( map.size(), 1 );
assertEquals( map.capacity(), 2 );
assertEquals(map.size(), 1);
assertEquals(map.capacity(), 2);
}
public void testSimpleCollision() throws Exception{
ObjectMap map = new ObjectMap( 2 );
ObjectMap map = new ObjectMap(2);
HashObject key1 = new HashObject( 1 );
HashObject key2 = new HashObject( 1 );
HashObject key1 = new HashObject(1);
HashObject key2 = new HashObject(1);
Object [][] contents = new Object[][] { {key1, "1" }, //$NON-NLS-1$
{key2, "2" } }; //$NON-NLS-1$
Object[][] contents = new Object[][] { {key1, "1" },
{key2, "2" } };
insertContents( map, contents );
insertContents(map, contents);
assertEquals( map.size(), 2 );
assertEquals( map.capacity(), 2 );
assertEquals(map.size(), 2);
assertEquals(map.capacity(), 2);
assertContents( map, contents );
assertContents(map, contents);
}
public void testResize() throws Exception{
ObjectMap map = new ObjectMap( 1 );
ObjectMap map = new ObjectMap(1);
assertEquals( map.size(), 0 );
assertEquals( map.capacity(), 2 );
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$
Object[][] res = new Object[][] { { "0", "o0" },
{ "1", "o1" },
{ "2", "o2" },
{ "3", "o3" },
{ "4", "o4" } };
insertContents( map, res );
assertEquals( map.capacity(), 8 );
assertContents( map, res );
insertContents(map, res);
assertEquals(map.capacity(), 8);
assertContents(map, res);
}
public void testCollisionResize() throws Exception{
ObjectMap map = new ObjectMap( 1 );
ObjectMap map = new ObjectMap(1);
assertEquals( map.size(), 0 );
assertEquals( map.capacity(), 2 );
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$
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 );
insertContents(map, res);
assertEquals(map.capacity(), 8);
assertContents(map, res);
}
public void testReAdd() throws Exception{
ObjectMap map = new ObjectMap( 1 );
ObjectMap map = new ObjectMap(1);
assertEquals( map.size(), 0 );
assertEquals( map.capacity(), 2 );
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$
Object[][] res = new Object[][] { { "0", "o0" },
{ "1", "o1" } };
insertContents( map, res );
assertEquals( map.capacity(), 2 );
assertContents( map, res );
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$
res = new Object[][]{ { "0", "o00" },
{ "1", "o01" },
{ "10", "o10" },
{ "11", "o11" } };
insertContents( map, res );
assertContents( map, res );
insertContents(map, res);
assertContents(map, res);
}
public void testResizeResolvesCollision() throws Exception{
ObjectMap map = new ObjectMap( 2 );
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 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$
Object[][] con = new Object[][] { { k1, "1" },
{ k2, "2" },
{ k3, "3" } };
insertContents( map, con );
assertContents( map, con );
insertContents(map, con);
assertContents(map, con);
}
public void testCharArrayUtils() throws Exception{
char [] buffer = "A::B::C".toCharArray(); //$NON-NLS-1$
public void testMapAdd() {
CharArrayObjectMap map = new CharArrayObjectMap(4);
char[] key1 = "key1".toCharArray();
Object value1 = new Integer(43);
map.put(key1, value1);
assertEquals( CharArrayUtils.lastIndexOf( "::".toCharArray(), buffer ), 4 ); //$NON-NLS-1$
assertTrue( CharArrayUtils.equals( CharArrayUtils.lastSegment( buffer, "::".toCharArray()), "C".toCharArray() ) ); //$NON-NLS-1$ //$NON-NLS-2$
char[] key2 = "key1".toCharArray();
Object value2 = map.get(key2);
assertEquals(value1, value2);
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$
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));
}
}