Beispiel #1
0
        public void BigEndianBitConverter_Int32()
        {
            const int n = sizeof(Int32);

            const Int32 pos  = 0x11223344;
            const Int32 zero = 0;
            const Int32 neg  = -pos;

            var posRepr = new byte[] { 0x11, 0x22, 0x33, 0x44 };

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToInt32(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToInt32(buffer));

            BigEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(Neg(posRepr)));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, BigEndianBitConverter.ToInt32(buffer));
        }
Beispiel #2
0
        public void BigEndianBitConverter_Double()
        {
            const int n = sizeof(Double);

            const Double pos  = Math.PI;
            const Double zero = 0;
            const Double neg  = -pos;

            var posRepr = GetCanonicalRepresentation(pos);
            var negRepr = GetCanonicalRepresentation(neg);

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToDouble(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToDouble(buffer));

            BigEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(negRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, BigEndianBitConverter.ToDouble(buffer));
        }
Beispiel #3
0
        public void BigEndianBitConverter_Decimal()
        {
            const int n = sizeof(Decimal);

            const Decimal pos  = 3.1415926535897932384626433833m;
            const Decimal zero = 0;
            const Decimal neg  = -pos;

            var posRepr = GetCanonicalRepresentation(pos);
            var negRepr = GetCanonicalRepresentation(neg);

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToDecimal(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToDecimal(buffer));

            BigEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(negRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, BigEndianBitConverter.ToDecimal(buffer));
        }
Beispiel #4
0
        public void BigEndianBitConverter_Boolean()
        {
            var buffer = new byte[1];

            BigEndianBitConverter.FillBytes(true, buffer);
            Assert.AreEqual(1, buffer[0]);
            Assert.IsTrue(BigEndianBitConverter.GetBytes(true).SequenceEqual(buffer));
            Assert.AreEqual(true, BigEndianBitConverter.ToBoolean(buffer));

            BigEndianBitConverter.FillBytes(false, buffer);
            Assert.AreEqual(0, buffer[0]);
            Assert.IsTrue(BigEndianBitConverter.GetBytes(false).SequenceEqual(buffer));
            Assert.AreEqual(false, BigEndianBitConverter.ToBoolean(buffer));
        }
Beispiel #5
0
        public void BigEndianBitConverter_UInt16()
        {
            const int n = sizeof(UInt16);

            const UInt16 pos  = 0x1122;
            const UInt16 zero = 0;

            var posRepr = new byte[] { 0x11, 0x22 };

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToUInt16(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToUInt16(buffer));
        }
Beispiel #6
0
        public void BigEndianBitConverter_UInt64()
        {
            const int n = sizeof(UInt64);

            const UInt64 pos  = 0x1122334455667788;
            const UInt64 zero = 0;

            var posRepr = new byte[] { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToUInt64(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToUInt64(buffer));
        }