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 #ifndef MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_TEST_HELPERS_H_
12 #define MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_TEST_HELPERS_H_
13 
14 #include "modules/audio_processing/beamformer/complex_matrix.h"
15 #include "modules/audio_processing/beamformer/matrix.h"
16 #include "test/gtest.h"
17 
18 namespace {
19 const float kTolerance = 0.001f;
20 }
21 
22 namespace webrtc {
23 
24 using std::complex;
25 
26 // Functions used in both matrix_unittest and complex_matrix_unittest.
27 class MatrixTestHelpers {
28  public:
29   template <typename T>
ValidateMatrixEquality(const Matrix<T> & expected,const Matrix<T> & actual)30   static void ValidateMatrixEquality(const Matrix<T>& expected,
31                                      const Matrix<T>& actual) {
32     EXPECT_EQ(expected.num_rows(), actual.num_rows());
33     EXPECT_EQ(expected.num_columns(), actual.num_columns());
34 
35     const T* const* expected_elements = expected.elements();
36     const T* const* actual_elements = actual.elements();
37     for (size_t i = 0; i < expected.num_rows(); ++i) {
38       for (size_t j = 0; j < expected.num_columns(); ++j) {
39         EXPECT_EQ(expected_elements[i][j], actual_elements[i][j]);
40       }
41     }
42   }
43 
ValidateMatrixEqualityFloat(const Matrix<float> & expected,const Matrix<float> & actual)44   static void ValidateMatrixEqualityFloat(const Matrix<float>& expected,
45                                           const Matrix<float>& actual) {
46     EXPECT_EQ(expected.num_rows(), actual.num_rows());
47     EXPECT_EQ(expected.num_columns(), actual.num_columns());
48 
49     const float* const* expected_elements = expected.elements();
50     const float* const* actual_elements = actual.elements();
51     for (size_t i = 0; i < expected.num_rows(); ++i) {
52       for (size_t j = 0; j < expected.num_columns(); ++j) {
53         EXPECT_NEAR(expected_elements[i][j], actual_elements[i][j], kTolerance);
54       }
55     }
56   }
57 
ValidateMatrixEqualityComplexFloat(const Matrix<complex<float>> & expected,const Matrix<complex<float>> & actual)58   static void ValidateMatrixEqualityComplexFloat(
59       const Matrix<complex<float> >& expected,
60       const Matrix<complex<float> >& actual) {
61     EXPECT_EQ(expected.num_rows(), actual.num_rows());
62     EXPECT_EQ(expected.num_columns(), actual.num_columns());
63 
64     const complex<float>* const* expected_elements = expected.elements();
65     const complex<float>* const* actual_elements = actual.elements();
66     for (size_t i = 0; i < expected.num_rows(); ++i) {
67       for (size_t j = 0; j < expected.num_columns(); ++j) {
68         EXPECT_NEAR(expected_elements[i][j].real(),
69                     actual_elements[i][j].real(),
70                     kTolerance);
71         EXPECT_NEAR(expected_elements[i][j].imag(),
72                     actual_elements[i][j].imag(),
73                     kTolerance);
74       }
75     }
76   }
77 
ValidateMatrixNearEqualityComplexFloat(const Matrix<complex<float>> & expected,const Matrix<complex<float>> & actual,float tolerance)78   static void ValidateMatrixNearEqualityComplexFloat(
79       const Matrix<complex<float> >& expected,
80       const Matrix<complex<float> >& actual,
81       float tolerance) {
82     EXPECT_EQ(expected.num_rows(), actual.num_rows());
83     EXPECT_EQ(expected.num_columns(), actual.num_columns());
84 
85     const complex<float>* const* expected_elements = expected.elements();
86     const complex<float>* const* actual_elements = actual.elements();
87     for (size_t i = 0; i < expected.num_rows(); ++i) {
88       for (size_t j = 0; j < expected.num_columns(); ++j) {
89         EXPECT_NEAR(expected_elements[i][j].real(),
90                     actual_elements[i][j].real(),
91                     tolerance);
92         EXPECT_NEAR(expected_elements[i][j].imag(),
93                     actual_elements[i][j].imag(),
94                     tolerance);
95       }
96     }
97   }
98 };
99 
100 }  // namespace webrtc
101 
102 #endif  // MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_TEST_HELPERS_H_
103