Stricter tests and consequent bugfixes
authorGeorgios Gousios <gousiosg@gmail.com>
Mon, 5 Dec 2011 13:04:38 +0000 (15:04 +0200)
committerGeorgios Gousios <gousiosg@gmail.com>
Mon, 5 Dec 2011 15:46:46 +0000 (17:46 +0200)
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/Timeslot.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/TimeslotTest.scala

index ee34150..98b44f4 100644 (file)
@@ -145,6 +145,9 @@ case class Timeslot(from: Date, to: Date) {
 
     val overlaps = list.filter(t => this.overlaps(t))
 
+    if (overlaps.isEmpty)
+      return List()
+
     def build(acc: List[Timeslot], listPart: List[Timeslot]): List[Timeslot] = {
 
       listPart match {
@@ -159,7 +162,7 @@ case class Timeslot(from: Date, to: Date) {
     val last = overlaps.reverse.head
 
     val start = if (head.startsAfter(this)) List(Timeslot(this.from, head.from)) else List()
-    val end = if (last.endsBefore(this)) List(Timeslot(this.from, last.from)) else List()
+    val end = if (last.endsBefore(this)) List(Timeslot(last.to, this.to)) else List()
 
     start ++ build(List(), overlaps) ++ end
   }
index 6961622..4764255 100644 (file)
@@ -74,6 +74,27 @@ class TimeslotTest extends TestMethods {
 
     var result = t.nonOverlappingTimeslots(list)
     assertEquals(2, result.size)
-    
+
+    t = Timeslot(new Date(9), new Date(20))
+    result = t.nonOverlappingTimeslots(list)
+    assertEquals(2, result.size)
+
+    t = Timeslot(new Date(9), new Date(20))
+    result = t.nonOverlappingTimeslots(list)
+    assertEquals(2, result.size)
+
+    t = Timeslot(new Date(0), new Date(20))
+    result = t.nonOverlappingTimeslots(list)
+    assertEquals(4, result.size)
+    assertEquals(Timeslot(new Date(0), new Date(1)), result.head)
+    assertEquals(Timeslot(new Date(3), new Date(6)), result.tail.head)
+
+    t = Timeslot(new Date(13), new Date(20))
+    result = t.nonOverlappingTimeslots(list)
+    assertEquals(1, result.size)
+    assertEquals(Timeslot(new Date(15), new Date(20)), result.head)
+
+    result = t.nonOverlappingTimeslots(List())
+    assertEquals(0, result.size)
   }
 }
\ No newline at end of file