Tests for reading and writing data.

This commit is contained in:
Alan Kligman 2013-07-22 14:31:44 -04:00
parent 40d3f25d9a
commit fb9fed0663
4 changed files with 105 additions and 14 deletions

1
dist/idbfs.js vendored
View File

@ -6593,6 +6593,7 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
} else {
var stats = {
dev: that.name,
size: result.size,
nlinks: result.nlinks,
atime: result.atime,
mtime: result.mtime,

View File

@ -24,17 +24,19 @@ var fs = new IDBFS.FileSystem('local', flags);
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
console.log('buffer', buffer);
var data = new Uint8Array(buffer.length);
console.log('data', data);
debugger;
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
console.log('descriptor:', fd);
fs.write(fd, buffer, 0, buffer.length, 0, function(error, nbytes) {
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, nbytes) {
console.log('write:', nbytes)
if(error) throw error;
fs.read(fd, data, 0, data.length, 0, function(error, nbytes) {
console.log('read:', nbytes);
console.log('data', data);
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, nbytes) {
console.log('write:', nbytes);
fs.stat('/myfile', function(error, stats) {
if(error) throw error;
console.log(stats);
});
});
});
});

View File

@ -793,6 +793,7 @@ define(function(require) {
} else {
var stats = {
dev: that.name,
size: result.size,
nlinks: result.nlinks,
atime: result.atime,
mtime: result.mtime,

View File

@ -25,15 +25,11 @@ function mk_db_name() {
};
function array_buffer_equal(left, right) {
if(!(left instanceof ArrayBuffer && right instanceof ArrayBuffer)) {
if(left.length !== right.length) {
return false;
}
if(left.byteLength !== right.byteLength) {
return false;
}
for(var i = 0; i < left.byteLength; ++ i) {
for(var i = 0; i < left.length; ++ i) {
if(left[i] !== right[i]) {
return false;
}
@ -143,6 +139,7 @@ describe('fs.stat', function() {
expect(_result).toBeDefined();
expect(_error).not.toBeDefined();
expect(_result['dev']).toEqual(that.db_name);
expect(_result['size']).toBeDefined();
expect(_result['nlinks']).toEqual(jasmine.any(Number));
expect(_result['atime']).toEqual(jasmine.any(Number));
expect(_result['mtime']).toEqual(jasmine.any(Number));
@ -526,7 +523,7 @@ describe('fs.write', function() {
it('should write data to a file', function() {
var complete = false;
var _error, _result;
var _error, _result, _stats;
var that = this;
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
@ -539,7 +536,13 @@ describe('fs.write', function() {
_error = error;
_result = result;
complete = true;
that.fs.stat('/myfile', function(error, result) {
if(error) throw error;
_stats = result;
complete = true;
});
})
});
@ -550,6 +553,49 @@ describe('fs.write', function() {
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(buffer.length);
expect(_stats.size).toEqual(buffer.length);
});
});
it('should update the current file position', function() {
var complete = false;
var _error, _result, _stats;
var that = this;
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
_result = 0;
that.fs.open('/myfile', 'w', function(error, result) {
if(error) throw error;
var fd = result;
that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
_result += result;
that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
_result += result;
that.fs.stat('/myfile', function(error, result) {
if(error) throw error;
_stats = result;
complete = true;
});
});
})
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(2 * buffer.length);
expect(_stats.size).toEqual(_result);
});
});
});
@ -597,6 +643,47 @@ describe('fs.read', function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(rbuffer.length);
expect(array_buffer_equal(wbuffer, rbuffer)).toEqual(true);
});
});
it('should update the current file position', function() {
var complete = false;
var _error, _result;
var that = this;
var wbuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var rbuffer = new Uint8Array(wbuffer.length);
_result = 0;
that.fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
var fd = result;
that.fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) {
if(error) throw error;
that.fs.read(fd, rbuffer, 0, rbuffer.length / 2, undefined, function(error, result) {
if(error) throw error;
_result += result;
that.fs.read(fd, rbuffer, rbuffer.length / 2, rbuffer.length, undefined, function(error, result) {
if(error) throw error;
_result += result;
complete = true;
});
});
})
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(rbuffer.length);