public void HasFlagTest()
        {
            TestLongEnum e64 = TestLongEnum.Alpha | TestLongEnum.Beta;

            Assert.IsTrue(Enum <TestLongEnum> .HasFlag(e64, TestLongEnum.None));
            Assert.IsTrue(Enum <TestLongEnum> .HasFlag(e64, TestLongEnum.Beta));
            Assert.IsFalse(Enum <TestLongEnum> .HasFlag(e64, TestLongEnum.Gamma));
            Assert.IsTrue(Enum <TestLongEnum> .HasFlag(e64, TestLongEnum.Alphabet));
            Assert.IsFalse(Enum <TestLongEnum> .HasFlag(e64, TestLongEnum.Alpha | TestLongEnum.Gamma));

            TestIntEnum e32 = TestIntEnum.Simple | TestIntEnum.Risky;

            Assert.IsTrue(Enum <TestIntEnum> .HasFlag(e32, TestIntEnum.None)); // Zero -> true
            Assert.IsFalse(Enum <TestIntEnum> .HasFlag(e32, TestIntEnum.Normal));
            Assert.IsTrue(Enum <TestIntEnum> .HasFlag(e32, TestIntEnum.Risky));
            Assert.IsTrue(Enum <TestIntEnum> .HasFlag(e32, (int)TestIntEnum.Risky));
            Assert.IsTrue(Enum <TestIntEnum> .HasFlag(e32, (long)TestIntEnum.Risky));
            Assert.IsTrue(Enum <TestIntEnum> .HasFlag(e32, 1 << 31));    // -2147483648: This is the value of Risky
            Assert.IsFalse(Enum <TestIntEnum> .HasFlag(e32, 1U << 31));  //  2147483648: This is not defined (cannot be represented in int)
            Assert.IsFalse(Enum <TestIntEnum> .HasFlag(e32, 1L << 31));  //  2147483648: This is not defined
            Assert.IsFalse(Enum <TestIntEnum> .HasFlag(e32, 1UL << 31)); //  2147483648: This is not defined

            TestULongEnum eu64 = TestULongEnum.Max;

            Assert.IsTrue(Enum <TestULongEnum> .HasFlag(eu64, 0UL)); // Zero -> true
            Assert.IsTrue(Enum <TestULongEnum> .HasFlag(eu64, TestULongEnum.Max));
        }
        public void ToStringTest()
        {
            Assert.AreEqual("Max", Enum <TestULongEnum> .ToString(TestULongEnum.Max));
            Assert.AreEqual("0", Enum <EmptyEnum> .ToString(default(EmptyEnum)));
            Assert.AreEqual("None", Enum <TestLongEnum> .ToString(default(TestLongEnum)));
            Assert.AreEqual("Alpha", Enum <TestLongEnum> .ToString(TestLongEnum.Alpha));
            Assert.AreEqual("-2147483648", Enum <EmptyEnum> .ToString((EmptyEnum)(1 << 31)));
            Assert.AreEqual("-2147483647", Enum <EmptyEnum> .ToString((EmptyEnum)((1 << 31) | 1)));
            Assert.AreEqual("1, -2147483648", Enum <EmptyEnum> .ToString((EmptyEnum)((1 << 31) | 1), EnumFormattingOptions.DistinctFlags));

            Assert.AreNotEqual("-10", Enum <TestULongEnum> .ToString(unchecked ((TestULongEnum)(-10))));
            Assert.AreEqual("-10", Enum <TestLongEnum> .ToString((TestLongEnum)(-10)));
            Assert.AreEqual("-10", Enum <TestIntEnum> .ToString((TestIntEnum)(-10)));

            TestLongEnum e = TestLongEnum.Gamma | TestLongEnum.Alphabet;

            Assert.AreEqual("Alphabet, Gamma", e.ToString(EnumFormattingOptions.Auto));
            Assert.AreEqual("7", e.ToString(EnumFormattingOptions.NonFlags));
            Assert.AreEqual("Alpha, Beta, Gamma", e.ToString(EnumFormattingOptions.DistinctFlags));
            Assert.AreEqual("Alphabet, Gamma", e.ToString(EnumFormattingOptions.CompoundFlagsOrNumber));
            Assert.AreEqual("Alphabet, Gamma", e.ToString(EnumFormattingOptions.CompoundFlagsAndNumber));

            e += 16;
            Assert.AreEqual("23", e.ToString(EnumFormattingOptions.Auto));
            Assert.AreEqual("23", e.ToString(EnumFormattingOptions.NonFlags));
            Assert.AreEqual("Alpha, Beta, Gamma, 16", e.ToString(EnumFormattingOptions.DistinctFlags));
            Assert.AreEqual("23", e.ToString(EnumFormattingOptions.CompoundFlagsOrNumber));
            Assert.AreEqual("16, Alphabet, Gamma", e.ToString(EnumFormattingOptions.CompoundFlagsAndNumber));

            TestIntEnum ie = TestIntEnum.Simple | TestIntEnum.Normal | TestIntEnum.Risky;

            Assert.AreEqual("Simple, Normal, Risky", ie.ToString(EnumFormattingOptions.Auto));
        }
Ejemplo n.º 3
0
        public void ParseTest()
        {
            Assert.AreEqual(default(EmptyEnum), Enum <EmptyEnum> .Parse("0"));
            Assert.AreEqual(default(EmptyEnum), Enum <EmptyEnum> .Parse("0".AsSegment()));
#if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
            Assert.AreEqual(default(EmptyEnum), Enum <EmptyEnum> .Parse("0".AsSpan()));
#endif
            Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse("Max"));
            Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse("Max".AsSegment()));
#if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
            Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse("Max".AsSpan()));
#endif
            Assert.AreEqual(TestULongEnum.Max, Enum <TestULongEnum> .Parse(UInt64.MaxValue.ToString()));
            Assert.AreEqual(TestLongEnum.Min, Enum <TestLongEnum> .Parse("Min"));
            Assert.AreEqual(TestLongEnum.Min, Enum <TestLongEnum> .Parse(Int64.MinValue.ToString()));
            Assert.AreEqual(TestLongEnum.Min, Enum <TestLongEnum> .Parse(" -9223372036854775808 "));
            Assert.AreEqual(TestLongEnum.Max, Enum <TestLongEnum> .Parse("9223372036854775807"));
            Assert.AreEqual((EmptyEnum)(-10), Enum <EmptyEnum> .Parse("-10"));

            Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("Alpha"));
            Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("AlphaRedefined"));
            Assert.AreEqual(TestLongEnum.AlphaRedefined, Enum <TestLongEnum> .Parse("AlphaRedefined"));
            Assert.AreEqual(TestLongEnum.AlphaRedefined, Enum <TestLongEnum> .Parse("Alpha"));
            Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("alpha", true));
            Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("ALPHAREDEFINED", true));
            Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("ALPHAREDEFINED".AsSegment(), true));
#if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
            Assert.AreEqual(TestLongEnum.Alpha, Enum <TestLongEnum> .Parse("ALPHAREDEFINED".AsSpan(), true));
#endif

            TestLongEnum e = TestLongEnum.Gamma | TestLongEnum.Alphabet;
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Gamma, Alphabet"));
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Gamma, Alphabet".AsSegment()));
#if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Gamma, Alphabet".AsSpan()));
#endif
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("7"));
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha, Beta, Gamma"));
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha Beta Gamma", " "));
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha | Beta | Gamma", "|"));

            e += 16;
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("23"));
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("Alpha, Beta, Gamma, 16"));
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("16, Gamma, Alphabet"));
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("16, Gamma, Alphabet".AsSegment()));
#if !(NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
            Assert.AreEqual(e, Enum <TestLongEnum> .Parse("16, Gamma, Alphabet".AsSpan()));
#endif

            Assert.IsFalse(Enum <TestLongEnum> .TryParse(UInt64.MaxValue.ToString(), out var _));
            Assert.IsFalse(Enum <TestLongEnum> .TryParse("Beta, Gamma, , Delta, 16", out var _));
            Assert.IsFalse(Enum <TestLongEnum> .TryParse(" ", out var _));
            Assert.IsFalse(Enum <TestLongEnum> .TryParse("9223372036854775808", out var _));
            Assert.IsFalse(Enum <TestLongEnum> .TryParse(" -9223372036854775809 ", out var _));

            TestIntEnum ie = TestIntEnum.Simple | TestIntEnum.Normal | TestIntEnum.Risky;
            Assert.AreEqual(ie, Enum <TestIntEnum> .Parse(ie.ToString(EnumFormattingOptions.Auto)));
        }
Ejemplo n.º 4
0
        public void SerializeDeserializeLongUtf8(TestLongEnum value)
        {
            var serialized = JsonSerializer.Generic.Utf8.Serialize <TestLongEnum, ExcludeNullCamelCaseIntegerEnumResolver <byte> >(value);

            Assert.Contains(((long)value).ToString(CultureInfo.InvariantCulture), Encoding.UTF8.GetString(serialized));
            var deserialized = JsonSerializer.Generic.Utf8.Deserialize <TestLongEnum, ExcludeNullCamelCaseIntegerEnumResolver <byte> >(serialized);

            Assert.Equal(value, deserialized);
        }
        public void TestEnumComparer()
        {
            var c1 = EnumComparer <EmptyEnum> .Comparer;

            c1.Compare((EmptyEnum)(-1), (EmptyEnum)1);
            var d1 = Comparer <EmptyEnum> .Default;
            var e1 = EqualityComparer <EmptyEnum> .Default;
            var v1 = new EmptyEnum[] { (EmptyEnum)(-1), (EmptyEnum)1 };

            Assert.AreEqual(d1.Compare(v1[0], v1[1]), c1.Compare(v1[0], v1[1]));
            Assert.AreEqual(d1.Compare(v1[1], v1[0]), c1.Compare(v1[1], v1[0]));
            Assert.AreEqual(d1.Compare(v1[1], v1[1]), c1.Compare(v1[1], v1[1]));
            Assert.AreEqual(e1.Equals(v1[0], v1[1]), c1.Equals(v1[0], v1[1]));
            Assert.AreEqual(e1.Equals(v1[1], v1[1]), c1.Equals(v1[1], v1[1]));
            Assert.AreEqual(e1.GetHashCode(v1[0]), c1.GetHashCode(v1[0]));
            Assert.AreNotEqual(c1.GetHashCode(v1[0]), c1.GetHashCode(v1[1]));

            var c2 = EnumComparer <TestLongEnum> .Comparer;
            var d2 = Comparer <TestLongEnum> .Default;
            var e2 = EqualityComparer <TestLongEnum> .Default;
            var v2 = new TestLongEnum[] { TestLongEnum.Min, TestLongEnum.Max };

            Assert.AreEqual(d2.Compare(v2[0], v2[1]), c2.Compare(v2[0], v2[1]));
            Assert.AreEqual(d2.Compare(v2[1], v2[0]), c2.Compare(v2[1], v2[0]));
            Assert.AreEqual(d2.Compare(v2[1], v2[1]), c2.Compare(v2[1], v2[1]));
            Assert.AreEqual(e2.Equals(v2[0], v2[1]), c2.Equals(v2[0], v2[1]));
            Assert.AreEqual(e2.Equals(v2[1], v2[1]), c2.Equals(v2[1], v2[1]));

            var c3 = EnumComparer <TestUlongEnum> .Comparer;
            var d3 = Comparer <TestUlongEnum> .Default;
            var e3 = EqualityComparer <TestUlongEnum> .Default;
            var v3 = new TestUlongEnum[] { TestUlongEnum.Max, (TestUlongEnum)1 };

            Assert.AreEqual(d3.Compare(v3[0], v3[1]), c3.Compare(v3[0], v3[1]));
            Assert.AreEqual(d3.Compare(v3[1], v3[0]), c3.Compare(v3[1], v3[0]));
            Assert.AreEqual(d3.Compare(v3[1], v3[1]), c3.Compare(v3[1], v3[1]));
            Assert.AreEqual(e3.Equals(v3[0], v3[1]), c3.Equals(v3[0], v3[1]));
            Assert.AreEqual(e3.Equals(v3[1], v3[1]), c3.Equals(v3[1], v3[1]));
            Assert.AreNotEqual(c3.GetHashCode(v3[0]), c3.GetHashCode(v3[1]));

            var clone = c1.DeepClone();

            Assert.AreSame(c1, clone);
        }