From 74c25314b5509f0f37b5132d7d3dbc09f140f36f Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Wed, 5 Dec 2012 12:47:04 +0000
Subject: [PATCH] Task #3696: Added tRanges and fixed Ranges bugs

---
 .gitattributes                     |  1 +
 RTCP/InputProc/src/Ranges.h        |  8 ++--
 RTCP/InputProc/test/CMakeLists.txt |  1 +
 RTCP/InputProc/test/tRanges.cc     | 74 ++++++++++++++++++++++++++++++
 4 files changed, 81 insertions(+), 3 deletions(-)
 create mode 100644 RTCP/InputProc/test/tRanges.cc

diff --git a/.gitattributes b/.gitattributes
index 89bff0e37e1..6d505c80d41 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -3642,6 +3642,7 @@ RTCP/InputProc/src/newInputSection.cc -text
 RTCP/InputProc/src/shmtest.cc -text
 RTCP/InputProc/test/CMakeLists.txt -text
 RTCP/InputProc/test/tRSPTimeStamp2.cc -text
+RTCP/InputProc/test/tRanges.cc -text
 RTCP/InputProc/test/tSharedMemory.cc -text
 RTCP/Interface/include/Interface/BGPAsm.h -text
 RTCP/Interface/include/Interface/BeamCoordinates.h -text
diff --git a/RTCP/InputProc/src/Ranges.h b/RTCP/InputProc/src/Ranges.h
index 7376c8bfaf1..bf2856d9b6d 100644
--- a/RTCP/InputProc/src/Ranges.h
+++ b/RTCP/InputProc/src/Ranges.h
@@ -31,6 +31,8 @@ public:
 
   SparseSet<int64> sparseSet( int64 first, int64 last ) const;
 
+  static size_t elementSize() { return sizeof(struct Range); }
+
 private:
   struct Range {
     // Write 'from' before 'to' to allow the following invariant:
@@ -110,7 +112,7 @@ void Ranges::excludeBefore( int64 to )
       continue;
     }
 
-    if (i->from > to) {
+    if (i->from < to) {
       // shorten
       i->from = to;
     }
@@ -138,8 +140,8 @@ bool Ranges::include( int64 from, int64 to )
   // new range is needed
   struct Range * const next = head + 1 == end ? begin : head + 1;
 
-  if (next->to < to - minHistory) {
-    // range at 'next' is old enough to toss away
+  if (next->to == 0 || next->to < to - minHistory) {
+    // range at 'next' is either unused or old enough to toss away
     next->from = from;
     next->to   = to;
 
diff --git a/RTCP/InputProc/test/CMakeLists.txt b/RTCP/InputProc/test/CMakeLists.txt
index 152757b40d7..baf082535cc 100644
--- a/RTCP/InputProc/test/CMakeLists.txt
+++ b/RTCP/InputProc/test/CMakeLists.txt
@@ -6,4 +6,5 @@ include(LofarCTest)
 include_directories(${PACKAGE_SOURCE_DIR}/src)
 
 lofar_add_test(tRSPTimeStamp2 tRSPTimeStamp2.cc)
+lofar_add_test(tRanges tRanges.cc)
 lofar_add_test(tSharedMemory tSharedMemory.cc)
diff --git a/RTCP/InputProc/test/tRanges.cc b/RTCP/InputProc/test/tRanges.cc
new file mode 100644
index 00000000000..795bd912d6a
--- /dev/null
+++ b/RTCP/InputProc/test/tRanges.cc
@@ -0,0 +1,74 @@
+#include <lofar_config.h>
+#include "Ranges.h"
+#include <Common/LofarLogger.h>
+#include <Common/Thread/Thread.h>
+#include <unistd.h>
+#include <vector>
+
+using namespace LOFAR;
+using namespace RTCP;
+using namespace std;
+
+int main( int, char **argv ) {
+  INIT_LOGGER( argv[0] );
+
+  size_t clock = 200 * 1000 * 1000;
+  bool result;
+
+  {
+    LOG_INFO("Basic tests");
+
+    vector<char> buf(10 * Ranges::elementSize());
+    Ranges r(&buf[0], buf.size(), clock / 1024, true);
+
+    result = r.include(10, 20);
+    ASSERT(result);
+
+    /* r == [10,20) */
+
+    ASSERT(r.anythingBetween(10, 20));
+    ASSERT(r.anythingBetween(0, 30));
+    ASSERT(r.anythingBetween(0, 15));
+    ASSERT(r.anythingBetween(15, 30));
+    ASSERT(!r.anythingBetween(0, 10));
+    ASSERT(!r.anythingBetween(20, 30));
+
+    result = r.include(30, 40);
+    ASSERT(result);
+
+    /* r == [10,20) + [30,40) */
+
+    SparseSet<int64> s(r.sparseSet(0,100));
+    ASSERT(!s.test(9));
+    ASSERT(s.test(10));
+    ASSERT(s.test(11));
+    ASSERT(s.test(19));
+    ASSERT(!s.test(20));
+    ASSERT(!s.test(29));
+    ASSERT(s.test(30));
+    ASSERT(s.test(31));
+    ASSERT(s.test(39));
+    ASSERT(!s.test(40));
+
+    SparseSet<int64> s2(r.sparseSet(15,35));
+    ASSERT(!s2.test(14));
+    ASSERT(s2.test(15));
+    ASSERT(s2.test(19));
+    ASSERT(!s2.test(20));
+    ASSERT(!s2.test(29));
+    ASSERT(s2.test(30));
+    ASSERT(s2.test(31));
+    ASSERT(!s2.test(35));
+
+    r.excludeBefore(35);
+
+    /* r == [35,40) */
+
+    ASSERT(!r.anythingBetween(0,35));
+    ASSERT(r.anythingBetween(35,40));
+    ASSERT(!r.anythingBetween(40,100));
+  }
+
+
+  return 0;
+}
-- 
GitLab