ronw@660: // Copyright 2013, Google, Inc. ronw@660: // Author: Ron Weiss ronw@660: // ronw@660: // This C++ file is part of an implementation of Lyon's cochlear model: ronw@660: // "Cascade of Asymmetric Resonators with Fast-Acting Compression" ronw@660: // to supplement Lyon's upcoming book "Human and Machine Hearing" ronw@660: // ronw@660: // Licensed under the Apache License, Version 2.0 (the "License"); ronw@660: // you may not use this file except in compliance with the License. ronw@660: // You may obtain a copy of the License at ronw@660: // ronw@660: // http://www.apache.org/licenses/LICENSE-2.0 ronw@660: // ronw@660: // Unless required by applicable law or agreed to in writing, software ronw@660: // distributed under the License is distributed on an "AS IS" BASIS, ronw@660: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ronw@660: // See the License for the specific language governing permissions and ronw@660: // limitations under the License. ronw@660: ronw@660: // Shared test utilities. ronw@660: ronw@660: #ifndef CARFAC_TEST_UTIL_H ronw@660: #define CARFAC_TEST_UTIL_H ronw@660: ronw@660: #include ronw@660: #include ronw@660: #include ronw@660: ronw@660: #include ronw@660: ronw@661: #include "gtest/gtest.h" ronw@661: ronw@660: #include "common.h" ronw@660: ronw@660: // Location of the text files produced by 'CARFAC_GenerateTestData.m' for ronw@660: // comparing the ouput of the Matlab implementation with the C++ one. ronw@660: static const char* kTestDataDir = "./test_data/"; ronw@660: ronw@660: // Reads a matrix (size rows vector of size columns Container objects) ronw@660: // from a text file written using the Matlab dlmwrite function. ronw@660: template ronw@660: std::vector LoadMatrix(const std::string& filename, int rows, ronw@660: int columns) { ronw@660: std::string fullfile = kTestDataDir + filename; ronw@660: std::ifstream file(fullfile.c_str()); ronw@660: std::vector output; ronw@660: if (ColMajor) { ronw@660: output.assign(rows, Container(columns)); ronw@660: } else { ronw@660: output.assign(columns, Container(rows)); ronw@660: } ronw@660: if (file.is_open()) { ronw@660: for (int i = 0; i < rows; ++i) { ronw@660: for (int j = 0; j < columns; ++j) { ronw@660: if (ColMajor) { ronw@660: file >> output[i][j]; ronw@660: } else { ronw@660: file >> output[j][i]; ronw@660: } ronw@660: } ronw@660: } ronw@660: } ronw@660: file.close(); ronw@660: return output; ronw@660: } ronw@660: ronw@661: void WriteMatrix(const std::string& filename, const ArrayXX& matrix) { ronw@661: std::string fullfile = kTestDataDir + filename; ronw@661: std::ofstream ofile(fullfile.c_str()); ronw@661: const int kPrecision = 9; ronw@661: ofile.precision(kPrecision); ronw@661: if (ofile.is_open()) { ronw@661: Eigen::IOFormat ioformat(kPrecision, Eigen::DontAlignCols); ronw@661: ofile << matrix.format(ioformat) << std::endl; ronw@661: } ronw@661: ofile.close(); ronw@661: } ronw@661: ronw@661: void AssertArrayNear(const ArrayX& expected, const ArrayX& actual, ronw@661: double precision) { ronw@661: ArrayX abs_difference = (expected - actual).cwiseAbs(); ronw@661: ASSERT_TRUE(abs_difference.maxCoeff() <= precision) ronw@661: << "expected differs from actual by more than " << precision ronw@661: << "\n max(abs(expected - actual)) = " << abs_difference.maxCoeff() ronw@661: << "\n max(abs(expected)) = " << expected.cwiseAbs().maxCoeff() ronw@661: << "\n max(abs(actual)) = " << actual.cwiseAbs().maxCoeff() ronw@661: << "\n" << abs_difference; ronw@660: } ronw@660: ronw@660: #endif // CARFAC_TEST_UTIL_H