From ada5ab2b071ce1f3488c280631eec859a3f7ebfa Mon Sep 17 00:00:00 2001
From: Janik Zikovsky <zikovskyjl@ornl.gov>
Date: Fri, 4 Mar 2011 20:05:40 +0000
Subject: [PATCH] Fixes #2591: filter a log with one value = it is a constant,
 don't delete it.

---
 .../inc/MantidKernel/TimeSeriesProperty.h     |  6 ++++
 .../Kernel/test/TimeSeriesPropertyTest.h      | 35 ++++++++++++++-----
 2 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/TimeSeriesProperty.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/TimeSeriesProperty.h
index 1fc755b9fc3..ef5fc81657b 100644
--- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/TimeSeriesProperty.h
+++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/TimeSeriesProperty.h
@@ -157,12 +157,18 @@ public:
   /**
    * Filter out a run by time. Takes out any TimeSeriesProperty log entries outside of the given
    *  absolute time range.
+   * EXCEPTION: If there is only one entry in the list, it is considered to mean
+   * "constant" so the value is kept even if the time is outside the range.
    *
    * @param start :: Absolute start time. Any log entries at times >= to this time are kept.
    * @param stop :: Absolute stop time. Any log entries at times < than this time are kept.
    */
   void filterByTime(const Kernel::DateAndTime start, const Kernel::DateAndTime stop)
   {
+    // Do nothing for single (constant) value
+    if (m_propertySeries.size() <= 1)
+      return;
+
     typename timeMap::iterator it;
     for (it = m_propertySeries.begin(); it != m_propertySeries.end(); /*increment within loop*/)
     {
diff --git a/Code/Mantid/Framework/Kernel/test/TimeSeriesPropertyTest.h b/Code/Mantid/Framework/Kernel/test/TimeSeriesPropertyTest.h
index cff07f367ce..def438f3c0f 100644
--- a/Code/Mantid/Framework/Kernel/test/TimeSeriesPropertyTest.h
+++ b/Code/Mantid/Framework/Kernel/test/TimeSeriesPropertyTest.h
@@ -27,7 +27,7 @@ public:
     delete sProp;
   }
 
-  void testConstructor()
+  void test_Constructor()
   {
     // Test that all the base class member variables are correctly assigned to
     TS_ASSERT( ! iProp->name().compare("intProp") );
@@ -46,14 +46,14 @@ public:
     //TS_ASSERT( sProp->isDefault() )
   }
 
-  void testSetValue()
+  void test_SetValue()
   {
     TS_ASSERT_THROWS( iProp->setValue("1"), Exception::NotImplementedError );
     TS_ASSERT_THROWS( dProp->setValue("5.5"), Exception::NotImplementedError );
     TS_ASSERT_THROWS( sProp->setValue("aValue"), Exception::NotImplementedError );
   }
 
-  void testAddValue()
+  void test_AddValue()
   {
     const std::string tester("2007-11-30T16:17:00");
     TS_ASSERT( iProp->addValue(tester,1) );
@@ -78,7 +78,7 @@ public:
     TS_ASSERT_EQUALS( sString.substr(0,27), "2007-Nov-30 16:17:00  test\n" );
   }
 
-  void testCasting()
+  void test_Casting()
   {
     TS_ASSERT_DIFFERS( dynamic_cast<Property*>(iProp), static_cast<Property*>(0) );
     TS_ASSERT_DIFFERS( dynamic_cast<Property*>(dProp), static_cast<Property*>(0) );
@@ -87,7 +87,7 @@ public:
 
 
   //----------------------------------------------------------------------------
-  void testAdditionOperator()
+  void test_AdditionOperator()
   {
     TimeSeriesProperty<int> * log  = new TimeSeriesProperty<int>("MyIntLog");
     TS_ASSERT( log->addValue("2007-11-30T16:17:00",1) );
@@ -107,8 +107,8 @@ public:
   }
 
   //----------------------------------------------------------------------------
-  // Ticket 2097: This caused an infinite loop
-  void testAdditionOperatorOnYourself()
+  /// Ticket 2097: This caused an infinite loop
+  void test_AdditionOperatorOnYourself()
   {
     TimeSeriesProperty<int> * log  = new TimeSeriesProperty<int>("MyIntLog");
     TS_ASSERT( log->addValue("2007-11-30T16:17:00",1) );
@@ -143,6 +143,25 @@ public:
   }
 
 
+  //----------------------------------------------------------------------------
+  /// Ticket #2591
+  void test_filterByTime_ifOnlyOneValue_assumes_constant_instead()
+  {
+    TimeSeriesProperty<int> * log  = new TimeSeriesProperty<int>("MyIntLog");
+    TS_ASSERT( log->addValue("2007-11-30T16:17:00",1) );
+    TS_ASSERT_EQUALS( log->realSize(), 1);
+    TS_ASSERT_EQUALS( log->getTotalValue(), 1);
+
+    DateAndTime start = DateAndTime("2007-11-30T16:17:10");
+    DateAndTime stop = DateAndTime("2007-11-30T16:17:40");
+    log->filterByTime(start, stop);
+
+    // Still there!
+    TS_ASSERT_EQUALS( log->realSize(), 1);
+    TS_ASSERT_EQUALS( log->getTotalValue(), 1);
+  }
+
+
 
   //----------------------------------------------------------------------------
   void test_makeFilterByValue()
@@ -327,7 +346,7 @@ public:
     TS_ASSERT( boost::math::isnan(stats.duration) );
   }
 
-  void testPlusEqualsOperator_Incompatible_Types()
+  void test_PlusEqualsOperator_Incompatible_Types_dontThrow()
   {
     // Adding incompatible types together should not throw, but issue a warning in the log
 
-- 
GitLab