protected TypeElement(IDiscriminatedElement parent, ScopeCategories scope, bool isAbstract, bool isSealed, bool isUnsafe, bool isPartial, bool isStatic) : base(parent) { if (!FastEnum.IsDefined(scope)) { throw new ArgumentOutOfRangeException(nameof(scope)); } if (isAbstract && (isStatic || isSealed)) { throw new ArgumentException( $"If {nameof(isAbstract)} is true then {nameof(isStatic)} or {nameof(isSealed)} can't true."); } if (isStatic && isSealed) { throw new ArgumentException($"{nameof(isStatic)} and {nameof(isSealed)} status are conflicted."); } IsAbstract = isAbstract; IsSealed = isSealed; Scope = scope; IsUnsafe = isUnsafe; IsPartial = isPartial; IsStatic = isStatic; }
public void IsDefined() { FastEnum.IsDefined <TEnum>(TEnum.MinValue).Should().BeTrue(); FastEnum.IsDefined <TEnum>(TEnum.Zero).Should().BeTrue(); FastEnum.IsDefined <TEnum>(TEnum.MaxValue).Should().BeTrue(); FastEnum.IsDefined <TEnum>((TEnum)123).Should().BeFalse(); TEnum.MinValue.IsDefined().Should().BeTrue(); TEnum.Zero.IsDefined().Should().BeTrue(); TEnum.MaxValue.IsDefined().Should().BeTrue(); FastEnum.IsDefined <TEnum>(nameof(TEnum.MinValue)).Should().BeTrue(); FastEnum.IsDefined <TEnum>(nameof(TEnum.Zero)).Should().BeTrue(); FastEnum.IsDefined <TEnum>(nameof(TEnum.MaxValue)).Should().BeTrue(); FastEnum.IsDefined <TEnum>("123").Should().BeFalse(); FastEnum.IsDefined <TEnum>("minvalue").Should().BeFalse(); FastEnum.IsDefined <TEnum>(TUnderlying.MinValue).Should().BeTrue(); FastEnum.IsDefined <TEnum>(TUnderlying.MaxValue).Should().BeTrue(); FastEnum.IsDefined <TEnum>((TUnderlying)123).Should().BeFalse(); FluentActions .Invoking(() => FastEnum.IsDefined <TEnum>((sbyte)123)) .Should() .Throw <ArgumentException>(); }
/// <summary> /// Asserts that this enumeration instance is a valid <typeparamref name="T"/> value.. /// </summary> /// <typeparam name="T">The type of the enumeration.</typeparam> /// <param name="assert">This <see cref="Asserter{T}"/>.</param> /// <returns>This <paramref name="assert"/>.</returns> public static Asserter <T> Valid <T>(this Asserter <T> assert) where T : struct, Enum { if (!FastEnum.IsDefined(assert.Actual)) { throw new AssertException($"The instance was not valid.\nActual: {assert.Actual}\nExpected one of: {FastEnum.GetNames<T>().Aggregate((total, next) => total + ", " + next)}"); } return(assert); }
public void IsDefined() { FastEnum.IsDefined((TEnum)123).Should().BeFalse(); FastEnum.IsDefined <TEnum>("123").Should().BeFalse(); FluentActions .Invoking(() => FastEnum.IsDefined <TEnum>((sbyte)123)) .Should() .Throw <ArgumentException>(); }
public TotalDiffStrategy(IPeerAllocationStrategy strategy, TotalDiffSelectionType selectionType = TotalDiffSelectionType.Better) { if (!FastEnum.IsDefined(selectionType)) { throw new InvalidEnumArgumentException(nameof(selectionType), (int)selectionType, typeof(TotalDiffSelectionType)); } _strategy = strategy ?? throw new ArgumentNullException(nameof(strategy)); _selectionType = selectionType; }
public static void Valid <E>(E value, String name) where E : struct, Enum { #if NETSTANDARD1_0 if (!Enum.IsDefined(typeof(E), value)) { #else if (!FastEnum.IsDefined(value)) { #endif throw new ArgumentOutOfRangeException(name, $"Argument '{name}' must be a valid '{typeof(E)}' value."); } } }
internal T ReadEnum <T>(EmberReader reader, string fieldName) where T : struct { Exception exception = null; try { var result = FastEnum.ToEnum <T>(reader.AssertAndReadContentsAsInt32()); if (FastEnum.IsDefined(result)) { return(result); } } catch (ModelException ex) { exception = ex; } const string Format = "The field {0} has an unexpected value for the element with the path {1}."; throw new ModelException( string.Format(CultureInfo.InvariantCulture, Format, fieldName, this.GetPath()), exception); }
public bool FastEnum_Discontinuous_IsDefined() { return(FastEnum.IsDefined <IntEnum2>(1000)); }
public void Setup() { UniEnum.IsDefined <IntEnum2>(1000); FastEnum.IsDefined <IntEnum2>(1000); }
public bool FastEnum_Short_IsDefined() { return(FastEnum.IsDefined <ShortEnum1>((short)1000)); }
public void Setup() { UniEnum.IsDefined <ShortEnum1>(1000); FastEnum.IsDefined <ShortEnum1>(1000); }
public bool FastEnum_Continuous_IsDefined() { return(FastEnum.IsDefined <IntEnum1>(1)); }
public static string?GetName(this Instruction instruction, bool isPostMerge = false) => (instruction == Instruction.PREVRANDAO && !isPostMerge) ? "DIFFICULTY" : FastEnum.IsDefined(instruction) ? FastEnum.GetName(instruction) : null;
protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket packet) { IByteBuffer content = packet.Content; EndPoint address = packet.Sender; byte[] msgBytes = new byte[content.ReadableBytes]; content.ReadBytes(msgBytes); Interlocked.Add(ref Metrics.DiscoveryBytesReceived, msgBytes.Length); if (msgBytes.Length < 98) { if (_logger.IsDebug) { _logger.Debug($"Incorrect discovery message, length: {msgBytes.Length}, sender: {address}"); } return; } byte typeRaw = msgBytes[97]; if (!FastEnum.IsDefined <MsgType>((int)typeRaw)) { if (_logger.IsDebug) { _logger.Debug($"Unsupported message type: {typeRaw}, sender: {address}, message {msgBytes.ToHexString()}"); } return; } MsgType type = (MsgType)typeRaw; if (_logger.IsTrace) { _logger.Trace($"Received message: {type}"); } DiscoveryMsg msg; try { msg = Deserialize(type, msgBytes); msg.FarAddress = (IPEndPoint)address; } catch (Exception e) { if (_logger.IsDebug) { _logger.Debug($"Error during deserialization of the message, type: {type}, sender: {address}, msg: {msgBytes.ToHexString()}, {e.Message}"); } return; } try { ReportMsgByType(msg); if (!ValidateMsg(msg, type, address, ctx, packet)) { return; } _discoveryManager.OnIncomingMsg(msg); } catch (Exception e) { if (_logger.IsDebug) { _logger.Error($"DEBUG/ERROR Error while processing message, type: {type}, sender: {address}, message: {msg}", e); } } }