コード例 #1
0
        public static Color Randomize(this Color color)
        {
            var random = new Random();

            color = new Color(random.Byte(), random.Byte(), random.Byte());
            return(color);
        }
コード例 #2
0
        public void DeserializationFailsWithInsufficientData()
        {
            var value  = new ProtocolHeader(Random.Chars(count: 4), Random.Byte(), new ProtocolVersion(Random.Byte(), Random.Byte(), Random.Byte()));
            var result = ProtocolHeader.Deserialize(new Byte[0], out var _, out var _);

            Assert.False(result);
        }
コード例 #3
0
 /// <summary>An endless stream of random bytes</summary>
 public static IEnumerable <byte> Bytes(this Random r)
 {
     for (;;)
     {
         yield return(r.Byte());
     }
 }
コード例 #4
0
        public void SerializedSizeIsEightBytes()
        {
            var value  = new ProtocolHeader(Random.Chars(count: 4), Random.Byte(), new ProtocolVersion(Random.Byte(), Random.Byte(), Random.Byte()));
            var buffer = new ArrayBufferWriter <Byte>(8);

            value.Serialize(buffer);

            Assert.Equal(expected: 8, actual: buffer.WrittenCount);
        }
コード例 #5
0
        public void SerializedSizeIsThreeBytes()
        {
            var value  = new ProtocolVersion(Random.Byte(), Random.Byte(), Random.Byte());
            var buffer = new ArrayBufferWriter <Byte>(3);

            value.Serialize(buffer);

            Assert.Equal(expected: 3, actual: buffer.WrittenCount);
        }
コード例 #6
0
        public void SerializationIsSymmetric()
        {
            var buffer = new ArrayBufferWriter <Byte>(8);
            var value  = new ProtocolHeader(Random.Chars(count: 4), Random.Byte(), new ProtocolVersion(Random.Byte(), Random.Byte(), Random.Byte()));

            value.Serialize(buffer);
            ProtocolHeader.Deserialize(buffer.WrittenMemory.Span, out var deserialized, out var _);

            Assert.Equal(expected: value, actual: deserialized);
        }
コード例 #7
0
        /// <summary>
        /// Thread-safe wrapper for Byte().
        /// </summary>
        public sealed override byte Byte()
        {
            byte b;

            lock (this)
            {
                b = _rand.Byte();
            }

            return(b);
        }
コード例 #8
0
        public void DeserializationFailsWithInvalidFrameType()
        {
            var buffer = new ArrayBufferWriter <Byte>(8);
            var value  = new RawFrame(Random.Enum <FrameType>(), Random.UShort(), Random.Bytes(Random.UShort()));

            value.Serialize(buffer);
            var modifiedBuffer = buffer.WrittenMemory.ToArray();

            modifiedBuffer[0] = Random.Byte(min: 10);

            Assert.Throws <ProtocolErrorException>(() => RawFrame.Deserialize(modifiedBuffer.AsSpan(), out var _, out var _));
        }
コード例 #9
0
        public void DeserializationReturnsSurplusData()
        {
            var value  = new ProtocolHeader(Random.Chars(count: 4), Random.Byte(), new ProtocolVersion(Random.Byte(), Random.Byte(), Random.Byte()));
            var extra  = Random.UInt();
            var buffer = new ArrayBufferWriter <Byte>(12);

            buffer.WriteSerializable(value)
            .WriteUInt32LE(extra);

            ProtocolHeader.Deserialize(buffer.WrittenMemory.Span, out var _, out var surplus);

            Assert.Equal(expected: sizeof(UInt32), actual: surplus.Length);
            Assert.Equal(expected: extra, actual: BitConverter.ToUInt32(surplus));
        }
コード例 #10
0
 ConnectionStart RandomSubject => new ConnectionStart(
     version: (Random.Byte(), Random.Byte()),
コード例 #11
0
        /// <summary>
        /// Draw a random and uniform byte.
        /// </summary>
        public sealed override byte Byte()
        {
            Switch();

            return(Rand.Byte());
        }
コード例 #12
0
 public byte Generate() => _rnd.Byte();