diff --git a/tests/nsl/dft/CMakeLists.txt b/tests/nsl/dft/CMakeLists.txt index 3f824932c..0e5fa640c 100644 --- a/tests/nsl/dft/CMakeLists.txt +++ b/tests/nsl/dft/CMakeLists.txt @@ -1,13 +1,13 @@ INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) -add_executable (nsldfttest NSLDFTTest.cpp) +add_executable (nsldfttest NSLDFTTest.cpp ../NSLTest.cpp ../../CommonTest.cpp) target_link_libraries(nsldfttest Qt5::Test) target_link_libraries(nsldfttest ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) IF (FFTW_FOUND) target_link_libraries(nsldfttest ${FFTW_LIBRARIES}) ENDIF () target_link_libraries(nsldfttest labplot2lib) add_test(NAME nsldfttest COMMAND nsldfttest) diff --git a/tests/nsl/dft/NSLDFTTest.cpp b/tests/nsl/dft/NSLDFTTest.cpp index 87a13c9e8..0ed5f32fd 100644 --- a/tests/nsl/dft/NSLDFTTest.cpp +++ b/tests/nsl/dft/NSLDFTTest.cpp @@ -1,288 +1,283 @@ /*************************************************************************** File : NSLDFTTest.cpp Project : LabPlot Description : NSL Tests for DFT -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "NSLDFTTest.h" #include "backend/lib/macros.h" extern "C" { #include "backend/nsl/nsl_dft.h" } -void NSLDFTTest::initTestCase() { - const QString currentDir = __FILE__; - m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); -} - #define ONESIDED 0 #define TWOSIDED 1 const int N = 10; //############################################################################## //################# one sided tests //############################################################################## void NSLDFTTest::testOnesided_real() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {10, 2, -5.85410196624968, 2, 0.854101966249685}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_real); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testOnesided_imag() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {0, -4.97979656976556, 0, 0.449027976579585, 0}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_imag); for (unsigned int i = 0; i < N/2; i++) { DEBUG(std::setprecision(15) << data[i]); if (i == 2) QCOMPARE(data[i] + 1., 1.); // -1.11022302462516e-16 (Win) else QCOMPARE(data[i], result[i]); } } void NSLDFTTest::testOnesided_magnitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {10, 5.36641163872553, 5.85410196624968, 2.04978684837013, 0.854101966249685}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_magnitude); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testOnesided_amplitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {1, 1.07328232774511, 1.17082039324994, 0.409957369674026, 0.170820393249937}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_amplitude); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testOnesided_power() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {10, 5.75967477524977, 6.85410196624968, 0.840325224750231, 0.145898033750316}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_power); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testOnesided_phase() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {0, 1.18889174012102, -3.14159265358979, -0.220851801285041, 0}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_phase); for (unsigned int i = 0; i < N/2; i++) { DEBUG(std::setprecision(15) << data[i]); if (i == 2) // sign can be + or - QCOMPARE(fabs(data[i]), fabs(result[i])); else QCOMPARE(data[i], result[i]); } } void NSLDFTTest::testOnesided_dB() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {0, 0.614279569861449, 1.36980556663896, -7.7452260401377, -15.3492056533593}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_dB); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testOnesided_squaremagnitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {100, 28.7983738762488, 34.2705098312484, 4.20162612375116, 0.729490168751578}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_squaremagnitude); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testOnesided_squareamplitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {1, 1.15193495504995, 1.37082039324994, 0.168065044950046, 0.0291796067500631}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_squareamplitude); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testOnesided_normdB() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {-1.36980556663896, -0.755525996777514, 0, -9.11503160677666, -16.7190112199983}; nsl_dft_transform(data, 1, N, ONESIDED, nsl_dft_result_normdB); for (unsigned int i = 0; i < N/2; i++) QCOMPARE(data[i], result[i]); } //############################################################################## //################# two sided tests //############################################################################## void NSLDFTTest::testTwosided_real() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {10, 2, -5.85410196624968, 2, 0.854101966249685, 2, 0.854101966249685, 2, -5.85410196624968, 2}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_real); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testTwosided_imag() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {0, -4.97979656976556, 0, 0.449027976579585, 0, 0, 0, -0.449027976579585, 0, 4.97979656976556}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_imag); for (unsigned int i = 0; i < N; i++) { DEBUG(std::setprecision(15) << data[i]); if (i == 2 || i == 8) QCOMPARE(data[i] + 1., 1.); // -1.11022302462516e-16 (Win) else QCOMPARE(data[i], result[i]); } } void NSLDFTTest::testTwosided_magnitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {10, 5.36641163872553, 5.85410196624968, 2.04978684837013, 0.854101966249685, 2, 0.854101966249685, 2.04978684837013, 5.85410196624968, 5.36641163872553}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_magnitude); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testTwosided_amplitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {1, 1.07328232774511, 1.17082039324994, 0.409957369674026, 0.170820393249937, 0.4, 0.170820393249937, 0.409957369674026, 1.17082039324994, 1.07328232774511}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_amplitude); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testTwosided_power() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {10, 5.75967477524977, 6.85410196624968, 0.840325224750231, 0.145898033750316, 0.8, 0.145898033750316, 0.840325224750231, 6.85410196624968, 5.75967477524977}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_power); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testTwosided_phase() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {0, 1.18889174012102, -3.14159265358979, -0.220851801285041, 0, 0, 0, 0.220851801285041, 3.14159265358979, -1.18889174012102}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_phase); for (unsigned int i = 0; i < N; i++) { DEBUG(std::setprecision(15) << data[i]); if (i == 2 || i == 8) // sign can be + or - QCOMPARE(fabs(data[i]), fabs(result[i])); else QCOMPARE(data[i], result[i]); } } void NSLDFTTest::testTwosided_dB() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {0, 0.614279569861449, 1.36980556663896, -7.7452260401377, -15.3492056533593, -7.95880017344075, -15.3492056533593, -7.7452260401377, 1.36980556663896, 0.614279569861449}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_dB); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testTwosided_squaremagnitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {100, 28.7983738762488, 34.2705098312484, 4.20162612375116, 0.729490168751578, 4, 0.729490168751578, 4.20162612375116, 34.2705098312484, 28.7983738762488}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_squaremagnitude); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testTwosided_squareamplitude() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {1, 1.15193495504995, 1.37082039324994, 0.168065044950046, 0.0291796067500631, 0.16, 0.0291796067500631, 0.168065044950046, 1.37082039324994, 1.15193495504995}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_squareamplitude); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } void NSLDFTTest::testTwosided_normdB() { double data[] = {1, 1, 3, 3, 1, -1, 0, 1, 1, 0}; double result[] = {-1.36980556663896, -0.755525996777514, 0, -9.11503160677666, -16.7190112199983, -9.32860574007971, -16.7190112199983, -9.11503160677666, 0, -0.755525996777514}; nsl_dft_transform(data, 1, N, TWOSIDED, nsl_dft_result_normdB); for (unsigned int i = 0; i < N; i++) QCOMPARE(data[i], result[i]); } //############################################################################## //################# performance //############################################################################## #ifdef HAVE_FFTW3 const int NN = 1e6; #else // GSL is much slower const int NN = 1e6; #endif void NSLDFTTest::testPerformance_onesided() { double* data = new double[NN]; for (int i = 0; i < NN; i++) data[i] = 1.; QBENCHMARK { nsl_dft_transform(data, 1, NN, ONESIDED, nsl_dft_result_real); } delete[] data; } void NSLDFTTest::testPerformance_twosided() { double* data = new double[NN]; for (int i = 0; i < NN; i++) data[i] = 1.; QBENCHMARK { nsl_dft_transform(data, 1, NN, TWOSIDED, nsl_dft_result_real); } delete[] data; } QTEST_MAIN(NSLDFTTest) diff --git a/tests/nsl/dft/NSLDFTTest.h b/tests/nsl/dft/NSLDFTTest.h index 076ca54b5..13c6c7b5e 100644 --- a/tests/nsl/dft/NSLDFTTest.h +++ b/tests/nsl/dft/NSLDFTTest.h @@ -1,66 +1,64 @@ /*************************************************************************** File : NSLDFTTest.h Project : LabPlot Description : NSL Tests for DFT -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef NSLDFTTEST_H #define NSLDFTTEST_H -#include +#include "../NSLTest.h" -class NSLDFTTest : public QObject { +class NSLDFTTest : public NSLTest { Q_OBJECT private slots: - void initTestCase(); - // one sided void testOnesided_real(); void testOnesided_imag(); void testOnesided_magnitude(); void testOnesided_amplitude(); void testOnesided_power(); void testOnesided_phase(); void testOnesided_dB(); void testOnesided_squaremagnitude(); void testOnesided_squareamplitude(); void testOnesided_normdB(); // two sided void testTwosided_real(); void testTwosided_imag(); void testTwosided_magnitude(); void testTwosided_amplitude(); void testTwosided_power(); void testTwosided_phase(); void testTwosided_dB(); void testTwosided_squaremagnitude(); void testTwosided_squareamplitude(); void testTwosided_normdB(); // performance void testPerformance_onesided(); void testPerformance_twosided(); private: QString m_dataDir; }; #endif diff --git a/tests/nsl/diff/CMakeLists.txt b/tests/nsl/diff/CMakeLists.txt index 157ea4d04..9185c91c6 100644 --- a/tests/nsl/diff/CMakeLists.txt +++ b/tests/nsl/diff/CMakeLists.txt @@ -1,9 +1,9 @@ INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) -add_executable (nsldifftest NSLDiffTest.cpp) +add_executable (nsldifftest NSLDiffTest.cpp ../NSLTest.cpp ../../CommonTest.cpp) target_link_libraries(nsldifftest Qt5::Test) target_link_libraries(nsldifftest ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) target_link_libraries(nsldifftest labplot2lib) add_test(NAME nsldifftest COMMAND nsldifftest) diff --git a/tests/nsl/diff/NSLDiffTest.cpp b/tests/nsl/diff/NSLDiffTest.cpp index 1d3bcf6c6..80223b953 100644 --- a/tests/nsl/diff/NSLDiffTest.cpp +++ b/tests/nsl/diff/NSLDiffTest.cpp @@ -1,210 +1,204 @@ /*************************************************************************** File : NSLDiffTest.cpp Project : LabPlot Description : NSL Tests for numerical differentiation -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "NSLDiffTest.h" -#include "backend/lib/macros.h" extern "C" { #include "backend/nsl/nsl_diff.h" } -void NSLDiffTest::initTestCase() { - const QString currentDir = __FILE__; - m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); -} - //############################################################################## //################# first derivative tests //############################################################################## const int N = 7; double xdata[] = {1, 2, 4, 8, 16, 32, 64}; void NSLDiffTest::testFirst_order2() { double ydata[] = {1, 4, 16, 64, 256, 1024, 4096}; int status = nsl_diff_first_deriv(xdata, ydata, N, 2); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], 2 * xdata[i]); } void NSLDiffTest::testFirst_order4() { double ydata[] = {1, 8, 64, 512, 4096, 32768, 262144}; int status = nsl_diff_first_deriv(xdata, ydata, N, 4); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], 3 * xdata[i]*xdata[i]); } void NSLDiffTest::testFirst_avg() { double ydata[] = {1, 4, 16, 64, 256, 1024, 4096}; double result[] = {3, 4.5, 9, 18, 36, 72, 96}; int status = nsl_diff_first_deriv_avg(xdata, ydata, N); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], result[i]); } //############################################################################## //################# second derivative tests //############################################################################## void NSLDiffTest::testSecond_order1() { double ydata[] = {1, 4, 16, 64, 256, 1024, 4096}; int status = nsl_diff_second_deriv(xdata, ydata, N, 1); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], 2.); } void NSLDiffTest::testSecond_order2() { double ydata[] = {1, 4, 16, 64, 256, 1024, 4096}; int status = nsl_diff_second_deriv(xdata, ydata, N, 2); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], 2.); } void NSLDiffTest::testSecond_order3() { double ydata[] = {1, 8, 64, 512, 4096, 32768, 262144}; int status = nsl_diff_second_deriv(xdata, ydata, N, 3); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], 6. * xdata[i]); } //############################################################################## //################# higher derivative tests //############################################################################## void NSLDiffTest::testThird_order2() { double ydata[] = {1, 8, 64, 512, 4096, 32768, 262144}; int status = nsl_diff_third_deriv(xdata, ydata, N, 2); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], 6.); } void NSLDiffTest::testFourth_order1() { double ydata[] = {1, 8, 64, 512, 4096, 32768, 262144}; int status = nsl_diff_fourth_deriv(xdata, ydata, N, 1); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i], 0.); } void NSLDiffTest::testFourth_order3() { double ydata[] = {1, 8, 64, 512, 4096, 32768, 262144}; int status = nsl_diff_fourth_deriv(xdata, ydata, N, 3); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i] + 1., 1.); } void NSLDiffTest::testFifth_order2() { double ydata[] = {1, 8, 64, 512, 4096, 32768, 262144}; int status = nsl_diff_fifth_deriv(xdata, ydata, N, 2); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i] + 1., 1.); } void NSLDiffTest::testSixth_order1() { double ydata[] = {1, 8, 64, 512, 4096, 32768, 262144}; int status = nsl_diff_sixth_deriv(xdata, ydata, N, 1); QCOMPARE(status, 0); for (unsigned int i = 0; i < N; i++) QCOMPARE(ydata[i] + 1., 1.); } //############################################################################## //################# performance //############################################################################## void NSLDiffTest::testPerformance_first() { const int NN = 1e6; double* xdata = new double[NN]; double* ydata = new double[NN]; for (int i = 0; i < NN; i++) xdata[i] = ydata[i] = (double)i; QBENCHMARK { int status = nsl_diff_first_deriv(xdata, ydata, NN, 2); QCOMPARE(status, 0); } delete[] xdata; delete[] ydata; } void NSLDiffTest::testPerformance_second() { const int NN = 1e6; double* xdata = new double[NN]; double* ydata = new double[NN]; for (int i = 0; i < NN; i++) xdata[i] = ydata[i] = (double)i; QBENCHMARK { int status = nsl_diff_second_deriv(xdata, ydata, NN, 2); QCOMPARE(status, 0); } delete[] xdata; delete[] ydata; } void NSLDiffTest::testPerformance_third() { const int NN = 1e6; double* xdata = new double[NN]; double* ydata = new double[NN]; for (int i = 0; i < NN; i++) xdata[i] = ydata[i] = (double)i; QBENCHMARK { int status = nsl_diff_third_deriv(xdata, ydata, NN, 2); QCOMPARE(status, 0); } delete[] xdata; delete[] ydata; } QTEST_MAIN(NSLDiffTest) diff --git a/tests/nsl/diff/NSLDiffTest.h b/tests/nsl/diff/NSLDiffTest.h index b0c653257..4dd92a0ed 100644 --- a/tests/nsl/diff/NSLDiffTest.h +++ b/tests/nsl/diff/NSLDiffTest.h @@ -1,59 +1,57 @@ /*************************************************************************** File : NSLDiffTest.h Project : LabPlot Description : NSL Tests for numerical differentiation -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef NSLDIFFTEST_H #define NSLDIFFTEST_H -#include +#include "../NSLTest.h" -class NSLDiffTest : public QObject { +class NSLDiffTest : public NSLTest { Q_OBJECT private slots: - void initTestCase(); - // first derivative void testFirst_order2(); void testFirst_order4(); void testFirst_avg(); // second derivative void testSecond_order1(); void testSecond_order2(); void testSecond_order3(); // higher derivative void testThird_order2(); void testFourth_order1(); void testFourth_order3(); void testFifth_order2(); void testSixth_order1(); // performance void testPerformance_first(); void testPerformance_second(); void testPerformance_third(); private: QString m_dataDir; }; #endif diff --git a/tests/nsl/int/CMakeLists.txt b/tests/nsl/int/CMakeLists.txt index 40fc40237..3eeb9ebfd 100644 --- a/tests/nsl/int/CMakeLists.txt +++ b/tests/nsl/int/CMakeLists.txt @@ -1,9 +1,9 @@ INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) -add_executable (nslinttest NSLIntTest.cpp) +add_executable (nslinttest NSLIntTest.cpp ../NSLTest.cpp ../../CommonTest.cpp) target_link_libraries(nslinttest Qt5::Test) target_link_libraries(nslinttest ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) target_link_libraries(nslinttest labplot2lib) add_test(NAME nslinttest COMMAND nslinttest) diff --git a/tests/nsl/int/NSLIntTest.cpp b/tests/nsl/int/NSLIntTest.cpp index 3103ab889..5ac13cd6e 100644 --- a/tests/nsl/int/NSLIntTest.cpp +++ b/tests/nsl/int/NSLIntTest.cpp @@ -1,186 +1,180 @@ /*************************************************************************** File : NSLIntTest.cpp Project : LabPlot Description : NSL Tests for numerical integration -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "NSLIntTest.h" -#include "backend/lib/macros.h" extern "C" { #include "backend/nsl/nsl_int.h" } -void NSLIntTest::initTestCase() { - const QString currentDir = __FILE__; - m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); -} - //############################################################################## //################# rule integral/area tests //############################################################################## const int N = 5; double xdata[] = {1, 2, 3, 5, 7}; void NSLIntTest::testRectangle_integral() { double ydata[] = {2, 2, 2, -2, -2}; int status = nsl_int_rectangle(xdata, ydata, N, 0); QCOMPARE(status, 0); QCOMPARE(ydata[N - 1], 4.); } void NSLIntTest::testRectangle_area() { double ydata[] = {2, 2, 2, -2, -2}; int status = nsl_int_rectangle(xdata, ydata, N, 1); QCOMPARE(status, 0); QCOMPARE(ydata[N - 1], 12.); } void NSLIntTest::testTrapezoid_integral() { double ydata[] = {1, 2, 3, -1, -3}; int status = nsl_int_trapezoid(xdata, ydata, N, 0); QCOMPARE(status, 0); QCOMPARE(ydata[N - 1], 2.); } void NSLIntTest::testTrapezoid_area() { double ydata[] = {1, 2, 3, -1, -3}; int status = nsl_int_trapezoid(xdata, ydata, N, 1); QCOMPARE(status, 0); QCOMPARE(ydata[N - 1], 10.5); } void NSLIntTest::test3Point_integral() { double ydata[] = {1, 2, 3, -1, -3}; int np = (int)nsl_int_simpson(xdata, ydata, N, 0); QCOMPARE(np, 3); QCOMPARE(ydata[np - 1], 4/3.); } void NSLIntTest::test4Point_integral() { double xdata2[]={1, 2, 3, 5, 7, 8, 9}; double ydata[] = {2, 2, 2, 2, 2, 2, 2, 2}; const int n = 7; int np = (int)nsl_int_simpson_3_8(xdata2, ydata, n, 0); QCOMPARE(np, 3); QCOMPARE(ydata[np - 1], 16.); } //############################################################################## //################# performance //############################################################################## void NSLIntTest::testPerformanceRectangle() { const size_t n = 1e6; double* xdata = new double[n]; double* ydata = new double[n]; for (size_t i = 0; i < n; i++) xdata[i] = (double)i; QBENCHMARK { for (size_t i = 0; i < n; i++) ydata[i] = 1.; int status = nsl_int_rectangle(xdata, ydata, n, 0); QCOMPARE(status, 0); } QCOMPARE(ydata[n - 1], (double)(n - 1)); delete[] xdata; delete[] ydata; } void NSLIntTest::testPerformanceTrapezoid() { const int n = 1e6; double* xdata = new double[n]; double* ydata = new double[n]; for (int i = 0; i < n; i++) xdata[i] = (double)i; QBENCHMARK { for (int i = 0; i < n; i++) ydata[i] = 1.; int status = nsl_int_trapezoid(xdata, ydata, n, 0); QCOMPARE(status, 0); } QCOMPARE(ydata[n - 1], (double)(n - 1)); delete[] xdata; delete[] ydata; } void NSLIntTest::testPerformance3Point() { const int n = 1e6; double* xdata = new double[n]; double* ydata = new double[n]; for (int i = 0; i < n; i++) xdata[i] = (double)i; int np; QBENCHMARK { for (int i = 0; i < n; i++) ydata[i] = 1.; np = (int)nsl_int_simpson(xdata, ydata, n, 0); QCOMPARE(np, n/2 + 1); } QCOMPARE(ydata[np - 1], (double)(n - 1)); delete[] xdata; delete[] ydata; } void NSLIntTest::testPerformance4Point() { const int n = 1e6; double* xdata = new double[n]; double* ydata = new double[n]; for (int i = 0; i < n; i++) xdata[i] = (double)i; int np; QBENCHMARK { for (int i = 0; i < n; i++) ydata[i] = 1.; np = (int)nsl_int_simpson_3_8(xdata, ydata, n, 0); QCOMPARE(np, n/3 + 1); } //TODO: //QCOMPARE(ydata[np - 1], (double)(n - 1)); printf("%.15g %.15g\n", ydata[np - 1], (double)(n-1)); delete[] xdata; delete[] ydata; } QTEST_MAIN(NSLIntTest) diff --git a/tests/nsl/int/NSLIntTest.h b/tests/nsl/int/NSLIntTest.h index c9b052bc6..3c7d7de61 100644 --- a/tests/nsl/int/NSLIntTest.h +++ b/tests/nsl/int/NSLIntTest.h @@ -1,53 +1,51 @@ /*************************************************************************** File : NSLIntTest.h Project : LabPlot Description : NSL Tests for numerical integration -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef NSLINTTEST_H #define NSLINTTEST_H -#include +#include "../NSLTest.h" -class NSLIntTest : public QObject { +class NSLIntTest : public NSLTest { Q_OBJECT private slots: - void initTestCase(); - // rules integral/area void testRectangle_integral(); void testRectangle_area(); void testTrapezoid_integral(); void testTrapezoid_area(); void test3Point_integral(); void test4Point_integral(); // performance void testPerformanceRectangle(); void testPerformanceTrapezoid(); void testPerformance3Point(); void testPerformance4Point(); private: QString m_dataDir; }; #endif diff --git a/tests/nsl/sf/CMakeLists.txt b/tests/nsl/sf/CMakeLists.txt index d87c9edfc..500c936ca 100644 --- a/tests/nsl/sf/CMakeLists.txt +++ b/tests/nsl/sf/CMakeLists.txt @@ -1,21 +1,21 @@ INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) # basic functions -add_executable (nslsfbasictest NSLSFBasicTest.cpp) +add_executable (nslsfbasictest NSLSFBasicTest.cpp ../NSLTest.cpp ../../CommonTest.cpp) target_link_libraries(nslsfbasictest Qt5::Test) target_link_libraries(nslsfbasictest ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) target_link_libraries(nslsfbasictest labplot2lib) add_test(NAME nslsfbasictest COMMAND nslsfbasictest) # window functions -add_executable (nslsfwindowtest NSLSFWindowTest.cpp) +add_executable (nslsfwindowtest NSLSFWindowTest.cpp ../NSLTest.cpp ../../CommonTest.cpp) target_link_libraries(nslsfwindowtest Qt5::Test) target_link_libraries(nslsfwindowtest ${GSL_LIBRARIES} ${GSL_CBLAS_LIBRARIES}) target_link_libraries(nslsfwindowtest labplot2lib) add_test(NAME nslsfwindowtest COMMAND nslsfwindowtest) diff --git a/tests/nsl/sf/NSLSFBasicTest.cpp b/tests/nsl/sf/NSLSFBasicTest.cpp index 9a3b9077f..507c1493c 100644 --- a/tests/nsl/sf/NSLSFBasicTest.cpp +++ b/tests/nsl/sf/NSLSFBasicTest.cpp @@ -1,112 +1,107 @@ /*************************************************************************** File : NSLSFBasicTest.cpp Project : LabPlot Description : NSL Tests for basic special functions -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "NSLSFBasicTest.h" extern "C" { #include "backend/nsl/nsl_sf_basic.h" } -void NSLSFBasicTest::initTestCase() { - const QString currentDir = __FILE__; - m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); -} - //############################################################################## //################# log2() tests //############################################################################## void NSLSFBasicTest::testlog2_int_C99() { QBENCHMARK { for (unsigned int i = 1; i < 1e7; i++) Q_UNUSED((int)log2(i)); } } void NSLSFBasicTest::testlog2_int() { for (unsigned int i = 1; i < 1e5; i++) { int result = nsl_sf_log2_int(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (unsigned int i = 1; i < 1e7; i++) nsl_sf_log2_int(i); } } void NSLSFBasicTest::testlog2_longlong() { #ifndef _MSC_VER /* not implemented yet */ for (unsigned long long i = 1; i < 1e5; i++) { int result = nsl_sf_log2_longlong(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (unsigned long long i = 1; i < 1e7; i++) nsl_sf_log2_longlong(i); } #endif } void NSLSFBasicTest::testlog2_int2() { for (int i = 1; i < 1e5; i++) { int result = nsl_sf_log2_int2(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (int i = 1; i < 1e7; i++) nsl_sf_log2_int2(i); } } void NSLSFBasicTest::testlog2_int3() { for (unsigned int i = 1; i < 1e5; i++) { int result = nsl_sf_log2_int3(i); QCOMPARE(result, (int)log2(i)); } QBENCHMARK { for (unsigned int i = 1; i < 1e7; i++) nsl_sf_log2_int3(i); } } void NSLSFBasicTest::testlog2p1_int() { for (int i = 1; i < 1e5; i++) { int result = nsl_sf_log2p1_int(i); QCOMPARE(result, (int)log2(i) + 1); } QBENCHMARK { for (int i = 1; i < 1e7; i++) nsl_sf_log2p1_int(i); } } QTEST_MAIN(NSLSFBasicTest) diff --git a/tests/nsl/sf/NSLSFBasicTest.h b/tests/nsl/sf/NSLSFBasicTest.h index 53fb61ba0..8b4c522f6 100644 --- a/tests/nsl/sf/NSLSFBasicTest.h +++ b/tests/nsl/sf/NSLSFBasicTest.h @@ -1,48 +1,46 @@ /*************************************************************************** File : NSLSFBasicTest.h Project : LabPlot Description : NSL Tests for the basic special functions -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef NSLSFBASICTEST_H #define NSLSFBASICTEST_H -#include +#include "../NSLTest.h" -class NSLSFBasicTest : public QObject { +class NSLSFBasicTest : public NSLTest { Q_OBJECT private slots: - void initTestCase(); - // log2 void testlog2_int_C99(); void testlog2_int(); void testlog2_longlong(); void testlog2_int2(); void testlog2_int3(); void testlog2p1_int(); private: QString m_dataDir; }; #endif diff --git a/tests/nsl/sf/NSLSFWindowTest.cpp b/tests/nsl/sf/NSLSFWindowTest.cpp index 2119d10d4..37c1d126a 100644 --- a/tests/nsl/sf/NSLSFWindowTest.cpp +++ b/tests/nsl/sf/NSLSFWindowTest.cpp @@ -1,100 +1,95 @@ /*************************************************************************** File : NSLSFWindowTest.cpp Project : LabPlot Description : NSL Tests for special window functions -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "NSLSFWindowTest.h" extern "C" { #include "backend/nsl/nsl_sf_window.h" } -void NSLSFWindowTest::initTestCase() { - const QString currentDir = __FILE__; - m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); -} - //############################################################################## //################# window types //############################################################################## void NSLSFWindowTest::testWindowTypes() { const int N = 10; double data[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; double result[][N] = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0.1, 0.3, 0.5, 0.7, 0.9, 0.9, 0.7, 0.5, 0.3, 0.1}, {0, 2/9., 4/9., 6/9., 8/9., 8/9., 6/9., 4/9., 2/9., 0}, {2/11., 4/11., 6/11., 8/11., 10/11., 10/11., 8/11., 6/11., 4/11., 2/11.}, {0.330578512396694, 0.59504132231405, 0.793388429752066, 0.925619834710744, 0.991735537190083, 0.991735537190083, 0.925619834710744, 0.793388429752066, 0.59504132231405, 0.330578512396694}, {0, 0.116977778440511, 0.413175911166535, 0.75, 0.969846310392954, 0.969846310392954, 0.75, 0.413175911166535, 0.116977778440511, 0}, {0.08, 0.18761955616527, 0.460121838273212, 0.77, 0.972258605561518, 0.972258605561518, 0.77, 0.460121838273212, 0.18761955616527, 0.08}, {0, 0.0508696326538654, 0.258000501503662, 0.63, 0.951129865842472, 0.951129865842472, 0.63, 0.258000501503662, 0.0508696326538655, 0}, {0, 0.0137486265628393, 0.141900826716656, 0.514746, 0.930560546720505, 0.930560546720505, 0.514746, 0.141900826716656, 0.0137486265628393, 0}, {0.0003628, 0.01789099867138, 0.15559612641629, 0.5292298, 0.933220224912329, 0.93322022491233, 0.5292298, 0.155596126416291, 0.0178909986713801, 0.0003628}, {6.e-05, 0.0150711734102182, 0.147039557862381, 0.520575, 0.9316592687274, 0.931659268727401, 0.520575, 0.147039557862382, 0.0150711734102182, 6.e-05}, {0, -0.0867710194112928, -0.331895219303666, 0.918, 4.00066623871496, 4.00066623871496, 0.918, -0.331895219303665, -0.0867710194112926, 0}, {0, 0.342020143325669, 0.642787609686539, 0.866025403784439, 0.984807753012208, 0.984807753012208, 0.866025403784439, 0.642787609686539, 0.342020143325669, 0}, {0, 0.142236444948122, 0.420680359153233, 0.73, 0.950416529231978, 0.950416529231979, 0.73, 0.420680359153233, 0.142236444948122, 0}, {0, 0.263064408273866, 0.564253278793615, 0.826993343132688, 0.979815536051017, 0.979815536051017, 0.826993343132688, 0.564253278793615, 0.263064408273866, 0}}; for (int t = (int)nsl_sf_window_uniform; t <= (int)nsl_sf_window_lanczos; t++) { nsl_sf_apply_window(data, N, (nsl_sf_window_type)t); for (int i = 0; i < N; i++) QCOMPARE(data[i] + 1., result[t][i] + 1.); } } //############################################################################## //################# performance //############################################################################## void NSLSFWindowTest::testPerformance_triangle() { const int N = 1e6; double* data = new double[N]; QBENCHMARK { nsl_sf_apply_window(data, N, nsl_sf_window_triangle); } } void NSLSFWindowTest::testPerformance_welch() { const int N = 1e6; double* data = new double[N]; QBENCHMARK { nsl_sf_apply_window(data, N, nsl_sf_window_welch); } } void NSLSFWindowTest::testPerformance_flat_top() { const int N = 1e6; double* data = new double[N]; QBENCHMARK { nsl_sf_apply_window(data, N, nsl_sf_window_flat_top); } } QTEST_MAIN(NSLSFWindowTest) diff --git a/tests/nsl/sf/NSLSFWindowTest.h b/tests/nsl/sf/NSLSFWindowTest.h index 274271ff8..eef08f40b 100644 --- a/tests/nsl/sf/NSLSFWindowTest.h +++ b/tests/nsl/sf/NSLSFWindowTest.h @@ -1,46 +1,44 @@ /*************************************************************************** File : NSLSFWindowTest.h Project : LabPlot Description : NSL Tests for special window functions -------------------------------------------------------------------- Copyright : (C) 2019 Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef NSLSFWINDOWTEST_H #define NSLSFWINDOWTEST_H -#include +#include "../NSLTest.h" -class NSLSFWindowTest : public QObject { +class NSLSFWindowTest : public NSLTest { Q_OBJECT private slots: - void initTestCase(); - void testWindowTypes(); void testPerformance_triangle(); void testPerformance_welch(); void testPerformance_flat_top(); private: QString m_dataDir; }; #endif