|
| 1 | +import test from 'ava'; |
| 2 | + |
| 3 | +import {table} from './_fixtures.js'; |
| 4 | + |
| 5 | +const isNext = (t, table, input) => { |
| 6 | + if (input === '') { |
| 7 | + t.deepEqual(table, [-1]); |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + // TODO this test is not complete |
| 12 | + |
| 13 | + t.is(table[0], -1); |
| 14 | + t.not(table[input.length], -1); |
| 15 | + for (let j = 1; j <= input.length; ++j) { |
| 16 | + const i = table[j]; |
| 17 | + // TODO test when i === -1 |
| 18 | + if (i !== -1) { |
| 19 | + // Prefix and suffix match |
| 20 | + t.true(input.slice(0, i) === input.slice(j - i, j)); |
| 21 | + // Next character does not match |
| 22 | + t.true(j === input.length || input[i] !== input[j]); |
| 23 | + // A longer proper prefix/suffix pair does not match |
| 24 | + // TODO More possibilities need to be checked |
| 25 | + t.true( |
| 26 | + i + 1 === j || |
| 27 | + input.slice(0, i + 1) !== input.slice(j - (i + 1), j) || |
| 28 | + i + 2 === j || |
| 29 | + j === input.length || |
| 30 | + input[i + 1] === input[j], |
| 31 | + ); |
| 32 | + } |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +const macro = (t, input, expected) => { |
| 37 | + const next = table(input); |
| 38 | + t.deepEqual(next, expected); |
| 39 | + isNext(t, next, input); |
| 40 | +}; |
| 41 | + |
| 42 | +macro.title = (title, input, expected) => |
| 43 | + title ?? `next(${input}) is ${JSON.stringify(expected)}`; |
| 44 | + |
| 45 | +const auto = (t, input) => { |
| 46 | + const next = table(input); |
| 47 | + isNext(t, next, input); |
| 48 | +}; |
| 49 | + |
| 50 | +auto.title = (title, input) => title ?? `isNext(next(${input}))`; |
| 51 | + |
| 52 | +test(macro, '', [-1]); |
| 53 | +test(macro, 'z', [-1, 0]); |
| 54 | +test(macro, 'abcd', [-1, 0, 0, 0, 0]); |
| 55 | +test(macro, 'aaaa', [-1, -1, -1, -1, 3]); |
| 56 | +test(macro, 'axax', [-1, 0, -1, 0, 2]); |
| 57 | +test(macro, 'axxa', [-1, 0, 0, -1, 1]); |
| 58 | +test(macro, 'aaaab', [-1, -1, -1, -1, 3, 0]); |
| 59 | +test(macro, 'abracadabra', [-1, 0, 0, -1, 1, -1, 1, -1, 0, 0, -1, 4]); |
| 60 | +test( |
| 61 | + macro, |
| 62 | + 'abaababaabaababaababa', |
| 63 | + [-1, 0, -1, 1, 0, -1, 3, -1, 1, 0, -1, 6, 0, -1, 3, -1, 1, 0, -1, 11, -1, 8], |
| 64 | +); |
| 65 | + |
| 66 | +test(auto, 'eifoiwhfeldkasjflkdshfldshflkkdadkkkkkkkkkkkkasjfdljfdleifo'); |
| 67 | +test(auto, 'aaaaaaaaaaaabbbbbbbbbbaaaaaaaaabbbbbbbb'); |
0 commit comments