Esempio n. 1
0
    private static void testMap(int size, bool compressed)
    {
        for (int it = 0; it < 6; it++)
        {
            byte[] mapLayer1 = new byte[size];
            byte[] mapLayer2 = new byte[size];
            byte[] mapLayer3 = new byte[size];
            Random rnd       = new Random(1);
            for (int i = 0; i < size; i++)
            {
                mapLayer1[i] = (byte)(rnd.NextDouble() * 255f);
            }
            rnd = new Random(2);
            for (int i = 0; i < size; i++)
            {
                mapLayer2[i] = (byte)(rnd.NextDouble() * 2f);
            }

            TUtils.Test($"save map {size}x{size}", () =>
            {
                using (var binaryView = new BinaryViewWriter("test.dat"))
                {
                    if (compressed)
                    {
                        binaryView.CompressAll();
                    }
                    binaryView.WriteString("map");
                    binaryView.WriteInt32(size);
                    binaryView.WriteSingle(0.45f);
                    binaryView.WriteArray(mapLayer1);
                    binaryView.WriteArray(mapLayer2);
                    binaryView.WriteArray(mapLayer3);
                }

                TUtils.WriteSucces($"OK {new FileInfo("test.dat").Length}b");
                return(TestResult.Success);
            });
            TUtils.Test($"load map {size}x{size}", () =>
            {
                bool result = true;
                using (var binaryView = new BinaryViewReader("test.dat"))
                {
                    if (compressed)
                    {
                        binaryView.DecompressAll();
                    }
                    result &= binaryView.ReadString() == "map";
                    result &= binaryView.ReadInt32() == size;
                    result &= binaryView.ReadSingle() == 0.45f;
                    result &= TUtils.IsIListEqual(mapLayer1, binaryView.ReadArray <byte>());
                    result &= TUtils.IsIListEqual(mapLayer2, binaryView.ReadArray <byte>());
                    result &= TUtils.IsIListEqual(mapLayer3, binaryView.ReadArray <byte>());
                }
                if (result)
                {
                    TUtils.WriteSucces("OK");
                    return(TestResult.Success);
                }
                else
                {
                    TUtils.WriteFail("FAIL");
                    return(TestResult.Failure);
                }
            });
            size *= 2;
        }
    }
Esempio n. 2
0
    private static void testCompression()
    {
        int size = 8;

        Random rnd = new Random(1);

        byte[] data0 = new byte[size];
        for (int i = 0; i < size; i++)
        {
            data0[i] = (byte)(rnd.NextDouble() * 255f);
        }
        byte[] data1 = new byte[size];
        for (int i = 0; i < size; i++)
        {
            data1[i] = (byte)(rnd.NextDouble() * 255f);
        }
        byte[] data2 = new byte[size];
        for (int i = 0; i < size; i++)
        {
            data2[i] = (byte)(rnd.NextDouble() * 255f);
        }

        TUtils.Test("Compress All", () =>
        {
            var bw = new BinaryViewWriter();
            bw.CompressAll();
            bw.WriteArray(data0);
            bw.Dispose();
            var file = bw.ToArray();

            if (file.Length == 0)
            {
                TUtils.WriteFail($"FAIL file length is 0");
                return(TestResult.Failure);
            }

            var br = new BinaryViewReader(file);
            br.DecompressAll();
            var rdata0 = br.ReadArray <byte>();
            br.Dispose();

            if (!TUtils.IsIListEqual(data0, rdata0))
            {
                TUtils.WriteFail($"FAIL data: {TUtils.IListToString(rdata0)}, expected: {TUtils.IListToString(data0)}");
                return(TestResult.Failure);
            }
            TUtils.WriteSucces($"OK");
            return(TestResult.Success);
        });

        TUtils.Test("Compress Section", () =>
        {
            var bw = new BinaryViewWriter();
            bw.BeginDeflateSection();
            bw.WriteArray(data0);
            bw.EndDeflateSection();
            bw.Dispose();
            var file = bw.ToArray();

            if (file.Length == 0)
            {
                TUtils.WriteFail($"FAIL file length is 0");
                return(TestResult.Failure);
            }

            var br = new BinaryViewReader(file);
            br.BeginDeflateSection();
            var rdata0 = br.ReadArray <byte>();
            br.EndDeflateSection();
            br.Dispose();

            if (!TUtils.IsIListEqual(data0, rdata0))
            {
                TUtils.WriteFail($"FAIL data: {TUtils.IListToString(rdata0)}, expected: {TUtils.IListToString(data0)}");
                return(TestResult.Failure);
            }
            TUtils.WriteSucces($"OK");
            return(TestResult.Success);
        });

        TUtils.Test("Compress 2 Sections", () =>
        {
            var bw = new BinaryViewWriter();
            bw.BeginDeflateSection();
            bw.WriteArray(data0);
            bw.EndDeflateSection();
            bw.WriteArray(data1);
            bw.BeginDeflateSection();
            bw.WriteArray(data2);
            bw.EndDeflateSection();
            bw.Dispose();
            var file = bw.ToArray();

            if (file.Length == 0)
            {
                TUtils.WriteFail($"FAIL file length is 0");
                return(TestResult.Failure);
            }

            var br = new BinaryViewReader(file);
            br.BeginDeflateSection();
            var rdata0 = br.ReadArray <byte>();
            br.EndDeflateSection();
            var rdata1 = br.ReadArray <byte>();
            br.BeginDeflateSection();
            var rdata2 = br.ReadArray <byte>();
            br.EndDeflateSection();
            br.Dispose();

            if (!TUtils.IsIListEqual(data0, rdata0))
            {
                TUtils.WriteFail($"FAIL data0: {TUtils.IListToString(rdata0)}, expected: {TUtils.IListToString(data0)}");
                return(TestResult.Failure);
            }
            if (!TUtils.IsIListEqual(data1, rdata1))
            {
                TUtils.WriteFail($"FAIL data1: {TUtils.IListToString(rdata1)}, expected: {TUtils.IListToString(data1)}");
                return(TestResult.Failure);
            }
            if (!TUtils.IsIListEqual(data2, rdata2))
            {
                TUtils.WriteFail($"FAIL data2: {TUtils.IListToString(rdata2)}, expected: {TUtils.IListToString(data2)}");
                return(TestResult.Failure);
            }
            TUtils.WriteSucces($"OK");
            return(TestResult.Success);
        });
    }