PHP Classes

File: test/js/test.js

Recommend this page to a friend!
  Packages of Nikos M.   Matchy   test/js/test.js   Download  
File: test/js/test.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Matchy
Perform exact or fuzzy searches in text strings
Author: By
Last change: v.4.0.0, in progress

* fix boyermoore
* option to return errors in match
* update tests
Date: 6 months ago
Size: 1,567 bytes
 

Contents

Class file image Download
"use strict"; const Matchy = require('../../src/js/Matchy.js'); const echo = console.log; function create_string(alphabet, n) { let s = ''; for (let i=0; i<n; ++i) { s += alphabet[Math.round(Math.random()*(alphabet.length-1))]; } return s; } function create_pattern(string, n) { const i = Math.round(Math.random()*(string.length-n)); return string.slice(i, i+n); } function test_case(matchy, algorithm, pattern, string, offset) { offset = offset || 0; const matcher = matchy[algorithm](pattern); const found = matcher(string, offset); const index = string.indexOf(pattern, offset); echo(algorithm+'("'+pattern+'", "'+string+'", '+offset+') = '+found+(found === index ? ' (true)' : ' (expected '+index+')')); } function test() { const matchy = new Matchy(); const algorithms = [ 'fsa', 'rabinkarp', 'knuthmorrispratt', 'twoway', 'boyermoore' ]; for (let i=0; i<10; ++i) { let string = create_string(['a', 'b'/*, 'c', 'd'*/], 10); let pattern = create_pattern(string, 5); echo(); algorithms.forEach(algorithm => test_case(matchy, algorithm, pattern, string)); } /* // problematic echo(); echo('problematic'); ([ ['boyermoore', "bbbbb", "aabaabbbbb"], ['twoway', "babab", "aababababb"], ['twoway', "babab", "baaabababb"], ['boyermoore', "babab", "aababababb"], ['boyermoore', "abaaa", "baabaaabab"] ]).forEach(entry => test_case(matchy, entry[0], entry[1], entry[2])); */ } test();