Skip to content

Commit

Permalink
Update Standard and port tests to Brittle (#14)
Browse files Browse the repository at this point in the history
* Implement as class and port tests to Brittle

* Ignore coverage

* Update RAS

* Use `RAM`

* Ticks
  • Loading branch information
kasperisager authored Aug 1, 2022
1 parent 010a294 commit 276abed
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
coverage
package-lock.json
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ npm install random-access-memory
## Usage

``` js
var ram = require('random-access-memory')
var file = ram()
const RAM = require('random-access-memory')
const file = new RAM()

file.write(0, Buffer.from('hello'), function () {
file.write(5, Buffer.from(' world'), function () {
Expand All @@ -19,10 +19,10 @@ file.write(0, Buffer.from('hello'), function () {
})
```

You can also initialize a `ram` instance with a `Buffer`:
You can also initialize a `RAM` instance with a `Buffer`:

```js
var file = ram(Buffer.from('hello world'))
const file = new RAM(Buffer.from('hello world'))
```

## License
Expand Down
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ram = require('./index')
const RAM = require('.')

const mem = ram()
const mem = new RAM()

mem.write(0, Buffer.from('hello world'), function () {
mem.read(0, 11, (_, data) => console.log(data.toString()))
Expand Down
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module.exports = class RAM extends RandomAccess {
constructor (opts) {
super()

if (typeof opts === 'number') opts = {length: opts}
if (typeof opts === 'number') opts = { length: opts }
if (!opts) opts = {}

if (b4a.isBuffer(opts)) {
opts = {length: opts.length, buffer: opts}
opts = { length: opts.length, buffer: opts }
}
if (!isOptions(opts)) opts = {}

Expand All @@ -24,13 +24,13 @@ module.exports = class RAM extends RandomAccess {
}

_stat (req) {
req.callback(null, {size: this.length})
req.callback(null, { size: this.length })
}

_write (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
let i = Math.floor(req.offset / this.pageSize)
let rel = req.offset - i * this.pageSize
let start = 0

const len = req.offset + req.size
if (len > this.length) this.length = len
Expand All @@ -51,9 +51,9 @@ module.exports = class RAM extends RandomAccess {
}

_read (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
let i = Math.floor(req.offset / this.pageSize)
let rel = req.offset - i * this.pageSize
let start = 0

if (req.offset + req.size > this.length) {
return req.callback(new Error('Could not satisfy length'), null)
Expand All @@ -76,12 +76,12 @@ module.exports = class RAM extends RandomAccess {
}

_del (req) {
var i = Math.floor(req.offset / this.pageSize)
var rel = req.offset - i * this.pageSize
var start = 0
let i = Math.floor(req.offset / this.pageSize)
let rel = req.offset - i * this.pageSize
let start = 0

if (rel && req.offset + req.size >= this.length) {
var buf = this.buffers[i]
const buf = this.buffers[i]
if (buf) buf.fill(0, rel)
}

Expand Down Expand Up @@ -113,7 +113,7 @@ module.exports = class RAM extends RandomAccess {
}

_page (i, upsert) {
var page = this.buffers[i]
let page = this.buffers[i]
if (page || !upsert) return page
page = this.buffers[i] = b4a.alloc(this.pageSize)
return page
Expand All @@ -122,7 +122,7 @@ module.exports = class RAM extends RandomAccess {
toBuffer () {
const buf = b4a.alloc(this.length)

for (var i = 0; i < this.buffers.length; i++) {
for (let i = 0; i < this.buffers.length; i++) {
if (this.buffers[i]) b4a.copy(this.buffers[i], buf, i * this.pageSize)
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"description": "Exposes the same interface as random-access-file but instead of writing/reading data to a file it maintains it in memory",
"main": "index.js",
"scripts": {
"test": "standard && tape test.js"
"test": "standard && brittle test.js"
},
"dependencies": {
"b4a": "^1.1.0",
"is-options": "^1.0.1",
"random-access-storage": "^2.1.0"
},
"devDependencies": {
"standard": "^11.0.1",
"tape": "^4.9.0"
"brittle": "^2.3.1",
"standard": "^17.0.0"
},
"repository": {
"type": "git",
Expand Down
101 changes: 55 additions & 46 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,91 @@
const tape = require('tape')
const RAM = require('./')
const test = require('brittle')
const RAM = require('.')

test('write and read', function (t) {
t.plan(3)

tape('write and read', function (t) {
const file = new RAM()

file.write(0, Buffer.from('hello'), function (err) {
t.error(err, 'no error')
t.absent(err, 'no error')
file.read(0, 5, function (err, buf) {
t.error(err, 'no error')
t.same(buf, Buffer.from('hello'))
t.end()
t.absent(err, 'no error')
t.alike(buf, Buffer.from('hello'))
})
})
})

tape('read empty', function (t) {
test('read empty', function (t) {
t.plan(2)

const file = new RAM()

file.read(0, 0, function (err, buf) {
t.error(err, 'no error')
t.same(buf, Buffer.alloc(0), 'empty buffer')
t.end()
t.absent(err, 'no error')
t.alike(buf, Buffer.alloc(0), 'empty buffer')
})
})

tape('read range > file', function (t) {
test('read range > file', function (t) {
t.plan(1)

const file = new RAM()

file.read(0, 5, function (err, buf) {
t.ok(err, 'not satisfiable')
t.end()
})
})

tape('random access write and read', function (t) {
test('random access write and read', function (t) {
t.plan(8)

const file = new RAM()

file.write(10, Buffer.from('hi'), function (err) {
t.error(err, 'no error')
t.absent(err, 'no error')
file.write(0, Buffer.from('hello'), function (err) {
t.error(err, 'no error')
t.absent(err, 'no error')
file.read(10, 2, function (err, buf) {
t.error(err, 'no error')
t.same(buf, Buffer.from('hi'))
t.absent(err, 'no error')
t.alike(buf, Buffer.from('hi'))
file.read(0, 5, function (err, buf) {
t.error(err, 'no error')
t.same(buf, Buffer.from('hello'))
t.absent(err, 'no error')
t.alike(buf, Buffer.from('hello'))
file.read(5, 5, function (err, buf) {
t.error(err, 'no error')
t.same(buf, Buffer.from([0, 0, 0, 0, 0]))
t.end()
t.absent(err, 'no error')
t.alike(buf, Buffer.from([0, 0, 0, 0, 0]))
})
})
})
})
})
})

tape('buffer constructor', function (t) {
test('buffer constructor', function (t) {
t.plan(2)

const file = new RAM(Buffer.from('contents'))

file.read(0, 7, function (err, buf) {
t.error(err)
t.deepEqual(buf, Buffer.from('content'))
t.end()
t.absent(err)
t.alike(buf, Buffer.from('content'))
})
})

tape('not sync', function (t) {
test('not sync', function (t) {
t.plan(3)

const file = new RAM()
let sync = true

file.write(10, Buffer.from('hi'), function () {
t.notOk(sync)
t.absent(sync)
sync = true
file.write(0, Buffer.from('hello'), function () {
t.notOk(sync)
t.absent(sync)
sync = true
file.read(10, 2, function () {
t.notOk(sync)
t.end()
t.absent(sync)
})
sync = false
})
Expand All @@ -87,7 +94,9 @@ tape('not sync', function (t) {
sync = false
})

tape('delete', function (t) {
test('delete', function (t) {
t.plan(6)

const pageSize = 1024
const file = new RAM({ pageSize })

Expand All @@ -96,34 +105,34 @@ tape('delete', function (t) {
const expected = Buffer.alloc(10, 0xff)

file.write(0, orig, function (err) {
t.error(err, 'no error')
t.absent(err, 'no error')
file.read(0, file.length, function (err, buf) {
t.error(err, 'no error')
t.same(buf, orig)
t.absent(err, 'no error')
t.alike(buf, orig)
file.del(10, Infinity, function (err) {
t.error(err, 'no error')
t.absent(err, 'no error')
file.read(0, file.length, function (err, buf) {
t.error(err, 'no error')
t.same(buf, expected)
t.end()
t.absent(err, 'no error')
t.alike(buf, expected)
})
})
})
})
})

tape('clone', function (t) {
test('clone', function (t) {
t.plan(4)

const file = new RAM()

file.write(0, Buffer.from('hello'), function (err) {
t.error(err, 'no error')
t.absent(err, 'no error')
const file2 = file.clone()
file.write(0, Buffer.from('world'), function (err) {
t.error(err, 'no error')
t.absent(err, 'no error')
file2.read(0, 5, function (err, buf) {
t.error(err, 'no error')
t.same(buf, Buffer.from('hello'))
t.end()
t.absent(err, 'no error')
t.alike(buf, Buffer.from('hello'))
})
})
})
Expand Down

0 comments on commit 276abed

Please sign in to comment.