Пример #1
0
        public static IEnumerable <object[]> TrySumBigEndian_Test_Values()
        {
            var deltas = new[] { long.MinValue, -255, -129, -127, -1, 0, 1, 127, 129, 255, long.MaxValue };
            var uints  = new uint[] { uint.MinValue, 1, 127, 129, 255, uint.MaxValue };

            foreach (var delta in deltas)
            {
                foreach (var input in uints)
                {
                    var inputBytes = BitConverter.IsLittleEndian
                                         ? BitConverter.GetBytes(input)
                                     .ReverseBytes()
                                         : BitConverter.GetBytes(input);

                    var deltaBytes = BitConverter.IsLittleEndian
                                         ? BitConverter.GetBytes(delta)
                                     .ReverseBytes()
                                         : BitConverter.GetBytes(delta);

                    if (delta < 0 &&
                        input - delta < 0)
                    {
                        yield return(new object[] { false, Array.Empty <byte>(), inputBytes, delta });

                        yield break;
                    }

                    var expectedBytes = delta < 0
                                            ? ByteArrayUtils.SubtractUnsignedBigEndian(inputBytes, deltaBytes)
                                            : ByteArrayUtils.AddUnsignedBigEndian(inputBytes, deltaBytes);

                    yield return(new object[] { true, expectedBytes, inputBytes, delta });
                }
            }
        }
Пример #2
0
        public static IEnumerable <object[]> Addition_Test_Values()
        {
            foreach (var(left, right) in Values())
            {
                var expected = new FixedBytes(ByteArrayUtils.AddUnsignedBigEndian(left, right));
                yield return(new object[] { expected, new FixedBytes(left), new FixedBytes(right) });
            }

            yield return(new object[] { null, null, new FixedBytes(new byte[] { 0xFF }) });

            yield return(new object[] { null, new FixedBytes(new byte[] { 0xFF }), null });

            yield return(new object[] { null, null, null });

            IEnumerable <(byte[] left, byte[] right)> Values()
            {
                yield return(new byte[] { 0xFF }, Array.Empty <byte>());

                yield return(new byte[] { 0x00 }, new byte[] { 0x00 });

                yield return(new byte[] { 0xF0 }, new byte[] { 0x0F });

                yield return(new byte[] { 0x42 }, new byte[] { 0xBD });

                yield return(new byte[] { 0xFF }, new byte[] { 0xBD });
            }
        }
Пример #3
0
 public void AddUnsignedBigEndian_NullInput_ThrowsArgumentNullException_Test(byte[] left,
                                                                             byte[] right)
 {
     // Arrange
     // Act
     // Assert
     Assert.Throws <ArgumentNullException>(() => ByteArrayUtils.AddUnsignedBigEndian(left, right));
 }
Пример #4
0
        public void AddUnsignedBigEndian_Test(byte[] expected,
                                              byte[] left,
                                              byte[] right)
        {
            // Arrange
            var leftOriginal  = left.ToArray();
            var rightOriginal = right.ToArray();

            // Act
            var result = ByteArrayUtils.AddUnsignedBigEndian(left, right);

            // Assert
            Assert.Equal(expected, result);

            Assert.Equal(leftOriginal, left);   // input value not affected
            Assert.Equal(rightOriginal, right); // input value not affected
        }
Пример #5
0
        public static void AddUnsignedBigEndianExample()
        {
            // Setup
            var lhs = new byte[] { 0xAD, 0xDE, 0xD0 };
            var rhs = new byte[] { 0xC0, 0xDE };

            // Act
            var result = ByteArrayUtils.AddUnsignedBigEndian(lhs, rhs);

            // Conclusion
            Console.WriteLine("AddUnsignedBigEndian Example");
            Console.WriteLine($"lhs:\t{lhs.ToString("H")}");
            Console.WriteLine($"rhs:\t{rhs.ToString("H")}");
            Console.WriteLine($"result:\t{result.ToString("H")}");
            Console.WriteLine(string.Empty);

            Console.WriteLine($"lhs:\t{lhs.ToString("IBE")}");
            Console.WriteLine($"rhs:\t{rhs.ToString("IBE")}");
            Console.WriteLine($"result:\t{result.ToString("IBE")}");
            Console.WriteLine(string.Empty);
        }