Пример #1
0
        public void CompareFastCrcWithSlowCrc()
        {
            long slowParserTime = TimeIt(5, @"TestData\large_file.fit", (stream) =>
            {
                int crc = 0;
                using (var reader = new BinaryReader(stream))
                {
                    for (int i = 0; i < stream.Length - 2; i++)
                    {
                        crc = CRC.Get16(crc, reader.ReadByte());
                    }
                    ushort fileCrc = reader.ReadUInt16();
                    Assert.AreEqual(fileCrc, crc);
                }
            });

            Console.WriteLine("CRC via Dynastream CRC16 implementation = {0}ms", slowParserTime);

            long fastParserTime = TimeIt(5, @"TestData\large_file.fit", (stream) =>
            {
                using (var reader = new BinaryReader(stream))
                {
                    ushort crc     = Crc16.ComputeCrc(reader, stream.Length - 2);
                    ushort fileCrc = reader.ReadUInt16();
                    Assert.AreEqual(fileCrc, crc);
                }
            });

            Console.WriteLine("CRC via 64 bit 8x256 CRC16 implementation = {0}ms", fastParserTime);
            Console.WriteLine("64 bit 8x256 CRC16 implementation is {0:0.0}x faster", (double)((double)slowParserTime / (double)fastParserTime));
        }
Пример #2
0
        public void TestSimpleStrings()
        {
            Assert.AreEqual(CrcTestData.SimpleStrings.Length, CrcTestData.SimpleStringsCrcs.Length);

            for (int i = 0; i < CrcTestData.SimpleStrings.Length; i++)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(CrcTestData.SimpleStrings[i]);
                using (var stream = new MemoryStream(bytes))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        ushort crc = Crc16.ComputeCrc(reader, bytes.Length);
                        Assert.AreEqual(CrcTestData.SimpleStringsCrcs[i], crc, "CRC16 for {0}", CrcTestData.SimpleStrings[i]);
                    }
                }
            }
        }