예제 #1
0
        public void HasValidValue()
        {
            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.HasValidValue(null));

            bool result = false;

            Assert.DoesNotThrow(() => result = EnumSimpleFlags.BitOne.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = (~EnumSimpleFlags.BitOne).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumSimpleFlags.None.HasValidValue());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = EnumSimpleDuplicateFlags.BitThree.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = EnumSimpleDuplicateFlags.BitAlsoThree.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = (~EnumSimpleDuplicateFlags.BitThree).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumSimpleDuplicateFlags.None.HasValidValue());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = EnumVanilla.Three.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = ((EnumVanilla)(-1)).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumVanilla.None.HasValidValue());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = EnumVanillaDuplicate.One.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = EnumVanillaDuplicate.AlsoOne.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = ((EnumVanillaDuplicate)(-1)).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumVanillaDuplicate.None.HasValidValue());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = EnumComboFlags.BitsOneThree.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = (EnumComboFlags.BitOne | (EnumComboFlags)(1 << 4)).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumComboFlags.BitsOneTwoThree.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = (EnumComboFlags.BitsOneTwoThree | (EnumComboFlags)(1 << 4)).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = ((EnumComboFlags)(0)).HasValidValue());
            Assert.False(result);

            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitsOneFour.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = (EnumComboOnlyFlags.BitOne | (EnumComboOnlyFlags)(1 << 5)).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitsOneTwoFour.HasValidValue());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = (EnumComboOnlyFlags.BitsOneTwoFour | (EnumComboOnlyFlags)(1 << 5)).HasValidValue());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = ((EnumComboOnlyFlags)(1 << 3)).HasValidValue());
            Assert.False(result);
        }
예제 #2
0
        public void Implements()
        {
            Type t = null;

            Assert.ThrowsExact <ArgumentNullException>(() => t.Implements <ITestImplement>());

            t = typeof(TestNoInterface);
            Assert.DoesNotThrow(() => t.Implements <ITestImplement>());
            Assert.False(t.Implements <ITestImplement>());

            t = typeof(TestInterface);
            Assert.DoesNotThrow(() => t.Implements <ITestImplement>());
            Assert.True(t.Implements <ITestImplement>());
        }
예제 #3
0
        public void HasNoFlags()
        {
            EnumSimpleFlags invalid = (EnumSimpleFlags)(-1);

            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.HasNoFlags(null));
            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.HasNoFlags(null, EnumSimpleFlags.None));
            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.HasNoFlags(EnumSimpleFlags.None, null));
            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.HasNoFlags(EnumSimpleFlags.None, EnumSimpleFlags.None, null));
            Assert.ThrowsExact <ArgumentException>(() => EnumExtensions.HasNoFlags(invalid, EnumSimpleFlags.None));
            Assert.ThrowsExact <ArgumentException>(() => EnumExtensions.HasNoFlags(EnumSimpleFlags.None, invalid));
            Assert.ThrowsExact <ArgumentException>(() => EnumExtensions.HasNoFlags(EnumSimpleFlags.None, EnumSimpleFlags.None, null as Enum));
            Assert.ThrowsExact <ArgumentException>(() => EnumExtensions.HasNoFlags(EnumSimpleFlags.None, EnumSimpleFlags.None, invalid));
            Assert.ThrowsExact <ArgumentException>(() => EnumExtensions.HasNoFlags(EnumComboFlags.BitOne, EnumSimpleFlags.None));
            Assert.ThrowsExact <ArgumentException>(() => EnumExtensions.HasNoFlags(EnumSimpleFlags.None, EnumComboFlags.BitOne));
            Assert.ThrowsExact <ArgumentException>(() => EnumExtensions.HasNoFlags(EnumSimpleFlags.None, EnumSimpleFlags.None, EnumComboFlags.BitOne));

            bool result = false;

            Assert.DoesNotThrow(() => result = EnumSimpleFlags.BitOne.HasNoFlags());
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumSimpleFlags.None.HasNoFlags());
            Assert.True(result);
            Assert.DoesNotThrow(() => result = (EnumSimpleFlags.BitOne | EnumSimpleFlags.BitTwo | EnumSimpleFlags.BitThree | EnumSimpleFlags.BitFour).HasNoFlags());
            Assert.False(result);

            Assert.DoesNotThrow(() => result = EnumComboFlags.BitOne.HasNoFlags(EnumComboFlags.BitsOneThree));
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumComboFlags.BitsOneThree.HasNoFlags(EnumComboFlags.BitOne));
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumComboFlags.BitsOneThree.HasNoFlags(EnumComboFlags.BitTwo));
            Assert.True(result);
            Assert.DoesNotThrow(() => result = EnumComboFlags.BitsOneThree.HasNoFlags(EnumComboFlags.BitOne, EnumComboFlags.BitTwo));
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumComboFlags.BitsOneThree.HasNoFlags(EnumComboFlags.BitOne, EnumComboFlags.BitThree));
            Assert.False(result);

            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitOne.HasNoFlags(EnumComboOnlyFlags.BitsOneFour));
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitsOneFour.HasNoFlags(EnumComboOnlyFlags.BitOne));
            Assert.False(result);
            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitsOneFour.HasNoFlags(EnumComboOnlyFlags.BitTwo));
            Assert.True(result);
            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitsOneFour.HasNoFlags(EnumComboOnlyFlags.None));
            Assert.True(result);
            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.None.HasNoFlags(EnumComboOnlyFlags.BitOne));
            Assert.True(result);
            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitsOneFour.HasNoFlags(EnumComboOnlyFlags.BitOne, EnumComboOnlyFlags.BitTwo));
            Assert.False(result);
        }
        public void Equals()
        {
            var comparer = new EqualityComparerStateful <int>((lhs, rhs) => lhs == rhs);

            Assert.True(comparer.Equals(1, 1));
            Assert.True(comparer.Equals(-1, -1));
            Assert.False(comparer.Equals(-1, 1));
            Assert.False(comparer.Equals(1, -1));

            Assert.False(comparer.Equals(2, 1));
            Assert.False(comparer.Equals(-2, -1));
            Assert.False(comparer.Equals(1, 2));
            Assert.False(comparer.Equals(-1, -2));
            Assert.False(comparer.Equals(2, -1));
            Assert.False(comparer.Equals(-2, 1));
            Assert.False(comparer.Equals(1, -2));
            Assert.False(comparer.Equals(-1, 2));

            var stringComparer = new EqualityComparerStateful <string>((lhs, rhs) => lhs == rhs);

            Assert.True(stringComparer.Equals("ABC", "ABC"));
            Assert.True(stringComparer.Equals(null, null));
            Assert.False(stringComparer.Equals("ABC", "abc"));
            Assert.False(stringComparer.Equals("abc", "ABC"));
            Assert.False(stringComparer.Equals("ABC", "CBA"));
            Assert.False(stringComparer.Equals("ABCD", "ABC"));
            Assert.False(stringComparer.Equals("ABC", "ABCD"));
            Assert.False(stringComparer.Equals("ABC", null));
            Assert.False(stringComparer.Equals(null, "ABC"));

            var objComparer = new EqualityComparerStateful <Testing>((lhs, rhs) => lhs.B.Length == rhs.A);
            var obj1        = new Testing(5, "ABC");
            var obj2        = new Testing(3, "NA");
            var obj3        = new Testing(2, "ABCDE");

            Assert.True(objComparer.Equals(obj1, obj2));
            Assert.False(objComparer.Equals(obj1, obj3));

            Assert.False(objComparer.Equals(obj2, obj1));
            Assert.True(objComparer.Equals(obj2, obj3));

            Assert.True(objComparer.Equals(obj3, obj1));
            Assert.False(objComparer.Equals(obj3, obj2));
        }
예제 #5
0
        public void IsFlagsType()
        {
            Type enumType = null;

            Assert.ThrowsExact <ArgumentNullException>(() => enumType.IsFlagsType());
            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.IsFlagsType(null as Enum));

            bool result = false;

            Assert.DoesNotThrow(() => result = EnumSimpleFlags.BitOne.IsFlagsType());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = EnumVanilla.Three.IsFlagsType());
            Assert.False(result);

            Assert.DoesNotThrow(() => result = EnumComboFlags.BitsOneThree.IsFlagsType());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = EnumComboOnlyFlags.BitsOneFour.IsFlagsType());
            Assert.True(result);
        }
예제 #6
0
        public void HasAttribute()
        {
            Func <Type, PropertyInfo> getProperty = (type) => type.GetProperty("Test", BindingFlags.Public | BindingFlags.Instance);
            PropertyInfo prop   = null;
            bool         result = false;

            Assert.ThrowsExact <ArgumentNullException>(() => prop.HasAttribute <TestingAttribute>());

            prop = getProperty(typeof(NoAttributeProperty));
            Assert.DoesNotThrow(() => result = prop.HasAttribute <TestingAttribute>());
            Assert.False(result);

            Assert.DoesNotThrow(() => result = prop.HasAttribute <DifferentAttribute>());
            Assert.False(result);

            prop = getProperty(typeof(TestingAttributeProperty));
            Assert.DoesNotThrow(() => result = prop.HasAttribute <TestingAttribute>());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = prop.HasAttribute <DifferentAttribute>());
            Assert.False(result);

            prop = getProperty(typeof(DifferentAttributeProperty));
            Assert.DoesNotThrow(() => result = prop.HasAttribute <TestingAttribute>());
            Assert.False(result);

            Assert.DoesNotThrow(() => result = prop.HasAttribute <DifferentAttribute>());
            Assert.True(result);

            prop = getProperty(typeof(MultiAttributeProperty));
            Assert.DoesNotThrow(() => result = prop.HasAttribute <TestingAttribute>());
            Assert.True(result);

            Assert.DoesNotThrow(() => result = prop.HasAttribute <DifferentAttribute>());
            Assert.True(result);
        }
예제 #7
0
        public void Basics()
        {
            PackedBitReader reader        = null;
            bool            boolValue     = false;
            sbyte           signedByte    = 0;
            byte            unsignedByte  = 0;
            short           signedShort   = 0;
            ushort          unsignedShort = 0;
            int             signedInt     = 0;
            uint            unsignedInt   = 0;
            long            signedLong    = 0;
            ulong           unsignedLong  = 0;
            string          stringValue   = null;
            double          doubleValue   = 0;
            float           floatValue    = 0;
            bool            success       = false;

            Assert.DoesNotThrow(() => reader = new PackedBitReader(sPackedBitWriterOutput));

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.False(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.False(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.False(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.False(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(4, out signedByte));
            Assert.True(success);
            Assert.Equal(signedByte, (sbyte)3);

            Assert.DoesNotThrow(() => success = reader.TryRead(4, out unsignedByte));
            Assert.True(success);
            Assert.Equal(unsignedByte, (byte)4);

            Assert.DoesNotThrow(() => success = reader.TryRead(6, out signedShort));
            Assert.True(success);
            Assert.Equal(signedShort, (short)13);

            Assert.DoesNotThrow(() => success = reader.TryRead(6, out unsignedShort));
            Assert.True(success);
            Assert.Equal(unsignedShort, (ushort)14);

            Assert.DoesNotThrow(() => success = reader.TryRead(6, out signedInt));
            Assert.True(success);
            Assert.Equal(signedInt, (int)23);

            Assert.DoesNotThrow(() => success = reader.TryRead(6, out unsignedInt));
            Assert.True(success);
            Assert.Equal(unsignedInt, (uint)24);

            Assert.DoesNotThrow(() => success = reader.TryRead(8, out signedLong));
            Assert.True(success);
            Assert.Equal(signedLong, (long)33);

            Assert.DoesNotThrow(() => success = reader.TryRead(8, out unsignedLong));
            Assert.True(success);
            Assert.Equal(unsignedLong, (ulong)34);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.False(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.False(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.False(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.True(success);
            Assert.True(boolValue);

            Assert.DoesNotThrow(() => success = reader.TryReadSigned(4, out signedByte));
            Assert.True(success);
            Assert.Equal(signedByte, (sbyte)-3);

            Assert.DoesNotThrow(() => success = reader.TryRead(out doubleValue));
            Assert.True(success);
            Assert.Equal(doubleValue, 2.4d);

            Assert.DoesNotThrow(() => success = reader.TryRead(out floatValue));
            Assert.True(success);
            Assert.Equal(floatValue, 88.4f);

            Assert.DoesNotThrow(() => success = reader.TryReadSigned(6, out signedShort));
            Assert.True(success);
            Assert.Equal(signedShort, (short)-13);

            Assert.DoesNotThrow(() => success = reader.TryReadSigned(6, out signedInt));
            Assert.True(success);
            Assert.Equal(signedInt, (int)-23);

            Assert.DoesNotThrow(() => success = reader.TryReadSigned(8, out signedLong));
            Assert.True(success);
            Assert.Equal(signedLong, (long)-33);

            Assert.DoesNotThrow(() => success = reader.TryRead(out stringValue));
            Assert.True(success);
            Assert.Equal(stringValue, "datà");

            Assert.DoesNotThrow(() => success = reader.TryRead(out stringValue, Encoding.ASCII));
            Assert.True(success);
            Assert.Equal(stringValue, "data");

            Assert.DoesNotThrow(() => success = reader.TryRead(2, out signedByte));
            Assert.True(success);
            Assert.Equal(signedByte, (sbyte)3);

            Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue));
            Assert.False(success);
        }
예제 #8
0
        public void Equals()
        {
            var obj1 = new Testing(5, "ABC");
            var obj2 = new Testing(3, "NA");
            var obj3 = new Testing(2, "ABCDE");
            var obj4 = new Testing(5, "NA");
            var obj5 = new Testing(7, null);

            var comparer = new EqualityComparerStateless <Testing, int>(obj => obj.A);

            Assert.True(comparer.Equals(obj1, obj1));
            Assert.False(comparer.Equals(obj1, obj2));
            Assert.False(comparer.Equals(obj1, obj3));
            Assert.True(comparer.Equals(obj1, obj4));
            Assert.False(comparer.Equals(obj1, obj5));

            Assert.False(comparer.Equals(obj2, obj1));
            Assert.True(comparer.Equals(obj2, obj2));
            Assert.False(comparer.Equals(obj2, obj3));
            Assert.False(comparer.Equals(obj2, obj4));
            Assert.False(comparer.Equals(obj2, obj5));

            Assert.False(comparer.Equals(obj3, obj1));
            Assert.False(comparer.Equals(obj3, obj2));
            Assert.True(comparer.Equals(obj3, obj3));
            Assert.False(comparer.Equals(obj3, obj4));
            Assert.False(comparer.Equals(obj3, obj5));

            Assert.True(comparer.Equals(obj4, obj1));
            Assert.False(comparer.Equals(obj4, obj2));
            Assert.False(comparer.Equals(obj4, obj3));
            Assert.True(comparer.Equals(obj4, obj4));
            Assert.False(comparer.Equals(obj4, obj5));

            Assert.False(comparer.Equals(obj5, obj1));
            Assert.False(comparer.Equals(obj5, obj2));
            Assert.False(comparer.Equals(obj5, obj3));
            Assert.False(comparer.Equals(obj5, obj4));
            Assert.True(comparer.Equals(obj5, obj5));

            var stringComparer = new EqualityComparerStateless <Testing, string>(obj => obj.B);

            Assert.True(stringComparer.Equals(obj1, obj1));
            Assert.False(stringComparer.Equals(obj1, obj2));
            Assert.False(stringComparer.Equals(obj1, obj3));
            Assert.False(stringComparer.Equals(obj1, obj4));
            Assert.False(stringComparer.Equals(obj1, obj5));

            Assert.False(stringComparer.Equals(obj2, obj1));
            Assert.True(stringComparer.Equals(obj2, obj2));
            Assert.False(stringComparer.Equals(obj2, obj3));
            Assert.True(stringComparer.Equals(obj2, obj4));
            Assert.False(stringComparer.Equals(obj2, obj5));

            Assert.False(stringComparer.Equals(obj3, obj1));
            Assert.False(stringComparer.Equals(obj3, obj2));
            Assert.True(stringComparer.Equals(obj3, obj3));
            Assert.False(stringComparer.Equals(obj3, obj4));
            Assert.False(stringComparer.Equals(obj3, obj5));

            Assert.False(stringComparer.Equals(obj4, obj1));
            Assert.True(stringComparer.Equals(obj4, obj2));
            Assert.False(stringComparer.Equals(obj4, obj3));
            Assert.True(stringComparer.Equals(obj4, obj4));
            Assert.False(stringComparer.Equals(obj4, obj5));

            Assert.False(stringComparer.Equals(obj5, obj1));
            Assert.False(stringComparer.Equals(obj5, obj2));
            Assert.False(stringComparer.Equals(obj5, obj3));
            Assert.False(stringComparer.Equals(obj5, obj4));
            Assert.True(stringComparer.Equals(obj5, obj5));
        }