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@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@660: bool ArraysNear(const ArrayX& expected, const ArrayX& actual, ronw@660: double precision) { ronw@660: return (expected - actual).cwiseAbs().maxCoeff() <= precision; ronw@660: } ronw@660: ronw@660: #endif // CARFAC_TEST_UTIL_H