-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
67 lines (52 loc) · 1.91 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
var test = require('tape')
var uuid = require('.')
var isUuid = require('is-uuid')
test('constants', function (assert) {
assert.same(typeof uuid.BYTES, 'number')
assert.ok(uuid.BYTES > 0)
assert.end()
})
test('returns valid uuid', function (assert) {
var uuid1 = uuid()
var uuid2 = uuid(Buffer.alloc(16))
var uuid3 = uuid(Buffer.alloc(32))
var uuid4 = uuid(Buffer.alloc(uuid.BYTES))
assert.notOk(uuid1.equals(uuid2), 'not same uuid')
assert.notOk(uuid1.equals(uuid3.slice(0, 16)), 'not same uuid')
assert.notOk(uuid1.equals(uuid4), 'not same uuid')
assert.notOk(uuid2.equals(uuid3.slice(0, 16)), 'not same uuid')
assert.notOk(uuid2.equals(uuid4), 'not same uuid')
assert.notOk(uuid3.slice(0, 16).equals(uuid4), 'not same uuid')
assert.ok(uuid3.slice(16).equals(Buffer.alloc(16)), 'upper 16 bytes still clear')
assert.ok(isUuid.v4(uuid.stringify(uuid1)), 'valid uuid v4')
assert.ok(isUuid.v4(uuid.stringify(uuid2)), 'valid uuid v4')
assert.ok(isUuid.v4(uuid.stringify(uuid3)), 'valid uuid v4')
assert.ok(isUuid.v4(uuid.stringify(uuid4)), 'valid uuid v4')
assert.end()
})
test('degenerate cases', function (assert) {
assert.throws(function () { uuid(Buffer.alloc(0)) })
assert.throws(function () { uuid(Buffer.alloc(1)) })
assert.throws(function () { uuid(Buffer.alloc(8)) })
assert.throws(function () { uuid(Buffer.alloc(15)) })
assert.throws(function () { uuid(Buffer.alloc(uuid.BYTES - 1)) })
assert.end()
})
test('parse/stringify', function (assert) {
for (var i = 0; i < 1e5; i++) {
var id = uuid()
var str = uuid.stringify(id)
var cid = uuid.parse(str)
if (!id.equals(cid)) assert.fail('id != cid')
}
assert.end()
})
test('isUUID', function (assert) {
for (var i = 0; i < 1e5; i++) {
var id = uuid()
var str = uuid.stringify(id)
if (!uuid.isUUID(id)) assert.fail('id not uuid')
if (!uuid.isUUID(str)) assert.fail('str not uuid')
}
assert.end()
})