Example #1
0
        public void GetValues()
        {
            var expect = Enum.GetValues(typeof(TEnum));
            var actual = FastEnum.GetValues <TEnum>();

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

        return(keys);
    }
Example #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);
    }
Example #4
0
        public void GetValues()
        {
            var expect = new[]
            {
                TUnderlying.MinValue,
                TUnderlying.MaxValue,
            };
            var actual = FastEnum.GetValues <TEnum>();

            actual.Should().BeEquivalentTo(expect);
        }
Example #5
0
        public int FastEnum_Values()
        {
            int sum = 0;

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

            return(sum);
        }
Example #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);
                }
            }
        }
Example #7
0
        /// <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);
        }
Example #8
0
 public void GetValues()
 => FastEnum.GetValues <TEnum>().Should().BeEmpty();
Example #9
0
 public IReadOnlyList <IntEnum1> FastEnum_Values()
 {
     return(FastEnum.GetValues <IntEnum1>());
 }
Example #10
0
 public void Setup()
 {
     _ = UniEnum.GetValues <IntEnum1>();
     _ = FastEnum.GetValues <IntEnum1>();
     _ = Enum.GetValues(typeof(IntEnum1));
 }