Beispiel #1
0
        public static string FastToString(this FlagsEnum value)
        {
            // If has defined value 0.
            if (value == FlagsEnum.None)
            {
                return("None");
            }

            List <string> flags = new(3);

            if (value.HasFlag(FlagsEnum.A))
            {
                flags.Add("A");
            }

            if (value.HasFlag(FlagsEnum.B))
            {
                flags.Add("B");
            }

            if (value.HasFlag(FlagsEnum.C))
            {
                flags.Add("C");
            }

            int length = flags.Count;

            if (length == 0)
            {
                return(((int)value).ToString());
            }

            if (length == 1)
            {
                return(flags[0]);
            }

            StringBuilder builder = new(length);

            builder.Append(flags[0]);

            for (int i = 1; i < length; i++)
            {
                builder
                .Append(',')
                .Append(' ')
                .Append(flags[i]);
            }

            return(builder.ToString());
        }
Beispiel #2
0
        public static FlagsEnum[] GetFlags(this FlagsEnum value)
        {
            List <FlagsEnum> flags = new(3);

            if (value.HasFlag(FlagsEnum.A))
            {
                flags.Add(FlagsEnum.A);
            }

            if (value.HasFlag(FlagsEnum.B))
            {
                flags.Add(FlagsEnum.B);
            }

            if (value.HasFlag(FlagsEnum.C))
            {
                flags.Add(FlagsEnum.C);
            }

            return(flags.ToArray());
        }
Beispiel #3
0
        public static long TestSpeedNatural(out int i)
        {
            var watch = Stopwatch.StartNew();

            FlagsEnum natural = FlagsEnum.Two | FlagsEnum.Four;
            FlagsEnum flag    = FlagsEnum.Four;

            bool discardMe = false;

            i = 0;
            for (; i < 2000000; i++)
            {
                discardMe = natural.HasFlag(flag);
            }

            watch.Stop();
            return(watch.ElapsedMilliseconds);
        }
Beispiel #4
0
        public void Test_Flags()
        {
            FlagsEnum.IsFlag.Should()
            .BeTrue();

            var t = FlagsEnum.None;

            t.Should()
            .Be(FlagsEnum.None);

            t.Should()
            .Be(0);

            t = FlagsEnum.Four;
            t.Should()
            .Be(FlagsEnum.Four);
            t.Should()
            .Be(4);

            t = "One";
            t.Should()
            .Be(FlagsEnum.One);

            t = 0;
            t.Should()
            .Be(FlagsEnum.None);

            t++;
            t.Should()
            .Be(FlagsEnum.One);

            t = FlagsEnum.One | FlagsEnum.Two;
            t.Should()
            .Be(3);

            string flags = t;

            flags
            .Should()
            .Be("One,Two");

            FlagsEnum stringTest = flags;

            stringTest.Should()
            .Be(t);

            FlagsEnum.HasFlag(t, FlagsEnum.Two)
            .Should()
            .BeTrue();
            FlagsEnum.HasFlag(t, FlagsEnum.One)
            .Should()
            .BeTrue();
            FlagsEnum.HasFlag(t, FlagsEnum.Four)
            .Should()
            .BeFalse();
            t.HasFlag(FlagsEnum.Two)
            .Should()
            .BeTrue();
            t.HasFlag(FlagsEnum.Four)
            .Should()
            .BeFalse();
        }
Beispiel #5
0
        public void CompareEnums()
        {
            var    sRegular  = RegularEnum.Running.ToString();
            string sEnhanced = StatusEnum.Running;
            string sFlags    = FlagsEnum.Four;

            var iRegular  = (int)RegularEnum.Running;
            int iEnhanced = StatusEnum.Running;
            int iFlags    = FlagsEnum.Four;

            // String to Enum
            regularEnum  = (RegularEnum)Enum.Parse(typeof(RegularEnum), sRegular);
            regularEnum  = Enum.Parse <RegularEnum>(sRegular);
            enhancedEnum = sEnhanced;
            flagsEnum    = sFlags;

            // Int to Enum
            regularEnum  = (RegularEnum)iRegular;
            enhancedEnum = iEnhanced;
            flagsEnum    = iFlags;

            // Flag Manipulation
            regularEnum = RegularEnum.Stopped | RegularEnum.Error;
            flagsEnum   = FlagsEnum.Four | FlagsEnum.Eight;
            regularEnum.HasFlag(RegularEnum.Stopped);
            flagsEnum.HasFlag(FlagsEnum.Four);

            // regularEnum.HasFlag(2); // Compile Error
            flagsEnum.HasFlag(2); // Valid check

            // Errors on conversion: Configurable to be NULL / throw exception.
            Func <RegularEnum> regularAct = () => regularEnum = (RegularEnum)55; // Assigns 55

            regularAct.Should()
            .NotThrow();
            regularEnum.Should()
            .Be(55);

            regularAct            =
                () => regularEnum =
                    (RegularEnum)Enum.Parse(typeof(RegularEnum), "Running1"); // throws ArgumentException
            regularAct.Should()
            .Throw <ArgumentException>();

            Func <StatusEnum> enhancedAct = () => enhancedEnum = 55; // returns null;

            enhancedAct.Should()
            .NotThrow();
            enhancedEnum.Should()
            .BeNull();

            enhancedAct = () => enhancedEnum = "Running1"; // returns null;
            enhancedAct.Should()
            .NotThrow();
            enhancedEnum.Should()
            .BeNull();

            StatusEnum.ThrowOnError = true;
            enhancedAct             = () => enhancedEnum = 55; // throws InvalidOperationException
            enhancedAct.Should()
            .Throw <InvalidOperationException>();
            enhancedAct = () => enhancedEnum = "Running1"; // throws InvalidOperationException
            enhancedAct.Should()
            .Throw <InvalidOperationException>();
        }