private void Overflow() { // If we haven't overflowed our m_ReadCache yet, then we record the first character and its // offset. So if we have to reset the cache, we can reset the decoder and put that first // character into the read buffer. // // If we have already overflowed, then we just continue doing so. m_ReadOffset is the number // of bytes we've consumed independent of if we've overflowed or not. int consume = 1; if (m_ReadOverflow == -1) { m_ReadOverflowChar[0] = m_ReadCache[0]; m_ReadOverflow = m_ReadOffsets[0]; if (m_ReadOffsets[1] == 0) { m_ReadOverflowUtf32 = true; m_ReadOverflowChar[1] = m_ReadCache[1]; consume = 2; } SerialTrace.TraceRT.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "Overflow: Capture UTF32={0}; Consumed{1}", m_ReadOverflowUtf32, consume); } m_ReadCache.Consume(consume); m_ReadOffsets.Consume(consume); }
void ISerialBufferStreamData.ReadConsume(int count) { lock (m_ReadLock) { m_ReadBuffer.Consume(count); if (m_ReadBuffer.Length == 0) { m_ReadBufferNotEmptyEvent.Reset(); } m_ReadBufferNotFullEvent.Set(); } }
private void Overflow() { // If we haven't overflowed our m_ReadCache yet, then we record the first character and its // offset. So if we have to reset the cache, we can reset the decoder and put that first // character into the read buffer. // // If we have already overflowed, then we just continue doing so. m_ReadOffset is the number // of bytes we've consumed independent of if we've overflowed or not. int consume = 1; if (m_ReadOverflow == -1) { m_ReadOverflowChar[0] = m_ReadCache[0]; m_ReadOverflow = m_ReadOffsets[0]; if (m_ReadOffsets[1] == 0) { m_ReadOverflowUtf32 = true; m_ReadOverflowChar[1] = m_ReadCache[1]; consume = 2; } } m_ReadCache.Consume(consume); m_ReadOffsets.Consume(consume); }
public void consomeExec() { while (true) { ThrWork tw = buf.Consume(); tw(); } }
void ISerialBufferSerialData.WriteBufferConsume(int count) { lock (m_WriteLock) { m_WriteBuffer.Consume(count); m_WriteBufferNotFullEvent.Set(); if (m_WriteBuffer.Length == 0) { m_WriteBufferNotEmptyEvent.Reset(); } } }
public void CircularBuffer_ReadBlock() { CircularBuffer<byte> cb = new CircularBuffer<byte>(50); // Move the pointer to the middle cb.Produce(25); cb.Consume(25); // Now allocate all space cb.Produce(25); cb.Produce(25); Assert.AreEqual(25, cb.ReadLength); Assert.AreEqual(25, cb.GetReadBlock(0)); Assert.AreEqual(20, cb.GetReadBlock(5)); Assert.AreEqual(1, cb.GetReadBlock(24)); Assert.AreEqual(25, cb.GetReadBlock(25)); Assert.AreEqual(20, cb.GetReadBlock(30)); Assert.AreEqual(1, cb.GetReadBlock(49)); Assert.AreEqual(0, cb.GetReadBlock(50)); }
public void CircularBufferExt_EncoderConvert_Full1() { Encoding enc = Encoding.GetEncoding("UTF-8"); Encoder e = enc.GetEncoder(); char[] c = new char[] { 'A', 'B', '€', 'C' }; CircularBuffer<byte> cb = new CircularBuffer<byte>(20); int bu; int cu; bool complete; cb.Produce(17); cb.Consume(3); e.Convert(c, 0, 4, cb, false, out cu, out bu, out complete); Assert.AreEqual(4, cu); Assert.AreEqual(6, bu); Assert.AreEqual(20, cb.Length); Assert.IsTrue(complete); Assert.AreEqual(0x41, cb[14]); Assert.AreEqual(0x42, cb[15]); Assert.AreEqual(0xE2, cb[16]); Assert.AreEqual(0x82, cb[17]); Assert.AreEqual(0xAC, cb[18]); Assert.AreEqual(0x43, cb[19]); }
public void CircularBufferExt_DecoderConvert3_BoundariesWithFlush() { Encoding enc = Encoding.GetEncoding("UTF-8"); Decoder d = enc.GetDecoder(); byte[] m = new byte[] { 0x41, 0xE2, 0x82, 0xAC }; CircularBuffer<byte> cb; CircularBuffer<char> cc; int bu; int cu; bool complete; cb = new CircularBuffer<byte>(m, 0, 3); cc = new CircularBuffer<char>(20); d.Convert(cb, cc, true, out bu, out cu, out complete); Assert.AreEqual(3, bu); Assert.AreEqual(2, cu); Assert.AreEqual(0, cb.Length); Assert.AreEqual(2, cc.Length); Assert.AreEqual('A', cc[0]); Assert.AreEqual((char)0xFFFD, cc[1]); Assert.IsTrue(complete); cb = new CircularBuffer<byte>(m, 0, 3); cc = new CircularBuffer<char>(20); cc.Produce(19); cc.Consume(18); d.Convert(cb, cc, true, out bu, out cu, out complete); cc.Consume(1); Assert.AreEqual(3, bu); Assert.AreEqual(2, cu); Assert.AreEqual(0, cb.Length); Assert.AreEqual(2, cc.Length); Assert.AreEqual('A', cc[0]); Assert.AreEqual((char)0xFFFD, cc[1]); Assert.IsTrue(complete); }
public void CircularBufferExt_GetStringOffsetLength() { CircularBuffer<char> cb = new CircularBuffer<char>(15); for (int i = 0; i < cb.Capacity; i++) { cb.Array[i] = (char)((int)'A' + i); } cb.Produce(10); Assert.AreEqual("ABCDEFGHIJ", cb.GetString(0, 10)); Assert.AreEqual("ABCDEFGHIJ", cb.GetString(0, 20)); Assert.AreEqual("", cb.GetString(0, 0)); Assert.AreEqual("FGHIJ", cb.GetString(5, 5)); Assert.AreEqual("BCDEFGHIJ", cb.GetString(1, 9)); Assert.AreEqual("BCDEFGH", cb.GetString(1, 7)); Assert.AreEqual("", cb.GetString(5, 0)); cb.Consume(5); Assert.AreEqual("FGHIJ", cb.GetString(0, 10)); Assert.AreEqual("FGHIJ", cb.GetString(0, 5)); Assert.AreEqual("HIJ", cb.GetString(2, 3)); Assert.AreEqual("", cb.GetString(5, 0)); Assert.AreEqual("", cb.GetString(5, 1)); cb.Produce(8); Assert.AreEqual("FGHIJKLMNOABC", cb.GetString(0, 13)); Assert.AreEqual("FGHIJ", cb.GetString(0, 5)); Assert.AreEqual("KLMNOABC", cb.GetString(5, 13)); Assert.AreEqual("KLMNOABC", cb.GetString(5, 8)); Assert.AreEqual("KLMNO", cb.GetString(5, 5)); Assert.AreEqual("ABC", cb.GetString(10, 3)); cb.Consume(13); Assert.AreEqual("", cb.GetString(0, 0)); Assert.AreEqual("", cb.GetString(5, 15)); Assert.AreEqual("", cb.GetString(10, 20)); Assert.AreEqual("", cb.GetString(10, 1)); cb = null; Assert.IsNull(cb.GetString(0, 5)); Assert.IsNull(cb.GetString(10, 0)); Assert.IsNull(cb.GetString(5, 15)); Assert.IsNull(cb.GetString(2, 20)); }
public void CircularBuffer_Revert() { CircularBuffer<byte> cb = new CircularBuffer<byte>(50); // Move the pointer to the middle cb.Produce(25); cb.Consume(25); // Now allocate all space cb.Produce(25); cb.Produce(25); Assert.AreEqual(25, cb.Start); Assert.AreEqual(25, cb.ReadLength); Assert.AreEqual(25, cb.End); Assert.AreEqual(0, cb.WriteLength); cb.Revert(5); Assert.AreEqual(25, cb.Start); Assert.AreEqual(25, cb.ReadLength); Assert.AreEqual(20, cb.End); Assert.AreEqual(5, cb.WriteLength); cb.Revert(20); Assert.AreEqual(25, cb.Start); Assert.AreEqual(25, cb.ReadLength); Assert.AreEqual(0, cb.End); Assert.AreEqual(25, cb.WriteLength); cb.Revert(20); Assert.AreEqual(25, cb.Start); Assert.AreEqual(5, cb.ReadLength); Assert.AreEqual(30, cb.End); Assert.AreEqual(20, cb.WriteLength); }
public void CircularBufferExt_DecoderConvert2_IncompleteBuff() { Encoding enc = Encoding.GetEncoding("UTF-8"); Decoder d = enc.GetDecoder(); byte[] m = new byte[] { 0xAC, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0xE2, 0x82 }; char[] c = new char[30]; CircularBuffer<byte> cb = new CircularBuffer<byte>(m, 16, 13); CircularBuffer<char> cc = new CircularBuffer<char>(c, 5, 0); int bu; int cu; bool complete; // Based on the test "Decoder_IncompleteBuff" d.Convert(cb, cc, cc.Free, false, out bu, out cu, out complete); Assert.IsTrue(complete); Assert.AreEqual(13, bu); Assert.AreEqual(0, cb.Length); Assert.AreEqual(12, cu); Assert.AreEqual("OPQRSTUVWXYZ", cc.GetString()); cc.Consume(cu); cb.Produce(17); d.Convert(cb, cc, cc.Free, false, out bu, out cu, out complete); Assert.IsTrue(complete); Assert.AreEqual(17, bu); Assert.AreEqual(0, cb.Length); Assert.AreEqual(16, cu); Assert.AreEqual("€@ABCDEFGHIJKLMN", cc.GetString()); }
public void CircularBufferExt_EncoderConvert() { Encoding enc = Encoding.GetEncoding("UTF-8"); Encoder e = enc.GetEncoder(); char[] c = new char[] { 'A', 'B', '€', 'C' }; CircularBuffer<byte> cb = new CircularBuffer<byte>(20); int bu; int cu; bool complete; e.Convert(c, 0, 3, cb, false, out cu, out bu, out complete); Assert.AreEqual(3, cu); Assert.AreEqual(5, bu); Assert.AreEqual(5, cb.Length); Assert.IsTrue(complete); e.Reset(); cb.Reset(); cb.Produce(17); cb.Consume(17); e.Convert(c, 0, 4, cb, false, out cu, out bu, out complete); Assert.AreEqual(4, cu); Assert.AreEqual(6, bu); Assert.AreEqual(6, cb.Length); Assert.IsTrue(complete); }
public void CircularBuffer_ReadWrite() { CircularBuffer<byte> cb = new CircularBuffer<byte>(50); cb.Produce(25); cb.Consume(25); cb.Produce(50); byte[] rd = new byte[50]; Random r = new Random(); r.NextBytes(rd); for (int i = 0; i < rd.Length; i++) { cb[i] = rd[i]; } for (int i = 0; i < rd.Length; i++) { Assert.AreEqual(rd[i], cb[i], "Index " + i.ToString() + " doesn't match"); } }
public void CircularBuffer_ProduceConsume() { CircularBuffer<byte> cb = new CircularBuffer<byte>(50); Assert.AreEqual(50, cb.Capacity); // Initial state Assert.AreEqual(0, cb.Start); Assert.AreEqual(0, cb.Length); Assert.AreEqual(0, cb.ReadLength); Assert.AreEqual(0, cb.End); Assert.AreEqual(50, cb.Free); Assert.AreEqual(50, cb.WriteLength); // Test 1: Allocate 50 bytes cb.Produce(50); Assert.AreEqual(0, cb.Start); Assert.AreEqual(50, cb.Length); Assert.AreEqual(50, cb.ReadLength); Assert.AreEqual(0, cb.End); Assert.AreEqual(0, cb.Free); Assert.AreEqual(0, cb.WriteLength); // Test 2: Free 50 bytes cb.Consume(50); Assert.AreEqual(0, cb.Start); Assert.AreEqual(0, cb.Length); Assert.AreEqual(0, cb.ReadLength); Assert.AreEqual(0, cb.End); Assert.AreEqual(50, cb.Free); Assert.AreEqual(50, cb.WriteLength); // Test 3: Allocate 25 bytes cb.Produce(25); Assert.AreEqual(0, cb.Start); Assert.AreEqual(25, cb.Length); Assert.AreEqual(25, cb.ReadLength); Assert.AreEqual(25, cb.End); Assert.AreEqual(25, cb.Free); Assert.AreEqual(25, cb.WriteLength); // Test 4: Free 24 bytes cb.Consume(24); Assert.AreEqual(24, cb.Start); Assert.AreEqual(1, cb.Length); Assert.AreEqual(1, cb.ReadLength); Assert.AreEqual(25, cb.End); Assert.AreEqual(49, cb.Free); Assert.AreEqual(25, cb.WriteLength); // Test 5: Alocate 49 bytes cb.Produce(49); Assert.AreEqual(24, cb.Start); Assert.AreEqual(50, cb.Length); Assert.AreEqual(26, cb.ReadLength); Assert.AreEqual(24, cb.End); Assert.AreEqual(0, cb.Free); Assert.AreEqual(0, cb.WriteLength); // Test 6: Reset cb.Reset(); Assert.AreEqual(0, cb.Start); Assert.AreEqual(0, cb.Length); Assert.AreEqual(0, cb.ReadLength); Assert.AreEqual(0, cb.End); Assert.AreEqual(50, cb.Free); Assert.AreEqual(50, cb.WriteLength); // Test 7: Test full wrapping around cb.Produce(25); cb.Consume(25); cb.Produce(50); Assert.AreEqual(25, cb.Start); Assert.AreEqual(50, cb.Length); Assert.AreEqual(25, cb.ReadLength); Assert.AreEqual(25, cb.End); Assert.AreEqual(0, cb.Free); Assert.AreEqual(0, cb.WriteLength); // Test 8: Free all data cb.Consume(50); Assert.AreEqual(25, cb.Start); Assert.AreEqual(0, cb.Length); Assert.AreEqual(0, cb.ReadLength); Assert.AreEqual(25, cb.End); Assert.AreEqual(50, cb.Free); Assert.AreEqual(25, cb.WriteLength); }
public void CircularBuffer_Indexing() { CircularBuffer<byte> cb = new CircularBuffer<byte>(50); // Write into the array directly for (int i = 0; i < cb.Array.Length; i++) { cb.Array[i] = (byte)i; } cb.Produce(50); Assert.AreEqual(50, cb.Length); Assert.AreEqual(0, cb.Start); // Access the array using the indexer for (int i = 0; i < cb.Length; i++) { Assert.AreEqual(i, cb[i]); } cb.Consume(25); cb.Produce(25); // Now the start is in the middle Assert.AreEqual(25, cb.Start); Assert.AreEqual(50, cb.Length); for (int i = 0; i < cb.Length; i++) { Assert.AreEqual((i + 25) % 50, cb[i], "Index " + i.ToString()); } for (int i = 0; i < cb.Length; i++) { Assert.AreEqual((i + 25) % 50, cb.Array[cb.ToArrayIndex(i)], "Index " + i.ToString()); } }
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()); }
public void CircularBufferExt_GetStringSimple() { CircularBuffer<char> cb = new CircularBuffer<char>(15); for (int i = 0; i < cb.Capacity; i++) { cb.Array[i] = (char)((int)'A' + i); } cb.Produce(10); Assert.AreEqual("ABCDEFGHIJ", cb.GetString()); cb.Consume(5); Assert.AreEqual("FGHIJ", cb.GetString()); cb.Produce(8); Assert.AreEqual("FGHIJKLMNOABC", cb.GetString()); cb.Consume(13); Assert.AreEqual("", cb.GetString()); cb = null; Assert.IsNull(cb.GetString()); }