Beispiel #1
0
        void TestNone64Helper <TEnum>(TEnum value, TEnum noneValue)
            where TEnum : struct, IComparable, IFormattable, IConvertible
        {
            var ebe = new EnumBitEncoder64 <TEnum>();

            Assert.IsTrue(ebe.HasNone);

            ulong bits;
            int   bit_index;

            {             // Test by-value encoding
                bits = 0; bit_index = 0;
                bits = ebe.BitEncode(value, bits, bit_index);
                Assert.AreEqual(value, ebe.BitDecode(bits, bit_index));
            }

            const int bit_index_offset = 5;
            {             // Test indexed by-value encoding
                bits = 0; bit_index = bit_index_offset;
                bits = ebe.BitEncode(value, bits, bit_index);
                Assert.AreEqual(value, ebe.BitDecode(bits, bit_index));
            }

            {             // Test by-value encoding
                bits = 0; bit_index = 0;
                bits = ebe.BitEncode(noneValue, bits, bit_index);
                Assert.AreEqual(noneValue, ebe.BitDecode(bits, bit_index));
            }
        }
Beispiel #2
0
        static void Test64Helper <TEnum, TEnumInformal>(TEnum value)
            where TEnum : struct, IComparable, IFormattable, IConvertible
            where TEnumInformal : struct, IComparable, IFormattable, IConvertible
        {
            var ebe_formal   = new EnumBitEncoder64 <TEnum>();
            var ebe_informal = new EnumBitEncoder64 <TEnumInformal>();

            // Test that informal calculations correlate with that taken from
            // formal enum types (which use a konstant member)
            Assert.AreEqual(ebe_formal.BitCountTrait, ebe_informal.BitCountTrait);
            Assert.AreEqual(ebe_formal.BitmaskTrait, ebe_informal.BitmaskTrait);

            Assert.IsFalse(ebe_formal.HasNone);
            Assert.IsFalse(ebe_informal.HasNone);

            ulong bits;
            int   bit_index;

            {             // Test by-value encoding
                bits = 0; bit_index = 0;
                bits = ebe_formal.BitEncode(value, bits, bit_index);
                Assert.AreEqual(value, ebe_formal.BitDecode(bits, bit_index));
            }
            {             // Test by-reference encoding
                bits = 0; bit_index = 0;
                ebe_formal.BitEncode(value, ref bits, ref bit_index);
                // Test if anything was actually encoded
                Assert.AreNotEqual(0, bits);
                // Test for the proper increment of the cursor
                Assert.AreEqual(EnumBitEncoder64 <TEnum> .kBitCount, bit_index);

                bit_index = 0;
                // Test for the proper decoding of the enum value
                Assert.AreEqual(value, ebe_formal.BitDecode(bits, ref bit_index));
                // Test for the proper increment of the cursor
                Assert.AreEqual(EnumBitEncoder64 <TEnum> .kBitCount, bit_index);
            }

            const int bit_index_offset = 5;
            {             // Test indexed by-value encoding
                bits = 0; bit_index = bit_index_offset;
                bits = ebe_formal.BitEncode(value, bits, bit_index);
                Assert.AreEqual(value, ebe_formal.BitDecode(bits, bit_index));
            }
            {             // Test indexed by-reference encoding
                bits = 0; bit_index = bit_index_offset;
                ebe_formal.BitEncode(value, ref bits, ref bit_index);
                // Test if anything was actually encoded
                Assert.AreNotEqual(0, bits);
                // Test for the proper increment of the cursor
                Assert.AreEqual(bit_index_offset + EnumBitEncoder64 <TEnum> .kBitCount, bit_index);

                bit_index = bit_index_offset;
                // Test for the proper decoding of the enum value
                Assert.AreEqual(value, ebe_formal.BitDecode(bits, ref bit_index));
                // Test for the proper increment of the cursor
                Assert.AreEqual(bit_index_offset + EnumBitEncoder64 <TEnum> .kBitCount, bit_index);
            }
        }
        /// <summary>Decode an enumeration value using an enumeration encoder object</summary>
        /// <typeparam name="TEnum">Enumeration type to decode</typeparam>
        /// <param name="value">Enumeration value decoded from this handle</param>
        /// <param name="decoder">Encoder for <typeparamref name="TEnum"/> objects</param>
        public void Decode64 <TEnum>(out TEnum value, EnumBitEncoder64 <TEnum> decoder)
            where TEnum : struct, IComparable, IFormattable, IConvertible
        {
            Contract.Requires <ArgumentNullException>(decoder != null);

            value = decoder.BitDecode(mBits.u64, ref mBitIndex);
        }