提交 16679fbf authored 作者: Thomas Mueller's avatar Thomas Mueller

Test

上级 ab51dbf6
...@@ -204,6 +204,7 @@ public class TestFileSystem extends TestBase { ...@@ -204,6 +204,7 @@ public class TestFileSystem extends TestBase {
} }
private void testRandomAccess(String fsBase) throws Exception { private void testRandomAccess(String fsBase) throws Exception {
StringBuilder buff = new StringBuilder();
FileSystem fs = FileSystem.getInstance(fsBase); FileSystem fs = FileSystem.getInstance(fsBase);
String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false); String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
File file = new File(TestBase.BASE_TEST_DIR + "/tmp"); File file = new File(TestBase.BASE_TEST_DIR + "/tmp");
...@@ -221,66 +222,77 @@ public class TestFileSystem extends TestBase { ...@@ -221,66 +222,77 @@ public class TestFileSystem extends TestBase {
f.sync(); f.sync();
Random random = new Random(1); Random random = new Random(1);
int size = getSize(100, 500); int size = getSize(100, 500);
for (int i = 0; i < size; i++) { try {
int pos = random.nextInt(10000); for (int i = 0; i < size; i++) {
switch(random.nextInt(7)) { int pos = random.nextInt(10000);
case 0: { switch(random.nextInt(7)) {
pos = (int) Math.min(pos, ra.length()); case 0: {
trace("seek " + pos); pos = (int) Math.min(pos, ra.length());
f.seek(pos); trace("seek " + pos);
ra.seek(pos); buff.append("seek " + pos + "\n");
break; f.seek(pos);
} ra.seek(pos);
case 1: { break;
byte[] buffer = new byte[random.nextInt(1000)]; }
random.nextBytes(buffer); case 1: {
trace("write " + buffer.length); byte[] buffer = new byte[random.nextInt(1000)];
f.write(buffer, 0, buffer.length); random.nextBytes(buffer);
ra.write(buffer, 0, buffer.length); trace("write " + buffer.length);
break; buff.append("write " + buffer.length + "\n");
} f.write(buffer, 0, buffer.length);
case 2: { ra.write(buffer, 0, buffer.length);
f.setFileLength(pos); break;
ra.setLength(pos); }
if (ra.getFilePointer() > pos) { case 2: {
f.seek(0); f.setFileLength(pos);
ra.seek(0); ra.setLength(pos);
if (ra.getFilePointer() > pos) {
f.seek(0);
ra.seek(0);
}
trace("setLength " + pos);
buff.append("setLength " + pos + "\n");
break;
}
case 3: {
int len = random.nextInt(1000);
len = (int) Math.min(len, ra.length() - ra.getFilePointer());
byte[] b1 = new byte[len];
byte[] b2 = new byte[len];
ra.readFully(b1, 0, len);
f.readFully(b2, 0, len);
trace("readFully " + len);
buff.append("readFully " + len + "\n");
assertEquals(b1, b2);
break;
}
case 4: {
trace("getFilePointer");
buff.append("getFilePointer\n");
assertEquals(ra.getFilePointer(), f.getFilePointer());
break;
}
case 5: {
trace("length " + ra.length());
buff.append("length " + ra.length() + "\n");
assertEquals(ra.length(), f.length());
break;
}
case 6: {
trace("reopen");
buff.append("reopen\n");
f.close();
ra.close();
ra = new RandomAccessFile(file, "rw");
f = fs.openFileObject(s, "rw");
assertEquals(ra.length(), f.length());
break;
}
default:
} }
trace("setLength " + pos);
break;
}
case 3: {
int len = random.nextInt(1000);
len = (int) Math.min(len, ra.length() - ra.getFilePointer());
byte[] b1 = new byte[len];
byte[] b2 = new byte[len];
ra.readFully(b1, 0, len);
f.readFully(b2, 0, len);
trace("readFully " + len);
assertEquals(b1, b2);
break;
}
case 4: {
trace("getFilePointer");
assertEquals(ra.getFilePointer(), f.getFilePointer());
break;
}
case 5: {
trace("length " + ra.length());
assertEquals(ra.length(), f.length());
break;
}
case 6: {
trace("reopen");
f.close();
ra.close();
ra = new RandomAccessFile(file, "rw");
f = fs.openFileObject(s, "rw");
assertEquals(ra.length(), f.length());
break;
}
default:
} }
} catch (Exception e) {
fail("Exception: " + e + "\n"+ buff.toString());
} }
f.close(); f.close();
ra.close(); ra.close();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论