예제 #1
0
파일: Tests.cs 프로젝트: Grille/BinaryView
    private static void testString(string str, LengthPrefix lengthPrefix = LengthPrefix.Default, CharSizePrefix charSizePrefix = CharSizePrefix.Default)
    {
        TUtils.Test($"read/write string[{charSizePrefix}].length:{lengthPrefix} ({str})", () =>
        {
            bw.Position = 0;
            bw.WriteString(str, lengthPrefix, charSizePrefix);
            bw.Position   = 0;
            string result = br.ReadString(lengthPrefix, charSizePrefix);
            if (result.Equals(str))
            {
                TUtils.WriteSucces("OK");
                return(TestResult.Success);
            }

            else
            {
                TUtils.WriteFail($"FAIL \"{result}\"");
                return(TestResult.Failure);
            }
        });
    }
예제 #2
0
파일: Tests.cs 프로젝트: Grille/BinaryView
    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;
        }
    }