|
| 1 | +import test from 'ava'; |
| 2 | + |
| 3 | +import {lpsa} from './_fixtures.js'; |
| 4 | + |
| 5 | +const isLongestPrefixSuffixArray = (t, table, input) => { |
| 6 | + t.is(table[0], -1); |
| 7 | + for (let i = 1; i <= input.length; ++i) { |
| 8 | + t.not(table[i], -1); |
| 9 | + // Prefix and suffix match |
| 10 | + t.is(input.slice(0, table[i]), input.slice(i - table[i], i)); |
| 11 | + if (table[i] + 1 < i) { |
| 12 | + // A longer prefix/suffix pair does not match |
| 13 | + t.not(input.slice(0, table[i] + 1), input.slice(i - (table[i] + 1), i)); |
| 14 | + } |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +const macro = (t, input, expected) => { |
| 19 | + const table = lpsa(input); |
| 20 | + t.deepEqual(table, expected); |
| 21 | + isLongestPrefixSuffixArray(t, table, input); |
| 22 | +}; |
| 23 | + |
| 24 | +macro.title = (title, input, expected) => |
| 25 | + title ?? `lpsa(${input}) is ${JSON.stringify(expected)}`; |
| 26 | + |
| 27 | +const auto = (t, input) => { |
| 28 | + const table = lpsa(input); |
| 29 | + isLongestPrefixSuffixArray(t, table, input); |
| 30 | +}; |
| 31 | + |
| 32 | +auto.title = (title, input) => |
| 33 | + title ?? `isLongestPrefixSuffixArray(lpsa(${input}))`; |
| 34 | + |
| 35 | +test(macro, '', [-1]); |
| 36 | +test(macro, 'abcd', [-1, 0, 0, 0, 0]); |
| 37 | +test(macro, 'aaaa', [-1, 0, 1, 2, 3]); |
| 38 | +test(macro, 'axax', [-1, 0, 0, 1, 2]); |
| 39 | +test(macro, 'axxa', [-1, 0, 0, 0, 1]); |
| 40 | +test(macro, 'aaaab', [-1, 0, 1, 2, 3, 0]); |
| 41 | +test(macro, 'abcabcacab', [-1, 0, 0, 0, 1, 2, 3, 4, 0, 1, 2]); |
| 42 | +test(macro, 'abracadabra', [-1, 0, 0, 0, 1, 0, 1, 0, 1, 2, 3, 4]); |
| 43 | +test( |
| 44 | + macro, |
| 45 | + 'abaababaabaababaababa', |
| 46 | + [-1, 0, 0, 1, 1, 2, 3, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9, 10, 11, 7, 8], |
| 47 | +); |
| 48 | + |
| 49 | +test(auto, 'eifoiwhfeldkasjflkdshfldshflkkdadkkkkkkkkkkkkasjfdljfdleifo'); |
| 50 | +test(auto, 'aaaaaaaaaaaabbbbbbbbbbaaaaaaaaabbbbbbbb'); |
0 commit comments