-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
77 lines (66 loc) · 1.6 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
var int53 = require('./index')
function assert (test) {
if(!test) throw new Error('assert failed')
}
function testUInt64(x) {
var b = new Buffer(8)
int53.writeUInt64BE(x, b)
assert(x === int53.readUInt64BE(b))
int53.writeUInt64LE(x, b)
assert(x === int53.readUInt64LE(b))
}
function testInt64(x) {
var b = new Buffer(8)
int53.writeInt64BE(x, b)
assert(x === int53.readInt64BE(b))
int53.writeInt64LE(x, b)
assert(x === int53.readInt64LE(b))
}
function error(func, number, message) {
try {
func(number)
assert(false)
}
catch (e) {
assert(e.message === message, e.message)
}
}
testUInt64(0)
testUInt64(1)
testUInt64(0xFFFFFFFF - 2)
testUInt64(0xFFFFFFFF - 1)
testUInt64(0xFFFFFFFF)
testUInt64(0xFFFFFFFF + 1)
testUInt64(0xFFFFFFFF + 2)
testUInt64(0xFFFFFFFFFFFFF)
testUInt64(0x1FFFFFFFFFFFFF)
error(testUInt64, 0x1FFFFFFFFFFFFF + 1, 'number out of range')
error(testUInt64, -1, 'number out of range')
error(testUInt64, 1.1, 'number must be an integer')
try {
var b = new Buffer('FFFFFFFFFFFFFFFF', 'hex')
var x = int53.readUInt64BE(b)
console.assert(false)
}
catch (e) {
console.assert(e.message === 'number too large', e.message)
}
testInt64(-2147483648)
testInt64(-1)
testInt64(1)
testInt64(-64424509440)
testInt64(-4294967297)
testInt64(-4294967296)
testInt64(-4294967295)
testInt64(-9007199254740991)
error(testInt64, -9007199254740992, 'number out of range')
error(testInt64, -1.1, 'number must be an integer')
try {
var b = new Buffer('FFDFFFFFFFFFFFFF', 'hex')
var x = int53.readInt64BE(b)
assert(false)
}
catch (e) {
assert(e.message === 'number too small', e.message)
}
console.log("SUCCESS!")