コード例 #1
0
ファイル: SameValueTest.cs プロジェクト: yamuun/FastEnum
        public void GetValues()
        {
            var expect = Enum.GetValues(typeof(TEnum));
            var actual = FastEnum.GetValues <TEnum>();

            actual.Should().BeEquivalentTo(expect);
        }
コード例 #2
0
    private static IReadOnlyList <T> GetEnumKeys(IReadOnlyList <T> keys)
    {
        if (typeof(T).IsEnum && keys.Count == 0)
        {
            keys = FastEnum.GetValues <T>().ToArray();
        }

        return(keys);
    }
コード例 #3
0
    /// <summary>
    /// Returns all combinations of enum values of type <see cref="T"/>.
    /// </summary>
    /// <typeparam name="T">Type of enumeration.</typeparam>
    /// <returns>All combinations of defined enum values.</returns>
    /// <remarks>
    /// For normal enums this is equivalent to all defined values.
    /// For <see cref="FlagsAttribute"/> enums this produces all combination of defined values.
    /// </remarks>
    public static IReadOnlyList <T> AllValuesCombinations <T>() where T : struct, Enum
    {
        // The return type of Enum.GetValues is Array but it is effectively int[] per docs
        // This bit converts to int[]
        IReadOnlyList <T> values = FastEnum.GetValues <T>();

        if (!typeof(T).GetCustomAttributes(typeof(FlagsAttribute), false).Any())
        {
            // We don't have flags so just return the result of GetValues
            return(values);
        }

        // TODO: in .net 7 rewrite with generic INumber based on FastEnum.GetUnderlyingType<T>()
        int[] valuesBinary = values.Cast <int>().ToArray();

        int[] valuesInverted = valuesBinary.Select(v => ~v).ToArray();
        int   max            = 0;

        for (int i = 0; i < valuesBinary.Length; i++)
        {
            max |= valuesBinary[i];
        }

        List <T> result = new();

        for (int i = 0; i <= max; i++)
        {
            int unaccountedBits = i;
            for (int j = 0; j < valuesInverted.Length; j++)
            {
                // This step removes each flag that is set in one of the Enums thus ensuring that an Enum with missing bits won't be passed an int that has those bits set
                unaccountedBits &= valuesInverted[j];
                if (unaccountedBits == 0)
                {
                    result.Add((T)(object)i);
                    break;
                }
            }
        }

        //Check for zero
        try
        {
            if (string.IsNullOrEmpty(Enum.GetName(typeof(T), (T)(object)0)))
            {
                result.Remove((T)(object)0);
            }
        }
        catch
        {
            result.Remove((T)(object)0);
        }

        return(result);
    }
コード例 #4
0
        public void GetValues()
        {
            var expect = new[]
            {
                TUnderlying.MinValue,
                TUnderlying.MaxValue,
            };
            var actual = FastEnum.GetValues <TEnum>();

            actual.Should().BeEquivalentTo(expect);
        }
コード例 #5
0
        public int FastEnum_Values()
        {
            int sum = 0;

            foreach (var v in FastEnum.GetValues <IntEnum1>())
            {
                sum += (int)v;
            }

            return(sum);
        }
コード例 #6
0
        protected override IEnumerable <(IIdentity x, IIdentity y, IIdentity z)> GenerateTransitiveSample()
        {
            foreach (var elem in FastEnum.GetValues <IdentityCategories>())
            {
                foreach (var scope in FastEnum.GetValues <ScopeCategories>())
                {
                    var x = new IdentityElement(new QualifiedElement(), scope, elem, scope.ToString() + elem.ToString());
                    var y = new IdentityElement(new QualifiedElement(), scope, elem, scope.ToString() + elem.ToString());
                    var z = new IdentityElement(new QualifiedElement(), scope, elem, scope.ToString() + elem.ToString());

                    yield return(x, y, z);
                }
            }
        }
コード例 #7
0
ファイル: UserController.cs プロジェクト: d01tsumath/Janken
        /// <summary>
        /// ユーザーの入力値を検証します。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private bool InputValidation(string input)
        {
            int n;

            if (!Int32.TryParse(input, out n))
            {
                return(false);
            }

            var hand = (Hand)Int32.Parse(input);

            if (FastEnum.GetValues <Hand>().Contains(hand))
            {
                return(true);
            }

            return(false);
        }
コード例 #8
0
ファイル: EmptyTest.cs プロジェクト: yamuun/FastEnum
 public void GetValues()
 => FastEnum.GetValues <TEnum>().Should().BeEmpty();
コード例 #9
0
 public IReadOnlyList <IntEnum1> FastEnum_Values()
 {
     return(FastEnum.GetValues <IntEnum1>());
 }
コード例 #10
0
 public void Setup()
 {
     _ = UniEnum.GetValues <IntEnum1>();
     _ = FastEnum.GetValues <IntEnum1>();
     _ = Enum.GetValues(typeof(IntEnum1));
 }