コード例 #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 As()
        {
            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.As <int>(null));
            Assert.ThrowsExact <InvalidOperationException>(() => EnumExtensions.As <double>(EnumVanilla.None));
            Assert.ThrowsExact <InvalidOperationException>(() => EnumExtensions.As <Guid>(EnumVanilla.None));
            Assert.ThrowsExact <InvalidOperationException>(() => EnumExtensions.As <DateTime>(EnumVanilla.None));

            int value = 0;

            value = 0;
            Assert.DoesNotThrow(() => value = EnumVanilla.One.As <sbyte>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.DoesNotThrow(() => value = EnumVanilla.One.As <short>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.DoesNotThrow(() => value = EnumVanilla.One.As <int>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.DoesNotThrow(() => value = (int)EnumVanilla.One.As <long>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.DoesNotThrow(() => value = EnumVanilla.One.As <byte>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.DoesNotThrow(() => value = EnumVanilla.One.As <ushort>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.DoesNotThrow(() => value = (int)EnumVanilla.One.As <uint>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.DoesNotThrow(() => value = (int)EnumVanilla.One.As <ulong>());
            Assert.Equal(value, 1);

            value = 0;
            Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <byte>());
            Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <sbyte>());
            Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <ushort>());
            Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <short>());
            Assert.ThrowsExact <OverflowException>(() => value = ((EnumVanilla)(-1)).As <ushort>());

            value = 0;
            Assert.DoesNotThrow(() => value = default(EnumNoValues).As <byte>());
            Assert.Equal(value, 0);
        }
コード例 #3
0
 public void Constructor()
 {
     Assert.ThrowsExact <ArgumentNullException>(() => new PackedBitSerializer(null));
     Assert.ThrowsExact <ArgumentException>(() => new PackedBitSerializer(typeof(NonImplementingType)));
     Assert.ThrowsExact <ArgumentException>(() => new PackedBitSerializer(typeof(NonImplementingTypeWithProperty)));
     Assert.DoesNotThrow(() => new PackedBitSerializer(typeof(ImplementingType)));
     Assert.DoesNotThrow(() => new PackedBitSerializer(typeof(ImplementingTypeWithProperty)));
     Assert.DoesNotThrow(() => new PackedBitSerializer(typeof(NonImplementingTypeWithAttribute)));
     Assert.ThrowsExact <ArgumentException>(() => new PackedBitSerializer(typeof(NonImplementingTypeWithConflictingAttribute)));
     Assert.ThrowsExact <ArgumentException>(() => new PackedBitSerializer(typeof(NonImplementingTypeWithString)));
     Assert.ThrowsExact <ArgumentException>(() => new PackedBitSerializer(typeof(NonImplementingTypeWithList)));
     Assert.DoesNotThrow(() => new PackedBitSerializer(typeof(ImplementingTypeWithStringProperty)));
 }
コード例 #4
0
ファイル: Extensions.cs プロジェクト: LazyBui/BitPackerTools
        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>());
        }
コード例 #5
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);
        }
コード例 #6
0
        public void GenericEnum()
        {
            Assert.ThrowsExact <TypeInitializationException>(() => new int[0].ThrowIfAnyInvalidEnums());
            Assert.ThrowsExact <TypeInitializationException>(() => new float[0].ThrowIfAnyInvalidEnums());
            Assert.ThrowsExact <TypeInitializationException>(() => new Guid[0].ThrowIfAnyInvalidEnums());
            Assert.ThrowsExact <TypeInitializationException>(() => new DateTime[0].ThrowIfAnyInvalidEnums());
            Assert.ThrowsExact <ArgumentNullException>(() => (null as EnumVanilla[]).ThrowIfAnyInvalidEnums());
            Assert.DoesNotThrow(() => new EnumVanilla[0].ThrowIfAnyInvalidEnums());
            Assert.DoesNotThrow(() => new EnumSimpleFlags[0].ThrowIfAnyInvalidEnums());
            Assert.DoesNotThrow(() => new EnumComboFlags[0].ThrowIfAnyInvalidEnums());

            Assert.DoesNotThrow(() => new[] { EnumSimpleFlags.BitOne }.ThrowIfAnyInvalidEnums());
            Assert.ThrowsExact <ArgumentException>(() => new[] { ~EnumSimpleFlags.BitOne }.ThrowIfAnyInvalidEnums());
            Assert.DoesNotThrow(() => new[] { EnumSimpleFlags.None }.ThrowIfAnyInvalidEnums());
            Assert.ThrowsExact <ArgumentException>(() => new[] { EnumSimpleFlags.BitOne, ~EnumSimpleFlags.BitOne, EnumSimpleFlags.None }.ThrowIfAnyInvalidEnums());
        }
コード例 #7
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);
        }
コード例 #8
0
ファイル: Extensions.cs プロジェクト: LazyBui/BitPackerTools
        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);
        }
コード例 #9
0
        public void ThrowIfInvalid()
        {
            Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.ThrowIfInvalid(null));

            EnumSimpleFlags testSimpleFlags1 = EnumSimpleFlags.BitOne;

            Assert.DoesNotThrow(() => testSimpleFlags1.ThrowIfInvalid());

            EnumSimpleFlags testSimpleFlags2 = ~EnumSimpleFlags.BitOne;

            Assert.ThrowsExact <ArgumentException>(() => testSimpleFlags2.ThrowIfInvalid());

            EnumSimpleFlags testSimpleFlags3 = EnumSimpleFlags.None;

            Assert.DoesNotThrow(() => testSimpleFlags3.ThrowIfInvalid());

            EnumSimpleDuplicateFlags testSimpleDuplicateFlags1 = EnumSimpleDuplicateFlags.BitThree;

            Assert.DoesNotThrow(() => testSimpleDuplicateFlags1.ThrowIfInvalid());

            EnumSimpleDuplicateFlags testSimpleDuplicateFlags2 = ~EnumSimpleDuplicateFlags.BitAlsoThree;

            Assert.ThrowsExact <ArgumentException>(() => testSimpleDuplicateFlags2.ThrowIfInvalid());

            EnumSimpleDuplicateFlags testSimpleDuplicateFlags3 = EnumSimpleDuplicateFlags.None;

            Assert.DoesNotThrow(() => testSimpleDuplicateFlags3.ThrowIfInvalid());

            EnumVanilla testVanilla1 = EnumVanilla.Three;

            Assert.DoesNotThrow(() => testVanilla1.ThrowIfInvalid());

            EnumVanilla testVanilla2 = (EnumVanilla)(-1);

            Assert.ThrowsExact <ArgumentException>(() => testVanilla2.ThrowIfInvalid());

            EnumVanilla testVanilla3 = EnumVanilla.None;

            Assert.DoesNotThrow(() => testVanilla3.ThrowIfInvalid());

            EnumVanillaDuplicate testVanillaDuplicate1 = EnumVanillaDuplicate.One;

            Assert.DoesNotThrow(() => testVanillaDuplicate1.ThrowIfInvalid());

            EnumVanillaDuplicate testVanillaDuplicate2 = EnumVanillaDuplicate.AlsoOne;

            Assert.DoesNotThrow(() => testVanillaDuplicate2.ThrowIfInvalid());

            EnumVanillaDuplicate testVanillaDuplicate3 = (EnumVanillaDuplicate)(-1);

            Assert.ThrowsExact <ArgumentException>(() => testVanillaDuplicate3.ThrowIfInvalid());

            EnumComboFlags testComboFlags1 = EnumComboFlags.BitsOneThree;

            Assert.DoesNotThrow(() => testComboFlags1.ThrowIfInvalid());

            EnumComboFlags testComboFlags2 = EnumComboFlags.BitOne | (EnumComboFlags)(1 << 4);

            Assert.ThrowsExact <ArgumentException>(() => testComboFlags2.ThrowIfInvalid());

            EnumComboFlags testComboFlags3 = EnumComboFlags.BitsOneTwoThree;

            Assert.DoesNotThrow(() => testComboFlags3.ThrowIfInvalid());

            EnumComboFlags testComboFlags4 = EnumComboFlags.BitsOneTwoThree | (EnumComboFlags)(1 << 4);

            Assert.ThrowsExact <ArgumentException>(() => testComboFlags4.ThrowIfInvalid());

            EnumComboFlags testComboFlags5 = 0;

            Assert.ThrowsExact <ArgumentException>(() => testComboFlags5.ThrowIfInvalid());

            EnumComboOnlyFlags testComboOnlyFlags1 = EnumComboOnlyFlags.BitsOneFour;

            Assert.DoesNotThrow(() => testComboOnlyFlags1.ThrowIfInvalid());

            EnumComboOnlyFlags testComboOnlyFlags2 = EnumComboOnlyFlags.BitOne | (EnumComboOnlyFlags)(1 << 5);

            Assert.ThrowsExact <ArgumentException>(() => testComboOnlyFlags2.ThrowIfInvalid());

            EnumComboOnlyFlags testComboOnlyFlags3 = EnumComboOnlyFlags.BitsOneTwoFour;

            Assert.DoesNotThrow(() => testComboOnlyFlags3.ThrowIfInvalid());

            EnumComboOnlyFlags testComboOnlyFlags4 = EnumComboOnlyFlags.BitsOneTwoFour | (EnumComboOnlyFlags)(1 << 5);

            Assert.ThrowsExact <ArgumentException>(() => testComboOnlyFlags4.ThrowIfInvalid());

            EnumComboOnlyFlags testComboOnlyFlags5 = (EnumComboOnlyFlags)(1 << 3);

            Assert.ThrowsExact <ArgumentException>(() => testComboOnlyFlags5.ThrowIfInvalid());

            EnumNoValues testNoValues = default(EnumNoValues);

            Assert.ThrowsExact <ArgumentException>(() => testNoValues.ThrowIfInvalid());
        }
コード例 #10
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);
        }
コード例 #11
0
 public void Constructor()
 {
     Assert.ThrowsExact <ArgumentNullException>(() => new PackedBitReader(null));
     Assert.ThrowsExact <ArgumentException>(() => new PackedBitReader(new byte[0]));
     Assert.DoesNotThrow(() => new PackedBitReader(new byte[] { 0x22 }));
 }
コード例 #12
0
 public void Constructor()
 {
     Assert.ThrowsExact <ArgumentNullException>(() => new EqualityComparerStateful <int>(null));
     Assert.DoesNotThrow(() => new EqualityComparerStateful <int>((lhs, rhs) => lhs == rhs));
 }
コード例 #13
0
        public void Basics()
        {
            PackedBitWriter writer = null;

            Assert.DoesNotThrow(() => writer = new PackedBitWriter());
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(false));
            Assert.DoesNotThrow(() => writer.Write(false));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(false));
            Assert.DoesNotThrow(() => writer.Write(false));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(4, (sbyte)3));
            Assert.DoesNotThrow(() => writer.Write(4, (byte)4));
            Assert.DoesNotThrow(() => writer.Write(6, (short)13));
            Assert.DoesNotThrow(() => writer.Write(6, (ushort)14));
            Assert.DoesNotThrow(() => writer.Write(6, (int)23));
            Assert.DoesNotThrow(() => writer.Write(6, (uint)24));
            Assert.DoesNotThrow(() => writer.Write(8, (long)33));
            Assert.DoesNotThrow(() => writer.Write(8, (ulong)34));
            Assert.DoesNotThrow(() => writer.Write(false));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.Write(false));
            Assert.DoesNotThrow(() => writer.Write(false));
            Assert.DoesNotThrow(() => writer.Write(true));
            Assert.DoesNotThrow(() => writer.WriteSigned(4, (sbyte)-3));
            Assert.DoesNotThrow(() => writer.Write(2.4d));
            Assert.DoesNotThrow(() => writer.Write(88.4f));
            Assert.DoesNotThrow(() => writer.WriteSigned(6, (short)-13));
            Assert.DoesNotThrow(() => writer.WriteSigned(6, (int)-23));
            Assert.DoesNotThrow(() => writer.WriteSigned(8, (long)-33));
            Assert.DoesNotThrow(() => writer.Write("datà"));
            Assert.DoesNotThrow(() => writer.Write("data", Encoding.ASCII));
            Assert.DoesNotThrow(() => writer.Write(2, (sbyte)3));

            byte[] result = null;
            // byteQuantity must have an exact bit count in order to be correct
            // Please ensure that the test above produces an exact bit count
            // Please also ensure that PackedBitReader is updated upon changes here
            int byteQuantity = (
                1 + 1 + 1 + 1 + 1 +
                1 + 1 + 1 + 1 + 1 +
                4 + 4 + 6 + 6 + 6 +
                6 + 8 + 8 + 1 + 1 +
                1 + 1 + 1 + 1 + 1 +
                1 + 64 + 32 + 5 + 7 + 7 + 9 +
                // datà should be 5 bytes in UTF8
                // length should always be 32 bits
                32 + 8 + 8 + 8 + 8 + 8 +
                // Since ASCII is 7-bit, should be chars = bytes
                32 + 8 + 8 + 8 + 8 +
                2
                ) / Constants.BitsInByte;

            Assert.DoesNotThrow(() => result = writer.ToArray());
            Assert.NotNull(result);
            Assert.Equal(result.Length, byteQuantity);

            var hex = string.Join(
                ", ",
                result.Select(v => "0x" + v.ToString("X2")));
        }
コード例 #14
0
 public void Constructor()
 {
     Assert.ThrowsExact <ArgumentNullException>(() => new EqualityComparerStateless <Testing, int>(null));
     Assert.DoesNotThrow(() => new EqualityComparerStateless <Testing, int>(obj => obj.A));
 }