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-
910TestCalculate::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
122123void 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
156155void 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
171170void 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
188186void 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
197195void 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
215214void 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.
238237void 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.
261261void 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.
277277void 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.
295295void 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.
319320void 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.
0 commit comments