Esempio n. 1
0
        public void WriteSingleBit()
        {
            _testObj.WriteBit(true);
            _testObj.Flush();

            var result = GetResult();

            Assert.Single(result);
            Assert.Equal(0b00000001, result[0]);
        }
Esempio n. 2
0
 /// <summary>
 /// Flushes any remaining bits to the stream.
 /// </summary>
 public override void Flush()
 {
     if (Mode == CompressionMode.Compress)
     {
         _coder.Encode(ZeroOrderAdaptiveByteModel.StreamTerminator, _adaptiveModel, _stream.Write);
         _coder.EncodeFinish(_stream.Write);
     }
     _stream.Flush();
 }
Esempio n. 3
0
        public void WriteBitThenBytesLsb(BitDirection direction)
        {
            BitStream stream = new BitStream(direction);

            stream.WriteBit(true);
            stream.Write(BitConverter.GetBytes((ushort)0xFFFF));
            stream.Flush();

            stream.Seek(0, SeekOrigin.Begin);

            Assert.Equal(1, stream.ReadBit());
            Span <byte> bytes = new byte[2];

            stream.Read(bytes);
            ushort us = BitConverter.ToUInt16(bytes);

            Assert.Equal(0xFFFF, us);
        }
Esempio n. 4
0
        public void WriteDifBufferReadSameBuffer()
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            FileStream writeStream    = new FileStream(filePath, FileMode.OpenOrCreate);
            BitStream  bitWriteStream = new BitStream(writeStream);

            Random          bufferSizeRandom = new Random();
            int             total            = 2048;
            List <BitArray> buffers          = new List <BitArray>();

            int curBufferSize  = bufferSizeRandom.Next(1, total);
            int curBufferIndex = 0;

            buffers.Add(new BitArray(curBufferSize));

            for (int i = 0; i < total; ++i)
            {
                if (curBufferIndex >= curBufferSize)
                {
                    bitWriteStream.WriteBits(buffers[buffers.Count - 1]);
                    curBufferSize  = bufferSizeRandom.Next(1, total - i + 1);
                    curBufferIndex = 0;

                    buffers.Add(new BitArray(curBufferSize));
                }
                buffers[buffers.Count - 1][curBufferIndex] = bufferSizeRandom.Next() % 2 == 0;
                curBufferIndex++;
            }
            bitWriteStream.WriteBits(buffers[buffers.Count - 1]);
            bitWriteStream.Flush();
            writeStream.Close();

            FileStream readStream    = new FileStream(filePath, FileMode.OpenOrCreate);
            BitStream  bitReadStream = new BitStream(readStream);

            int      readBufferSize      = 128;
            BitArray buffer              = bitReadStream.ReadBits(readBufferSize);
            int      curReadIndex        = 0;
            int      curWriteBufferIndex = 0;
            int      curWriteBuffer      = 0;
            bool     differ              = false;

            for (int i = 0; i < total; ++i)
            {
                if (curReadIndex == readBufferSize)
                {
                    curReadIndex = 0;
                    buffer       = bitReadStream.ReadBits(readBufferSize);
                }
                if (curWriteBufferIndex == buffers[curWriteBuffer].Count)
                {
                    curWriteBuffer++;
                    curWriteBufferIndex = 0;
                }
                if (buffer[curReadIndex] != buffers[curWriteBuffer][curWriteBufferIndex])
                {
                    differ = true;
                }
                curReadIndex++;
                curWriteBufferIndex++;
            }
            readStream.Close();
            if (differ)
            {
                Console.WriteLine("Error: Different Write Buffers, Same Read Buffer: Original and written arrays differ");
            }
            else
            {
                Console.WriteLine("Success: Different Write Buffers, Same Read Buffer: Original and written arrays are the same");
            }
        }
Esempio n. 5
0
        public void ReadWriteSameBuffer()
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            FileStream writeStream    = new FileStream(filePath, FileMode.OpenOrCreate);
            BitStream  bitWriteStream = new BitStream(writeStream);

            BitArray oneArray   = new BitArray(new bool[] { false, false, true, false, true, false, true });
            BitArray twoArray   = new BitArray(new bool[] { true, true, false });
            BitArray threeArray = new BitArray(new bool[] { false });

            bitWriteStream.WriteBits(oneArray);
            bitWriteStream.WriteBits(twoArray);
            bitWriteStream.WriteBits(threeArray);

            bitWriteStream.Flush();
            writeStream.Close();

            FileStream readStream    = new FileStream(filePath, FileMode.Open);
            BitStream  bitReadStream = new BitStream(readStream);

            BitArray oneTestArr   = bitReadStream.ReadBits(oneArray.Length);
            BitArray twoTestArr   = bitReadStream.ReadBits(twoArray.Length);
            BitArray threeTestArr = bitReadStream.ReadBits(threeArray.Length);

            bool differ = false;

            for (int i = 0; i < oneArray.Length; ++i)
            {
                if (oneArray[i] != oneTestArr[i])
                {
                    differ = true;
                }
            }
            for (int i = 0; i < twoArray.Length; ++i)
            {
                if (twoArray[i] != twoTestArr[i])
                {
                    differ = true;
                }
            }
            for (int i = 0; i < threeArray.Length; ++i)
            {
                if (threeArray[i] != threeTestArr[i])
                {
                    differ = true;
                }
            }
            readStream.Close();

            if (differ)
            {
                Console.WriteLine("Error: Same Write and Read Buffer sizes: Original and written arrays differ");
            }
            else
            {
                Console.WriteLine("Success: Same Write and Read Buffer sizes: Original and written arrays are the same");
            }
        }