AudioEncoderOpusTest.PacketLossRateOptimized: Fix bug and make prettier

Fix bug 4981, which caused the second half (decreasing loss rates) to
not test anything. In the process, the test is changed slightly to
make it less dependent on the exact rounding behavior of doubles (by
not testing exactly at the the points where the effective loss rate
goes through a step---just very very close). A bunch of symbolic
constants are also replaced with easy-to-read literal numbers.

BUG=4981

Review URL: https://codereview.webrtc.org/1316673010

Cr-Commit-Position: refs/heads/master@{#9908}
This commit is contained in:
kwiberg
2015-09-09 06:42:56 -07:00
committed by Commit bot
parent 2feafdb742
commit 942a699f14

View File

@ -101,25 +101,24 @@ TEST_F(AudioEncoderOpusTest, SetBitrate) {
namespace {
// These constants correspond to those used in
// AudioEncoderOpus::SetProjectedPacketLossRate.
const double kPacketLossRate20 = 0.20;
const double kPacketLossRate10 = 0.10;
const double kPacketLossRate5 = 0.05;
const double kPacketLossRate1 = 0.01;
const double kLossRate20Margin = 0.02;
const double kLossRate10Margin = 0.01;
const double kLossRate5Margin = 0.01;
// Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1),
// ..., b.
std::vector<double> IntervalSteps(double a, double b, size_t n) {
DCHECK_GT(n, 1u);
const double step = (b - a) / (n - 1);
std::vector<double> points;
for (size_t i = 0; i < n; ++i)
points.push_back(a + i * step);
return points;
}
// Repeatedly sets packet loss rates in the range [from, to], increasing by
// 0.01 in each step. The function verifies that the actual loss rate is
// |expected_return|.
// Sets the packet loss rate to each number in the vector in turn, and verifies
// that the loss rate as reported by the encoder is |expected_return| for all
// of them.
void TestSetPacketLossRate(AudioEncoderOpus* encoder,
double from,
double to,
const std::vector<double>& losses,
double expected_return) {
for (double loss = from; loss <= to;
(to >= from) ? loss += 0.01 : loss -= 0.01) {
for (double loss : losses) {
encoder->SetProjectedPacketLossRate(loss);
EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate());
}
@ -129,33 +128,24 @@ void TestSetPacketLossRate(AudioEncoderOpus* encoder,
TEST_F(AudioEncoderOpusTest, PacketLossRateOptimized) {
CreateCodec(1);
auto I = [](double a, double b) { return IntervalSteps(a, b, 10); };
const double eps = 1e-15;
// Note that the order of the following calls is critical.
TestSetPacketLossRate(encoder_.get(), 0.0, 0.0, 0.0);
TestSetPacketLossRate(encoder_.get(), kPacketLossRate1,
kPacketLossRate5 + kLossRate5Margin - 0.01,
kPacketLossRate1);
TestSetPacketLossRate(encoder_.get(), kPacketLossRate5 + kLossRate5Margin,
kPacketLossRate10 + kLossRate10Margin - 0.01,
kPacketLossRate5);
TestSetPacketLossRate(encoder_.get(), kPacketLossRate10 + kLossRate10Margin,
kPacketLossRate20 + kLossRate20Margin - 0.01,
kPacketLossRate10);
TestSetPacketLossRate(encoder_.get(), kPacketLossRate20 + kLossRate20Margin,
1.0, kPacketLossRate20);
TestSetPacketLossRate(encoder_.get(), kPacketLossRate20 + kLossRate20Margin,
kPacketLossRate20 - kLossRate20Margin,
kPacketLossRate20);
TestSetPacketLossRate(
encoder_.get(), kPacketLossRate20 - kLossRate20Margin - 0.01,
kPacketLossRate10 - kLossRate10Margin, kPacketLossRate10);
TestSetPacketLossRate(encoder_.get(),
kPacketLossRate10 - kLossRate10Margin - 0.01,
kPacketLossRate5 - kLossRate5Margin, kPacketLossRate5);
TestSetPacketLossRate(encoder_.get(),
kPacketLossRate5 - kLossRate5Margin - 0.01,
kPacketLossRate1, kPacketLossRate1);
TestSetPacketLossRate(encoder_.get(), 0.0, 0.0, 0.0);
// clang-format off
TestSetPacketLossRate(encoder_.get(), I(0.00 , 0.01 - eps), 0.00);
TestSetPacketLossRate(encoder_.get(), I(0.01 + eps, 0.06 - eps), 0.01);
TestSetPacketLossRate(encoder_.get(), I(0.06 + eps, 0.11 - eps), 0.05);
TestSetPacketLossRate(encoder_.get(), I(0.11 + eps, 0.22 - eps), 0.10);
TestSetPacketLossRate(encoder_.get(), I(0.22 + eps, 1.00 ), 0.20);
TestSetPacketLossRate(encoder_.get(), I(1.00 , 0.18 + eps), 0.20);
TestSetPacketLossRate(encoder_.get(), I(0.18 - eps, 0.09 + eps), 0.10);
TestSetPacketLossRate(encoder_.get(), I(0.09 - eps, 0.04 + eps), 0.05);
TestSetPacketLossRate(encoder_.get(), I(0.04 - eps, 0.01 + eps), 0.01);
TestSetPacketLossRate(encoder_.get(), I(0.01 - eps, 0.00 ), 0.00);
// clang-format on
}
} // namespace webrtc