1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "gtest/gtest.h"
7 #include "BufferReader.h"
8 
TEST(BufferReader,ReaderCursor)9 TEST(BufferReader, ReaderCursor)
10 {
11   // Allocate a buffer and create a BufferReader.
12   const size_t BUFFER_SIZE = 10;
13   uint8_t buffer[BUFFER_SIZE] = {0};
14 
15   const uint8_t* const HEAD = reinterpret_cast<uint8_t*>(buffer);
16   const uint8_t* const TAIL = HEAD + BUFFER_SIZE;
17 
18   BufferReader reader(HEAD, BUFFER_SIZE);
19   ASSERT_EQ(reader.Offset(), static_cast<size_t>(0));
20   ASSERT_EQ(reader.Peek(BUFFER_SIZE), HEAD);
21 
22   // Keep reading to the end, and make sure the final read failed.
23   const size_t READ_SIZE = 4;
24   ASSERT_NE(BUFFER_SIZE % READ_SIZE, static_cast<size_t>(0));
25   for (const uint8_t* ptr = reader.Peek(0); ptr != nullptr;
26        ptr = reader.Read(READ_SIZE)) {
27   }
28 
29   // Check the reading cursor of the BufferReader is correct
30   // after reading and seeking.
31   const uint8_t* tail = reader.Peek(0);
32   const uint8_t* head = reader.Seek(0);
33 
34   EXPECT_EQ(head, HEAD);
35   EXPECT_EQ(tail, TAIL);
36 }