From 2d76af60c7861037b278c6842deb31b32610b6f6 Mon Sep 17 00:00:00 2001 From: Georgios Gousios Date: Mon, 28 Nov 2011 12:23:00 +0200 Subject: [PATCH] Stricter test of succesiveness for computed timeslots --- .../aquarium/logic/accounting/dsl/DSLUtils.scala | 4 +-- .../grnet/aquarium/logic/test/DSLUtilsTest.scala | 35 +++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala index ef25307..19c6273 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala @@ -46,14 +46,12 @@ import java.util.{GregorianCalendar, Calendar, Date} trait DSLUtils extends DateUtils { /** - * Get a list of all timeslots within which a algorithm/pricelist + * Get a list of all timeslots within which a timeframe * is effective. */ def allEffectiveTimeslots(spec: DSLTimeFrame, from: Date, to: Date): List[(Date, Date)] = { - - spec.repeat.flatMap{r =>effectiveTimeslots(r, from, Some(to))} sortWith sorter } diff --git a/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLUtilsTest.scala b/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLUtilsTest.scala index 8ab3c82..4def03c 100644 --- a/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLUtilsTest.scala +++ b/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLUtilsTest.scala @@ -38,8 +38,9 @@ package gr.grnet.aquarium.logic.test import org.junit.Test import org.junit.Assert._ import java.util.Date -import gr.grnet.aquarium.logic.accounting.dsl.{DSLTimeFrameRepeat, DSL, DSLUtils, DSLTimeSpec} import gr.grnet.aquarium.util.TestMethods +import gr.grnet.aquarium.logic.accounting.dsl._ +import annotation.tailrec class DSLUtilsTest extends DSLUtils with TestMethods with DSL { @@ -99,6 +100,7 @@ class DSLUtilsTest extends DSLUtils with TestMethods with DSL { var result = effectiveTimeslots(repeat, from, Some(to)) assertNotEmpty(result) + testSuccessiveTimeslots(result) assertEquals(31, result.size) //Expansion outside timeframe @@ -110,11 +112,42 @@ class DSLUtilsTest extends DSLUtils with TestMethods with DSL { repeat = DSLTimeFrameRepeat(parseCronString("00 12 * * 5"), parseCronString("00 14 * * 1")) result = effectiveTimeslots(repeat, from, Some(to)) + testSuccessiveTimeslots(result) assertEquals(4, result.size) repeat = DSLTimeFrameRepeat(parseCronString("00 12 * * Mon,Wed,Fri"), parseCronString("00 14 * * Tue,Thu,Sat")) result = effectiveTimeslots(repeat, from, Some(to)) + testSuccessiveTimeslots(result) assertEquals(13, result.size) } + + @Test + def testAllEffectiveTimeslots = { + val from = new Date(1321621969000L) //Fri Nov 18 15:12:49 +0200 2011 + val to = new Date(1324214719000L) //Sun Dec 18 15:25:19 +0200 2011 + + val repeat1 = DSLTimeFrameRepeat(parseCronString("00 12 * * *"), + parseCronString("00 14 * * *")) + val repeat2 = DSLTimeFrameRepeat(parseCronString("00 18 * * 5"), + parseCronString("00 20 * * 5")) + val tf = DSLTimeFrame(from, None, List(repeat1, repeat2)) + + val result = allEffectiveTimeslots(tf, from, to) + assertEquals(36, result.size) + testSuccessiveTimeslots(result) + } + + @tailrec + private def testSuccessiveTimeslots(result: List[(Date, Date)]): Unit = { + if (result.isEmpty) return + if (result.tail.isEmpty) return + if (result.head._2.after(result.tail.head._1)) + fail("Effectivity timeslots not successive: %s %s".format(result.head, result.tail.head)) + testSuccessiveTimeslots(result.tail) + } + + private def printTimeslots(result: List[(Date, Date)]) = { + result.foreach(p => print("from:%s to:%s\n".format(p._1, p._2))) + } } \ No newline at end of file -- 1.7.10.4