1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 09:25:31 +02:00

Cosmetics.

This commit is contained in:
Sergey Prigogin 2014-05-06 18:08:03 -07:00
parent ae2d1154f9
commit a55526430c
2 changed files with 53 additions and 49 deletions

View file

@ -31,7 +31,7 @@ import org.eclipse.core.runtime.IPath;
public class DBTest extends BaseTestCase {
protected Database db;
@Override
protected void setUp() throws Exception {
super.setUp();
@ -39,11 +39,11 @@ public class DBTest extends BaseTestCase {
new ChunkCache(), 0, false);
db.setExclusiveLock();
}
public static Test suite() {
return suite(DBTest.class);
}
protected IPath getTestDir() {
IPath path = CTestPlugin.getDefault().getStateLocation().append("tests/");
File file = path.toFile();
@ -51,7 +51,7 @@ public class DBTest extends BaseTestCase {
file.mkdir();
return path;
}
@Override
protected void tearDown() throws Exception {
db.close();
@ -60,7 +60,7 @@ public class DBTest extends BaseTestCase {
}
db= null;
}
public void testBlockSizeAndFirstBlock() throws Exception {
assertEquals(0, db.getVersion());
@ -68,7 +68,7 @@ public class DBTest extends BaseTestCase {
final int deltas = (realsize + Database.BLOCK_HEADER_SIZE + Database.BLOCK_SIZE_DELTA - 1) / Database.BLOCK_SIZE_DELTA;
final int blocksize = deltas * Database.BLOCK_SIZE_DELTA;
final int freeDeltas= Database.CHUNK_SIZE / Database.BLOCK_SIZE_DELTA - deltas;
long mem = db.malloc(realsize);
assertEquals(-blocksize, db.getShort(mem - Database.BLOCK_HEADER_SIZE));
db.free(mem);
@ -81,7 +81,7 @@ public class DBTest extends BaseTestCase {
File tmp= File.createTempFile("readOnlyEmpty", ".db");
try {
tmp.setReadOnly();
/* check opening a readonly file for rw access fails */
try {
new Database(tmp, ChunkCache.getSharedInstance(), 0, false);
@ -89,7 +89,7 @@ public class DBTest extends BaseTestCase {
} catch (CoreException e) {
// we expect to get a failure here
}
/* check opening a readonly file for read access does not fail */
try {
new Database(tmp, ChunkCache.getSharedInstance(), 0, true);
@ -100,7 +100,7 @@ public class DBTest extends BaseTestCase {
tmp.delete(); // this may be pointless on some platforms
}
}
public void testFreeBlockLinking() throws Exception {
final int realsize = 42;
final int deltas = (realsize + Database.BLOCK_HEADER_SIZE + Database.BLOCK_SIZE_DELTA - 1) / Database.BLOCK_SIZE_DELTA;
@ -117,19 +117,19 @@ public class DBTest extends BaseTestCase {
assertEquals(mem2, db.getRecPtr(mem1));
assertEquals(0, db.getRecPtr(mem1 + Database.INT_SIZE));
}
public void testSimpleAllocationLifecycle() throws Exception {
public void testSimpleAllocationLifecycle() throws Exception {
long mem1 = db.malloc(42);
db.free(mem1);
long mem2 = db.malloc(42);
assertEquals(mem2, mem1);
}
private static class FindVisitor implements IBTreeVisitor {
private Database db;
private String key;
private long record;
public FindVisitor(Database db, String key) {
this.db = db;
this.key = key;
@ -139,18 +139,18 @@ public class DBTest extends BaseTestCase {
public int compare(long record) throws CoreException {
return db.getString(db.getRecPtr(record + 4)).compare(key, true);
}
@Override
public boolean visit(long record) throws CoreException {
this.record = record;
return false;
}
public long getRecord() {
return record;
}
}
public void testStringsInBTree() throws Exception {
// Tests inserting and retrieving strings
File f = getTestDir().append("testStrings.dat").toFile();
@ -183,7 +183,7 @@ public class DBTest extends BaseTestCase {
"ALPHA",
"BETA"
};
IBTreeComparator comparator = new IBTreeComparator() {
@Override
public int compare(long record1, long record2) throws CoreException {
@ -202,7 +202,7 @@ public class DBTest extends BaseTestCase {
db.putRecPtr(record + 4, string.getRecord());
btree.insert(record);
}
for (int i = 0; i < names.length; ++i) {
String name = names[i];
FindVisitor finder = new FindVisitor(db, name);
@ -214,38 +214,38 @@ public class DBTest extends BaseTestCase {
assertTrue(rname.equals(name));
}
}
private final int GT = 1, LT = -1, EQ = 0;
public void testShortStringComparison() throws CoreException {
Random r= new Random(90210);
assertCMP("", EQ, "", true);
assertCMP("", EQ, "", false);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH / 2, r, true);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH / 2, r, false);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH, r, true);
doTrials(1000, 1, ShortString.MAX_BYTE_LENGTH, r, false);
assertCMP("a", LT, "b", true);
assertCMP("aa", LT, "ab", true);
assertCMP("a", EQ, "a", true);
assertCMP("a", GT, "A", true);
assertCMP("aa", GT, "aA", true);
assertCMP("a", GT, "B", true);
assertCMP("a", EQ, "a", false);
assertCMP("a", EQ, "A", false);
}
public void testLongStringComparison() throws CoreException {
Random r= new Random(314159265);
doTrials(100, ShortString.MAX_BYTE_LENGTH + 1, ShortString.MAX_BYTE_LENGTH * 2, r, true);
doTrials(100, ShortString.MAX_BYTE_LENGTH + 1, ShortString.MAX_BYTE_LENGTH * 2, r, false);
}
private void doTrials(int n, int min, int max, Random r, boolean caseSensitive) throws CoreException {
long start = System.currentTimeMillis();
for (int i= 0; i < n; i++) {
@ -257,21 +257,25 @@ public class DBTest extends BaseTestCase {
// System.out.print("Trials: " + n + " Max length: " + max + " ignoreCase: " + !caseSensitive);
// System.out.println(" Time: " + (System.currentTimeMillis() - start));
}
private String randomString(int min, int max, Random r) {
StringBuilder result = new StringBuilder();
int len = min + r.nextInt(max - min);
return randomString(len, r);
}
private String randomString(int len, Random r) {
StringBuilder result = new StringBuilder(len);
for (int i= 0; i < len; i++) {
result.append(randomChar(r));
}
return result.toString();
}
private char randomChar(Random r) {
// we only match String.compareToIgnoreCase behavior within this limited range
return (char) (32 + r.nextInt(40));
// We only match String.compareToIgnoreCase behavior within this limited range.
return (char) (32 + r.nextInt(40));
}
private void assertCMP(String a, int expected, String b, boolean caseSensitive) throws CoreException {
char[] acs = a.toCharArray();
char[] bcs = b.toCharArray();
@ -279,29 +283,29 @@ public class DBTest extends BaseTestCase {
IString biss = db.newString(b);
IString aisc = db.newString(acs);
IString bisc = db.newString(bcs);
assertEquals(a.hashCode(), aiss.hashCode());
assertEquals(a.hashCode(), aisc.hashCode());
assertEquals(b.hashCode(), biss.hashCode());
assertEquals(b.hashCode(), bisc.hashCode());
assertEquals(aiss, a);
assertEquals(aisc, a);
assertEquals(biss, b);
assertEquals(bisc, b);
assertSignEquals(expected, aiss.compare(bcs, caseSensitive));
assertSignEquals(expected, aiss.compare(biss, caseSensitive));
assertSignEquals(expected, aiss.compare(bisc, caseSensitive));
assertSignEquals(expected, aiss.compare(b, caseSensitive));
assertSignEquals(expected, aiss.comparePrefix(bcs, caseSensitive));
assertSignEquals(expected, -biss.compare(acs, caseSensitive));
assertSignEquals(expected, -biss.compare(aiss, caseSensitive));
assertSignEquals(expected, -biss.compare(aisc, caseSensitive));
assertSignEquals(expected, -biss.compare(a, caseSensitive));
assertSignEquals(expected, -biss.comparePrefix(acs, caseSensitive));
if (!caseSensitive && expected != 0) {
assertSignEquals(expected, aiss.compareCompatibleWithIgnoreCase(bcs));
assertSignEquals(expected, aiss.compareCompatibleWithIgnoreCase(biss));
@ -312,7 +316,7 @@ public class DBTest extends BaseTestCase {
assertSignEquals(expected, -biss.compareCompatibleWithIgnoreCase(aisc));
}
}
private void assertSignEquals(int a, int b) {
a= a < 0 ? -1 : (a > 0 ? 1 : 0);
b= b < 0 ? -1 : (b > 0 ? 1 : 0);

View file

@ -27,14 +27,14 @@ public class LongString implements IString {
private final long record;
private int hash;
// Additional fields of first record
private static final int LENGTH = 0; // must be first to match ShortString
// Additional fields of first record.
private static final int LENGTH = 0; // Must be first to match ShortString.
private static final int NEXT1 = 4;
private static final int CHARS1 = 8;
private static final int NUM_CHARS1 = (Database.MAX_MALLOC_SIZE - CHARS1) / 2;
// Additional fields of subsequent records
// Additional fields of subsequent records.
private static final int NEXTN = 0;
private static final int CHARSN = 4;
@ -52,7 +52,7 @@ public class LongString implements IString {
this.db = db;
this.record = db.malloc(Database.MAX_MALLOC_SIZE);
// Write the first record
// Write the first record.
final int length = chars.length;
db.putInt(this.record, useBytes ? -length : length);
Chunk chunk= db.getChunk(this.record);
@ -63,7 +63,7 @@ public class LongString implements IString {
chunk.putChars(this.record + CHARS1, chars, 0, numChars1);
}
// write the subsequent records
// Write the subsequent records.
long lastNext = this.record + NEXT1;
int start = numChars1;
while (length - start > numCharsn) {
@ -79,7 +79,7 @@ public class LongString implements IString {
lastNext = nextRecord + NEXTN;
}
// Write last record
// Write the last record.
int remaining= length - start;
long nextRecord = db.malloc(CHARSN + (useBytes ? remaining : remaining * 2));
db.putRecPtr(lastNext, nextRecord);
@ -133,7 +133,7 @@ public class LongString implements IString {
chunk.getChars(p + CHARSN, chars, start, partLen);
}
start += partLen;
p=p + NEXTN;
p= p + NEXTN;
}
return chars;
}
@ -151,9 +151,9 @@ public class LongString implements IString {
}
long nextRecord = db.getRecPtr(record + NEXT1);
db.free(record);
length-= numChars1;
length -= numChars1;
// Middle records
// Middle records.
while (length > numCharsn) {
length -= numCharsn;
long nextnext = db.getRecPtr(nextRecord + NEXTN);
@ -161,7 +161,7 @@ public class LongString implements IString {
nextRecord = nextnext;
}
// Last record
// Last record.
db.free(nextRecord);
}