import java.util.*;
import java.lang.foreign.*;

public class UnexpectedBehavior {
    public static void main(String[] args) throws Exception {
        byte[] exampleData = new byte[]{1, 2, 3, 4,
                -1, -1, -1, -1,
                1, 2, 8, 8,
                -1, -1, -1, -1};
        // In the real world, this is a large memory mapped file
        MemorySegment memory = MemorySegment.ofArray(exampleData);
        // In the real app these are of course dynamic
        int searchTermOffset = 0;
        int len = 4;

        int checkOffset = 8;

        long missMatch = MemorySegment.mismatch(memory, searchTermOffset, searchTermOffset+len, memory, checkOffset, checkOffset+len);
        int debug = Arrays.mismatch(exampleData,searchTermOffset, searchTermOffset+len,exampleData, checkOffset, checkOffset+len);
        if(missMatch == -1){
            System.out.println("Unexpected match!!!");
            System.out.println("Array.mismatch gives: " + debug);
            throw new Exception("What, should not match?");
        } else{
            System.out.println("Missmatch, as expected");
        }
    }
}
