public static Color Randomize(this Color color) { var random = new Random(); color = new Color(random.Byte(), random.Byte(), random.Byte()); return(color); }
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); }
/// <summary>An endless stream of random bytes</summary> public static IEnumerable <byte> Bytes(this Random r) { for (;;) { yield return(r.Byte()); } }
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); }
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); }
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); }
/// <summary> /// Thread-safe wrapper for Byte(). /// </summary> public sealed override byte Byte() { byte b; lock (this) { b = _rand.Byte(); } return(b); }
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 _)); }
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)); }
ConnectionStart RandomSubject => new ConnectionStart( version: (Random.Byte(), Random.Byte()),
/// <summary> /// Draw a random and uniform byte. /// </summary> public sealed override byte Byte() { Switch(); return(Rand.Byte()); }
public byte Generate() => _rnd.Byte();