mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-06 09:16:02 +02:00
Separated tests for CharArrayObjectMap.
Change-Id: I5ccfdee8e81fb155b254b5423ce33baeb6c182d0
This commit is contained in:
parent
11de0ed6b6
commit
045b4534d0
6 changed files with 87 additions and 46 deletions
|
@ -0,0 +1,74 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2016 Google, Inc and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*
|
||||||
|
* Contributors:
|
||||||
|
* Sergey Prigogin (Google) - initial API and implementation
|
||||||
|
*******************************************************************************/
|
||||||
|
package org.eclipse.cdt.core.parser.tests;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link CharArrayObjectMap}.
|
||||||
|
*/
|
||||||
|
public class CharArrayObjectMapTest extends TestCase {
|
||||||
|
|
||||||
|
public void testMapAdd() {
|
||||||
|
CharArrayObjectMap map = new CharArrayObjectMap(4);
|
||||||
|
char[] key1 = "key1".toCharArray();
|
||||||
|
Integer value1 = 43;
|
||||||
|
map.put(key1, value1);
|
||||||
|
|
||||||
|
char[] key2 = "key1".toCharArray();
|
||||||
|
Object value2 = map.get(key2);
|
||||||
|
assertEquals(value1, value2);
|
||||||
|
|
||||||
|
for (int i = 0; i < 25; ++i) {
|
||||||
|
map.put(("ikey" + i).toCharArray(), Integer.valueOf(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 25; ++i) {
|
||||||
|
Object ivalue1 = map.get(("ikey" + i).toCharArray());
|
||||||
|
assertEquals(i, ivalue1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDuplicates() {
|
||||||
|
CharArrayObjectMap map = new CharArrayObjectMap(4);
|
||||||
|
String[] keys = new String[] {
|
||||||
|
"a", "b", "c", "c", "value", "value", "context", "context", "result", "d", "e", "f", "g", "h", "i", "j",
|
||||||
|
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "z"
|
||||||
|
};
|
||||||
|
for (int i = 0; i < keys.length; i++) {
|
||||||
|
String key = keys[i];
|
||||||
|
map.put(key.toCharArray(), key + i);
|
||||||
|
}
|
||||||
|
assertEquals(29, map.size());
|
||||||
|
for (int i = 0; i < keys.length; i++) {
|
||||||
|
String key = keys[i];
|
||||||
|
if (i != 2 && i != 4 && i != 6 && i != 31) {
|
||||||
|
assertEquals(key + i, map.get(key.toCharArray()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCollisionRatio() {
|
||||||
|
Random random = new Random(239);
|
||||||
|
CharArrayObjectMap map = new CharArrayObjectMap(1);
|
||||||
|
for (int i = 0; i < 20000; i++) {
|
||||||
|
int r = random.nextInt();
|
||||||
|
map.put(("key" + Integer.toUnsignedString(i)).toCharArray(), i);
|
||||||
|
double collisionRatio = (double) map.countCollisions() / map.size();
|
||||||
|
assertTrue(String.format("Collision ratio %.3f is unexpectedly high for map size of %d.", collisionRatio, map.size()),
|
||||||
|
collisionRatio <= 0.4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,15 +14,12 @@
|
||||||
*/
|
*/
|
||||||
package org.eclipse.cdt.core.parser.tests;
|
package org.eclipse.cdt.core.parser.tests;
|
||||||
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
|
|
||||||
import org.eclipse.cdt.core.parser.util.ObjectMap;
|
import org.eclipse.cdt.core.parser.util.ObjectMap;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author aniefer
|
* Tests for {@link ObjectMap}.
|
||||||
*/
|
*/
|
||||||
public class ObjectMapTest extends TestCase {
|
public class ObjectMapTest extends TestCase {
|
||||||
|
|
||||||
|
@ -138,36 +135,4 @@ public class ObjectMapTest extends TestCase {
|
||||||
insertContents(map, res);
|
insertContents(map, res);
|
||||||
assertContents(map, res);
|
assertContents(map, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMapAdd() {
|
|
||||||
CharArrayObjectMap map = new CharArrayObjectMap(4);
|
|
||||||
char[] key1 = "key1".toCharArray();
|
|
||||||
Integer value1 = 43;
|
|
||||||
map.put(key1, value1);
|
|
||||||
|
|
||||||
char[] key2 = "key1".toCharArray();
|
|
||||||
Object value2 = map.get(key2);
|
|
||||||
assertEquals(value1, value2);
|
|
||||||
|
|
||||||
for (int i = 0; i < 25; ++i) {
|
|
||||||
map.put(("ikey" + i).toCharArray(), Integer.valueOf(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < 25; ++i) {
|
|
||||||
Object ivalue1 = map.get(("ikey" + i).toCharArray());
|
|
||||||
assertEquals(i, ivalue1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testCollisionRatio() {
|
|
||||||
Random random = new Random(239);
|
|
||||||
CharArrayObjectMap map = new CharArrayObjectMap(1);
|
|
||||||
for (int i = 0; i < 20000; i++) {
|
|
||||||
int r = random.nextInt();
|
|
||||||
map.put(("key" + Integer.toUnsignedString(i)).toCharArray(), i);
|
|
||||||
double collisionRatio = (double) map.countCollisions() / map.size();
|
|
||||||
assertTrue(String.format("Collision ratio %.3f is unexpectedly high for map size of %d.", collisionRatio, map.size()),
|
|
||||||
collisionRatio <= 0.4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,6 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.core.parser.tests;
|
package org.eclipse.cdt.core.parser.tests;
|
||||||
|
|
||||||
import junit.framework.Test;
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import junit.framework.TestSuite;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.tests.CModelElementsTests;
|
import org.eclipse.cdt.core.model.tests.CModelElementsTests;
|
||||||
import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests;
|
import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests;
|
||||||
import org.eclipse.cdt.core.parser.tests.ast2.DOMGCCParserExtensionTestSuite;
|
import org.eclipse.cdt.core.parser.tests.ast2.DOMGCCParserExtensionTestSuite;
|
||||||
|
@ -22,6 +18,10 @@ import org.eclipse.cdt.core.parser.tests.ast2.DOMParserTestSuite;
|
||||||
import org.eclipse.cdt.core.parser.tests.ast2.SemanticsTests;
|
import org.eclipse.cdt.core.parser.tests.ast2.SemanticsTests;
|
||||||
import org.eclipse.cdt.core.parser.tests.scanner.ScannerTestSuite;
|
import org.eclipse.cdt.core.parser.tests.scanner.ScannerTestSuite;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Combines all tests for the parsers.
|
* Combines all tests for the parsers.
|
||||||
*/
|
*/
|
||||||
|
@ -34,6 +34,7 @@ public class ParserTestSuite extends TestCase {
|
||||||
suite.addTestSuite(ContentAssistMatcherFactoryTest.class);
|
suite.addTestSuite(ContentAssistMatcherFactoryTest.class);
|
||||||
suite.addTestSuite(CModelElementsTests.class);
|
suite.addTestSuite(CModelElementsTests.class);
|
||||||
suite.addTestSuite(StructuralCModelElementsTests.class);
|
suite.addTestSuite(StructuralCModelElementsTests.class);
|
||||||
|
suite.addTestSuite(CharArrayObjectMapTest.class);
|
||||||
suite.addTestSuite(ObjectMapTest.class);
|
suite.addTestSuite(ObjectMapTest.class);
|
||||||
suite.addTestSuite(SemanticsTests.class);
|
suite.addTestSuite(SemanticsTests.class);
|
||||||
suite.addTest(ScannerTestSuite.suite());
|
suite.addTest(ScannerTestSuite.suite());
|
||||||
|
|
|
@ -25,7 +25,7 @@ import java.util.function.Consumer;
|
||||||
/**
|
/**
|
||||||
* @author Doug Schaefer
|
* @author Doug Schaefer
|
||||||
*/
|
*/
|
||||||
public class CharArrayObjectMap <T> extends CharTable {
|
public class CharArrayObjectMap<T> extends CharTable {
|
||||||
public static final CharArrayObjectMap<?> EMPTY_MAP = new CharArrayObjectMap<Object>(0) {
|
public static final CharArrayObjectMap<?> EMPTY_MAP = new CharArrayObjectMap<Object>(0) {
|
||||||
@Override
|
@Override
|
||||||
public Object clone() { return this; }
|
public Object clone() { return this; }
|
||||||
|
|
|
@ -169,7 +169,7 @@ public class CharTable extends HashTable {
|
||||||
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||||
return i;
|
return i;
|
||||||
|
|
||||||
// Follow the next chain
|
// Follow the next chain.
|
||||||
for (i = nextTable[i] - 1; i >= 0 && i != nextTable[i] - 1; i = nextTable[i] - 1) {
|
for (i = nextTable[i] - 1; i >= 0 && i != nextTable[i] - 1; i = nextTable[i] - 1) {
|
||||||
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||||
return i;
|
return i;
|
||||||
|
|
|
@ -173,17 +173,18 @@ public class HashTable implements Cloneable {
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void linkIntoHashTable(int i, int hash) {
|
protected final void linkIntoHashTable(int i, int hash) {
|
||||||
if (nextTable == null)
|
if (nextTable == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (hashTable[hash] == 0) {
|
int j = hashTable[hash];
|
||||||
|
if (j == 0) {
|
||||||
hashTable[hash] = i + 1;
|
hashTable[hash] = i + 1;
|
||||||
} else {
|
} else {
|
||||||
// Need to link.
|
// Need to link.
|
||||||
int j = hashTable[hash] - 1;
|
j--;
|
||||||
int k;
|
int k;
|
||||||
while ((k = nextTable[j]) != 0) {
|
while ((k = nextTable[j]) != 0 && k != j + 1) {
|
||||||
j = k - 1;
|
j = k - 1;
|
||||||
}
|
}
|
||||||
nextTable[j] = i + 1;
|
nextTable[j] = i + 1;
|
||||||
|
|
Loading…
Add table
Reference in a new issue