/** * Test getting / setting IOVec work. */ void IOQueueTest::testIOVec() { uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; m_buffer->Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, m_buffer->Size()); OLA_ASSERT_FALSE(m_buffer->Empty()); int iocnt; const struct IOVec *vector = m_buffer->AsIOVec(&iocnt); OLA_ASSERT_EQ(9u, SumLengthOfIOVec(vector, iocnt)); OLA_ASSERT_EQ(1, iocnt); m_buffer->FreeIOVec(vector); // try a smaller block size MemoryBlockPool pool(4); IOQueue queue(&pool); m_buffer.reset(new IOQueue(&pool)); queue.Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, queue.Size()); vector = queue.AsIOVec(&iocnt); OLA_ASSERT_EQ(3, iocnt); OLA_ASSERT_EQ(9u, SumLengthOfIOVec(vector, iocnt)); queue.FreeIOVec(vector); }
/** * Test that Peek behaves */ void IOQueueTest::testPeek() { uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; m_buffer->Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, m_buffer->Size()); OLA_ASSERT_FALSE(m_buffer->Empty()); const unsigned int DATA_SIZE = 20; uint8_t *output_data = new uint8_t[DATA_SIZE]; // peek at the first four bytes unsigned int output_size = m_buffer->Peek(output_data, 4); ASSERT_DATA_EQUALS(__LINE__, data1, 4, output_data, output_size); OLA_ASSERT_EQ(9u, m_buffer->Size()); // peek at the first 9 bytes output_size = m_buffer->Peek(output_data, 9); ASSERT_DATA_EQUALS(__LINE__, data1, 9, output_data, output_size); OLA_ASSERT_EQ(9u, m_buffer->Size()); // peek at more bytes that exist in the buffer output_size = m_buffer->Peek(output_data, DATA_SIZE); OLA_ASSERT_EQ(9u, output_size); ASSERT_DATA_EQUALS(__LINE__, data1, sizeof(data1), output_data, output_size); OLA_ASSERT_EQ(9u, m_buffer->Size()); // Now try a buffer with smaller blocks MemoryBlockPool pool(4); IOQueue queue(&pool); queue.Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, queue.Size()); // peek at he same amount as the first block size output_size = queue.Peek(output_data, 4); ASSERT_DATA_EQUALS(__LINE__, data1, 4, output_data, output_size); OLA_ASSERT_EQ(9u, queue.Size()); OLA_ASSERT_FALSE(queue.Empty()); // peek at data from more than one block output_size = queue.Peek(output_data, 6); ASSERT_DATA_EQUALS(__LINE__, data1, 6, output_data, output_size); OLA_ASSERT_EQ(9u, queue.Size()); OLA_ASSERT_FALSE(queue.Empty()); // peek at data on the two block boundry output_size = queue.Peek(output_data, 8); ASSERT_DATA_EQUALS(__LINE__, data1, 8, output_data, output_size); OLA_ASSERT_EQ(9u, queue.Size()); OLA_ASSERT_FALSE(queue.Empty()); // peek at all the data output_size = queue.Peek(output_data, 9); ASSERT_DATA_EQUALS(__LINE__, data1, 9, output_data, output_size); OLA_ASSERT_EQ(9u, queue.Size()); OLA_ASSERT_FALSE(queue.Empty()); // peek at more data than what exists output_size = queue.Peek(output_data, DATA_SIZE); OLA_ASSERT_EQ(9u, output_size); ASSERT_DATA_EQUALS(__LINE__, data1, 9, output_data, output_size); OLA_ASSERT_EQ(9u, queue.Size()); OLA_ASSERT_FALSE(queue.Empty()); delete[] output_data; }
/** * Test that Pop behaves */ void IOQueueTest::testPop() { uint8_t data1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; m_buffer->Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, m_buffer->Size()); OLA_ASSERT_FALSE(m_buffer->Empty()); m_buffer->Pop(9); OLA_ASSERT_EQ(0u, m_buffer->Size()); OLA_ASSERT_TRUE(m_buffer->Empty()); // try to pop off more data m_buffer->Pop(1); OLA_ASSERT_EQ(0u, m_buffer->Size()); OLA_ASSERT_TRUE(m_buffer->Empty()); // add the data back, then try to pop off more than we have m_buffer->Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, m_buffer->Size()); OLA_ASSERT_FALSE(m_buffer->Empty()); m_buffer->Pop(10); OLA_ASSERT_EQ(0u, m_buffer->Size()); OLA_ASSERT_TRUE(m_buffer->Empty()); // one more time m_buffer->Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, m_buffer->Size()); // Now try a buffer with smaller blocks MemoryBlockPool pool(4); IOQueue queue(&pool); queue.Write(data1, sizeof(data1)); OLA_ASSERT_EQ(9u, queue.Size()); // pop the same amount as the first block size queue.Pop(4); OLA_ASSERT_EQ(5u, queue.Size()); OLA_ASSERT_FALSE(queue.Empty()); // now pop more than the buffer size queue.Pop(6); OLA_ASSERT_EQ(0u, queue.Size()); OLA_ASSERT_TRUE(queue.Empty()); // test the block boundry uint8_t *output_data = new uint8_t[4]; m_buffer.reset(new IOQueue(&pool)); queue.Write(data1, 4); OLA_ASSERT_EQ(4u, queue.Size()); unsigned int output_size = queue.Peek(output_data, 4); ASSERT_DATA_EQUALS(__LINE__, data1, 4, output_data, output_size); queue.Pop(4); OLA_ASSERT_TRUE(queue.Empty()); // now add some more data queue.Write(data1 + 4, 4); OLA_ASSERT_EQ(4u, queue.Size()); output_size = queue.Peek(output_data, 4); ASSERT_DATA_EQUALS(__LINE__, data1 + 4, 4, output_data, output_size); queue.Pop(4); OLA_ASSERT_TRUE(queue.Empty()); delete[] output_data; }