|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const { TernarySearchTree } = require('../lib/core/tree') |
| 3 | +const { TernarySearchTree, tree } = require('../lib/core/tree') |
| 4 | +const { wellknownHeaderNames, headerNameLowerCasedRecord } = require('../lib/core/constants') |
4 | 5 | const { test } = require('tap')
|
5 | 6 |
|
6 | 7 | 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) |
19 | 9 |
|
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 | + }) |
21 | 17 |
|
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 | + }) |
26 | 27 |
|
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 | + }) |
33 | 35 |
|
34 |
| - t.test('all', (t) => { |
| 36 | + t.test('fuzz', (t) => { |
| 37 | + const LENGTH = 2000 |
35 | 38 | 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 | + |
36 | 63 | for (let i = 0; i < LENGTH; ++i) {
|
37 | 64 | t.equal(tst.lookup(randomBuffer[i]), random[i])
|
38 | 65 | }
|
|
0 commit comments