Beispiel #1
0
        static void TestUShortGetAndSet()
        {
            ushort field = 0;

            const ushort expectedLow  = 0xAA;
            const ushort expectedHigh = 0xBB;

            field = BitPack.Set(field, expectedLow, 8);
            field = BitPack.Set(field, expectedHigh, 8, 8);

            var subFieldLow  = BitPack.Get(field, 8);
            var subFieldHigh = BitPack.Get(field, 8, 8);

            Validate(subFieldLow, subFieldHigh, expectedLow, expectedHigh);

            const ushort one      = 1;
            const ushort subField = 0xA;

            field = 0;
            field = BitPack.Set(field, one, 1);
            field = BitPack.Set(field, subField, 8, 1);

            subFieldLow  = BitPack.Get(field, 1);
            subFieldHigh = BitPack.Get(field, 8, 1);

            Validate(subFieldLow, subFieldHigh, one, subField);
        }
Beispiel #2
0
        static void TestUIntGetAndSet()
        {
            uint field = 0;

            const uint expectedLow  = 0xAAAAu;
            const uint expectedHigh = 0xBBBBu;

            field = BitPack.Set(field, expectedLow, 16);
            field = BitPack.Set(field, expectedHigh, 16, 16);

            var subFieldLow  = BitPack.Get(field, 16);
            var subFieldHigh = BitPack.Get(field, 16, 16);

            Validate(subFieldLow, subFieldHigh, expectedLow, expectedHigh);

            const uint one      = 1u;
            const uint subField = 0xAAu;

            field = 0;
            field = BitPack.Set(field, one, 1);
            field = BitPack.Set(field, subField, 8, 1);

            subFieldLow  = BitPack.Get(field, 1);
            subFieldHigh = BitPack.Get(field, 8, 1);

            Validate(subFieldLow, subFieldHigh, one, subField);
        }
Beispiel #3
0
        static void TestULongGetAndSet()
        {
            ulong field = 0;

            const ulong expectedLow  = 0xAAAA_AAAAul;
            const ulong expectedHigh = 0xBBBB_BBBBul;

            field = BitPack.Set(field, expectedLow, 32);
            field = BitPack.Set(field, expectedHigh, 32, 32);

            var subFieldLow  = BitPack.Get(field, 32);
            var subFieldHigh = BitPack.Get(field, 32, 32);

            Validate(subFieldLow, subFieldHigh, expectedLow, expectedHigh);

            const ulong one      = 1ul;
            const ulong subField = 0xAAAAul;

            field = 0;
            field = BitPack.Set(field, one, 1);
            field = BitPack.Set(field, subField, 16, 1);

            subFieldLow  = BitPack.Get(field, 1);
            subFieldHigh = BitPack.Get(field, 16, 1);

            Validate(subFieldLow, subFieldHigh, one, subField);
        }
Beispiel #4
0
        static void TestByteGetAndSet()
        {
            byte field = 0;

            const byte expectedLow  = 0xA;
            const byte expectedHigh = 0xB;

            field = BitPack.Set(field, expectedLow, 4);
            field = BitPack.Set(field, expectedHigh, 4, 4);

            var subFieldLow  = BitPack.Get(field, 4);
            var subFieldHigh = BitPack.Get(field, 4, 4);

            Validate(subFieldLow, subFieldHigh, expectedLow, expectedHigh);
        }
Beispiel #5
0
        public void CanCarelessUserCorruptULong()
        {
            ulong field = 0;

            field = BitPack.Set(field, 0xAAAA_AAAA, 32);
            field = BitPack.Set(field, 0xBBBB_BBBB, 32, 32);

            // oops!
            field = BitPack.Set(field, 0xABAB_ABAB, 32, 16);

            var subFieldLow  = BitPack.Get(field, 32);
            var subFieldHigh = BitPack.Get(field, 32, 32);
            var subFieldOops = BitPack.Get(field, 32, 16);

            Assert.That(subFieldLow != 0xAAAA_AAAA, $"Unexpected Value {subFieldLow:x8}, should NOT be: 0xAAAAAAAA!");
            Assert.That(subFieldHigh != 0xBBBB_BBBB, $"Unexpected Value {subFieldHigh:x8}, should NOT be: 0xBBBBBBBB!");
            Assert.That(subFieldOops == 0xABAB_ABAB, $"Unexpected Value {subFieldOops:x8}, should be: ABABABAB!");
        }
Beispiel #6
0
        public void CanCarelessUserCorruptUInt()
        {
            uint field = 0;

            field = BitPack.Set(field, 0xAAAA, 16);
            field = BitPack.Set(field, 0xBBBB, 16, 16);

            // oops!
            field = BitPack.Set(field, 0xABAB, 16, 8);

            var subFieldLow  = BitPack.Get(field, 16);
            var subFieldHigh = BitPack.Get(field, 16, 16);
            var subFieldOops = BitPack.Get(field, 16, 8);

            Assert.That(subFieldLow != 0xAAAA, $"Unexpected Value {subFieldLow:x4}, should NOT be: 0xAAAA!");
            Assert.That(subFieldHigh != 0xBBBB, $"Unexpected Value {subFieldHigh:x4}, should NOT be: 0xBBBB!");
            Assert.That(subFieldOops == 0xABAB, $"Unexpected Value {subFieldOops:x4}, should be: ABAB!");
        }
Beispiel #7
0
        public void CanCarelessUserCorruptByte()
        {
            byte field = 0;

            field = BitPack.Set(field, 0xA, 4);
            field = BitPack.Set(field, 0xB, 4, 4);

            // oops!
            field = BitPack.Set(field, 0x3, 6, 2);

            var subFieldLow  = BitPack.Get(field, 4);
            var subFieldHigh = BitPack.Get(field, 4, 4);
            var subFieldOops = BitPack.Get(field, 6, 2);

            Assert.That(subFieldLow != 0xA, $"Unexpected Value {subFieldLow:x1}, should NOT be: 0xA!");
            Assert.That(subFieldHigh != 0xB, $"Unexpected Value {subFieldHigh:x1}, should NOT be: 0xB!");
            Assert.That(subFieldOops == 0x3, $"Unexpected Value {subFieldOops:x1}, should be: 0x3!");
        }