public void CalculateCrcForSingleCommand()
        {
            var data = new byte[] { 0x09 };
            var crc  = CheckSumCalculator.Calculate(data);

            Assert.Equal(0xD7, crc.High);
            Assert.Equal(0xEB, crc.Low);
        }
        public void CalculateCrcForLongCommand()
        {
            var data = new byte[] { 0x1C, 0xD7, 0x09 };
            var crc  = CheckSumCalculator.Calculate(data);

            Assert.Equal(0x5E, crc.High);
            Assert.Equal(0x13, crc.Low);
        }
        public void Return_The_CheckSum_Of_The_Puzzle_Entered()
        {
            List <string> rowsList = new List <string> {
                "5 1 9 5", "7 5 3", "2 4 6 8"
            };
            CheckSumCalculator checkSumCalculator = new CheckSumCalculator(rowsList);

            Check.That(checkSumCalculator.Calculate()).IsEqualTo(18);
        }
Exemplo n.º 4
0
        public void CalculateCheckSum_Big()
        {
            var expectedResult = 6888;
            var day2           = new CheckSumCalculator();

            var result = day2.CalculateChecksum(Day2Consts.Input1Path);

            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 5
0
        public void CalculateAppearence_LiveInputs(string input, bool expectedTwos, bool expcetedThrees)
        {
            var day2 = new CheckSumCalculator();

            var result = day2.CalculateAppearence(input);

            Assert.Equal(expectedTwos, result.ContainsTwo);
            Assert.Equal(expcetedThrees, result.ContainsThree);
        }
Exemplo n.º 6
0
        public IValidationResult Validate(Entity entity)
        {
            var weights = new[] { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };

            var calculator = new CheckSumCalculator();
            var checkSum   = calculator.Calculate(weights, entity.Pesel.Substring(0, 10));

            if (checkSum == (int)char.GetNumericValue(entity.Pesel[10]))
            {
                entity.PeselStruct.ControlNumber = Convert.ToByte(checkSum);
                return(new OkValidationResult());
            }

            return(new InvalidCheckSumValidationResult());
        }
Exemplo n.º 7
0
        public ArraySegment <byte> Read()
        {
            SkipBeginMarker();
            var content = ReadContent().ToArray();
            var crc     = CheckSumCalculator.Calculate(content, 0, content.Length - 2);

            if (crc.High != content[content.Length - 2] || crc.Low != content[content.Length - 1])
            {
                throw new InvalidDataException("CRC verification failed");
            }

            var result = new ArraySegment <byte>(content, 0, content.Length - 2);

            return(result);
        }
Exemplo n.º 8
0
        private async Task Write(byte[] packet, CancellationToken token)
        {
            stream.WriteByte(frameSynchronizationMarker);
            stream.WriteByte(frameSynchronizationMarker);
            foreach (var b in packet)
            {
                stream.WriteByte(b);
                if (b == frameSynchronizationMarker)
                {
                    stream.WriteByte(0xF0);
                }
            }
            var crc = CheckSumCalculator.Calculate(packet);

            stream.WriteByte(crc.High);
            stream.WriteByte(crc.Low);
            stream.WriteByte(frameSynchronizationMarker);
            stream.WriteByte(frameCompletedMarker);
            await stream.FlushAsync(token);
        }