1 // Copyright 2015 The Chromium OS 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 <gtest/gtest.h>
6
7 #include "byte_buffer.h"
8
9 namespace {
10
TEST(ByteBuffer,ReadWrite)11 TEST(ByteBuffer, ReadWrite) {
12 struct byte_buffer *b;
13 uint8_t *data;
14 unsigned int data_size;
15
16 b = byte_buffer_create(100);
17 EXPECT_EQ(100, buf_available_bytes(b));
18 EXPECT_EQ(0, buf_queued_bytes(b));
19
20 data = buf_read_pointer_size(b, &data_size);
21 EXPECT_NE((void *)NULL, data);
22 EXPECT_EQ(0, data_size);
23
24 data = buf_write_pointer_size(b, &data_size);
25 EXPECT_NE((void *)NULL, data);
26 EXPECT_EQ(100, data_size);
27
28 buf_increment_write(b, 50);
29 data = buf_read_pointer_size(b, &data_size);
30 EXPECT_NE((void *)NULL, data);
31 EXPECT_EQ(50, data_size);
32
33 buf_increment_read(b, 40);
34 EXPECT_EQ(10, buf_queued_bytes(b));
35 EXPECT_EQ(90, buf_available_bytes(b));
36
37 /* Test write to the end of ring buffer. */
38 data = buf_write_pointer_size(b, &data_size);
39 EXPECT_NE((void *)NULL, data);
40 EXPECT_EQ(50, data_size);
41
42 buf_increment_write(b, 50);
43 data = buf_write_pointer_size(b, &data_size);
44 EXPECT_NE((void *)NULL, data);
45 EXPECT_EQ(40, data_size);
46
47 byte_buffer_destroy(b);
48 }
49
TEST(ByteBuffer,SetUsedSizeReadWrite)50 TEST(ByteBuffer, SetUsedSizeReadWrite) {
51 struct byte_buffer *b;
52 uint8_t *data;
53 unsigned int data_size;
54
55 b = byte_buffer_create(100);
56 EXPECT_EQ(100, buf_available_bytes(b));
57 EXPECT_EQ(0, buf_queued_bytes(b));
58
59 /* Test set used_size to limit the initial allocated max size. */
60 byte_buffer_set_used_size(b, 90);
61 EXPECT_EQ(90, buf_available_bytes(b));
62
63 data = buf_write_pointer_size(b, &data_size);
64 EXPECT_NE((void *)NULL, data);
65 EXPECT_EQ(90, data_size);
66
67 buf_increment_write(b, 90);
68 data = buf_read_pointer_size(b, &data_size);
69 EXPECT_NE((void *)NULL, data);
70 EXPECT_EQ(90, data_size);
71
72 buf_increment_read(b, 50);
73 EXPECT_EQ(50, buf_available_bytes(b));
74 EXPECT_EQ(40, buf_queued_bytes(b));
75
76 data = buf_write_pointer_size(b, &data_size);
77 EXPECT_NE((void *)NULL, data);
78 EXPECT_EQ(50, data_size);
79
80 buf_increment_write(b, 50);
81 data = buf_write_pointer_size(b, &data_size);
82 EXPECT_NE((void *)NULL, data);
83 EXPECT_EQ(0, data_size);
84
85 /* Test read to the end of ring buffer. */
86 data = buf_read_pointer_size(b, &data_size);
87 EXPECT_NE((void *)NULL, data);
88 EXPECT_EQ(40, data_size);
89
90 buf_increment_read(b, 40);
91 data = buf_read_pointer_size(b, &data_size);
92 EXPECT_NE((void *)NULL, data);
93 EXPECT_EQ(50, data_size);
94
95 byte_buffer_destroy(b);
96 }
97
98 } // namespace
99
main(int argc,char ** argv)100 int main(int argc, char **argv) {
101 ::testing::InitGoogleTest(&argc, argv);
102 return RUN_ALL_TESTS();
103 }