Exemple #1
0
            public void SimplePeekCharMultipleBytesMbcsUtf32ByteByByte()
            {
                byte[] bbuffer = { 0xF3, 0xA0, 0x82, 0x84, 0x30 };
                CircularBuffer <byte> buffer = new CircularBuffer <byte>(256);
                ReadToCache           rtc    = new ReadToCache();

                Assert.That(rtc.PeekChar(buffer), Is.False);

                buffer.Append(bbuffer[0]);
                Assert.That(rtc.PeekChar(buffer), Is.False);

                buffer.Append(bbuffer[1]);
                Assert.That(rtc.PeekChar(buffer), Is.False);

                buffer.Append(bbuffer[2]);
                Assert.That(rtc.PeekChar(buffer), Is.False);

                buffer.Append(bbuffer[3]);
                Assert.That(rtc.PeekChar(buffer), Is.True);

                buffer.Append(bbuffer[4]);
                Assert.That(rtc.PeekChar(buffer), Is.True);

                Assert.That(rtc.PeekChar(buffer), Is.False);
            }
Exemple #2
0
        /// <summary>
        /// Reads a single character from the byte buffer provided, putting it into the internal cache
        /// </summary>
        /// <param name="sbuffer">The buffer to read.</param>
        /// <returns><c>true</c> if a character was found; <c>false</c> otherwise.</returns>
        /// <exception cref="System.ArgumentNullException">buffer may not be null.</exception>
        /// <remarks>
        /// It is expected that this method be used iteratively to find a single character. Data is not consumed
        /// from the <paramref name="sbuffer"/>, but state is updated in this class to advance the byte offset
        /// into the buffer to the next character. Should data be consumed from the internal buffer, you will
        /// need to reset the state of this object.
        /// </remarks>
        private bool PeekChar(SerialBuffer sbuffer)
        {
            // Once the bug from Mono is fixed, we just drop the code above.
            if (sbuffer == null)
            {
                throw new ArgumentNullException("sbuffer");
            }
            int readLen = sbuffer.Serial.ReadBuffer.Length;

            if (Log.ReadToTrace(System.Diagnostics.TraceEventType.Verbose))
            {
                Log.ReadTo.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0,
                                      "PeekChar: readlen={0}; m_ReadOffset={1}; m_ReadCache.Free={2}", readLen, m_ReadOffset, m_ReadCache.Free);
            }
            if (m_ReadOffset >= readLen)
            {
                return(false);
            }

            if (m_ReadCache.Free <= 1)
            {
                Overflow();
            }

            char[] oneChar = new char[2];
            int    cu      = 0;

            while (cu == 0 && m_ReadOffset < readLen)
            {
                int bu; bool complete;
                try {
                    // Some UTF8 sequences may result in two UTF16 characters being generated.
                    Decoder.Convert(sbuffer.Serial.ReadBuffer.Array,
                                    sbuffer.Serial.ReadBuffer.ToArrayIndex(m_ReadOffset),
                                    1, oneChar, 0, 1, false, out bu, out cu, out complete);
                } catch (System.ArgumentException ex) {
                    if (!ex.ParamName.Equals("chars"))
                    {
                        throw;
                    }
                    Decoder.Convert(sbuffer.Serial.ReadBuffer.Array,
                                    sbuffer.Serial.ReadBuffer.ToArrayIndex(m_ReadOffset),
                                    1, oneChar, 0, 2, false, out bu, out cu, out complete);
                }
                m_ReadOffset += bu;
            }
            if (cu == 0)
            {
                return(false);
            }

            m_ReadCache.Append(oneChar, 0, cu);
            m_ReadOffsets.Append(m_ReadOffset - m_LastChar);
            if (cu > 1)
            {
                m_ReadOffsets.Append(0);
            }
            m_LastChar = m_ReadOffset;
            return(true);
        }
Exemple #3
0
            /// <summary>
            /// Reads a single character from the byte buffer provided, putting it into the internal cache
            /// </summary>
            /// <param name="buffer">The buffer to read.</param>
            /// <returns><c>true</c> if a character was found; <c>false</c> otherwise.</returns>
            /// <exception cref="System.ArgumentNullException">buffer may not be null.</exception>
            /// <remarks>
            /// It is expected that this method be used iteratively to find a single character.
            /// </remarks>
            public bool PeekChar(CircularBuffer <byte> buffer)
            {
                int bytesRead = 0;

                if (buffer == null)
                {
                    throw new ArgumentNullException("buffer");
                }
                int readlen = buffer.Length;

                if (m_ReadOffset >= readlen)
                {
                    return(false);
                }

                char[] oneChar = new char[2];
                int    cu      = 0;

                while (cu == 0 && m_ReadOffset < readlen)
                {
                    int bu; bool complete;
                    try {
                        // Some UTF8 sequences may result in two UTF16 characters being generated.
                        Decoder.Convert(buffer.Array, buffer.ToArrayIndex(m_ReadOffset), 1, oneChar, 0, 1, false, out bu, out cu, out complete);
                    } catch (System.ArgumentException ex) {
                        if (!ex.ParamName.Equals("chars"))
                        {
                            throw;
                        }
                        Decoder.Convert(buffer.Array, buffer.ToArrayIndex(m_ReadOffset), 1, oneChar, 0, 2, false, out bu, out cu, out complete);
                    }
                    bytesRead += bu;
                    m_ReadOffset++;
                }
                if (cu == 0)
                {
                    return(false);
                }

                if (m_ReadCache.Free <= 1)
                {
                    Overflow();
                }

                m_ReadCache.Append(oneChar, 0, cu);
                m_ReadOffsets.Append(m_ReadOffset - m_LastChar);
                if (cu > 1)
                {
                    m_ReadOffsets.Append(0);
                }
                m_LastChar = m_ReadOffset;
                return(true);
            }
Exemple #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldEvictElementsWhenClearing()
        public virtual void ShouldEvictElementsWhenClearing()
        {
            // given
            CircularBuffer <int> buffer = new CircularBuffer <int>(3);

            int?[] evictions = new int?[3];
            buffer.Append(1);
            buffer.Append(2);

            // when
            buffer.Clear(evictions);

            // then
            assertEquals(0, buffer.Size());
            assertArrayEquals(evictions, new int?[] { 1, 2, null });
        }
Exemple #5
0
        private void ComprehensivelyTestAppendRead(int capacity, int operations)
        {
            CircularBuffer <int> buffer  = new CircularBuffer <int>(capacity);
            List <int>           numbers = new List <int>(operations);

            // when: adding a bunch of random numbers
            for (int i = 0; i < operations; i++)
            {
                int number = _tlr.Next();
                numbers.Add(number);
                buffer.Append(number);
            }

            // then: these should have been knocked out
            for (int i = 0; i < operations - capacity; i++)
            {
                numbers.RemoveAt(0);
            }

            // and these should remain
            int i = 0;

            while (numbers.Count > 0)
            {
                assertEquals(numbers.RemoveAt(0), buffer.Read(i++));
            }
        }
Exemple #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void removeShouldReturnNullWhenEmpty()
        public virtual void RemoveShouldReturnNullWhenEmpty()
        {
            // given
            CircularBuffer <object> buffer = new CircularBuffer <object>(3);

            buffer.Append(1L);
            buffer.Append(2L);
            buffer.Append(3L);

            // when
            buffer.Remove();
            buffer.Remove();
            buffer.Remove();

            // then
            assertNull(buffer.Remove());
        }
Exemple #7
0
        public TimeBlockSequence(Phase startPhase, Phase endPhase, int bufferCapacity = DEFAULT_BUFFER_CAPACITY)
        {
            StartPhase = startPhase;
            EndPhase   = endPhase;
            TimeBlock <T> initialTimeBlock = new TimeBlock <T>(startPhase, endPhase);

            timeBlockBuffer = new CircularBuffer <TimeBlock <T> >(bufferCapacity);
            timeBlockBuffer.Append(initialTimeBlock);
        }
Exemple #8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNullClearedElements()
        public virtual void ShouldNullClearedElements()
        {
            // given
            CircularBuffer <int> buffer = new CircularBuffer <int>(3);

            int?[] evictions = new int?[3];
            buffer.Append(1);
            buffer.Append(2);
            buffer.Append(3);

            // when
            buffer.Clear(evictions);

            // then
            assertNull(buffer.Read(0));
            assertNull(buffer.Read(1));
            assertNull(buffer.Read(2));
        }
Exemple #9
0
 int ISerialBufferStreamData.Write(byte[] buffer, int offset, int count)
 {
     lock (m_WriteLock) {
         int bytes = m_WriteBuffer.Append(buffer, offset, count);
         m_WriteBufferNotEmptyEvent.Set();
         m_TxEmptyEvent.Reset();
         if (m_WriteBuffer.Free == 0)
         {
             m_WriteBufferNotFullEvent.Reset();
         }
         OnWriteEvent(this, new EventArgs());
         return(bytes);
     }
 }
Exemple #10
0
            public void OverflowWithUtf32AtEnd()
            {
                ReadToCache rtc = new ReadToCache();

                Random r = new Random();

                byte[] bbuffer = new byte[2048];

                // Ensure that each byte is ASCII for simplicity
                for (int i = 0; i < 1022; i++)
                {
                    bbuffer[i] = (byte)r.Next(48, 126);
                }
                bbuffer[1023] = 0xF3;
                bbuffer[1024] = 0xA0;
                bbuffer[1025] = 0x82;
                bbuffer[1026] = 0x84;
                for (int i = 1027; i < bbuffer.Length; i++)
                {
                    bbuffer[i] = (byte)r.Next(48, 126);
                }

                CircularBuffer <byte> buffer = new CircularBuffer <byte>(4096);

                buffer.Append(bbuffer);

                // We now peek 1023 times, which we shouldn't have an overflow
                for (int i = 0; i < 1023; i++)
                {
                    Assert.That(rtc.PeekChar(buffer), Is.True);
                }

                // Shouldn't overflow, but we've now got 1025 characters
                Assert.That(rtc.PeekChar(buffer), Is.True);

                // With this one we should overflow.
                //  m_ReadOverflow = 1
                //  m_ReadOverflowChar[0] is the first character
                //  m_ReadOverflowUtf32 = false
                Assert.That(rtc.PeekChar(buffer), Is.True);

                // Check that the next overflow is still correct
                //  m_ReadOverflow, m_ReadOverflowChar, m_ReadOverflowUtf32 shouldn't change
                Assert.That(rtc.PeekChar(buffer), Is.True);

                rtc.Reset(true);
            }
Exemple #11
0
            public void OverflowWithUtf32AtBeginning()
            {
                ReadToCache rtc = new ReadToCache();

                Random r = new Random();

                byte[] bbuffer = new byte[2048];

                bbuffer[0] = 0xF3;
                bbuffer[1] = 0xA0;
                bbuffer[2] = 0x82;
                bbuffer[3] = 0x84;
                // Ensure that each byte is ASCII for simplicity
                for (int i = 4; i < bbuffer.Length; i++)
                {
                    bbuffer[i] = (byte)r.Next(48, 126);
                }

                CircularBuffer <byte> buffer = new CircularBuffer <byte>(4096);

                buffer.Append(bbuffer);

                // We now peek 1023 times, which we shouldn't have an overflow
                for (int i = 0; i < 1023; i++)
                {
                    Assert.That(rtc.PeekChar(buffer), Is.True);
                }

                // 1024 times, fills the buffer. No overflow expected here.
                Assert.That(rtc.PeekChar(buffer), Is.True);

                // With this one we should overflow.
                //  m_ReadOverflow = 4
                //  m_ReadOverflowChar[0] and [1] is the first character
                //  m_ReadOverflowUtf32 = true
                Assert.That(rtc.PeekChar(buffer), Is.True);

                // Check that the next overflow is still correct
                //  m_ReadOverflow, m_ReadOverflowChar, m_ReadOverflowUtf32 shouldn't change
                Assert.That(rtc.PeekChar(buffer), Is.True);

                rtc.Reset(true);
            }
        public void CircularBuffer_AppendBuffer()
        {
            CircularBuffer<char> cb1 = new CircularBuffer<char>(20);
            CircularBuffer<char> cb2 = new CircularBuffer<char>(20);

            // Read is one chunk, write is one chunk
            cb2.Produce(3);
            cb2.Append("123456789012345".ToCharArray());
            cb2.Consume(3);

            cb1.Append(cb2);
            Assert.AreEqual(15, cb1.Length);
            Assert.AreEqual(5, cb1.Free);
            Assert.AreEqual("123456789012345", cb1.GetString());

            // Write is one chunk, but read is two chunks
            cb1.Reset();
            cb2.Reset();
            cb2.Produce(15);
            cb2.Consume(14);
            cb2.Append("123456789012345".ToCharArray());
            cb2.Consume(1);

            cb1.Append(cb2);
            Assert.AreEqual(15, cb1.Length);
            Assert.AreEqual(5, cb1.Free);
            Assert.AreEqual("123456789012345", cb1.GetString());

            // Write is two chunks, read is one chunk
            cb1.Reset();
            cb2.Reset();
            cb1.Produce(10);
            cb1.Consume(9);
            cb2.Append("123456789012345".ToCharArray());

            cb1.Append(cb2);
            cb1.Consume(1);
            Assert.AreEqual(15, cb1.Length);
            Assert.AreEqual(5, cb1.Free);
            Assert.AreEqual("123456789012345", cb1.GetString());

            // Write is two chunks, read is two chunks, readlength < writelength
            cb1.Reset();
            cb2.Reset();
            cb1.Produce(10);
            cb1.Consume(9);
            cb2.Produce(15);
            cb2.Consume(14);
            cb2.Append("123456789012345".ToCharArray());
            cb2.Consume(1);

            cb1.Append(cb2);
            cb1.Consume(1);
            Assert.AreEqual(15, cb1.Length);
            Assert.AreEqual(5, cb1.Free);
            Assert.AreEqual("123456789012345", cb1.GetString());

            // Write is two chunks, read is two chunks, readlength > writelength
            cb1.Reset();
            cb2.Reset();
            cb1.Produce(10);
            cb1.Consume(9);
            cb2.Produce(7);
            cb2.Consume(6);
            cb2.Append("123456789012345".ToCharArray());
            cb2.Consume(1);

            cb1.Append(cb2);
            cb1.Consume(1);
            Assert.AreEqual(15, cb1.Length);
            Assert.AreEqual(5, cb1.Free);
            Assert.AreEqual("123456789012345", cb1.GetString());
        }
        public void CircularBuffer_AppendArray()
        {
            CircularBuffer<char> cb = new CircularBuffer<char>(20);
            cb.Produce(10);
            cb.Consume(9);

            Assert.AreEqual(1, cb.Length);
            Assert.AreEqual(19, cb.Free);

            cb.Append("ABCDEFGHIJKLMN".ToCharArray(), 0, 14);
            Assert.AreEqual(15, cb.Length);
            Assert.AreEqual(5, cb.Free);

            Assert.AreEqual("ABCDEFGHIJKLMN", cb.GetString(1, 14));
            cb.Consume(15);
            Assert.AreEqual(0, cb.Length);
            Assert.AreEqual(20, cb.Free);

            cb.Append("12345678901234567890".ToCharArray());
            Assert.AreEqual(0, cb.Free);
            Assert.AreEqual(20, cb.Length);
            Assert.AreEqual("12345678901234567890", cb.GetString());
        }
Exemple #14
0
 public void AppendTimeBlock(TimeBlock <T> timeBlockToAppend)
 {
     EndPhase = timeBlockToAppend.EndPhase;
     timeBlockBuffer.Append(timeBlockToAppend);
 }