Example #1
0
        public void TestDouble()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                double[] vals = new double[Config.MULTI_TEST_COUNT];
                byte[] bytes = new byte[8];

                Random rand = new Random();
                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                {
                    rand.NextBytes(bytes);

                    vals[i] = BitConverter.ToDouble(bytes, 0);
                    bw.WriteDouble(vals[i], 64);
                }

                bw.Flush();

                ms.Position = 0;

                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                    Assert.AreEqual(vals[i], br.ReadDouble(64));
            }
        }
Example #2
0
        public void TestFloat()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                float[] vals = new float[Config.MULTI_TEST_COUNT];
                byte[] bytes = new byte[4];

                Random rand = new Random();
                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                {
                    rand.NextBytes(bytes);

                    vals[i] = BitConverter.ToSingle(bytes, 0);
                    bw.WriteFloat(vals[i], 32);
                }

                bw.Flush();

                ms.Position = 0;

                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                    Assert.AreEqual(vals[i], br.ReadFloat(32));
            }
        }
        public void TestByte()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                for (int i = 0; i < 256; i++)
                    bw.WriteByte((byte)i);
                bw.Flush();

                ms.Position = 0;

                for (int i = 0; i < 256; i++)
                    Assert.AreEqual((byte)i, br.ReadByte());
            }
        }
Example #4
0
        public void TestUTF8Chars()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms, new UTF8Encoding()))
            using (BinaryBitReader br = new BinaryBitReader(ms, new UTF8Encoding()))
            {
                for (int i = 0; i < 55296; i++)
                    bw.WriteChar((char)i);
                bw.Flush();

                ms.Position = 0;

                for (int i = 0; i < 55296; i++)
                    Assert.AreEqual((char)i, br.ReadChar());
            }
        }
        public void TestBits()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                byte[] writeBytes = new byte[Config.MULTI_TEST_COUNT];
                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                    writeBytes[i] = 1;
                bw.WriteBits(writeBytes);
                bw.Flush();

                ms.Position = 0;

                byte[] readBytes = br.ReadBits(Config.MULTI_TEST_COUNT);
                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                    Assert.IsTrue((readBytes[i / 8] & (1 << i % 8)) > 0);
            }
        }
        public void TestBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                Random rand = new Random();

                byte[] writeBytes = new byte[Config.MULTI_TEST_COUNT];
                rand.NextBytes(writeBytes);
                bw.WriteBytes(writeBytes);
                bw.Flush();

                ms.Position = 0;

                byte[] readBytes = br.ReadBytes(Config.MULTI_TEST_COUNT);
                for (int i = 0; i < readBytes.Length; i++)
                    Assert.AreEqual(writeBytes[i], readBytes[i]);
            }
        }
Example #7
0
        public void TestUTF32String()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms, new UTF32Encoding()))
            using (BinaryBitReader br = new BinaryBitReader(ms, new UTF32Encoding()))
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 55296; i++)
                    sb.Append((char)i);

                string result = sb.ToString() + sb.ToString() + sb.ToString();

                bw.WriteString(result);
                bw.Flush();

                ms.Position = 0;

                Assert.AreEqual(br.ReadString(), result);
            }
        }
Example #8
0
        public void TestDecimal()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                decimal[] vals = new decimal[Config.MULTI_TEST_COUNT];

                Random rand = new Random();
                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                {
                    vals[i] = new decimal(rand.Next(0, int.MaxValue), rand.Next(0, int.MaxValue), rand.Next(0, int.MaxValue), rand.Next(0, 2) == 1, (byte)rand.Next(0, 29));
                    bw.WriteDecimal(vals[i], 128);
                }

                bw.Flush();

                ms.Position = 0;

                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                    Assert.AreEqual(vals[i], br.ReadDecimal(128));
            }
        }
        public void TestMultiFloat()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                br.BufferSize = bw.BufferSize = Config.MULTI_TEST_BUFFER_COUNT * sizeof(float);

                Random rand = new Random();
                float[] vals = new float[Config.MULTI_TEST_COUNT];
                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                {
                    vals[i] = (float)(rand.NextDouble() * float.MaxValue);
                    bw.WriteDouble(vals[i]);
                }

                bw.Flush();

                ms.Position = 0;

                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                    Assert.AreEqual(vals[i], br.ReadDouble());
            }
        }
Example #10
0
        public void TestUnsignedShort()
        {
            using (MemoryStream ms = new MemoryStream())
            using (BinaryBitWriter bw = new BinaryBitWriter(ms))
            using (BinaryBitReader br = new BinaryBitReader(ms))
            {
                ushort[] vals = new ushort[Config.MULTI_TEST_COUNT];
                byte[] bytes = new byte[4];

                Random rand = new Random();
                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                {
                    rand.NextBytes(bytes);

                    vals[i] = BitConverter.ToUInt16(bytes, 0);
                    bw.WriteUInt(vals[i], 16);
                }

                bw.Flush();

                ms.Position = 0;

                for (int i = 0; i < Config.MULTI_TEST_COUNT; i++)
                    Assert.AreEqual(vals[i], br.ReadUInt(16));
            }
        }