1 // Copyright 2017 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 "components/zucchini/buffer_sink.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <vector>
11
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace zucchini {
15
16 constexpr uint8_t kUninit = 0xFF;
17
18 class BufferSinkTest : public testing::Test {
19 protected:
BufferSinkTest()20 BufferSinkTest()
21 : buffer_(10, kUninit), sink_(buffer_.data(), buffer_.size()) {}
22
23 std::vector<uint8_t> buffer_;
24 BufferSink sink_;
25 };
26
TEST_F(BufferSinkTest,PutValue)27 TEST_F(BufferSinkTest, PutValue) {
28 EXPECT_EQ(size_t(10), sink_.Remaining());
29
30 EXPECT_TRUE(sink_.PutValue(uint32_t(0x76543210)));
31 EXPECT_EQ(size_t(6), sink_.Remaining());
32
33 EXPECT_TRUE(sink_.PutValue(uint32_t(0xFEDCBA98)));
34 EXPECT_EQ(size_t(2), sink_.Remaining());
35
36 EXPECT_FALSE(sink_.PutValue(uint32_t(0x00)));
37 EXPECT_EQ(size_t(2), sink_.Remaining());
38
39 EXPECT_TRUE(sink_.PutValue(uint16_t(0x0010)));
40 EXPECT_EQ(size_t(0), sink_.Remaining());
41
42 // Assuming little-endian architecture.
43 EXPECT_EQ(std::vector<uint8_t>(
44 {0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE, 0x10, 0x00}),
45 buffer_);
46 }
47
TEST_F(BufferSinkTest,PutRange)48 TEST_F(BufferSinkTest, PutRange) {
49 std::vector<uint8_t> range = {0x10, 0x32, 0x54, 0x76, 0x98, 0xBA,
50 0xDC, 0xFE, 0x10, 0x00, 0x42};
51
52 EXPECT_EQ(size_t(10), sink_.Remaining());
53 EXPECT_FALSE(sink_.PutRange(range.begin(), range.end()));
54 EXPECT_EQ(size_t(10), sink_.Remaining());
55
56 EXPECT_TRUE(sink_.PutRange(range.begin(), range.begin() + 8));
57 EXPECT_EQ(size_t(2), sink_.Remaining());
58 EXPECT_EQ(std::vector<uint8_t>({0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC,
59 0xFE, kUninit, kUninit}),
60 buffer_);
61
62 EXPECT_FALSE(sink_.PutRange(range.begin(), range.begin() + 4));
63 EXPECT_EQ(size_t(2), sink_.Remaining());
64
65 // range is not written
66 EXPECT_EQ(std::vector<uint8_t>({0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC,
67 0xFE, kUninit, kUninit}),
68 buffer_);
69 }
70
71 } // namespace zucchini
72