From 3ca8235b94491971b2878fa9276daea338fbe57d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 8 Jan 2024 22:41:30 +0100 Subject: [PATCH] test: make named require test works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- benchmark/interval-tree/performance.js | 2 +- benchmark/misc/dynamic-arrays.js | 2 +- benchmark/misc/hashmap.js | 32 +++++++++++------------ experiments/critbit.js | 6 ++--- index.d.ts | 2 +- index.js | 15 +++++++++-- index.mjs | 2 +- package.json | 6 ++--- test/cjs/{require.js => named-require.js} | 8 +++--- test/esm/import.mjs | 8 +++--- test/types.ts | 6 +++-- vector.d.ts | 2 +- 12 files changed, 54 insertions(+), 37 deletions(-) rename test/cjs/{require.js => named-require.js} (97%) diff --git a/benchmark/interval-tree/performance.js b/benchmark/interval-tree/performance.js index 6f0e0c53..e6acb89f 100644 --- a/benchmark/interval-tree/performance.js +++ b/benchmark/interval-tree/performance.js @@ -22,7 +22,7 @@ console.log('Intervals:', SIZE); console.time('StaticIntervalTree.from'); var tree = StaticIntervalTree.from(INTERVALS); console.timeEnd('StaticIntervalTree.from'); -console.log('Tree height: ' + tree.height) +console.log('Tree height: ' + tree.height); p = random(0, MAX); diff --git a/benchmark/misc/dynamic-arrays.js b/benchmark/misc/dynamic-arrays.js index 5e554827..2c40263c 100644 --- a/benchmark/misc/dynamic-arrays.js +++ b/benchmark/misc/dynamic-arrays.js @@ -1,4 +1,4 @@ -var SIZE = 100000 +var SIZE = 100000; function Wrapper() { this.array = new Float64Array(SIZE); diff --git a/benchmark/misc/hashmap.js b/benchmark/misc/hashmap.js index fa2c0772..271dce58 100644 --- a/benchmark/misc/hashmap.js +++ b/benchmark/misc/hashmap.js @@ -55,21 +55,21 @@ function exponentialSearch(array, key) { return -1; } -function interpolationSearch(arrayToSearch, valueToSearch){ +function interpolationSearch(arrayToSearch, valueToSearch) { var length = arrayToSearch.length; var low = 0; - var high = length-1; + var high = length - 1; var position = -1; var delta = -1; - while(low <= high + while (low <= high && valueToSearch >= arrayToSearch[low] - && valueToSearch <= arrayToSearch[high]){ - delta = (valueToSearch-arrayToSearch[low])/(arrayToSearch[high]-arrayToSearch[low]); - position = low + Math.floor((high-low)*delta); - if (arrayToSearch[position] == valueToSearch){ + && valueToSearch <= arrayToSearch[high]) { + delta = (valueToSearch - arrayToSearch[low]) / (arrayToSearch[high] - arrayToSearch[low]); + position = low + Math.floor((high - low) * delta); + if (arrayToSearch[position] == valueToSearch) { return position; } - if (arrayToSearch[position] < valueToSearch){ + if (arrayToSearch[position] < valueToSearch) { low = position + 1; } else { high = position - 1; @@ -104,7 +104,7 @@ function optimized(array, value, lo, hi) { } return -1; -}; +} function better(array, key) { var mid = -1; @@ -123,7 +123,7 @@ function better(array, key) { else if (key > current) lo = mid + 1; else - return mid + return mid; } return -1; @@ -145,7 +145,7 @@ function PerfectMap(strings) { PerfectMap.prototype.set = function(key, value) { var index = binarySearch(this.hashes, fnv32a(key)); this.values[index] = value; -} +}; PerfectMap.prototype.get = function(key, value) { var index = binarySearch(this.hashes, fnv32a(key)); @@ -159,7 +159,7 @@ var map = new Map(); var sorted = words.slice().sort(); perfect.set('hello', 36); -console.log(perfect.get('hello')) +console.log(perfect.get('hello')); console.time('Perfect set'); for (var i = 0; i < words.length; i++) @@ -217,9 +217,9 @@ function hash(str) { 'use asm'; var h = 5381, - i = str.length; + i = str.length; - while(i) { + while (i) { h *= 33; h ^= str.charCodeAt(--i); } @@ -298,7 +298,7 @@ console.timeEnd('Get'); console.time('GetObject'); for (var w = 0, y = words.length; w < y; w++) - v = ob[words[w]] + v = ob[words[w]]; console.timeEnd('GetObject'); console.time('GetMap'); @@ -313,7 +313,7 @@ console.timeEnd('GetMap'); console.time('MissesObject'); for (var w = 0, y = words.length; w < y; w++) - v = ob[randomString(4, 10)] + v = ob[randomString(4, 10)]; console.timeEnd('MissesObject'); console.time('MissesMap'); diff --git a/experiments/critbit.js b/experiments/critbit.js index a5c57659..bee61f06 100644 --- a/experiments/critbit.js +++ b/experiments/critbit.js @@ -102,7 +102,7 @@ function criticalGt(a, b) { } function InternalNode(critical) { - this.critical = critical + this.critical = critical; this.left = null; this.right = null; } @@ -270,7 +270,7 @@ CritBitTree.prototype.delete = function(key) { var node = this.root, parent = null, - wentRightForParent = false + wentRightForParent = false; grandparent = null, wentRightForGranparent = false; @@ -358,7 +358,7 @@ function log(tree) { const title = printNode; - const children = node => (node instanceof ExternalNode ? null : [node.left , node.right]); + const children = node => (node instanceof ExternalNode ? null : [node.left, node.right]); console.log(asciitree(tree.root, title, children)); } diff --git a/index.d.ts b/index.d.ts index abaef492..57654a60 100644 --- a/index.d.ts +++ b/index.d.ts @@ -44,5 +44,5 @@ export {default as SuffixArray, GeneralizedSuffixArray} from './suffix-array'; export {default as SymSpell} from './symspell'; export {default as Trie} from './trie'; export {default as TrieMap} from './trie-map'; -export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Array} from './vector'; +export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Vector} from './vector'; export {default as VPTree} from './vp-tree'; diff --git a/index.js b/index.js index 9b43b873..34b1a46d 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,8 @@ var Heap = require('./heap.js'), FibonacciHeap = require('./fibonacci-heap.js'), SuffixArray = require('./suffix-array.js'), - BiMap = require('./bi-map.js'); + BiMap = require('./bi-map.js'), + Vector = require('./vector.js'); module.exports = { BiMap: BiMap, @@ -55,6 +56,16 @@ module.exports = { SymSpell: require('./symspell.js'), Trie: require('./trie.js'), TrieMap: require('./trie-map.js'), - Vector: require('./vector.js'), + Vector: Vector, + Uint8Vector: Vector.Uint8Vector, + Uint8ClampedVector: Vector.Uint8ClampedVector, + Int8Vector: Vector.Int8Vector, + Uint16Vector: Vector.Uint16Vector, + Int16Vector: Vector.Int16Vector, + Uint32Vector: Vector.Uint32Vector, + Int32Vector: Vector.Int32Vector, + Float32Vector: Vector.Float32Vector, + Float64Vector: Vector.Float64Vector, + PointerVector: Vector.PointerVector, VPTree: require('./vp-tree.js') }; diff --git a/index.mjs b/index.mjs index 20ba210f..140d7443 100644 --- a/index.mjs +++ b/index.mjs @@ -44,5 +44,5 @@ export {default as SparseSet} from './sparse-set.js'; export {default as SymSpell} from './symspell.js'; export {default as Trie} from './trie.js'; export {default as TrieMap} from './trie-map.js'; -export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Array} from './vector.js'; +export {default as Vector, Uint8Vector, Uint8ClampedVector, Int8Vector, Uint16Vector, Int16Vector, Uint32Vector, Int32Vector, Float32Vector, Float64Vector} from './vector.js'; export {default as VPTree} from './vp-tree.js'; diff --git a/package.json b/package.json index 685ba6b9..371eadfe 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,11 @@ "version": "0.39.7", "description": "Curated collection of data structures for the JavaScript/TypeScript.", "scripts": { - "lint": "eslint --cache ./*.js ./*.mjs ./utils ./test", - "lint:fix": "eslint --cache --fix ./*.js ./*.mjs ./utils ./test", + "lint": "eslint --cache --ext .js,.mjs ./*.js ./*.mjs ./utils ./test", + "lint:fix": "eslint --cache --fix --ext .js,.mjs ./*.js ./*.mjs ./utils ./test", "prepublishOnly": "npm run lint && npm test && npm run test:types", "test": "mocha", - "test:cjs": "cd test/cjs && npm i && node require.js", + "test:cjs": "cd test/cjs && npm i && node named-require.js", "test:esm": "cd test/esm && npm i && node import.mjs", "test:types": "tsc --target es2015 --noEmit --noImplicitAny --noImplicitReturns ./test/types.ts" }, diff --git a/test/cjs/require.js b/test/cjs/named-require.js similarity index 97% rename from test/cjs/require.js rename to test/cjs/named-require.js index 3f47cb20..8e0f8e68 100644 --- a/test/cjs/require.js +++ b/test/cjs/named-require.js @@ -124,7 +124,8 @@ fuzzymultimapadd.add({title: 'Hello', n: 45}); * HashedArrayTree. */ let hashedArrayTree = new HashedArrayTree(Int8Array, 34); -hashedArrayTree.set(3, 4); +hashedArrayTree.push(1); +hashedArrayTree.set(0, 4); /** * Heap. @@ -189,7 +190,7 @@ let lrumwdWasRemoved = lrumwd.delete('one'); let multiset = new MultiSet(); multiset.add(45); multiset = MultiSet.from([1, 2, 3]); -multiset = MultiSet.from({'one': 1}); +multiset = MultiSet.from({one: 1}); /** * MultiMap. @@ -307,7 +308,8 @@ arrayTrieMap.set(['the', 'cat', 'eats', 'the', 'mouse'], 45); * Vector. */ let vector = new Vector(Uint32Array, 10); -vector.set(45, 2); +vector.push(1); +vector.set(0, 2); let uint16vector = Uint16Vector.from([1, 2, 3]); uint16vector.pop(); diff --git a/test/esm/import.mjs b/test/esm/import.mjs index 8be7fb6b..72c46529 100644 --- a/test/esm/import.mjs +++ b/test/esm/import.mjs @@ -124,7 +124,8 @@ fuzzymultimapadd.add({title: 'Hello', n: 45}); * HashedArrayTree. */ let hashedArrayTree = new HashedArrayTree(Int8Array, 34); -hashedArrayTree.set(3, 4); +hashedArrayTree.push(1); +hashedArrayTree.set(0, 4); /** * Heap. @@ -189,7 +190,7 @@ let lrumwdWasRemoved = lrumwd.delete('one'); let multiset = new MultiSet(); multiset.add(45); multiset = MultiSet.from([1, 2, 3]); -multiset = MultiSet.from({'one': 1}); +multiset = MultiSet.from({one: 1}); /** * MultiMap. @@ -307,7 +308,8 @@ arrayTrieMap.set(['the', 'cat', 'eats', 'the', 'mouse'], 45); * Vector. */ let vector = new Vector(Uint32Array, 10); -vector.set(45, 2); +vector.push(1); +vector.set(0, 2); let uint16vector = Uint16Vector.from([1, 2, 3]); uint16vector.pop(); diff --git a/test/types.ts b/test/types.ts index 08a42724..94c80cec 100644 --- a/test/types.ts +++ b/test/types.ts @@ -124,7 +124,8 @@ fuzzymultimapadd.add({title: 'Hello', n: 45}); * HashedArrayTree. */ let hashedArrayTree: HashedArrayTree = new HashedArrayTree(Int8Array, 34); -hashedArrayTree.set(3, 4); +hashedArrayTree.push(1); +hashedArrayTree.set(0, 4); /** * Heap. @@ -308,7 +309,8 @@ arrayTrieMap.set(['the', 'cat', 'eats', 'the', 'mouse'], 45); * Vector. */ let vector = new Vector(Uint32Array, 10); -vector.set(45, 2); +vector.push(1); +vector.set(0, 2); let uint16vector = Uint16Vector.from([1, 2, 3]); uint16vector.pop(); diff --git a/vector.d.ts b/vector.d.ts index 5ed513d7..64c1aa1f 100644 --- a/vector.d.ts +++ b/vector.d.ts @@ -78,4 +78,4 @@ export class Uint16Vector extends TypedVector {} export class Int32Vector extends TypedVector {} export class Uint32Vector extends TypedVector {} export class Float32Vector extends TypedVector {} -export class Float64Array extends TypedVector {} +export class Float64Vector extends TypedVector {}