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 |
11 #include "webrtc/base/buffer.h" | 11 #include "webrtc/base/buffer.h" |
12 #include "webrtc/base/gunit.h" | 12 #include "webrtc/base/gunit.h" |
13 | 13 |
14 #include <algorithm> // std::swap (pre-C++11) | 14 #include <type_traits> |
15 #include <utility> // std::swap (C++11 and later) | 15 #include <utility> |
16 | 16 |
17 namespace rtc { | 17 namespace rtc { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // clang-format off | 21 // clang-format off |
22 const uint8_t kTestData[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, | 22 const uint8_t kTestData[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, |
23 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; | 23 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; |
24 // clang-format on | 24 // clang-format on |
25 | 25 |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 | 294 |
295 for (size_t i = 0; i != 7u; ++i) { | 295 for (size_t i = 0; i != 7u; ++i) { |
296 buf[i] = kTestData[i]; | 296 buf[i] = kTestData[i]; |
297 } | 297 } |
298 | 298 |
299 for (size_t i = 0; i != 7u; ++i) { | 299 for (size_t i = 0; i != 7u; ++i) { |
300 EXPECT_EQ(buf[i], kTestData[i]); | 300 EXPECT_EQ(buf[i], kTestData[i]); |
301 } | 301 } |
302 } | 302 } |
303 | 303 |
| 304 TEST(BufferTest, TestInt16) { |
| 305 static constexpr int16_t test_data[] = {14, 15, 16, 17, 18}; |
| 306 BufferT<int16_t> buf(test_data); |
| 307 EXPECT_EQ(buf.size(), 5u); |
| 308 EXPECT_EQ(buf.capacity(), 5u); |
| 309 EXPECT_NE(buf.data(), nullptr); |
| 310 for (size_t i = 0; i != buf.size(); ++i) { |
| 311 EXPECT_EQ(test_data[i], buf[i]); |
| 312 } |
| 313 BufferT<int16_t> buf2(test_data); |
| 314 EXPECT_EQ(buf, buf2); |
| 315 buf2[0] = 9; |
| 316 EXPECT_NE(buf, buf2); |
| 317 } |
| 318 |
| 319 TEST(BufferTest, TestFloat) { |
| 320 static constexpr float test_data[] = {14, 15, 16, 17, 18}; |
| 321 BufferT<float> buf; |
| 322 EXPECT_EQ(buf.size(), 0u); |
| 323 EXPECT_EQ(buf.capacity(), 0u); |
| 324 EXPECT_EQ(buf.data(), nullptr); |
| 325 buf.SetData(test_data); |
| 326 EXPECT_EQ(buf.size(), 5u); |
| 327 EXPECT_EQ(buf.capacity(), 5u); |
| 328 EXPECT_NE(buf.data(), nullptr); |
| 329 float* p1 = buf.data(); |
| 330 while (buf.data() == p1) { |
| 331 buf.AppendData(test_data); |
| 332 } |
| 333 EXPECT_EQ(buf.size(), buf.capacity()); |
| 334 EXPECT_GT(buf.size(), 5u); |
| 335 EXPECT_EQ(buf.size() % 5, 0u); |
| 336 EXPECT_NE(buf.data(), nullptr); |
| 337 for (size_t i = 0; i != buf.size(); ++i) { |
| 338 EXPECT_EQ(test_data[i % 5], buf[i]); |
| 339 } |
| 340 } |
| 341 |
| 342 TEST(BufferTest, TestStruct) { |
| 343 struct BloodStone { |
| 344 bool blood; |
| 345 const char* stone; |
| 346 }; |
| 347 BufferT<BloodStone> buf(4); |
| 348 EXPECT_EQ(buf.size(), 4u); |
| 349 EXPECT_EQ(buf.capacity(), 4u); |
| 350 EXPECT_NE(buf.data(), nullptr); |
| 351 BufferT<BloodStone*> buf2(4); |
| 352 for (size_t i = 0; i < buf2.size(); ++i) { |
| 353 buf2[i] = &buf[i]; |
| 354 } |
| 355 static const char* kObsidian = "obsidian"; |
| 356 buf2[2]->stone = kObsidian; |
| 357 EXPECT_EQ(kObsidian, buf[2].stone); |
| 358 } |
| 359 |
304 } // namespace rtc | 360 } // namespace rtc |
OLD | NEW |