-
Notifications
You must be signed in to change notification settings - Fork 2
/
check.js
52 lines (43 loc) · 1.33 KB
/
check.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
// Read in a patch file and check that the patches all apply correctly.
const fs = require('fs')
const assert = require('assert')
const zlib = require('zlib')
const filename = process.argv[2]
if (filename == null) {
console.error(`Usage: $ node check.js file.json[.gz]`)
process.exit(1)
}
const {
startContent,
endContent,
txns
} = JSON.parse(
filename.endsWith('.gz')
? zlib.gunzipSync(fs.readFileSync(filename))
: fs.readFileSync(filename, 'utf-8')
)
let content = startContent
console.log('applying', txns.length, 'txns...')
console.time('apply')
let lastTime = 0
for (let i = 0; i < txns.length; i++) {
if (i % 10000 == 0) console.log(i)
const {patches} = txns[i]
// assert(time != null)
assert(patches != null)
assert(patches.length > 0)
// let t = new Date(time).getTime()
// assert(t >= lastTime)
// lastTime = t
for (const [pos, delHere, insContent] of patches) {
// if (insContent != '') console.log('Ins', {pos, content: insContent})
// if (delHere != 0) console.log('Del', {pos, len: delHere})
assert(content.length >= pos + delHere)
const before = content.slice(0, pos)
const after = content.slice(pos + delHere)
content = before + insContent + after
}
}
console.timeEnd('apply')
assert.strictEqual(content, endContent)
console.log(`Looking good - ${txns.length} apply cleanly.`)