Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        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>();
        }
Ejemplo n.º 3
0
 /// <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);
 }
Ejemplo n.º 4
0
 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>();
 }
Ejemplo n.º 5
0
 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;
 }
Ejemplo n.º 6
0
        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.");
            }
        }
    }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
 public bool FastEnum_Discontinuous_IsDefined()
 {
     return(FastEnum.IsDefined <IntEnum2>(1000));
 }
Ejemplo n.º 9
0
 public void Setup()
 {
     UniEnum.IsDefined <IntEnum2>(1000);
     FastEnum.IsDefined <IntEnum2>(1000);
 }
Ejemplo n.º 10
0
 public bool FastEnum_Short_IsDefined()
 {
     return(FastEnum.IsDefined <ShortEnum1>((short)1000));
 }
Ejemplo n.º 11
0
 public void Setup()
 {
     UniEnum.IsDefined <ShortEnum1>(1000);
     FastEnum.IsDefined <ShortEnum1>(1000);
 }
Ejemplo n.º 12
0
 public bool FastEnum_Continuous_IsDefined()
 {
     return(FastEnum.IsDefined <IntEnum1>(1));
 }
Ejemplo n.º 13
0
 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);
            }
        }
    }