OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64)); | 47 EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64)); |
48 | 48 |
49 // GetBE converts little endian to big endian here. | 49 // GetBE converts little endian to big endian here. |
50 EXPECT_EQ(n16 << 8, GetBE16(&n16)); | 50 EXPECT_EQ(n16 << 8, GetBE16(&n16)); |
51 EXPECT_EQ(n32 << 24, GetBE32(&n32)); | 51 EXPECT_EQ(n32 << 24, GetBE32(&n32)); |
52 EXPECT_EQ(n64 << 56, GetBE64(&n64)); | 52 EXPECT_EQ(n64 << 56, GetBE64(&n64)); |
53 } | 53 } |
54 } | 54 } |
55 | 55 |
56 TEST(ByteBufferTest, TestBufferLength) { | 56 TEST(ByteBufferTest, TestBufferLength) { |
57 ByteBuffer buffer; | 57 ByteBufferWriter buffer; |
58 size_t size = 0; | 58 size_t size = 0; |
59 EXPECT_EQ(size, buffer.Length()); | 59 EXPECT_EQ(size, buffer.Length()); |
60 | 60 |
61 buffer.WriteUInt8(1); | 61 buffer.WriteUInt8(1); |
62 ++size; | 62 ++size; |
63 EXPECT_EQ(size, buffer.Length()); | 63 EXPECT_EQ(size, buffer.Length()); |
64 | 64 |
65 buffer.WriteUInt16(1); | 65 buffer.WriteUInt16(1); |
66 size += 2; | 66 size += 2; |
67 EXPECT_EQ(size, buffer.Length()); | 67 EXPECT_EQ(size, buffer.Length()); |
68 | 68 |
69 buffer.WriteUInt24(1); | 69 buffer.WriteUInt24(1); |
70 size += 3; | 70 size += 3; |
71 EXPECT_EQ(size, buffer.Length()); | 71 EXPECT_EQ(size, buffer.Length()); |
72 | 72 |
73 buffer.WriteUInt32(1); | 73 buffer.WriteUInt32(1); |
74 size += 4; | 74 size += 4; |
75 EXPECT_EQ(size, buffer.Length()); | 75 EXPECT_EQ(size, buffer.Length()); |
76 | 76 |
77 buffer.WriteUInt64(1); | 77 buffer.WriteUInt64(1); |
78 size += 8; | 78 size += 8; |
79 EXPECT_EQ(size, buffer.Length()); | 79 EXPECT_EQ(size, buffer.Length()); |
80 | |
81 EXPECT_TRUE(buffer.Consume(0)); | |
82 EXPECT_EQ(size, buffer.Length()); | |
83 | |
84 EXPECT_TRUE(buffer.Consume(4)); | |
85 size -= 4; | |
86 EXPECT_EQ(size, buffer.Length()); | |
87 } | |
88 | |
89 TEST(ByteBufferTest, TestGetSetReadPosition) { | |
90 ByteBuffer buffer("ABCDEF", 6); | |
91 EXPECT_EQ(6U, buffer.Length()); | |
92 ByteBuffer::ReadPosition pos(buffer.GetReadPosition()); | |
93 EXPECT_TRUE(buffer.SetReadPosition(pos)); | |
94 EXPECT_EQ(6U, buffer.Length()); | |
95 std::string read; | |
96 EXPECT_TRUE(buffer.ReadString(&read, 3)); | |
97 EXPECT_EQ("ABC", read); | |
98 EXPECT_EQ(3U, buffer.Length()); | |
99 EXPECT_TRUE(buffer.SetReadPosition(pos)); | |
100 EXPECT_EQ(6U, buffer.Length()); | |
101 read.clear(); | |
102 EXPECT_TRUE(buffer.ReadString(&read, 3)); | |
103 EXPECT_EQ("ABC", read); | |
104 EXPECT_EQ(3U, buffer.Length()); | |
105 // For a resize by writing Capacity() number of bytes. | |
106 size_t capacity = buffer.Capacity(); | |
107 buffer.ReserveWriteBuffer(buffer.Capacity()); | |
108 EXPECT_EQ(capacity + 3U, buffer.Length()); | |
109 EXPECT_FALSE(buffer.SetReadPosition(pos)); | |
110 read.clear(); | |
111 EXPECT_TRUE(buffer.ReadString(&read, 3)); | |
112 EXPECT_EQ("DEF", read); | |
113 } | 80 } |
114 | 81 |
115 TEST(ByteBufferTest, TestReadWriteBuffer) { | 82 TEST(ByteBufferTest, TestReadWriteBuffer) { |
116 ByteBuffer::ByteOrder orders[2] = { ByteBuffer::ORDER_HOST, | 83 ByteBufferWriter::ByteOrder orders[2] = { ByteBufferWriter::ORDER_HOST, |
117 ByteBuffer::ORDER_NETWORK }; | 84 ByteBufferWriter::ORDER_NETWORK }; |
118 for (size_t i = 0; i < arraysize(orders); i++) { | 85 for (size_t i = 0; i < arraysize(orders); i++) { |
119 ByteBuffer buffer(orders[i]); | 86 ByteBufferWriter buffer(orders[i]); |
120 EXPECT_EQ(orders[i], buffer.Order()); | 87 EXPECT_EQ(orders[i], buffer.Order()); |
| 88 ByteBufferReader read_buf(nullptr, 0, orders[i]); |
| 89 EXPECT_EQ(orders[i], read_buf.Order()); |
121 uint8_t ru8; | 90 uint8_t ru8; |
122 EXPECT_FALSE(buffer.ReadUInt8(&ru8)); | 91 EXPECT_FALSE(read_buf.ReadUInt8(&ru8)); |
123 | 92 |
124 // Write and read uint8_t. | 93 // Write and read uint8_t. |
125 uint8_t wu8 = 1; | 94 uint8_t wu8 = 1; |
126 buffer.WriteUInt8(wu8); | 95 buffer.WriteUInt8(wu8); |
127 EXPECT_TRUE(buffer.ReadUInt8(&ru8)); | 96 ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]); |
| 97 EXPECT_TRUE(read_buf1.ReadUInt8(&ru8)); |
128 EXPECT_EQ(wu8, ru8); | 98 EXPECT_EQ(wu8, ru8); |
129 EXPECT_EQ(0U, buffer.Length()); | 99 EXPECT_EQ(0U, read_buf1.Length()); |
| 100 buffer.Clear(); |
130 | 101 |
131 // Write and read uint16_t. | 102 // Write and read uint16_t. |
132 uint16_t wu16 = (1 << 8) + 1; | 103 uint16_t wu16 = (1 << 8) + 1; |
133 buffer.WriteUInt16(wu16); | 104 buffer.WriteUInt16(wu16); |
| 105 ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]); |
134 uint16_t ru16; | 106 uint16_t ru16; |
135 EXPECT_TRUE(buffer.ReadUInt16(&ru16)); | 107 EXPECT_TRUE(read_buf2.ReadUInt16(&ru16)); |
136 EXPECT_EQ(wu16, ru16); | 108 EXPECT_EQ(wu16, ru16); |
137 EXPECT_EQ(0U, buffer.Length()); | 109 EXPECT_EQ(0U, read_buf2.Length()); |
| 110 buffer.Clear(); |
138 | 111 |
139 // Write and read uint24. | 112 // Write and read uint24. |
140 uint32_t wu24 = (3 << 16) + (2 << 8) + 1; | 113 uint32_t wu24 = (3 << 16) + (2 << 8) + 1; |
141 buffer.WriteUInt24(wu24); | 114 buffer.WriteUInt24(wu24); |
| 115 ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]); |
142 uint32_t ru24; | 116 uint32_t ru24; |
143 EXPECT_TRUE(buffer.ReadUInt24(&ru24)); | 117 EXPECT_TRUE(read_buf3.ReadUInt24(&ru24)); |
144 EXPECT_EQ(wu24, ru24); | 118 EXPECT_EQ(wu24, ru24); |
145 EXPECT_EQ(0U, buffer.Length()); | 119 EXPECT_EQ(0U, read_buf3.Length()); |
| 120 buffer.Clear(); |
146 | 121 |
147 // Write and read uint32_t. | 122 // Write and read uint32_t. |
148 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1; | 123 uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1; |
149 buffer.WriteUInt32(wu32); | 124 buffer.WriteUInt32(wu32); |
| 125 ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]); |
150 uint32_t ru32; | 126 uint32_t ru32; |
151 EXPECT_TRUE(buffer.ReadUInt32(&ru32)); | 127 EXPECT_TRUE(read_buf4.ReadUInt32(&ru32)); |
152 EXPECT_EQ(wu32, ru32); | 128 EXPECT_EQ(wu32, ru32); |
153 EXPECT_EQ(0U, buffer.Length()); | 129 EXPECT_EQ(0U, read_buf3.Length()); |
| 130 buffer.Clear(); |
154 | 131 |
155 // Write and read uint64_t. | 132 // Write and read uint64_t. |
156 uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5; | 133 uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5; |
157 uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32; | 134 uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32; |
158 buffer.WriteUInt64(wu64); | 135 buffer.WriteUInt64(wu64); |
| 136 ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]); |
159 uint64_t ru64; | 137 uint64_t ru64; |
160 EXPECT_TRUE(buffer.ReadUInt64(&ru64)); | 138 EXPECT_TRUE(read_buf5.ReadUInt64(&ru64)); |
161 EXPECT_EQ(wu64, ru64); | 139 EXPECT_EQ(wu64, ru64); |
162 EXPECT_EQ(0U, buffer.Length()); | 140 EXPECT_EQ(0U, read_buf5.Length()); |
| 141 buffer.Clear(); |
163 | 142 |
164 // Write and read string. | 143 // Write and read string. |
165 std::string write_string("hello"); | 144 std::string write_string("hello"); |
166 buffer.WriteString(write_string); | 145 buffer.WriteString(write_string); |
| 146 ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]); |
167 std::string read_string; | 147 std::string read_string; |
168 EXPECT_TRUE(buffer.ReadString(&read_string, write_string.size())); | 148 EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size())); |
169 EXPECT_EQ(write_string, read_string); | 149 EXPECT_EQ(write_string, read_string); |
170 EXPECT_EQ(0U, buffer.Length()); | 150 EXPECT_EQ(0U, read_buf6.Length()); |
| 151 buffer.Clear(); |
171 | 152 |
172 // Write and read bytes | 153 // Write and read bytes |
173 char write_bytes[] = "foo"; | 154 char write_bytes[] = "foo"; |
174 buffer.WriteBytes(write_bytes, 3); | 155 buffer.WriteBytes(write_bytes, 3); |
| 156 ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]); |
175 char read_bytes[3]; | 157 char read_bytes[3]; |
176 EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3)); | 158 EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3)); |
177 for (int i = 0; i < 3; ++i) { | 159 for (int i = 0; i < 3; ++i) { |
178 EXPECT_EQ(write_bytes[i], read_bytes[i]); | 160 EXPECT_EQ(write_bytes[i], read_bytes[i]); |
179 } | 161 } |
180 EXPECT_EQ(0U, buffer.Length()); | 162 EXPECT_EQ(0U, read_buf7.Length()); |
| 163 buffer.Clear(); |
181 | 164 |
182 // Write and read reserved buffer space | 165 // Write and read reserved buffer space |
183 char* write_dst = buffer.ReserveWriteBuffer(3); | 166 char* write_dst = buffer.ReserveWriteBuffer(3); |
184 memcpy(write_dst, write_bytes, 3); | 167 memcpy(write_dst, write_bytes, 3); |
| 168 ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]); |
185 memset(read_bytes, 0, 3); | 169 memset(read_bytes, 0, 3); |
186 EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3)); | 170 EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3)); |
187 for (int i = 0; i < 3; ++i) { | 171 for (int i = 0; i < 3; ++i) { |
188 EXPECT_EQ(write_bytes[i], read_bytes[i]); | 172 EXPECT_EQ(write_bytes[i], read_bytes[i]); |
189 } | 173 } |
190 EXPECT_EQ(0U, buffer.Length()); | 174 EXPECT_EQ(0U, read_buf8.Length()); |
| 175 buffer.Clear(); |
191 | 176 |
192 // Write and read in order. | 177 // Write and read in order. |
193 buffer.WriteUInt8(wu8); | 178 buffer.WriteUInt8(wu8); |
194 buffer.WriteUInt16(wu16); | 179 buffer.WriteUInt16(wu16); |
195 buffer.WriteUInt24(wu24); | 180 buffer.WriteUInt24(wu24); |
196 buffer.WriteUInt32(wu32); | 181 buffer.WriteUInt32(wu32); |
197 buffer.WriteUInt64(wu64); | 182 buffer.WriteUInt64(wu64); |
198 EXPECT_TRUE(buffer.ReadUInt8(&ru8)); | 183 ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]); |
| 184 EXPECT_TRUE(read_buf9.ReadUInt8(&ru8)); |
199 EXPECT_EQ(wu8, ru8); | 185 EXPECT_EQ(wu8, ru8); |
200 EXPECT_TRUE(buffer.ReadUInt16(&ru16)); | 186 EXPECT_TRUE(read_buf9.ReadUInt16(&ru16)); |
201 EXPECT_EQ(wu16, ru16); | 187 EXPECT_EQ(wu16, ru16); |
202 EXPECT_TRUE(buffer.ReadUInt24(&ru24)); | 188 EXPECT_TRUE(read_buf9.ReadUInt24(&ru24)); |
203 EXPECT_EQ(wu24, ru24); | 189 EXPECT_EQ(wu24, ru24); |
204 EXPECT_TRUE(buffer.ReadUInt32(&ru32)); | 190 EXPECT_TRUE(read_buf9.ReadUInt32(&ru32)); |
205 EXPECT_EQ(wu32, ru32); | 191 EXPECT_EQ(wu32, ru32); |
206 EXPECT_TRUE(buffer.ReadUInt64(&ru64)); | 192 EXPECT_TRUE(read_buf9.ReadUInt64(&ru64)); |
207 EXPECT_EQ(wu64, ru64); | 193 EXPECT_EQ(wu64, ru64); |
208 EXPECT_EQ(0U, buffer.Length()); | 194 EXPECT_EQ(0U, read_buf9.Length()); |
| 195 buffer.Clear(); |
209 } | 196 } |
210 } | 197 } |
211 | 198 |
212 } // namespace rtc | 199 } // namespace rtc |
OLD | NEW |