Skip to content

Commit 4ed060f

Browse files
authored
chore: update tst test (#2538)
1 parent 5fb6dc0 commit 4ed060f

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

test/tree.js

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,65 @@
11
'use strict'
22

3-
const { TernarySearchTree } = require('../lib/core/tree')
3+
const { TernarySearchTree, tree } = require('../lib/core/tree')
4+
const { wellknownHeaderNames, headerNameLowerCasedRecord } = require('../lib/core/constants')
45
const { test } = require('tap')
56

67
test('Ternary Search Tree', (t) => {
7-
t.plan(1)
8-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
9-
const charactersLength = characters.length
10-
11-
function generateAsciiString (length) {
12-
let result = ''
13-
for (let i = 0; i < length; ++i) {
14-
result += characters[Math.floor(Math.random() * charactersLength)]
15-
}
16-
return result
17-
}
18-
const tst = new TernarySearchTree()
8+
t.plan(4)
199

20-
const LENGTH = 5000
10+
t.test('The empty key cannot be added.', (t) => {
11+
t.plan(2)
12+
t.throws(() => new TernarySearchTree().insert(Buffer.from(''), ''))
13+
const tst = new TernarySearchTree()
14+
tst.insert(Buffer.from('a'), 'a')
15+
t.throws(() => tst.insert(Buffer.from(''), ''))
16+
})
2117

22-
/** @type {string[]} */
23-
const random = new Array(LENGTH)
24-
/** @type {Buffer[]} */
25-
const randomBuffer = new Array(LENGTH)
18+
t.test('duplicate key', (t) => {
19+
t.plan(2)
20+
const tst = new TernarySearchTree()
21+
const key = Buffer.from('a')
22+
tst.insert(key, 'a')
23+
t.equal(tst.lookup(key), 'a')
24+
tst.insert(key, 'b')
25+
t.equal(tst.lookup(key), 'b')
26+
})
2627

27-
for (let i = 0; i < LENGTH; ++i) {
28-
const key = generateAsciiString((Math.random() * 100 + 5) | 0)
29-
const lowerCasedKey = random[i] = key.toLowerCase()
30-
randomBuffer[i] = Buffer.from(key)
31-
tst.insert(Buffer.from(lowerCasedKey), lowerCasedKey)
32-
}
28+
t.test('tree', (t) => {
29+
t.plan(wellknownHeaderNames.length)
30+
for (let i = 0; i < wellknownHeaderNames.length; ++i) {
31+
const key = wellknownHeaderNames[i]
32+
t.equal(tree.lookup(Buffer.from(key)), headerNameLowerCasedRecord[key])
33+
}
34+
})
3335

34-
t.test('all', (t) => {
36+
t.test('fuzz', (t) => {
37+
const LENGTH = 2000
3538
t.plan(LENGTH)
39+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
40+
const charactersLength = characters.length
41+
42+
function generateAsciiString (length) {
43+
let result = ''
44+
for (let i = 0; i < length; ++i) {
45+
result += characters[Math.floor(Math.random() * charactersLength)]
46+
}
47+
return result
48+
}
49+
const tst = new TernarySearchTree()
50+
51+
/** @type {string[]} */
52+
const random = new Array(LENGTH)
53+
/** @type {Buffer[]} */
54+
const randomBuffer = new Array(LENGTH)
55+
56+
for (let i = 0; i < LENGTH; ++i) {
57+
const key = generateAsciiString((Math.random() * 100 + 5) | 0)
58+
const lowerCasedKey = random[i] = key.toLowerCase()
59+
randomBuffer[i] = Buffer.from(key)
60+
tst.insert(Buffer.from(lowerCasedKey), lowerCasedKey)
61+
}
62+
3663
for (let i = 0; i < LENGTH; ++i) {
3764
t.equal(tst.lookup(randomBuffer[i]), random[i])
3865
}

0 commit comments

Comments
 (0)