I have checked Check if an array is subset of another array and related questions but they aren't quite the same. They wanted to see if all elements of a sub array are within some other array (independent of order), which you can tell by the answers is fairly straightforward. I am not sure if now the following question is as straightforward.
I am trying to write JavaScript compiler testing for a compiler written in JS (it doesn't compile JS, in my situation now it just has to do with testing the final memory layout is what is expected). So I am trying to write a Jest matcher. The matcher part is easy but the algorithm for matching the grid of elements doesn't seem easy and is making my head spin.
Basically, this is sort of like an image matching problem. How do you find if a square/rectangle of pixels is exactly within another image? And do so somewhat efficiently?
In the same way, I would like to find is a sequence of bytes in a "test" array is within a larger array, in the exact order as defined.
So for example:
var given = [
1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
21, 23, 25, 27, 29, 31, 33, 35, 37,
39, 41, 43, 45, 47, ...
]
var expectedSubset = [
13, 15, 17, 19, 21, 23, 25, 27
]
expect(given).toContainSequence(expectedSubset)
Any ideas how to do this? Maybe it's simple, but it seems complex.
Where it seems to get tricky is here:
var given = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 5, 8, 0, 0, 0, 0, 0, 0,
0, 5, 8, 11, 14, 0, 0, 0, 0, 0, 0,
...
]
var expected = [ 0, 0, 0, 0, 5, 8, 11 ]