aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_processing/beamformer/matrix_test_helpers.h
blob: 9891a8220cbcacd8735c6e1d27f21f9c84180b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_TEST_HELPERS_H_
#define WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_TEST_HELPERS_H_

#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/modules/audio_processing/beamformer/complex_matrix.h"
#include "webrtc/modules/audio_processing/beamformer/matrix.h"

namespace {
const float kTolerance = 0.001f;
}

namespace webrtc {

using std::complex;

// Functions used in both matrix_unittest and complex_matrix_unittest.
class MatrixTestHelpers {
 public:
  template <typename T>
  static void ValidateMatrixEquality(const Matrix<T>& expected,
                                     const Matrix<T>& actual) {
    EXPECT_EQ(expected.num_rows(), actual.num_rows());
    EXPECT_EQ(expected.num_columns(), actual.num_columns());

    const T* const* expected_elements = expected.elements();
    const T* const* actual_elements = actual.elements();
    for (size_t i = 0; i < expected.num_rows(); ++i) {
      for (size_t j = 0; j < expected.num_columns(); ++j) {
        EXPECT_EQ(expected_elements[i][j], actual_elements[i][j]);
      }
    }
  }

  static void ValidateMatrixEqualityFloat(const Matrix<float>& expected,
                                          const Matrix<float>& actual) {
    EXPECT_EQ(expected.num_rows(), actual.num_rows());
    EXPECT_EQ(expected.num_columns(), actual.num_columns());

    const float* const* expected_elements = expected.elements();
    const float* const* actual_elements = actual.elements();
    for (size_t i = 0; i < expected.num_rows(); ++i) {
      for (size_t j = 0; j < expected.num_columns(); ++j) {
        EXPECT_NEAR(expected_elements[i][j], actual_elements[i][j], kTolerance);
      }
    }
  }

  static void ValidateMatrixEqualityComplexFloat(
      const Matrix<complex<float> >& expected,
      const Matrix<complex<float> >& actual) {
    EXPECT_EQ(expected.num_rows(), actual.num_rows());
    EXPECT_EQ(expected.num_columns(), actual.num_columns());

    const complex<float>* const* expected_elements = expected.elements();
    const complex<float>* const* actual_elements = actual.elements();
    for (size_t i = 0; i < expected.num_rows(); ++i) {
      for (size_t j = 0; j < expected.num_columns(); ++j) {
        EXPECT_NEAR(expected_elements[i][j].real(),
                    actual_elements[i][j].real(),
                    kTolerance);
        EXPECT_NEAR(expected_elements[i][j].imag(),
                    actual_elements[i][j].imag(),
                    kTolerance);
      }
    }
  }

  static void ValidateMatrixNearEqualityComplexFloat(
      const Matrix<complex<float> >& expected,
      const Matrix<complex<float> >& actual,
      float tolerance) {
    EXPECT_EQ(expected.num_rows(), actual.num_rows());
    EXPECT_EQ(expected.num_columns(), actual.num_columns());

    const complex<float>* const* expected_elements = expected.elements();
    const complex<float>* const* actual_elements = actual.elements();
    for (size_t i = 0; i < expected.num_rows(); ++i) {
      for (size_t j = 0; j < expected.num_columns(); ++j) {
        EXPECT_NEAR(expected_elements[i][j].real(),
                    actual_elements[i][j].real(),
                    tolerance);
        EXPECT_NEAR(expected_elements[i][j].imag(),
                    actual_elements[i][j].imag(),
                    tolerance);
      }
    }
  }
};

}  // namespace webrtc

#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_TEST_HELPERS_H_