Ejemplo n.º 1
0
 public FST(INPUT_TYPE inputType, Outputs <T> outputs, int bytesPageBits)
 {
     this.inputType     = inputType;
     this.outputs       = outputs;
     this.bytesPageBits = bytesPageBits;
     this.bytes         = new BytesStore(bytesPageBits);
     // pad: ensure no node gets address 0 which is reserved to mean
     // the stop state w/ no arcs
     bytes.writeByte((byte)0);
 }
Ejemplo n.º 2
0
 private void writeLabel(BytesStore bOut, int v)
 {
     Debug.Assert(v >= 0, "v=" + v);
     if (inputType == INPUT_TYPE.BYTE1)
     {
         Debug.Assert(v <= 255, "v=" + v);
         bOut.writeByte((byte)v);
     }
     else if (inputType == INPUT_TYPE.BYTE2)
     {
         Debug.Assert(v <= 65535, "v=" + v);
         bOut.writeShort((short)v);
     }
     else
     {
         bOut.writeVInt(v);
     }
 }
Ejemplo n.º 3
0
        public void test()
        {
            int iters    = r.intBetween(10, 100);
            int maxBytes = 20000;

            for (int iter = 0; iter < iters; iter++)
            {
                int        numBytes  = r.intBetween(1, maxBytes);
                byte[]     expected  = new byte[numBytes];
                int        blockBits = r.intBetween(8, 15);
                BytesStore bytes     = new BytesStore(blockBits);
                log.Information("TEST: iter={iter} numBytes={numBytes}, blockBits={blockBits}", iter, numBytes, blockBits);

                int pos     = 0;
                int subIter = 0;
                while (pos < numBytes)
                {
                    int op = r.intBetween(0, 7);
                    log.Information("> cycle pos={pos} op={op}, subIter={subIter}", pos, op, subIter);
                    switch (op)
                    {
                    case 0:
                    {
                        byte b = (byte)r.intBetween(0, 255);
                        log.Information(">> writeByte b={b}", b);
                        expected[pos++] = b;
                        bytes.writeByte(b);
                        break;
                    }

                    case 1:
                    {
                        int    len  = r.r().Next(Math.Min(numBytes - pos, 100));
                        byte[] temp = new byte[len];
                        r.bytes(temp);
                        log.Information(">> writeBytes len={len} bytes={bytes}", len, bytes);
                        Array.Copy(temp, 0, expected, pos, temp.Length);
                        bytes.writeBytes(temp, 0, temp.Length);
                        pos += len;
                        break;
                    }

                    case 2:
                    {
                        if (pos > 4)
                        {
                            int x         = r.r().Next();
                            int randomPos = r.r().Next(pos - 4);
                            log.Information(">> abs writeInt pos={pos} x={x}", randomPos, x);
                            bytes.writeInt(randomPos, x);
                            expected[randomPos++] = (byte)(x >> 24);
                            expected[randomPos++] = (byte)(x >> 16);
                            expected[randomPos++] = (byte)(x >> 8);
                            expected[randomPos++] = (byte)x;
                        }
                        break;
                    }

                    case 3:
                    {
                        if (pos > 1)
                        {
                            int len = r.intBetween(2, Math.Min(100, pos));
                            int start;
                            if (len == pos)
                            {
                                start = 0;
                            }
                            else
                            {
                                start = r.r().Next(pos - len);
                            }
                            int end = start + len - 1;
                            log.Information(">> reverse start={start} end={end} len={len}, pos={pos}", start, end, len, pos);
                            bytes.reverse(start, end);

                            while (start <= end)
                            {
                                byte b = expected[end];
                                expected[end]   = expected[start];
                                expected[start] = b;
                                start++;
                                end--;
                            }
                        }
                        break;
                    }

                    case 4:
                    {
                        if (pos > 2)
                        {
                            int    randomPos = r.r().Next(pos - 1);
                            int    len       = r.intBetween(1, Math.Min(pos - randomPos - 1, 100));
                            byte[] temp      = new byte[len];
                            r.r().NextBytes(temp);
                            log.Information(">> abs writeBytes pos={randomPos} len={len} bytes={bytes}", randomPos, len, temp);
                            Array.Copy(temp, 0, expected, randomPos, temp.Length);
                            bytes.writeBytes(randomPos, temp, 0, temp.Length);
                        }
                        break;
                    }

                    case 5:
                    {
                        if (pos > 1)
                        {
                            int src  = r.r().Next(pos - 1);
                            int dest = r.intBetween(src + 1, pos - 1);
                            int len  = r.intBetween(1, Math.Min(300, pos - dest));
                            log.Information(">> copyBytes src={src} dest={dest} len={len}", src, dest, len);
                            Array.Copy(expected, src, expected, dest, len);
                            bytes.copyBytes(src, dest, len);
                        }
                        break;
                    }

                    case 6:
                    {
                        int len = r.r().Next(Math.Min(100, numBytes - pos));
                        log.Information(">> skip len={len}", len);
                        pos += len;
                        bytes.skipBytes(len);
                        if (len > 0)
                        {
                            byte[] zeros = new byte[len];
                            bytes.writeBytes(pos - len, zeros, 0, len);
                        }
                        break;
                    }

                    case 7:
                    {
                        if (pos > 0)
                        {
                            int  dest = r.r().Next(pos);
                            byte b    = (byte)r.r().Next(256);
                            log.Information(">> abs writeByte dest={len} byte={b}", dest, b);
                            expected[dest] = b;
                            bytes.writeByte(dest, b);
                        }
                        break;
                    }
                    }

                    Assert.Equal(pos, bytes.getPosition());

                    if (pos > 0 && r.r().Next(50) == 17)
                    {
                        int len = r.intBetween(1, Math.Min(pos, 100));
                        log.Debug(">> truncate to size: {newSize}", (pos - len));
                        bytes.truncate(pos - len);
                        pos -= len;
                        Array.Clear(expected, pos, len);
                    }
                    if (pos > 0 && r.r().Next(200) == 17)
                    {
                        verify(bytes, expected, pos);
                    }
                    subIter++;
                }

                BytesStore bytesToVerify;
                if (r.boolean())
                {
                    //TODO: save to disk, then reload
                    bytesToVerify = bytes;
                }
                else
                {
                    bytesToVerify = bytes;
                }

                verify(bytesToVerify, expected, numBytes);
            }
        }