Beispiel #1
0
        public void Enum_BitStreamerUpCastTest()
        {
            const int k_bit_count = 64;

            using (var ms = new System.IO.MemoryStream())
                using (var s = new IO.BitStream(ms))
                {
                    const System.TypeCode kExpectedValue = System.TypeCode.String;
                    var value = kExpectedValue;

                    s.StreamMode = System.IO.FileAccess.Write;
                    TypeCodeStreamer64.Write(s, value, k_bit_count);
                    s.Flush(); s.SeekToStart();
                    s.StreamMode = System.IO.FileAccess.Read;
                    TypeCodeStreamer64.Read(s, out value, k_bit_count);

                    Assert.AreEqual(kExpectedValue, value);

                    //////////////////////////////////////////////////////////////////////////
                    // Test the instance interface
                    var streamer_instance = TypeCodeStreamer64.Instance;
                    s.SeekToStart();

                    s.StreamMode = System.IO.FileAccess.Write;
                    streamer_instance.Write(s, value, k_bit_count);
                    s.Flush(); s.SeekToStart();
                    s.StreamMode = System.IO.FileAccess.Read;
                    streamer_instance.Read(s, out value, k_bit_count);

                    Assert.AreEqual(kExpectedValue, value);
                }
        }
Beispiel #2
0
        public void EnumBitStreamer_TestNoneSentinelEncoding()
        {
            const int k_bit_count = 32;

            using (var ms = new System.IO.MemoryStream())
                using (var s = new IO.BitStream(ms))
                {
                    const NoneSentinelEncodedEnum kExpectedValue = NoneSentinelEncodedEnum.Three;
                    var value = kExpectedValue;

                    s.StreamMode = System.IO.FileAccess.Write;
                    NoneSentinelEncodedEnumStreamer.Stream(s, ref value, k_bit_count);
                    s.Flush(); s.SeekToStart();
                    s.StreamMode = System.IO.FileAccess.Read;
                    NoneSentinelEncodedEnumStreamer.Stream(s, ref value, k_bit_count);

                    Assert.AreEqual(kExpectedValue, value);

                    //////////////////////////////////////////////////////////////////////////
                    // Test the instance interface
                    var streamer_instance = NoneSentinelEncodedEnumStreamer.Instance;
                    s.SeekToStart();

                    s.StreamMode = System.IO.FileAccess.Write;
                    streamer_instance.Stream(s, ref value, k_bit_count);
                    s.Flush(); s.SeekToStart();
                    s.StreamMode = System.IO.FileAccess.Read;
                    streamer_instance.Stream(s, ref value, k_bit_count);

                    Assert.AreEqual(kExpectedValue, value);
                }
        }
Beispiel #3
0
        public void Enum_BitStreamerUsingUnderlyingTypeTest()
        {
            const int k_bit_count = 32;

            using (var ms = new System.IO.MemoryStream())
                using (var s = new IO.BitStream(ms))
                {
                    const EnumBinaryStreamerTest.UInt32Enum kExpectedValue = EnumBinaryStreamerTest.UInt32Enum.DeadBeef;
                    var value = kExpectedValue;

                    s.StreamMode = System.IO.FileAccess.Write;
                    UInt32EnumStreamer.Write(s, value, k_bit_count);
                    s.Flush(); s.SeekToStart();
                    s.StreamMode = System.IO.FileAccess.Read;
                    UInt32EnumStreamer.Read(s, out value, k_bit_count);

                    Assert.AreEqual(kExpectedValue, value);

                    //////////////////////////////////////////////////////////////////////////
                    // Test the instance interface
                    var streamer_instance = UInt32EnumStreamer.Instance;
                    s.SeekToStart();

                    s.StreamMode = System.IO.FileAccess.Write;
                    streamer_instance.Write(s, value, k_bit_count);
                    s.Flush(); s.SeekToStart();
                    s.StreamMode = System.IO.FileAccess.Read;
                    streamer_instance.Read(s, out value, k_bit_count);

                    Assert.AreEqual(kExpectedValue, value);
                }
        }
        public string Encode()
        {
            string encoded_porition;

            using (var ms = new System.IO.MemoryStream(kBitStreamSizeInBytes))
                using (var bs = new IO.BitStream(ms))
                {
                    bs.StreamMode = System.IO.FileAccess.Write;
                    Serialize(bs);
                    bs.Flush();

                    byte[] bits = ms.ToArray();
                    encoded_porition = kRadixEncoding.Encode(bits);
                }

            Contract.Assert(encoded_porition.Length <= kEncodedPortionLength);

            return(Type.ToEncodingPrefix() + encoded_porition);
        }
Beispiel #5
0
        void WriteBitStream(IO.EndianWriter s, long hashPosition)
        {
            byte[] bs_bytes = new byte[GetBitStreamSize()];
            int    bs_length;

            using (var ms = new System.IO.MemoryStream(bs_bytes))
                using (var bs = new IO.BitStream(ms, System.IO.FileAccess.Write, streamName: "GameVariant"))
                {
                    bs.StreamMode = System.IO.FileAccess.Write;

                    Data.Serialize(bs);
                    bs.Flush();

                    bs_length = (int)ms.Position;
                }

            #region calculate hash
            byte[] calculated_hash;
            using (var hasher = Program.GetGen3RuntimeDataHasher())
            {
                byte[] bs_length_bytes = BitConverter.GetBytes(bs_length);
                if (!s.ByteOrder.IsSameAsRuntime())
                {
                    Bitwise.ByteSwap.SwapData(Bitwise.ByteSwap.kInt32Definition, bs_length_bytes);
                }

                hasher.TransformBlock(bs_length_bytes, 0, bs_length_bytes.Length, null, 0);
                hasher.TransformFinalBlock(bs_bytes, 0, bs_length);
                calculated_hash = hasher.Hash;
            }
            #endregion

            s.Write(bs_length);
            s.Write(bs_bytes);

            long position = s.BaseStream.Position;
            s.Seek(hashPosition);
            s.Write(calculated_hash);
            s.Seek(position);
        }