Skip to content

Commit 865c634

Browse files
committed
Added unit tests
1 parent 14013a8 commit 865c634

12 files changed

Lines changed: 682 additions & 58 deletions

src/MieSimulatorGUI.pro

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
##################################################
2+
## main project file
3+
##################################################
4+
15
TEMPLATE = subdirs
6+
CONFIG += ordered
27

38
SUBDIRS += \
4-
app \
5-
test
9+
test \
10+
app

src/app/app.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
##################################################
2+
## app project file
3+
##################################################
4+
15
QT += core gui widgets printsupport
26

37
greaterThan(QT_MAJOR_VERSION, 5): QT += widgets

src/app/dialog/mainwindow.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void MainWindow::on_pushButton_RunSimulation_clicked()
8787
else
8888
{
8989

90-
if (!mPara->CheckCommonParameters(ui->radioButton_MonoDisperse,
90+
if (mPara->CheckCommonParameters(ui->radioButton_MonoDisperse,
9191
ui->radioButton_NumDen,
9292
ui->radioButton_VolFrac))
9393
{
@@ -113,7 +113,7 @@ void MainWindow::on_pushButton_RunSimulation_clicked()
113113
//Poly disperse
114114
if (ui->radioButton_PolyDisperse->isChecked())
115115
{
116-
if (!mPara->CheckDistributionParameters(ui->comboBox_Distribution)) //sanity check
116+
if (mPara->CheckDistributionParameters(ui->comboBox_Distribution)) //sanity check
117117
{
118118
support.ProcessDistribution(ui, mPara, static_cast<unsigned int>(ui->comboBox_Distribution->currentIndex()));
119119
support.ProcessPolyDisperse(ui,mPara);
@@ -162,11 +162,11 @@ void MainWindow::on_pushButton_ShowDistributionAndCustom_clicked()
162162
else
163163
{
164164
support.LoadInputData(ui, mPara);
165-
if (!mPara->CheckCommonParameters(ui->radioButton_MonoDisperse,
165+
if (mPara->CheckCommonParameters(ui->radioButton_MonoDisperse,
166166
ui->radioButton_NumDen,
167167
ui->radioButton_VolFrac)) //sanity check
168168
{
169-
if (!mPara->CheckDistributionParameters(ui->comboBox_Distribution)) //sanity check
169+
if (mPara->CheckDistributionParameters(ui->comboBox_Distribution)) //sanity check
170170
{
171171
support.SetWidgets(ui);
172172
support.ProcessDistribution(ui, mPara, static_cast<unsigned int>(ui->comboBox_Distribution->currentIndex()));

src/test/test.pro

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
# This is the project file for the unit test.
1+
##################################################
2+
## test project file for unit tests
3+
##################################################
24

3-
# Set the template to create an application executable.
45
TEMPLATE = app
56

6-
# Add the Qt Test library, which is required for unit testing.
7-
# We also include 'widgets' as the app under test is a GUI app,
8-
# and some functions might implicitly require it (though not directly in 'calculate').
97
QT += testlib widgets
108

11-
# The target name for our test executable.
12-
TARGET = test_calculate
13-
14-
# Configure the build to be in debug and release modes.
9+
TARGET = MieSimulatorGUI_test
1510
CONFIG += warn_on console
11+
DEFINES += QT_DEPRECATED_WARNINGS
1612

17-
# Include the headers from the main application to access the classes we want to test.
1813
INCLUDEPATH += ../app \
1914
../app/calc
2015

21-
# Add the sources for our test suite and the classes we are testing.
22-
# We include the calculate.cpp directly, as well as mock implementations for its dependencies.
23-
# The `mock_` files are simple stubs that allow us to test the `calculate` class in isolation.
2416
SOURCES += \
17+
test_main.cpp \
18+
test_parameters.cpp \
2519
test_calculate.cpp \
20+
test_miesimulation.cpp \
21+
test_utilities.cpp \
2622
../app/parameters.cpp \
2723
../app/calc/calculate.cpp \
2824
../app/calc/miesimulation.cpp \
2925
../app/calc/utilities.cpp
3026

31-
# Add the headers for the test suite and the classes we are testing.
32-
HEADERS += \
27+
HEADERS += \
28+
test_parameters.h \
3329
test_calculate.h \
30+
test_miesimulation.h \
31+
test_utilities.h \
3432
../app/parameters.h \
3533
../app/calc/calculate.h \
3634
../app/calc/miesimulation.h \
3735
../app/calc/utilities.h
3836

39-
# Define some preprocessor macros if needed.
40-
DEFINES += QT_TESTLIB_WIDGETS_LIB

src/test/test_calculate.cpp

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
/**********************************************************************
2+
** Tests for functions in calculate.cpp
3+
**********************************************************************/
4+
15
#include <QDebug>
26
#include <QtMath>
37
#include <complex>
4-
58
#include "test_calculate.h"
69

7-
QTEST_MAIN(TestCalculate)
8-
910
TestCalculate::TestCalculate()
1011
{
1112
}
@@ -118,10 +119,9 @@ void TestCalculate::cleanup()
118119
mPara = nullptr;
119120
}
120121

121-
// Test case for DoSimulationdata
122+
// Test case: Check DoSimulationdata
122123
void TestCalculate::test_DoSimulationdata()
123124
{
124-
// Set up a simple mono-disperse case at 632.8nm and 7
125125
mPara->nRadius = 1;
126126
mPara->nTheta = 361;
127127
mPara->nWavel = 1;
@@ -140,19 +140,18 @@ void TestCalculate::test_DoSimulationdata()
140140
QLabel mockLabel;
141141
mCalc->DoSimulation(&mockLabel, mPara);
142142

143-
double expectedMus1 = 93.3690054109;
144-
145-
QCOMPARE(mPara->mus[0], expectedMus1);
143+
double expectedMus = 93.36900541;
144+
double expectedG = 0.861445339;
145+
double expectedForward = 96.201215357;
146+
double expectedBackward = 3.7987846427;
146147

147-
// Verify that the g factor is not NaN
148-
QVERIFY(!std::isnan(mPara->g[0]));
149-
150-
// Verify that forward/backward scattering is non-zero
151-
QVERIFY(mPara->forward[0] > 0.0);
152-
QVERIFY(mPara->backward[0] > 0.0);
148+
QVERIFY(fabs(mPara->mus[0] - expectedMus) <1e-8);
149+
QVERIFY(fabs(mPara->g[0] - expectedG) <1e-8);
150+
QVERIFY(fabs(mPara->forward[0] - expectedForward) <1e-8);
151+
QVERIFY(fabs(mPara->backward[0] - expectedBackward) <1e-8);
153152
}
154153

155-
// Test case for DiameterRangeSetting with log-normal distribution
154+
// Test case: Check DiameterRangeSetting with log-normal distribution
156155
void TestCalculate::test_DiameterRangeSetting_logNormal()
157156
{
158157
mPara->meanRadius = 1.0;
@@ -167,7 +166,7 @@ void TestCalculate::test_DiameterRangeSetting_logNormal()
167166
QVERIFY(mPara->maxRadius > mPara->meanRadius);
168167
}
169168

170-
// Test case for DiameterRangeSetting with Gaussian distribution
169+
// Test case: Check DiameterRangeSetting with Gaussian distribution
171170
void TestCalculate::test_DiameterRangeSetting_gaussian()
172171
{
173172
mPara->meanRadius = 1.0;
@@ -180,11 +179,10 @@ void TestCalculate::test_DiameterRangeSetting_gaussian()
180179
QVERIFY(mPara->minRadius < mPara->meanRadius);
181180
QVERIFY(mPara->maxRadius > mPara->minRadius);
182181
QVERIFY(mPara->maxRadius > mPara->meanRadius);
183-
// For large radii with small stDev, the difference between max and mean should be
184-
// roughly equal to mean and min.
185182
QCOMPARE(mPara->maxRadius - mPara->meanRadius, mPara->meanRadius - mPara->minRadius);
186183
}
187184

185+
// Test case: Check DiameterRangeSetting for mono-disperse spheres
188186
void TestCalculate::test_DiameterRangeSetting_monoDisperse()
189187
{
190188
mPara->meanRadius = 5.0;
@@ -193,7 +191,7 @@ void TestCalculate::test_DiameterRangeSetting_monoDisperse()
193191
QCOMPARE(mPara->maxRadius, 5.0);
194192
}
195193

196-
// Test case for SetSphereRadiusAndRefIndex for mono-disperse spheres
194+
// Test case: SetSphereRadiusAndRefIndex for mono-disperse spheres
197195
void TestCalculate::test_SetSphereRadiusAndRefIndex_monoDisperse()
198196
{
199197
mPara->nRadius = 1;
@@ -202,7 +200,8 @@ void TestCalculate::test_SetSphereRadiusAndRefIndex_monoDisperse()
202200
mPara->scatRefReal = 1.5;
203201
mPara->scatRefImag = 0.1;
204202

205-
mCalc->SetSphereRadiusAndRefIndex(mPara, 0, true); // The distribution index doesn't matter for nRadius=1.
203+
// The distribution index doesn't matter for nRadius=1.
204+
mCalc->SetSphereRadiusAndRefIndex(mPara, 0, true);
206205

207206
//The elements in Array[0] are the same as the initial values
208207
QCOMPARE(mPara->radArray[0], mPara->meanRadius);
@@ -211,7 +210,7 @@ void TestCalculate::test_SetSphereRadiusAndRefIndex_monoDisperse()
211210
QCOMPARE(mPara->scatRefImagArray[0], mPara->scatRefImag);
212211
}
213212

214-
// Test case for SetSphereRadiusAndRefIndex for poly-disperse with volume fraction
213+
// Test case: SetSphereRadiusAndRefIndex for poly-disperse with volume fraction
215214
void TestCalculate::test_SetSphereRadiusAndRefIndex_poly_logNormal_volfrac()
216215
{
217216
mPara->nRadius = 21;
@@ -234,7 +233,7 @@ void TestCalculate::test_SetSphereRadiusAndRefIndex_poly_logNormal_volfrac()
234233
QVERIFY(sumNumDensity > 0.0);
235234
}
236235

237-
// Test case for SetSphereRadiusAndRefIndex for poly-disperse with concentration.
236+
// Test case: SetSphereRadiusAndRefIndex for poly-disperse with concentration.
238237
void TestCalculate::test_SetSphereRadiusAndRefIndex_poly_gaussian_conc()
239238
{
240239
mPara->nRadius = 21;
@@ -246,9 +245,10 @@ void TestCalculate::test_SetSphereRadiusAndRefIndex_poly_gaussian_conc()
246245
mCalc->DiameterRangeSetting(mPara, 1);
247246
mCalc->SetSphereRadiusAndRefIndex(mPara, 1, false); // 1 = Gaussian, false = use concentration.
248247

249-
// Check if the arrays have been populated.
250-
QVERIFY(mPara->radArray[0] > 0);
251-
QVERIFY(mPara->numDensityArray[0] > 0);
248+
//The elements in Array[0] are the same as the initial values
249+
QCOMPARE(mPara->radArray[0], 1e-10);
250+
QCOMPARE(mPara->numDensityArray[0], 466260);
251+
252252
// Check that the sum of the number densities is not zero.
253253
double sumNumDensity = 0.0;
254254
for (unsigned int i = 0; i < mPara->nRadius; ++i) {
@@ -257,7 +257,7 @@ void TestCalculate::test_SetSphereRadiusAndRefIndex_poly_gaussian_conc()
257257
QVERIFY(qFuzzyCompare(sumNumDensity, mPara->sphNumDensity));
258258
}
259259

260-
// Test case for CalculateG.
260+
// Test case: Check CalculateG.
261261
void TestCalculate::test_CalculateG()
262262
{
263263
std::complex<double> S1[361], S2[361];
@@ -273,7 +273,7 @@ void TestCalculate::test_CalculateG()
273273
QVERIFY(g_value< 1e-12);
274274
}
275275

276-
// Test case for CalculateForwardBackward.
276+
// Test case: Check CalculateForwardBackward.
277277
void TestCalculate::test_CalculateForwardBackward()
278278
{
279279
std::complex<double> S1[361], S2[361];
@@ -291,10 +291,11 @@ void TestCalculate::test_CalculateForwardBackward()
291291
QVERIFY(fabs(forward - backward) <1e-12);
292292
}
293293

294-
// Test case for CalculatePowerLawAutoFitSimple.
294+
// Test case: Check CalculatePowerLawAutoFitSimple.
295295
void TestCalculate::test_CalculatePowerLawAutoFitSimple()
296296
{
297297
//Test for complex power law with b=2.
298+
double b = 2.0;
298299
mPara->refWavel = 800;
299300
mPara->refWavelIdx = 3;
300301
mPara->muspAtRefWavel[3] = 10.0;
@@ -306,23 +307,25 @@ void TestCalculate::test_CalculatePowerLawAutoFitSimple()
306307

307308
for (unsigned int i = 0; i < mPara->nWavel; ++i) {
308309
mPara->wavelArray[i] = 600.0 + 50.0 * i; // Wavelengths from 600 to 1000 nm.
309-
mPara->mus[i] = mPara->muspAtRefWavel[3] * pow(mPara->wavelArray[i] / mPara->refWavel, -2.0);
310+
mPara->mus[i] = mPara->muspAtRefWavel[3] * pow(mPara->wavelArray[i] / mPara->refWavel, -b);
310311
mPara->g[i] = 0; //Assume g= 0;
311312
}
312313
mCalc->CalculatePowerLawAutoFitSimple(mPara);
313314

314315
// The calculated bMie should be very close to 2.0.
315-
QCOMPARE(mPara->bMie, 2.0);
316+
QCOMPARE(mPara->bMie, b);
316317
}
317318

318-
// Test case for CalculatePowerLawAutoFitComplex.
319+
// Test case: Check CalculatePowerLawAutoFitComplex.
319320
void TestCalculate::test_CalculatePowerLawAutoFitComplex()
320321
{
321322
//Test for complex power law with fRay=0.5 and bMie=1.5.
323+
double fRay = 0.5;
324+
double bMie = 1.5;
322325
mPara->refWavel = 800;
323326
mPara->refWavelIdx = 3;
324327
mPara->muspAtRefWavel[3] = 10.0;
325-
mPara->nWavel = 9;
328+
mPara->nWavel = 9;
326329

327330
mPara->wavelArray = new double[mPara->nWavel];
328331
mPara->mus = new double[mPara->nWavel];
@@ -331,14 +334,15 @@ void TestCalculate::test_CalculatePowerLawAutoFitComplex()
331334
for (unsigned int i = 0; i < mPara->nWavel; ++i) {
332335
mPara->wavelArray[i] = 600.0 + 50.0 * i; // Wavelengths from 600 to 1000 nm.
333336
double x = mPara->wavelArray[i] / mPara->refWavel;
334-
mPara->mus[i] = mPara->muspAtRefWavel[3] * (0.5 * pow(x, -4.0) + (1.0 - 0.5) * pow(x, -1.5));
337+
mPara->mus[i] = mPara->muspAtRefWavel[3] * (fRay * pow(x, -4.0) + (1.0 - fRay) * pow(x, -bMie));
335338
mPara->g[i] = 0; //Assume g= 0;
336339
}
337340
mCalc->CalculatePowerLawAutoFitComplex(mPara);
338341

342+
339343
// The calculated values should be close to the expected values.
340-
QCOMPARE(mPara->fRay, 0.5);
341-
QCOMPARE(mPara->bMie, 1.5);
344+
QCOMPARE(mPara->fRay, fRay);
345+
QCOMPARE(mPara->bMie, bMie);
342346
}
343347

344348
// Sanity check for the ComputeMuspAtRefWavel method.

src/test/test_main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <QTest>
2+
#include "test_calculate.h"
3+
#include "test_parameters.h"
4+
#include "test_miesimulation.h"
5+
#include "test_utilities.h"
6+
7+
int main(int argc, char *argv[])
8+
{
9+
QApplication a(argc, argv);
10+
int status = 0;
11+
12+
status |= QTest::qExec(new TestParameters(), argc, argv);
13+
status |= QTest::qExec(new TestCalculate(), argc, argv);
14+
status |= QTest::qExec(new TestMieSimulation(), argc, argv);
15+
status |= QTest::qExec(new TestUtilities(), argc, argv);
16+
17+
return status;
18+
}

0 commit comments

Comments
 (0)