bug 2094: adjust floating point comparison epsilon based on inputs
authorTom Henderson <tomh@tomh.org>
Sun, 12 Apr 2015 21:44:41 -0700
changeset 11292 2fad26bb3070
parent 11291 8c68d7368185
child 11293 e9fef4a7a2e8
bug 2094: adjust floating point comparison epsilon based on inputs
src/spectrum/test/tv-spectrum-transmitter-test.cc
--- a/src/spectrum/test/tv-spectrum-transmitter-test.cc	Fri Apr 10 12:53:52 2015 +0200
+++ b/src/spectrum/test/tv-spectrum-transmitter-test.cc	Sun Apr 12 21:44:41 2015 -0700
@@ -37,7 +37,9 @@
 using namespace ns3;
 
 const double TOLERANCE = 1e-15;
-
+// Bug 2094: Adjust floating point comparison epsilon based on inputs.
+//           Follows http://realtimecollisiondetection.net/blog/?p=89
+double epsilon;
 
 class TvSpectrumTransmitterTestCase : public TestCase
 {
@@ -121,29 +123,33 @@
   if (m_tvType == TvSpectrumTransmitter::TVTYPE_8VSB) // pilot has highest PSD
     {
       double expectedPsd = (0.502 * basePsdWattsHz) + (21.577 * basePsdWattsHz);
+      epsilon = TOLERANCE * std::max (1.0, std::max (maxValue, expectedPsd));
       NS_TEST_ASSERT_MSG_EQ_TOL (maxValue, 
                                  expectedPsd, 
-                                 TOLERANCE, 
+                                 epsilon,
                                  "peak PSD value (" << maxValue << ") is incorrect");
     }
   else // highest PSD is base PSD
     {
+      epsilon = TOLERANCE * std::max (1.0, std::max (maxValue, basePsdWattsHz));
       NS_TEST_ASSERT_MSG_EQ_TOL (maxValue, 
                                  basePsdWattsHz, 
-                                 TOLERANCE, 
+                                 epsilon, 
                                  "peak PSD value (" << maxValue << ") is incorrect");
     }
 
   /* Test frequency range */
   Bands::const_iterator bandStart = psd->ConstBandsBegin ();
   Bands::const_iterator bandEnd = psd->ConstBandsEnd ();
+  epsilon = TOLERANCE * std::max (1.0, std::max ((*bandStart).fc, m_startFrequency));
   NS_TEST_ASSERT_MSG_EQ_TOL ((*bandStart).fc, 
                              m_startFrequency, 
-                             TOLERANCE, 
+                             epsilon, 
                              "start frequency value (" << (*bandStart).fc << ") is incorrect");
+  epsilon = TOLERANCE * std::max (1.0, std::max ((*bandStart).fc, (m_startFrequency + m_channelBandwidth)));
   NS_TEST_ASSERT_MSG_EQ_TOL ((*(bandEnd - 1)).fc, 
                              m_startFrequency + m_channelBandwidth, 
-                             TOLERANCE, 
+                             epsilon, 
                              "end frequency value (" << (*(bandEnd - 1)).fc << ") is incorrect");
 }