public void EqualsOther()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AA");
            var v2 = ReadOnlyFixedBytes.Parse("AA");

            Assert.True(v1.Equals(v2));
        }
        public void InequalityOperatorFalse()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AA");
            var v2 = ReadOnlyFixedBytes.Parse("AA");

            Assert.False(v1 != v2);
        }
        public void CompareToGreaterThan()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AB");
            var v2 = ReadOnlyFixedBytes.Parse("AA");

            Assert.Equal(1, v1.CompareTo(v2));
        }
        public void CompareToEqual()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AA");
            var v2 = ReadOnlyFixedBytes.Parse("AA");

            Assert.Equal(0, v1.CompareTo(v2));
        }
        public void EqualityOperatorTrue()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AA");
            var v2 = ReadOnlyFixedBytes.Parse("AA");

            Assert.True(v1 == v2);
        }
        public void GetHashCodeNotEqual()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AA");
            var v2 = ReadOnlyFixedBytes.Parse("AB");

            Assert.NotEqual(v1.GetHashCode(), v2.GetHashCode());
        }
        public void EqualsObject()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AA");
            var v2 = ReadOnlyFixedBytes.Parse("AA");

            Assert.True(v1.Equals((object)v2));
        }
        public void ConstructFromStringThrowsOnInvalid(string value)
        {
            Action            action = () => Assert.Null(ReadOnlyFixedBytes.Parse(value));
            ArgumentException e      = Assert.Throws <ArgumentException>(action);

            Assert.Contains("is not a recognized hex string", e.Message, StringComparison.OrdinalIgnoreCase);
        }
Beispiel #9
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Fingerprint"/> struct.
        /// </summary>
        public static Fingerprint Deserialize(BinaryReader reader)
        {
            Contract.Requires(reader != null);
            var length = reader.ReadByte();
            var buffer = ReadOnlyFixedBytes.ReadFrom(reader, length);

            return(new Fingerprint(buffer, length));
        }
        public void DefaultConstructorCreatesAllZeros()
        {
#pragma warning disable SA1129 // Do not use default value type constructor
            var v = new ReadOnlyFixedBytes();
#pragma warning restore SA1129 // Do not use default value type constructor
            var bytes = v.ToByteArray();
            Assert.True(bytes.All(b => b == 0));
        }
        public void TryParseSuccess(string value)
        {
            ReadOnlyFixedBytes v;
            int length;

            Assert.True(ReadOnlyFixedBytes.TryParse(value, out v, out length), value);
            Assert.Equal(value.Length / 2, length);
        }
        public void TryParseFail(string value)
        {
            ReadOnlyFixedBytes v;
            int length;

            Assert.False(ReadOnlyFixedBytes.TryParse(value, out v, out length));
            Assert.Equal(0, length);
        }
Beispiel #13
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Fingerprint"/> struct.
        /// </summary>
        public Fingerprint(int length, BinaryReader reader)
        {
            unchecked {
                _length = (byte)length;
            }

            _bytes = ReadOnlyFixedBytes.ReadFrom(reader, length);
        }
        public void StringRoundtrip()
        {
            var v1         = ReadOnlyFixedBytes.Random();
            var serialized = v1.Serialize();
            var v2         = ReadOnlyFixedBytes.Parse(serialized);

            Assert.Equal(v1, v2);
        }
Beispiel #15
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Fingerprint"/> struct.
        /// </summary>
        public Fingerprint(ReadOnlyFixedBytes fixedBytes, int length)
        {
            _bytes = fixedBytes;

            unchecked {
                _length = (byte)length;
            }
        }
Beispiel #16
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Fingerprint"/> struct.
        /// </summary>
        public Fingerprint(FixedBytes fixedBytes, int length)
        {
            _bytes = ReadOnlyFixedBytes.FromFixedBytes(ref fixedBytes);

            unchecked {
                _length = (byte)length;
            }
        }
        public void BufferRoundtrip()
        {
            var buffer = new byte[ReadOnlyFixedBytes.MaxLength];
            var v1     = ReadOnlyFixedBytes.Random();

            v1.Serialize(buffer);
            var v2 = new ReadOnlyFixedBytes(buffer);

            Assert.Equal(v1, v2);
        }
        public void InequalityOperatorTrue()
        {
            var v1 = ReadOnlyFixedBytes.Parse("AA");
            var v2 = ReadOnlyFixedBytes.Parse("AB");

            Assert.True(v1 != v2);
            Assert.True(v1 < v2);
            Assert.False(v1 > v2);
            Assert.False(v2 < v1);
            Assert.True(v2 > v1);
        }
        public void BufferPositiveOffsetRoundtrip()
        {
            const int offset = 3;
            var       buffer = new byte[ReadOnlyFixedBytes.MaxLength + offset];
            var       v1     = ReadOnlyFixedBytes.Random();

            v1.Serialize(buffer, ReadOnlyFixedBytes.MaxLength, offset);
            var v2 = new ReadOnlyFixedBytes(buffer, ReadOnlyFixedBytes.MaxLength, offset);

            Assert.Equal(v1, v2);
        }
        public void IndexerGet()
        {
            var bytes      = Enumerable.Range(0, ReadOnlyFixedBytes.MaxLength).Select(i => (byte)i).ToArray();
            var fixedBytes = new ReadOnlyFixedBytes(bytes);

            foreach (var i in Enumerable.Range(0, ReadOnlyFixedBytes.MaxLength))
            {
                var b = fixedBytes[i];
                Assert.Equal((byte)i, b);
            }
        }
 public void FixedBytesShouldNotFailOnEmptyStream()
 {
     // The following code should not fail.
     using (var ms = new MemoryStream(new byte[0]))
     {
         using (var reader = new BinaryReader(ms))
         {
             var fb = ReadOnlyFixedBytes.ReadFrom(reader);
             Assert.Equal(fb[0], 0);
         }
     }
 }
Beispiel #22
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="Fingerprint"/> struct.
        /// </summary>
        public Fingerprint(byte[] buffer, int length = MaxLength, int offset = 0)
        {
            Contract.Requires(buffer != null);

            var len = Math.Min(buffer.Length, length);

            _bytes = new ReadOnlyFixedBytes(buffer, len, offset);

            unchecked {
                _length = (byte)len;
            }
        }
        public void ToHexTest()
        {
            var bytes      = Enumerable.Range(0, ReadOnlyFixedBytes.MaxLength).Select(i => (byte)i).ToArray();
            var fixedBytes = new ReadOnlyFixedBytes(bytes);

            var string1 = fixedBytes.ToHex();

            var sb = new StringBuilder();

            fixedBytes.ToHex(sb, 0, fixedBytes.Length);
            var string2 = sb.ToString();

            Assert.Equal(string1, string2);
        }
Beispiel #24
0
        /// <summary>
        ///     Try to parse the serialized form.
        /// </summary>
        public static bool TryParse(string serialized, out Fingerprint value)
        {
            if (!ReadOnlyFixedBytes.TryParse(serialized, out var v, out var length))
            {
                value = default(Fingerprint);
                return(false);
            }

            if (length == 0 || length > MaxLength)
            {
                value = default(Fingerprint);
                return(false);
            }

            value = new Fingerprint(v, length);
            return(true);
        }
        public void BinaryRoundtrip()
        {
            using (var ms = new MemoryStream())
            {
                using (var writer = new BinaryWriter(ms))
                {
                    var v1 = ReadOnlyFixedBytes.Random();
                    v1.Serialize(writer);
                    ms.Position = 0;

                    using (var reader = new BinaryReader(ms))
                    {
                        var v2 = ReadOnlyFixedBytes.ReadFrom(reader);
                        Assert.Equal(v1, v2);
                    }
                }
            }
        }
        public void RoundtripShortHashBinary(HashType hashType)
        {
            using (var ms = new MemoryStream())
            {
                using (var writer = new BinaryWriter(ms))
                {
                    var h1         = ContentHash.Random(hashType);
                    var shortHash1 = new ShortHash(h1);
                    shortHash1.Serialize(writer);
                    Assert.Equal(ShortHash.SerializedLength, ms.Length);
                    ms.Position = 0;

                    using (var reader = new BinaryReader(ms))
                    {
                        var shortHash2 = new ShortHash(ReadOnlyFixedBytes.ReadFrom(reader, ShortHash.SerializedLength));
                        Assert.Equal(hashType, shortHash2.HashType);
                        Assert.Equal(shortHash1.ToString(), shortHash2.ToString());
                    }
                }
            }
        }
        public void ConstructFromStringZeroPads(string value)
        {
            var v = ReadOnlyFixedBytes.Parse(value);

            Assert.Equal(ReadOnlyFixedBytes.MaxHexLength - 2, v.ToHex().LastIndexOf("00", StringComparison.Ordinal));
        }
        public void ToStringIsToHex()
        {
            var v = ReadOnlyFixedBytes.Parse("ABCD");

            Assert.Equal(v.ToHex(), v.ToString());
        }
 public void ToHexIsUppercase()
 {
     Assert.Equal("BB00", ReadOnlyFixedBytes.Parse("bb").ToHex(2));
 }
 public void SerializeToString()
 {
     Assert.Equal("770000000000000000000000000000000000000000000000000000000000000000", ReadOnlyFixedBytes.Parse("77").Serialize());
 }