diff --git a/tests/nsl/CMakeLists.txt b/tests/nsl/CMakeLists.txt index 51469881c..c8c5f31e5 100644 --- a/tests/nsl/CMakeLists.txt +++ b/tests/nsl/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(dft) add_subdirectory(diff) +add_subdirectory(int) add_subdirectory(sf) diff --git a/tests/nsl/diff/NSLDiffTest.cpp b/tests/nsl/diff/NSLDiffTest.cpp index 11c4f2b2e..1d3bcf6c6 100644 --- a/tests/nsl/diff/NSLDiffTest.cpp +++ b/tests/nsl/diff/NSLDiffTest.cpp @@ -1,211 +1,210 @@ /*************************************************************************** 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/int/CMakeLists.txt b/tests/nsl/int/CMakeLists.txt new file mode 100644 index 000000000..40fc40237 --- /dev/null +++ b/tests/nsl/int/CMakeLists.txt @@ -0,0 +1,9 @@ +INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) +add_executable (nslinttest NSLIntTest.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 new file mode 100644 index 000000000..3103ab889 --- /dev/null +++ b/tests/nsl/int/NSLIntTest.cpp @@ -0,0 +1,186 @@ +/*************************************************************************** + 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 new file mode 100644 index 000000000..c9b052bc6 --- /dev/null +++ b/tests/nsl/int/NSLIntTest.h @@ -0,0 +1,53 @@ +/*************************************************************************** + 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 + +class NSLIntTest : public QObject { + 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 486174d1d..d87c9edfc 100644 --- a/tests/nsl/sf/CMakeLists.txt +++ b/tests/nsl/sf/CMakeLists.txt @@ -1,18 +1,21 @@ INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR}) + +# basic functions add_executable (nslsfbasictest NSLSFBasicTest.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) 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)