Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / common_audio / real_fourier_unittest.cc
1 /*
2  *  Copyright (c) 2014 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 "webrtc/common_audio/real_fourier.h"
12
13 #include <stdlib.h>
14
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace webrtc {
18
19 using std::complex;
20
21 TEST(RealFourierStaticsTest, AllocatorAlignment) {
22   {
23     RealFourier::fft_real_scoper real;
24     real = RealFourier::AllocRealBuffer(3);
25     ASSERT_TRUE(real.get() != nullptr);
26     int64_t ptr_value = reinterpret_cast<int64_t>(real.get());
27     ASSERT_EQ(ptr_value % RealFourier::kFftBufferAlignment, 0);
28   }
29   {
30     RealFourier::fft_cplx_scoper cplx;
31     cplx = RealFourier::AllocCplxBuffer(3);
32     ASSERT_TRUE(cplx.get() != nullptr);
33     int64_t ptr_value = reinterpret_cast<int64_t>(cplx.get());
34     ASSERT_EQ(ptr_value % RealFourier::kFftBufferAlignment, 0);
35   }
36 }
37
38 TEST(RealFourierStaticsTest, OrderComputation) {
39   ASSERT_EQ(RealFourier::FftOrder(2000000), -1);
40   ASSERT_EQ(RealFourier::FftOrder((1 << RealFourier::kMaxFftOrder) + 1), -1);
41   ASSERT_EQ(RealFourier::FftOrder(1 << RealFourier::kMaxFftOrder),
42             RealFourier::kMaxFftOrder);
43   ASSERT_EQ(RealFourier::FftOrder(13), 4);
44   ASSERT_EQ(RealFourier::FftOrder(32), 5);
45   ASSERT_EQ(RealFourier::FftOrder(2), 1);
46   ASSERT_EQ(RealFourier::FftOrder(1), 0);
47   ASSERT_EQ(RealFourier::FftOrder(0), 0);
48 }
49
50 TEST(RealFourierStaticsTest, ComplexLengthComputation) {
51   ASSERT_EQ(RealFourier::ComplexLength(1), 2);
52   ASSERT_EQ(RealFourier::ComplexLength(2), 3);
53   ASSERT_EQ(RealFourier::ComplexLength(3), 5);
54   ASSERT_EQ(RealFourier::ComplexLength(4), 9);
55   ASSERT_EQ(RealFourier::ComplexLength(5), 17);
56   ASSERT_EQ(RealFourier::ComplexLength(7), 65);
57 }
58
59 class RealFourierTest : public ::testing::Test {
60  protected:
61   RealFourierTest()
62       : rf_(new RealFourier(2)),
63         real_buffer_(RealFourier::AllocRealBuffer(4)),
64         cplx_buffer_(RealFourier::AllocCplxBuffer(3)) {}
65
66   ~RealFourierTest() {
67     delete rf_;
68   }
69
70   const RealFourier* rf_;
71   const RealFourier::fft_real_scoper real_buffer_;
72   const RealFourier::fft_cplx_scoper cplx_buffer_;
73 };
74
75 TEST_F(RealFourierTest, SimpleForwardTransform) {
76   real_buffer_[0] = 1.0f;
77   real_buffer_[1] = 2.0f;
78   real_buffer_[2] = 3.0f;
79   real_buffer_[3] = 4.0f;
80
81   rf_->Forward(real_buffer_.get(), cplx_buffer_.get());
82
83   ASSERT_NEAR(cplx_buffer_[0].real(), 10.0f, 1e-8f);
84   ASSERT_NEAR(cplx_buffer_[0].imag(), 0.0f, 1e-8f);
85   ASSERT_NEAR(cplx_buffer_[1].real(), -2.0f, 1e-8f);
86   ASSERT_NEAR(cplx_buffer_[1].imag(), 2.0f, 1e-8f);
87   ASSERT_NEAR(cplx_buffer_[2].real(), -2.0f, 1e-8f);
88   ASSERT_NEAR(cplx_buffer_[2].imag(), 0.0f, 1e-8f);
89 }
90
91 TEST_F(RealFourierTest, SimpleBackwardTransform) {
92   cplx_buffer_[0] = complex<float>(10.0f, 0.0f);
93   cplx_buffer_[1] = complex<float>(-2.0f, 2.0f);
94   cplx_buffer_[2] = complex<float>(-2.0f, 0.0f);
95
96   rf_->Inverse(cplx_buffer_.get(), real_buffer_.get());
97
98   ASSERT_NEAR(real_buffer_[0], 1.0f, 1e-8f);
99   ASSERT_NEAR(real_buffer_[1], 2.0f, 1e-8f);
100   ASSERT_NEAR(real_buffer_[2], 3.0f, 1e-8f);
101   ASSERT_NEAR(real_buffer_[3], 4.0f, 1e-8f);
102 }
103
104 }  // namespace webrtc
105