Chromium Code Reviews| Index: media/base/audio_fifo_unittest.cc | 
| diff --git a/media/base/audio_fifo_unittest.cc b/media/base/audio_fifo_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..a6e59279d9b5b9eb6f104898a0279056da2ba6a0 | 
| --- /dev/null | 
| +++ b/media/base/audio_fifo_unittest.cc | 
| @@ -0,0 +1,198 @@ | 
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include <limits> | 
| + | 
| +#include "media/base/audio_bus.h" | 
| +#include "media/base/audio_fifo.h" | 
| +#include "testing/gtest/include/gtest/gtest.h" | 
| + | 
| +namespace media { | 
| + | 
| +class AudioFifoTest : public testing::Test { | 
| + public: | 
| + AudioFifoTest() {} | 
| + ~AudioFifoTest() {} | 
| + | 
| + void VerifyValue(const float data[], int size, float value) { | 
| + for (int i = 0; i < size; ++i) | 
| + ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i; | 
| + } | 
| + | 
| + protected: | 
| + DISALLOW_COPY_AND_ASSIGN(AudioFifoTest); | 
| +}; | 
| + | 
| +// Verify that construction works as intended. | 
| +TEST_F(AudioFifoTest, Construct) { | 
| + const int kChannels = 6; | 
| 
 
DaleCurtis
2012/09/05 10:34:18
static here and below. Used elsewhere too, so mayb
 
henrika (OOO until Aug 14)
2012/09/05 12:49:04
I actually use different for each test.
OK to kee
 
DaleCurtis
2012/09/05 13:09:22
Should still be static, non-global is fine with me
 
 | 
| + const int kMaxFrameCount = 128; | 
| + AudioFifo fifo(kChannels, kMaxFrameCount); | 
| + EXPECT_EQ(fifo.channels(), kChannels); | 
| + EXPECT_EQ(fifo.max_size(), kMaxFrameCount); | 
| + EXPECT_EQ(fifo.size(), 0); | 
| +} | 
| + | 
| +// Append audio bus objects to a FIFO and fill it up to different degrees. | 
| +// Also, verify that it is not possible to overflow the FIFO. | 
| +TEST_F(AudioFifoTest, Append) { | 
| + const int kChannels = 2; | 
| + const int kMaxFrameCount = 128; | 
| + AudioFifo fifo(kChannels, kMaxFrameCount); | 
| + { | 
| + SCOPED_TRACE("append-50%"); | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() / 2); | 
| + EXPECT_TRUE(fifo.empty()); | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_EQ(fifo.size(), bus->frames()); | 
| + fifo.Clear(); | 
| + } | 
| + { | 
| + SCOPED_TRACE("append-100%"); | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size()); | 
| + EXPECT_TRUE(fifo.empty()); | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_EQ(fifo.size(), bus->frames()); | 
| + EXPECT_TRUE(fifo.full()); | 
| + fifo.Clear(); | 
| + } | 
| + { | 
| + SCOPED_TRACE("overflow"); | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() + 1); | 
| + EXPECT_TRUE(fifo.empty()); | 
| + EXPECT_FALSE(fifo.Append(bus.get())); | 
| + EXPECT_TRUE(fifo.empty()); | 
| + } | 
| +} | 
| + | 
| +// Remove audio bus objects from a FIFO and empty it to different degrees. | 
| +// Also, verify that it is not possible to ask for more data than the FIFO | 
| +// contains (corresponds to underrun). | 
| +TEST_F(AudioFifoTest, Remove) { | 
| + const int kChannels = 2; | 
| + const int kMaxFrameCount = 128; | 
| + AudioFifo fifo(kChannels, kMaxFrameCount); | 
| + { | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size()); | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_TRUE(fifo.full()); | 
| + } | 
| + { | 
| + SCOPED_TRACE("remove-50%"); | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() / 2); | 
| + EXPECT_TRUE(fifo.Remove(bus.get())); | 
| + EXPECT_TRUE(fifo.size() == bus->frames()); | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_TRUE(fifo.full()); | 
| + } | 
| + { | 
| + SCOPED_TRACE("remove-100%"); | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size()); | 
| + EXPECT_TRUE(fifo.Remove(bus.get())); | 
| + EXPECT_TRUE(fifo.empty()); | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_TRUE(fifo.full()); | 
| + } | 
| + { | 
| + SCOPED_TRACE("underrun"); | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() + 1); | 
| + EXPECT_FALSE(fifo.Remove(bus.get())); | 
| + EXPECT_TRUE(fifo.full()); | 
| + } | 
| +} | 
| + | 
| +// Verify that the size() method of the FIFO works as intended while appending | 
| +// and removing audio bus elements to/from the FIFO. | 
| +TEST_F(AudioFifoTest, Size) { | 
| + const int kChannels = 2; | 
| + const int kMaxFrameCount = 480; | 
| + AudioFifo fifo(kChannels, kMaxFrameCount); | 
| + | 
| + // Fill up the FIFO and verify that the size grows as it should while adding | 
| + // one audio frame each time. | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1); | 
| + int n = 0; | 
| + while (!fifo.full()) { | 
| 
 
DaleCurtis
2012/09/05 10:34:18
How long does this take to run? Not sure we need 4
 
henrika (OOO until Aug 14)
2012/09/05 12:49:04
It takes 4ms on my machine. Reduced it to 64 => 1m
 
 | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_EQ(fifo.size(), ++n); | 
| + } | 
| + | 
| + // Ensure that we can't append more data when the FIFO is full. | 
| + EXPECT_TRUE(fifo.full()); | 
| + EXPECT_FALSE(fifo.Append(bus.get())); | 
| + | 
| + // Empty the FIFO and verify that the size decreases as it should. | 
| + // Reduce the size of the FIFO by one frame each time. | 
| + while (!fifo.empty()) { | 
| + EXPECT_TRUE(fifo.Remove(bus.get())); | 
| + EXPECT_EQ(fifo.size(), --n); | 
| + } | 
| + | 
| + // Ensure that we can't remove more data when the FIFO is empty. | 
| + EXPECT_TRUE(fifo.empty()); | 
| + EXPECT_FALSE(fifo.Remove(bus.get())); | 
| + | 
| + // Verify that a steady-state size of one frame in the FIFO is maintained | 
| + // during a sequence of Append/Remove calls. | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_EQ(fifo.size(), 1); | 
| + for (int n = 0; n < 100; ++n) { | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_TRUE(fifo.Remove(bus.get())); | 
| + EXPECT_TRUE(fifo.size() == 1); | 
| + } | 
| +} | 
| + | 
| +// Do a more realistic test of how the FIFO will be used and also verify that | 
| +// the actual data content is correct. | 
| +TEST_F(AudioFifoTest, AppdendAndRemove) { | 
| 
 
Chris Rogers
2012/09/04 20:07:20
typo: AppdendAndRemove
 
henrika (OOO until Aug 14)
2012/09/05 10:14:36
Done.
 
 | 
| + const int kChannels = 2; | 
| + const int kFrameCount = 256; | 
| + const int kMaxAudioBusesInFifoCount = 128; | 
| + | 
| + scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); | 
| + AudioFifo fifo(kChannels, bus->frames() * kMaxAudioBusesInFifoCount); | 
| + EXPECT_EQ(fifo.max_size(), kFrameCount * kMaxAudioBusesInFifoCount); | 
| + | 
| + // Start by filling up half the FIFO with audio frames. The first audio frame | 
| + // will contain all 1's, the second all 2's etc. All channels contain the | 
| + // same value. | 
| + int value = 1; | 
| + while (fifo.size() < bus->frames() * (kMaxAudioBusesInFifoCount / 2)) { | 
| + // Fill all channels in the bus with dummy values. | 
| + for (int j = 0; j < bus->channels(); ++j) | 
| + std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_EQ(fifo.size(), bus->frames() * value); | 
| + ++value; | 
| + } | 
| + | 
| + // Do a sequence of Remove/Append calls and verify that the acquired values | 
| + // are correct and that the wrap-around mechanism works as intended. | 
| + // The size of the FIFO will be constant during this step. | 
| + const int steady_state_offset = fifo.size() / bus->frames(); | 
| + for (int i = 0; i < kMaxAudioBusesInFifoCount; ++i) { | 
| + EXPECT_TRUE(fifo.Remove(bus.get())); | 
| + value = steady_state_offset + i + 1; | 
| + for (int j = 0; j < bus->channels(); ++j) { | 
| + VerifyValue(bus->channel(j), bus->frames(), value - steady_state_offset); | 
| + std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); | 
| + } | 
| + EXPECT_TRUE(fifo.Append(bus.get())); | 
| + EXPECT_EQ(fifo.size(), bus->frames() * (kMaxAudioBusesInFifoCount / 2)); | 
| + } | 
| + | 
| + // As a last step, flush the FIFO and verify that the retrieved values | 
| + // are correct. | 
| + value = value - steady_state_offset + 1; | 
| + while (!fifo.empty()) { | 
| + EXPECT_TRUE(fifo.Remove(bus.get())); | 
| + for (int j = 0; j < bus->channels(); ++j) { | 
| + VerifyValue(bus->channel(j), bus->frames(), value); | 
| + } | 
| + value++; | 
| + } | 
| +} | 
| + | 
| +} // namespace media |