1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <string> 6 #include <vector> 7 8 #include "content/browser/speech/chunked_byte_buffer.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace content { 12 13 typedef std::vector<uint8> ByteVector; 14 15 TEST(ChunkedByteBufferTest, BasicTest) { 16 ChunkedByteBuffer buffer; 17 18 const uint8 kChunks[] = { 19 0x00, 0x00, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04, // Chunk 1: 4 bytes 20 0x00, 0x00, 0x00, 0x02, 0x05, 0x06, // Chunk 2: 2 bytes 21 0x00, 0x00, 0x00, 0x01, 0x07 // Chunk 3: 1 bytes 22 }; 23 24 EXPECT_EQ(0U, buffer.GetTotalLength()); 25 EXPECT_FALSE(buffer.HasChunks()); 26 27 // Append partially chunk 1. 28 buffer.Append(kChunks, 2); 29 EXPECT_EQ(2U, buffer.GetTotalLength()); 30 EXPECT_FALSE(buffer.HasChunks()); 31 32 // Complete chunk 1. 33 buffer.Append(kChunks + 2, 6); 34 EXPECT_EQ(8U, buffer.GetTotalLength()); 35 EXPECT_TRUE(buffer.HasChunks()); 36 37 // Append fully chunk 2. 38 buffer.Append(kChunks + 8, 6); 39 EXPECT_EQ(14U, buffer.GetTotalLength()); 40 EXPECT_TRUE(buffer.HasChunks()); 41 42 // Remove and check chunk 1. 43 scoped_ptr<ByteVector> chunk; 44 chunk = buffer.PopChunk(); 45 EXPECT_TRUE(chunk != NULL); 46 EXPECT_EQ(4U, chunk->size()); 47 EXPECT_EQ(0, std::char_traits<uint8>::compare(kChunks + 4, 48 &(*chunk)[0], 49 chunk->size())); 50 EXPECT_EQ(6U, buffer.GetTotalLength()); 51 EXPECT_TRUE(buffer.HasChunks()); 52 53 // Read and check chunk 2. 54 chunk = buffer.PopChunk(); 55 EXPECT_TRUE(chunk != NULL); 56 EXPECT_EQ(2U, chunk->size()); 57 EXPECT_EQ(0, std::char_traits<uint8>::compare(kChunks + 12, 58 &(*chunk)[0], 59 chunk->size())); 60 EXPECT_EQ(0U, buffer.GetTotalLength()); 61 EXPECT_FALSE(buffer.HasChunks()); 62 63 // Append fully chunk 3. 64 buffer.Append(kChunks + 14, 5); 65 EXPECT_EQ(5U, buffer.GetTotalLength()); 66 67 // Remove and check chunk 3. 68 chunk = buffer.PopChunk(); 69 EXPECT_TRUE(chunk != NULL); 70 EXPECT_EQ(1U, chunk->size()); 71 EXPECT_EQ((*chunk)[0], kChunks[18]); 72 EXPECT_EQ(0U, buffer.GetTotalLength()); 73 EXPECT_FALSE(buffer.HasChunks()); 74 } 75 76 } // namespace content 77