Index: third_party/WebKit/Source/platform/audio/PushPullFIFOTest.cpp |
diff --git a/third_party/WebKit/Source/platform/audio/PushPullFIFOTest.cpp b/third_party/WebKit/Source/platform/audio/PushPullFIFOTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c66482a6fa035d6c5dbc9b4fd11f7c6d08113f02 |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/audio/PushPullFIFOTest.cpp |
@@ -0,0 +1,135 @@ |
+// Copyright 2016 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 "platform/audio/PushPullFIFO.h" |
+ |
+#include "platform/audio/AudioUtilities.h" |
+#include "platform/testing/TestingPlatformSupport.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "wtf/PtrUtil.h" |
+#include <memory> |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
+void fillBusWithValue(AudioBus* targetBus, float value) { |
+ for (unsigned c = 0; c < targetBus->numberOfChannels(); ++c) { |
+ float* busChannel = targetBus->channel(c)->mutableData(); |
+ std::fill(busChannel, busChannel + targetBus->channel(c)->length(), value); |
+ } |
+} |
+ |
+// Fills up an array with a series of indexes; [0, 1, 2, 3, 4, 5, 6, ....] |
+void fillBusWithIncrementalIndex(AudioBus* targetBus) { |
+ for (unsigned c = 0; c < targetBus->numberOfChannels(); ++c) { |
+ float* busChannel = targetBus->channel(c)->mutableData(); |
+ for (unsigned i = 0; i < targetBus->channel(c)->length(); ++i) |
+ busChannel[i] = i; |
+ } |
+} |
+ |
+bool verifyBusValueAtIndex(AudioBus* targetBus, int index, float value) { |
+ for (unsigned c = 0; c < targetBus->numberOfChannels(); ++c) { |
+ float* busChannel = targetBus->channel(c)->mutableData(); |
+ if (busChannel[index] != value) { |
+ LOG(ERROR) << "[FAIL] index = " << index |
+ << " value = " << busChannel[index]; |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+struct PushPullFIFOTestParam { |
+ PushPullFIFOTestParam(size_t fifoSize, |
+ unsigned numberOfChannels, |
+ size_t framesAvailable, |
+ size_t framesToPush, |
+ size_t framesToPull, |
+ unsigned expectedReadIndex, |
+ float expectedValueAtReadIndex, |
+ unsigned expectedWriteIndex, |
+ float expectedValueAtWriteIndex) |
+ : fifoSize(fifoSize), |
+ numberOfChannels(numberOfChannels), |
+ framesAvailable(framesAvailable), |
+ framesToPush(framesToPush), |
+ framesToPull(framesToPull), |
+ expectedReadIndex(expectedReadIndex), |
+ expectedValueAtReadIndex(expectedValueAtReadIndex), |
+ expectedWriteIndex(expectedWriteIndex), |
+ expectedValueAtWriteIndex(expectedValueAtWriteIndex) {} |
+ ~PushPullFIFOTestParam() {} |
+ |
+ const size_t fifoSize; |
o1ka
2017/01/24 11:09:02
Could you provide comments on what each value mean
hongchan
2017/01/26 22:33:18
I have restructured the unit test. PTAL the new de
|
+ const unsigned numberOfChannels; |
+ const size_t framesAvailable; |
+ const size_t framesToPush; |
+ const size_t framesToPull; |
+ const unsigned expectedReadIndex; |
+ const float expectedValueAtReadIndex; |
+ const unsigned expectedWriteIndex; |
+ const float expectedValueAtWriteIndex; |
+}; |
+ |
+std::ostream& operator<<(std::ostream& out, |
+ const PushPullFIFOTestParam& param) { |
+ out << "fifoSze=" << param.fifoSize |
+ << " numberOfChannels=" << param.numberOfChannels |
+ << " framesAvailable=" << param.framesAvailable |
+ << " framesToPush=" << param.framesToPush |
+ << " framesToPull=" << param.framesToPull |
+ << " expectedReadIndex=" << param.expectedReadIndex |
+ << " expectedValueAtReadIndex=" << param.expectedValueAtReadIndex |
+ << " expectedWriteIndex=" << param.expectedWriteIndex |
+ << " expectedValueAtWriteIndex=" << param.expectedValueAtWriteIndex; |
+ return out; |
+} |
+ |
+class PushPullFIFOTest |
+ : public ::testing::TestWithParam<PushPullFIFOTestParam> {}; |
+ |
+TEST_P(PushPullFIFOTest, BasicOperation) { |
+ std::unique_ptr<PushPullFIFO> fifo = WTF::wrapUnique( |
+ new PushPullFIFO(GetParam().numberOfChannels, GetParam().fifoSize)); |
+ |
+ // Simulates existing frames in the FIFO. These frames are initialized with |
+ // -1. |
+ RefPtr<AudioBus> existingBus = |
+ AudioBus::create(GetParam().numberOfChannels, GetParam().framesAvailable); |
+ fillBusWithValue(existingBus.get(), -1); |
+ fifo->push(existingBus.get()); |
+ |
+ // Then push data with given length. The content of input bus is a series of |
+ // incremental index. |
+ RefPtr<AudioBus> inputBus = |
+ AudioBus::create(GetParam().numberOfChannels, GetParam().framesToPush); |
+ fillBusWithIncrementalIndex(inputBus.get()); |
+ fifo->push(inputBus.get()); |
+ |
+ // Lastly, pull the data from FIFO to reach the state required by |
+ // |framesToPull|. |
+ RefPtr<AudioBus> outputBus = |
o1ka
2017/01/24 11:09:02
This looks good.
Another set of cases to test is l
hongchan
2017/01/26 22:33:18
Done.
|
+ AudioBus::create(GetParam().numberOfChannels, GetParam().framesToPull); |
+ fifo->pull(outputBus.get(), GetParam().framesToPull); |
+ |
+ // Verify the position of current indexes and their values. |
o1ka
2017/01/24 11:09:02
We also want to check the content of |outputBus|.
hongchan
2017/01/26 22:33:18
Done.
|
+ EXPECT_EQ(true, |
+ verifyBusValueAtIndex(fifo->bus(), GetParam().expectedReadIndex, |
+ GetParam().expectedValueAtReadIndex)); |
+ EXPECT_EQ(true, |
+ verifyBusValueAtIndex(fifo->bus(), GetParam().expectedWriteIndex, |
+ GetParam().expectedValueAtWriteIndex)); |
+} |
+ |
+INSTANTIATE_TEST_CASE_P( |
+ PushPullFIFOTest, |
+ PushPullFIFOTest, |
+ ::testing::Values( |
+ PushPullFIFOTestParam(8192, 2, 128, 256, 128, 128, 0, 384, 0))); |
+ |
+} // namespace |
+ |
+} // namespace blink |