Filestorage tests: Avoid globals to improve script readability

This commit is contained in:
Ernest Wong 2018-11-21 21:41:14 +13:00 committed by Fabian
commit ba8a67f249

View file

@ -53,9 +53,9 @@ function assert_uint8array_equal(actual, expected)
return true;
}
const db = new Map();
function mock_indexeddb()
{
const db = new Map();
return {
transaction(store_name, mode) {
return {
@ -81,42 +81,36 @@ function mock_indexeddb()
};
}
// Oracle without chunking.
const memory_file_storage = new MemoryFileStorage();
// IUT with chunking.
const indexeddb_file_storage = new IndexedDBFileStorage(mock_indexeddb());
async function test_read(key, offset, count) // jshint ignore:line
async function test_read(oracle, iut, key, offset, count) // jshint ignore:line
{
const expected = await memory_file_storage.read(key, offset, count); // jshint ignore:line
const actual = await indexeddb_file_storage.read(key, offset, count); // jshint ignore:line
const expected = await oracle.read(key, offset, count); // jshint ignore:line
const actual = await iut.read(key, offset, count); // jshint ignore:line
return assert_uint8array_equal(actual, expected);
}
async function test_with_file(key, file_data) // jshint ignore:line
async function test_with_file(oracle, iut, key, file_data) // jshint ignore:line
{
console.log("Testing file with size: %d", file_data.length);
await memory_file_storage.set(key, file_data); // jshint ignore:line
await indexeddb_file_storage.set(key, file_data); // jshint ignore:line
await oracle.set(key, file_data); // jshint ignore:line
await iut.set(key, file_data); // jshint ignore:line
// Some boundary values.
if(!await test_read(key, 0, 0)) return; // jshint ignore:line
if(!await test_read(key, 0, 1)) return; // jshint ignore:line
if(!await test_read(key, 0, 4096)) return; // jshint ignore:line
if(!await test_read(key, 0, 4097)) return; // jshint ignore:line
if(!await test_read(key, 4095, 2)) return; // jshint ignore:line
if(!await test_read(key, 4096, 1)) return; // jshint ignore:line
if(!await test_read(key, 4096, 4096)) return; // jshint ignore:line
if(!await test_read(key, 4097, 1)) return; // jshint ignore:line
if(!await test_read(key, 4097, 4095)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 0, 0)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 0, 1)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 0, 4096)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 0, 4097)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 4095, 2)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 4096, 1)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 4096, 4096)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 4097, 1)) return; // jshint ignore:line
if(!await test_read(oracle, iut, key, 4097, 4095)) return; // jshint ignore:line
// Random ranges.
for(let i = 0; i < NUMBER_OF_TESTREADS; i++)
{
const offset = Math.floor(Math.random() * MAX_TESTFILE_SIZE);
const count = Math.floor(Math.random() * MAX_TESTFILE_SIZE);
const pass = await test_read(key, offset, count); // jshint ignore:line
const pass = await test_read(oracle, iut, key, offset, count); // jshint ignore:line
if(!pass)
{
log_fail("Test case offset=%d, count=%d", offset, count);
@ -129,17 +123,24 @@ async function test_with_file(key, file_data) // jshint ignore:line
async function test_start() // jshint ignore:line
{
if(!await test_with_file("empty", new Uint8Array(0))) return; // jshint ignore:line
if(!await test_with_file("single", new Uint8Array(1).map(v => Math.random() * 0xFF))) return; // jshint ignore:line
if(!await test_with_file("1block", new Uint8Array(4096).map(v => Math.random() * 0xFF))) return; // jshint ignore:line
// Test oracle without chunking.
const oracle = new MemoryFileStorage();
// Implementation under test with chunking.
const iut = new IndexedDBFileStorage(mock_indexeddb());
if(!await test_with_file(oracle, iut, "empty", new Uint8Array(0))) return; // jshint ignore:line
if(!await test_with_file(oracle, iut, "single", new Uint8Array(1).map(v => Math.random() * 0xFF))) return; // jshint ignore:line
if(!await test_with_file(oracle, iut, "1block", new Uint8Array(4096).map(v => Math.random() * 0xFF))) return; // jshint ignore:line
for(let i = 0; i < NUMBER_OF_TESTFILES; i++)
{
const size = Math.floor(Math.random() * MAX_TESTFILE_SIZE);
const file_data = new Uint8Array(size).map(v => Math.random() * 0xFF);
const pass = await test_with_file(i.toString(), file_data); // jshint ignore:line
const pass = await test_with_file(oracle, iut, i.toString(), file_data); // jshint ignore:line
if(!pass) return;
}
log_pass("All tests passed!");
}