Exemple #1
0
        private static void RunTests <E, T>()
            where E : class, IComparable, IFormattable, IConvertible
            where T : struct, E
        {
            string[] names = EnumUtilBase <E> .GetNames <T>();

            T[] values = EnumUtilBase <E> .GetValues <T>();

            IReadOnlyDictionary <T, string> nameValues =
                EnumUtilBase <E> .GetValueName <T>();

            Assert.IsNotNull(nameValues);

            foreach (var nm in nameValues)
            {
                Assert.IsNotNull(nm.Value);
            }

            Assert.IsTrue(nameValues.Count == values.Length);
            Assert.IsTrue(Enum.GetValues(typeof(T)).Length == values.Length);
            Assert.IsTrue(names.Length == values.Length);

            T aggregate = values.Aggregate((x, y) => EnumUtilBase <E> .BitwiseOr <T>(x, y));

            Assert.IsTrue(Convert.ToInt32(aggregate) == 15);
            T aggregate2 = values.Aggregate((x, y) => EnumUtilBase <E> .BitwiseAnd <T>(x, y));

            Assert.IsTrue(Convert.ToInt32(aggregate2) == 0);

            for (int i = 0; i < values.Length; i++)
            {
                T      value = values[i];
                string name  = names[i];

                byte byteVal = EnumUtilBase <E> .ToByte(value);

                Assert.AreEqual(EnumUtilBase <E> .FromByte <T>(byteVal), value);

                sbyte sbyteVal = EnumUtilBase <E> .ToSByte(value);

                Assert.AreEqual(EnumUtilBase <E> .FromSByte <T>(sbyteVal), value);

                short int16Val = EnumUtilBase <E> .ToInt16(value);

                Assert.AreEqual(EnumUtilBase <E> .FromInt16 <T>(int16Val), value);

                ushort uint16Val = EnumUtilBase <E> .ToUInt16(value);

                Assert.AreEqual(EnumUtilBase <E> .FromUInt16 <T>(uint16Val), value);

                int intVal = EnumUtilBase <E> .ToInt32(value);

                Assert.AreEqual(EnumUtilBase <E> .FromInt32 <T>(intVal), value);

                uint uintVal = EnumUtilBase <E> .ToUInt32(value);

                Assert.AreEqual(EnumUtilBase <E> .FromUInt32 <T>(uintVal), value);

                long longVal = EnumUtilBase <E> .ToInt64(value);

                Assert.AreEqual(EnumUtilBase <E> .FromInt64 <T>(longVal), value);

                ulong val = EnumUtilBase <E> .ToUInt64(value);

                Assert.AreEqual(EnumUtilBase <E> .FromUInt64 <T>(val), value);

                float floatVal = EnumUtilBase <E> .ToSingle(value);

                Assert.AreEqual(EnumUtilBase <E> .FromSingle <T>(floatVal), value);

                double doubleVal = EnumUtilBase <E> .ToDouble(value);

                Assert.AreEqual(EnumUtilBase <E> .FromDouble <T>(doubleVal), value);

                Assert.IsTrue(EnumUtilBase <E> .BitwiseOr(value, value).Equals(value));
                Assert.IsTrue(EnumUtilBase <E> .BitwiseAnd(value, value).Equals(value));
                Assert.IsTrue(EnumUtilBase <E> .BitwiseExclusiveOr(value, value).Equals(default(T)));

                Assert.IsTrue(EnumUtilBase <E> .HasFlag(aggregate, value));
                Assert.IsFalse(EnumUtilBase <E> .HasFlag(value, aggregate));
                Assert.IsTrue(EnumUtilBase <E> .HasFlag(aggregate, default(T)));

                Assert.IsFalse(EnumUtilBase <E> .HasFlagsAttribute <T>());
                Assert.IsFalse(EnumUtilBase <E> .HasAttribute <FlagsAttribute, T>());

                Assert.IsTrue(EnumUtilBase <E> .IsDefined(value));
                Assert.IsFalse(EnumUtilBase <E> .IsDefined(default(T)));

                Assert.AreEqual(name, EnumUtilBase <E> .GetName(value));
                Assert.AreEqual(value, EnumUtilBase <E> .Parse <T>(name));

                Type t = EnumUtilBase <E> .GetUnderlyingType <T>();

                Assert.AreEqual(t, Enum.GetUnderlyingType(typeof(T)));

                Assert.IsTrue(EnumUtilBase <E> .TryParse(name, out value));
                Assert.IsTrue(EnumUtilBase <E> .TryParse(name, false, out value));
                Assert.IsFalse(EnumUtilBase <E> .TryParse(name.ToLower(), false, out value));
                Assert.IsFalse(EnumUtilBase <E> .TryParse(name.ToUpper(), false, out value));

                Assert.IsTrue(EnumUtilBase <E> .TryParse(name.ToLower(), true, out value));
                Assert.IsTrue(EnumUtilBase <E> .TryParse(name.ToUpper(), true, out value));
                Assert.IsTrue(EnumUtilBase <E> .TryParse(name, true, out value));
            }
        }