You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 The WebRTC Project Authors. All rights reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/buffer_queue.h"
  11. #include <string.h>
  12. #include "test/gtest.h"
  13. namespace rtc {
  14. TEST(BufferQueueTest, TestAll) {
  15. const size_t kSize = 16;
  16. const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
  17. char out[kSize * 2];
  18. size_t bytes;
  19. BufferQueue queue1(1, kSize);
  20. BufferQueue queue2(2, kSize);
  21. // The queue is initially empty.
  22. EXPECT_EQ(0u, queue1.size());
  23. EXPECT_FALSE(queue1.ReadFront(out, kSize, &bytes));
  24. // A write should succeed.
  25. EXPECT_TRUE(queue1.WriteBack(in, kSize, &bytes));
  26. EXPECT_EQ(kSize, bytes);
  27. EXPECT_EQ(1u, queue1.size());
  28. // The queue is full now (only one buffer allowed).
  29. EXPECT_FALSE(queue1.WriteBack(in, kSize, &bytes));
  30. EXPECT_EQ(1u, queue1.size());
  31. // Reading previously written buffer.
  32. EXPECT_TRUE(queue1.ReadFront(out, kSize, &bytes));
  33. EXPECT_EQ(kSize, bytes);
  34. EXPECT_EQ(0, memcmp(in, out, kSize));
  35. // The queue is empty again now.
  36. EXPECT_FALSE(queue1.ReadFront(out, kSize, &bytes));
  37. EXPECT_EQ(0u, queue1.size());
  38. // Reading only returns available data.
  39. EXPECT_TRUE(queue1.WriteBack(in, kSize, &bytes));
  40. EXPECT_EQ(kSize, bytes);
  41. EXPECT_EQ(1u, queue1.size());
  42. EXPECT_TRUE(queue1.ReadFront(out, kSize * 2, &bytes));
  43. EXPECT_EQ(kSize, bytes);
  44. EXPECT_EQ(0, memcmp(in, out, kSize));
  45. EXPECT_EQ(0u, queue1.size());
  46. // Reading maintains buffer boundaries.
  47. EXPECT_TRUE(queue2.WriteBack(in, kSize / 2, &bytes));
  48. EXPECT_EQ(1u, queue2.size());
  49. EXPECT_TRUE(queue2.WriteBack(in + kSize / 2, kSize / 2, &bytes));
  50. EXPECT_EQ(2u, queue2.size());
  51. EXPECT_TRUE(queue2.ReadFront(out, kSize, &bytes));
  52. EXPECT_EQ(kSize / 2, bytes);
  53. EXPECT_EQ(0, memcmp(in, out, kSize / 2));
  54. EXPECT_EQ(1u, queue2.size());
  55. EXPECT_TRUE(queue2.ReadFront(out, kSize, &bytes));
  56. EXPECT_EQ(kSize / 2, bytes);
  57. EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 2));
  58. EXPECT_EQ(0u, queue2.size());
  59. // Reading truncates buffers.
  60. EXPECT_TRUE(queue2.WriteBack(in, kSize / 2, &bytes));
  61. EXPECT_EQ(1u, queue2.size());
  62. EXPECT_TRUE(queue2.WriteBack(in + kSize / 2, kSize / 2, &bytes));
  63. EXPECT_EQ(2u, queue2.size());
  64. // Read first packet partially in too-small buffer.
  65. EXPECT_TRUE(queue2.ReadFront(out, kSize / 4, &bytes));
  66. EXPECT_EQ(kSize / 4, bytes);
  67. EXPECT_EQ(0, memcmp(in, out, kSize / 4));
  68. EXPECT_EQ(1u, queue2.size());
  69. // Remainder of first packet is truncated, reading starts with next packet.
  70. EXPECT_TRUE(queue2.ReadFront(out, kSize, &bytes));
  71. EXPECT_EQ(kSize / 2, bytes);
  72. EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 2));
  73. EXPECT_EQ(0u, queue2.size());
  74. }
  75. } // namespace rtc