Skip to content

Commit

Permalink
chore: update tst test (nodejs#2538)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored and crysmags committed Feb 27, 2024
1 parent 155c732 commit 035725c
Showing 1 changed file with 52 additions and 25 deletions.
77 changes: 52 additions & 25 deletions test/tree.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
'use strict'

const { TernarySearchTree } = require('../lib/core/tree')
const { TernarySearchTree, tree } = require('../lib/core/tree')
const { wellknownHeaderNames, headerNameLowerCasedRecord } = require('../lib/core/constants')
const { test } = require('tap')

test('Ternary Search Tree', (t) => {
t.plan(1)
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length

function generateAsciiString (length) {
let result = ''
for (let i = 0; i < length; ++i) {
result += characters[Math.floor(Math.random() * charactersLength)]
}
return result
}
const tst = new TernarySearchTree()
t.plan(4)

const LENGTH = 5000
t.test('The empty key cannot be added.', (t) => {
t.plan(2)
t.throws(() => new TernarySearchTree().insert(Buffer.from(''), ''))
const tst = new TernarySearchTree()
tst.insert(Buffer.from('a'), 'a')
t.throws(() => tst.insert(Buffer.from(''), ''))
})

/** @type {string[]} */
const random = new Array(LENGTH)
/** @type {Buffer[]} */
const randomBuffer = new Array(LENGTH)
t.test('duplicate key', (t) => {
t.plan(2)
const tst = new TernarySearchTree()
const key = Buffer.from('a')
tst.insert(key, 'a')
t.equal(tst.lookup(key), 'a')
tst.insert(key, 'b')
t.equal(tst.lookup(key), 'b')
})

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

t.test('all', (t) => {
t.test('fuzz', (t) => {
const LENGTH = 2000
t.plan(LENGTH)
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length

function generateAsciiString (length) {
let result = ''
for (let i = 0; i < length; ++i) {
result += characters[Math.floor(Math.random() * charactersLength)]
}
return result
}
const tst = new TernarySearchTree()

/** @type {string[]} */
const random = new Array(LENGTH)
/** @type {Buffer[]} */
const randomBuffer = new Array(LENGTH)

for (let i = 0; i < LENGTH; ++i) {
const key = generateAsciiString((Math.random() * 100 + 5) | 0)
const lowerCasedKey = random[i] = key.toLowerCase()
randomBuffer[i] = Buffer.from(key)
tst.insert(Buffer.from(lowerCasedKey), lowerCasedKey)
}

for (let i = 0; i < LENGTH; ++i) {
t.equal(tst.lookup(randomBuffer[i]), random[i])
}
Expand Down

0 comments on commit 035725c

Please sign in to comment.