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/. */
6 #include "gtest/gtest.h"
7 #include "BufferReader.h"
9 using namespace mozilla
;
11 TEST(BufferReader
, ReaderCursor
)
13 // Allocate a buffer and create a BufferReader.
14 const size_t BUFFER_SIZE
= 10;
15 uint8_t buffer
[BUFFER_SIZE
] = {0};
17 const uint8_t* const HEAD
= reinterpret_cast<uint8_t*>(buffer
);
18 const uint8_t* const TAIL
= HEAD
+ BUFFER_SIZE
;
20 BufferReader
reader(HEAD
, BUFFER_SIZE
);
21 ASSERT_EQ(reader
.Offset(), static_cast<size_t>(0));
22 ASSERT_EQ(reader
.Peek(BUFFER_SIZE
), HEAD
);
24 // Keep reading to the end, and make sure the final read failed.
25 const size_t READ_SIZE
= 4;
26 ASSERT_NE(BUFFER_SIZE
% READ_SIZE
, static_cast<size_t>(0));
27 for (const uint8_t* ptr
= reader
.Peek(0); ptr
!= nullptr;
28 ptr
= reader
.Read(READ_SIZE
)) {
31 // Check the reading cursor of the BufferReader is correct
32 // after reading and seeking.
33 const uint8_t* tail
= reader
.Peek(0);
34 const uint8_t* head
= reader
.Seek(0);
36 EXPECT_EQ(head
, HEAD
);
37 EXPECT_EQ(tail
, TAIL
);
40 TEST(BufferReader
, UnalignedRead
)
42 // Allocate a buffer and create a BufferReader.
43 const size_t BUFFER_SIZE
= 5;
44 uint8_t buffer
[BUFFER_SIZE
] = {0};
46 const uint8_t* const HEAD
= reinterpret_cast<uint8_t*>(buffer
);
48 BufferReader
reader(HEAD
, BUFFER_SIZE
);
49 // adjust the offset so that it's unaligned
51 // read an int which needs 4 byte alignment
52 reader
.ReadType
<uint32_t>();