-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
121 lines (101 loc) · 2.72 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const test = require('brittle')
const RAO = require('./')
const RAM = require('random-access-memory')
test('write and read', function (t) {
t.plan(3)
const file = new RAO(new RAM())
file.write(0, Buffer.from('hello'), function (err) {
t.not(err, 'no error')
file.read(0, 5, function (err, buf) {
t.not(err, 'no error')
t.alike(buf, Buffer.from('hello'))
})
})
})
test('read empty', function (t) {
t.plan(2)
const file = new RAO(new RAM())
file.read(0, 0, function (err, buf) {
t.not(err, 'no error')
t.alike(buf, Buffer.alloc(0), 'empty buffer')
})
})
test('read range > file', function (t) {
t.plan(1)
const file = new RAO(new RAM())
file.read(0, 5, function (err, buf) {
t.ok(err, 'not satisfiable')
})
})
test('random access write and read', function (t) {
t.plan(8)
const file = new RAO(new RAM())
file.write(10, Buffer.from('hi'), function (err) {
t.not(err, 'no error')
file.write(0, Buffer.from('hello'), function (err) {
t.not(err, 'no error')
file.read(10, 2, function (err, buf) {
t.not(err, 'no error')
t.alike(buf, Buffer.from('hi'))
file.read(0, 5, function (err, buf) {
t.not(err, 'no error')
t.alike(buf, Buffer.from('hello'))
file.read(5, 5, function (err, buf) {
t.not(err, 'no error')
t.alike(buf, Buffer.from([0, 0, 0, 0, 0]))
})
})
})
})
})
})
test('buffer constructor', function (t) {
t.plan(2)
const file = new RAO(new RAM(Buffer.from('contents')))
file.read(0, 7, function (err, buf) {
t.not(err)
t.alike(buf, Buffer.from('content'))
})
})
test('not sync', function (t) {
t.plan(3)
const file = new RAO(new RAM())
let sync = true
file.write(10, Buffer.from('hi'), function () {
t.not(sync)
sync = true
file.write(0, Buffer.from('hello'), function () {
t.not(sync)
sync = true
file.read(10, 2, function () {
t.not(sync)
})
sync = false
})
sync = false
})
sync = false
})
test('dels and stuff', function (t) {
t.plan(5)
const file = new RAO(new RAM(Buffer.from('contents')), { pageSize: 2 })
file.write(0, Buffer.from('d'), function (err) {
t.not(err)
file.read(0, 3, function (_, buf) {
t.alike(buf, Buffer.from('don'))
file.write(7, Buffer.from('zzz'), function () {
file.read(0, 10, function (_, buf) {
t.alike(buf, Buffer.from('dontentzzz'))
file.read(0, 11, function (err) {
t.ok(err)
file.del(9, Infinity, function () {
file.stat(function (_, st) {
t.is(st.size, 9)
})
})
})
})
})
})
})
})