blob: 14093fa4a6df0d5715daf6d689664a2557cbbe80 [file] [log] [blame]
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +00001/*
2 * Copyright (c) 2011 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
11#include "gtest/gtest.h"
12
kjellander@webrtc.org0403ef42011-11-17 08:35:47 +000013#include "common_audio/resampler/include/resampler.h"
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000014
15// TODO(andrew): this is a work-in-progress. Many more tests are needed.
16
17namespace webrtc {
18namespace {
19const ResamplerType kTypes[] = {
20 kResamplerSynchronous,
21 kResamplerAsynchronous,
22 kResamplerSynchronousStereo,
23 kResamplerAsynchronousStereo
24 // kResamplerInvalid excluded
25};
26const size_t kTypesSize = sizeof(kTypes) / sizeof(*kTypes);
27
28// Rates we must support.
29const int kMaxRate = 96000;
30const int kRates[] = {
31 8000,
32 16000,
33 32000,
34 44000,
35 48000,
36 kMaxRate
37};
38const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates);
39const size_t kDataSize = kMaxRate / 100;
40
41// TODO(andrew): should we be supporting these combinations?
42bool ValidRates(int in_rate, int out_rate) {
43 // Not the most compact notation, for clarity.
44 if ((in_rate == 44000 && (out_rate == 48000 || out_rate == 96000)) ||
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000045 (out_rate == 44000 && (in_rate == 48000 || in_rate == 96000))) {
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000046 return false;
47 }
48
49 return true;
50}
51
52class ResamplerTest : public testing::Test {
53 protected:
54 ResamplerTest();
55 virtual void SetUp();
56 virtual void TearDown();
57
58 Resampler rs_;
59 int16_t data_in_[kDataSize];
60 int16_t data_out_[kDataSize];
61};
62
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000063ResamplerTest::ResamplerTest() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000064
bjornv@webrtc.org0edb25d2011-12-13 09:06:54 +000065void ResamplerTest::SetUp() {
66 // Initialize input data with anything. The tests are content independent.
67 memset(data_in_, 1, sizeof(data_in_));
68}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000069
bjornv@webrtc.org48b68c02011-11-23 13:50:41 +000070void ResamplerTest::TearDown() {}
andrew@webrtc.org19eefdc2011-09-14 17:02:44 +000071
72TEST_F(ResamplerTest, Reset) {
73 // The only failure mode for the constructor is if Reset() fails. For the
74 // time being then (until an Init function is added), we rely on Reset()
75 // to test the constructor.
76
77 // Check that all required combinations are supported.
78 for (size_t i = 0; i < kRatesSize; ++i) {
79 for (size_t j = 0; j < kRatesSize; ++j) {
80 for (size_t k = 0; k < kTypesSize; ++k) {
81 std::ostringstream ss;
82 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
83 << ", type: " << kTypes[k];
84 SCOPED_TRACE(ss.str());
85 if (ValidRates(kRates[i], kRates[j]))
86 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kTypes[k]));
87 else
88 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kTypes[k]));
89 }
90 }
91 }
92}
93
94TEST_F(ResamplerTest, Synchronous) {
95 for (size_t i = 0; i < kRatesSize; ++i) {
96 for (size_t j = 0; j < kRatesSize; ++j) {
97 std::ostringstream ss;
98 ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
99 SCOPED_TRACE(ss.str());
100
101 if (ValidRates(kRates[i], kRates[j])) {
102 int in_length = kRates[i] / 100;
103 int out_length = 0;
104 EXPECT_EQ(0, rs_.Reset(kRates[i], kRates[j], kResamplerSynchronous));
105 EXPECT_EQ(0, rs_.Push(data_in_, in_length, data_out_, kDataSize,
106 out_length));
107 EXPECT_EQ(kRates[j] / 100, out_length);
108 } else {
109 EXPECT_EQ(-1, rs_.Reset(kRates[i], kRates[j], kResamplerSynchronous));
110 }
111 }
112 }
113
114 // TODO(andrew): test stereo.
115}
116} // namespace
117} // namespace webrtc