Chromium Code Reviews| Index: webrtc/modules/remote_bitrate_estimator/test/bwe_unittest.cc |
| diff --git a/webrtc/modules/remote_bitrate_estimator/test/bwe_unittest.cc b/webrtc/modules/remote_bitrate_estimator/test/bwe_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..096d208ef35362cf85e3d2aa005caffda7673ffa |
| --- /dev/null |
| +++ b/webrtc/modules/remote_bitrate_estimator/test/bwe_unittest.cc |
| @@ -0,0 +1,198 @@ |
| +/* |
| + * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/remote_bitrate_estimator/test/bwe.h" |
| + |
| +#include <vector> |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace webrtc { |
| +namespace testing { |
| +namespace bwe { |
| + |
| +const int kSetCapacity = 10000; |
| + |
| +class LinkedSetTest : public ::testing::Test { |
| + public: |
| + LinkedSetTest() : linked_set_(kSetCapacity) {} |
| + ~LinkedSetTest() {} |
| + |
| + protected: |
| + LinkedSet linked_set_; |
| +}; |
| + |
| +TEST_F(LinkedSetTest, EmptySet) { |
| + EXPECT_EQ(linked_set_.OldestSeqNumber(), 0); |
| + EXPECT_EQ(linked_set_.NewestSeqNumber(), 0); |
| +} |
| + |
| +TEST_F(LinkedSetTest, SinglePacket) { |
| + const uint16_t kSeqNumber = 1; // Arbitrary. |
| + // Other parameters don't matter here. |
| + linked_set_.Insert(kSeqNumber, 0, 0, 0); |
| + EXPECT_EQ(linked_set_.OldestSeqNumber(), kSeqNumber); |
| + EXPECT_EQ(linked_set_.NewestSeqNumber(), kSeqNumber); |
| +} |
| + |
| +TEST_F(LinkedSetTest, MultiplePackets) { |
| + const uint16_t kNumberPackets = 100; |
| + |
| + std::vector<uint16_t> sequence_numbers; |
| + for (size_t i = 0; i < kNumberPackets; ++i) { |
| + sequence_numbers.push_back(static_cast<uint16_t>(i + 1)); |
| + } |
| + random_shuffle(sequence_numbers.begin(), sequence_numbers.end()); |
| + |
| + for (size_t i = 0; i < kNumberPackets; ++i) { |
| + // Other parameters don't matter here. |
| + linked_set_.Insert(static_cast<uint16_t>(i), 0, 0, 0); |
| + } |
| + |
| + // Packets arriving out of order should not affect the following values: |
| + EXPECT_EQ(linked_set_.OldestSeqNumber(), 0); |
| + EXPECT_EQ(linked_set_.NewestSeqNumber(), kNumberPackets - 1); |
| +} |
| + |
| +TEST_F(LinkedSetTest, Overflow) { |
| + const int kFirstSeqNumber = -100; |
| + const int kLastSeqNumber = 100; |
| + |
| + for (int i = kFirstSeqNumber; i <= kLastSeqNumber; ++i) { |
| + // Other parameters don't matter here. |
| + linked_set_.Insert(static_cast<uint16_t>(i), 0, 0, 0); |
| + } |
| + |
| + // Packets arriving out of order should not affect the following values: |
| + EXPECT_EQ(linked_set_.OldestSeqNumber(), |
| + static_cast<uint16_t>(kFirstSeqNumber)); |
| + EXPECT_EQ(linked_set_.NewestSeqNumber(), |
| + static_cast<uint16_t>(kLastSeqNumber)); |
| +} |
| + |
| +class SequenceNumberOlderThanTest : public ::testing::Test { |
| + public: |
| + SequenceNumberOlderThanTest() {} |
| + ~SequenceNumberOlderThanTest() {} |
| + |
| + protected: |
| + SequenceNumberOlderThan comparator_; |
| +}; |
| + |
| +TEST_F(SequenceNumberOlderThanTest, Operator) { |
| + // Operator()(x, y) returns true <==> y is newer than x. |
| + EXPECT_TRUE(comparator_.operator()(0, 1)); |
| + EXPECT_TRUE(comparator_.operator()(1, 1000)); |
| + EXPECT_FALSE(comparator_.operator()(1, 0)); |
| + EXPECT_FALSE(comparator_.operator()(2, 2)); |
| + EXPECT_TRUE(comparator_.operator()(-10, 10)); |
| + EXPECT_FALSE(comparator_.operator()(10, -10)); |
| + EXPECT_TRUE(comparator_.operator()(0, 0x8000)); |
| + EXPECT_FALSE(comparator_.operator()(0x8000, 0)); |
| +} |
| + |
| +class LossAccountTest : public ::testing::Test { |
| + public: |
| + LossAccountTest() {} |
| + ~LossAccountTest() {} |
| + |
| + protected: |
| + LossAccount loss_account_; |
| +}; |
| + |
| +TEST_F(LossAccountTest, Operations) { |
| + const size_t kTotal = 100; // Arbitrary values. |
| + const size_t kLost = 10; |
| + |
| + LossAccount rhs(kTotal, kLost); |
| + |
| + loss_account_.Add(rhs); |
| + EXPECT_EQ(loss_account_.num_total, kTotal); |
| + EXPECT_EQ(loss_account_.num_lost, kLost); |
| + EXPECT_NEAR(loss_account_.LossRatio(), static_cast<float>(kLost) / kTotal, |
| + 0.001f); |
| + |
| + loss_account_.Subtract(rhs); |
| + EXPECT_EQ(loss_account_.num_total, 0UL); |
| + EXPECT_EQ(loss_account_.num_lost, 0UL); |
| + EXPECT_NEAR(loss_account_.LossRatio(), 0.0f, 0.001f); |
| +} |
| + |
| +const int kFlowId = 1; // Arbitrary. |
| + |
| +class BweReceiverTest : public ::testing::Test, public BweReceiver { |
|
stefan-webrtc
2015/07/09 09:55:00
I would prefer to make BweReceiver a member of thi
magalhaesc
2015/07/09 11:20:07
Right, some methods will become public then.
|
| + public: |
| + BweReceiverTest() : BweReceiver(kFlowId) {} |
| + ~BweReceiverTest() {} |
| +}; |
| + |
| +TEST_F(BweReceiverTest, RecentKbps) { |
| + EXPECT_EQ(RecentKbps(), 0U); |
| + |
| + const size_t kPacketSizeBytes = 1200; |
| + const int kNumPackets = 100; |
| + |
| + double window_size_s = this->window_size_s(); |
| + |
| + // Receive packets at the same time. |
| + for (int i = 0; i < kNumPackets; ++i) { |
| + MediaPacket packet(kFlowId, 0, kPacketSizeBytes, i); |
| + ReceivePacket(0, packet); |
| + } |
| + |
| + EXPECT_NEAR( |
| + RecentKbps(), |
| + (8 * kNumPackets * kPacketSizeBytes) / (1000 * window_size_s), |
| + (8 * kNumPackets * kPacketSizeBytes) / (1000 * window_size_s * 20)); |
| + |
| + int64_t time_gap_ms = |
| + 2 * 1000 * window_size_s; // Larger than rate_counter time window. |
| + |
| + MediaPacket packet(kFlowId, time_gap_ms, kPacketSizeBytes, kNumPackets); |
| + ReceivePacket(time_gap_ms, packet); |
| + |
| + EXPECT_EQ(RecentKbps(), |
| + (8 * kPacketSizeBytes) / (1000 * window_size_s)); |
| +} |
| + |
| +TEST_F(BweReceiverTest, Loss) { |
| + |
| + EXPECT_NEAR(GlobalReceiverPacketLossRatio(), 0.0f, 0.001f); |
| + |
| + LossAccount loss_account = LinkedSetPacketLossRatio(); |
| + EXPECT_NEAR(loss_account.LossRatio(), 0.0f, 0.001f); |
| + |
| + // Insert packets 1-50 and 151-200; |
| + for (int i=1; i<=200; ++i){ |
| + // Packet size and timestamp do not matter here. |
| + MediaPacket packet(kFlowId, 0, 0, i); |
| + ReceivePacket(0, packet); |
| + if (i==50){ |
| + i += 100; |
| + } |
| + } |
| + |
| + loss_account = LinkedSetPacketLossRatio(); |
| + EXPECT_NEAR(loss_account.LossRatio(), 0.5f, 0.001f); |
| + |
| + RelieveSetAndUpdateLoss(); |
| + EXPECT_EQ(received_packets_.size(), 100U/10); |
| + |
| + // No packet loss within the preserved packets. |
| + loss_account = LinkedSetPacketLossRatio(); |
| + EXPECT_NEAR(loss_account.LossRatio(), 0.0f, 0.001f); |
| + |
| + // RelieveSetAndUpdateLoss automatically updates loss account. |
| + EXPECT_NEAR(GlobalReceiverPacketLossRatio(), 0.5f, 0.001f); |
| +} |
| + |
| +} // namespace bwe |
| +} // namespace testing |
| +} // namespace webrtc |