9a259f98216ee122756f6943e5083eecc004da49
[aquarium] / src / test / scala / gr / grnet / aquarium / logic / test / TimeslotTest.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.logic.test
37
38 import gr.grnet.aquarium.util.TestMethods
39 import org.junit.Assert._
40 import org.junit.{Test}
41 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
42 import java.util.Date
43 import gr.grnet.aquarium.util.date.MutableDateCalc
44
45 /**
46  * Tests for the Timeslot class
47  *
48  * @author Georgios Gousios <gousiosg@gmail.com>
49  */
50 class TimeslotTest extends TestMethods {
51
52   @Test
53   def testOverlappingTimeslots = {
54     var t = Timeslot(new Date(7), new Date(20))
55     val list = List(Timeslot(new Date(1), new Date(3)),
56       Timeslot(new Date(6), new Date(8)),
57       Timeslot(new Date(11), new Date(15)))
58
59     var result = t.overlappingTimeslots(list)
60     assertEquals(2, result.size)
61     assertEquals(Timeslot(new Date(7), new Date(8)), result.head)
62     assertEquals(Timeslot(new Date(11), new Date(15)), result.tail.head)
63
64     t = Timeslot(new Date(9), new Date(10))
65     result = t.overlappingTimeslots(list)
66     assertEquals(0, result.size)
67
68     t = Timeslot(new Date(10), new Date(50))
69     result = t.overlappingTimeslots(List(Timeslot(new Date(0), new Date(100))))
70     assertEquals(1, result.size)
71     assertEquals(t, result.head)
72
73     t = Timeslot(new Date(0), new Date(100))
74     result = t.overlappingTimeslots(List(Timeslot(new Date(10), new Date(50))))
75     assertEquals(1, result.size)
76     assertEquals(Timeslot(new Date(10), new Date(50)), result.head)
77   }
78
79   @Test
80   def testNonOverlappingTimeslots = {
81     var t = Timeslot(new Date(7L), new Date(20L))
82     val list = List(Timeslot(new Date(1L), new Date(3L)),
83       Timeslot(new Date(6L), new Date(8L)),
84       Timeslot(new Date(11L), new Date(15L)))
85
86     var result = t.nonOverlappingTimeslots(list)
87     assertEquals(2L, result.size)
88
89     t = Timeslot(new Date(9L), new Date(20L))
90     result = t.nonOverlappingTimeslots(list)
91     assertEquals(2, result.size)
92
93     t = Timeslot(new Date(9), new Date(20))
94     result = t.nonOverlappingTimeslots(list)
95     assertEquals(2, result.size)
96
97     t = Timeslot(new Date(0), new Date(20))
98     result = t.nonOverlappingTimeslots(list)
99     assertEquals(4, result.size)
100     assertEquals(Timeslot(new Date(0), new Date(1)), result.head)
101     assertEquals(Timeslot(new Date(3), new Date(6)), result.tail.head)
102
103     t = Timeslot(new Date(13), new Date(20))
104     result = t.nonOverlappingTimeslots(list)
105     assertEquals(1, result.size)
106     assertEquals(Timeslot(new Date(15), new Date(20)), result.head)
107
108     result = t.nonOverlappingTimeslots(List())
109     assertEquals(1, result.size)
110     assertEquals(t, result.head)
111   }
112
113   @Test
114   def testMergeOverlaps = {
115     var l = List(Timeslot(new Date(3), new Date(5)),Timeslot(new Date(1), new Date(3)))
116
117     var result = Timeslot.mergeOverlaps(l)
118     assertEquals(1, result.size)
119     assertEquals(Timeslot(new Date(1), new Date(5)), result.head)
120
121     l = l ++ List(Timeslot(new Date(4), new Date(6)))
122     result = Timeslot.mergeOverlaps(l)
123     assertEquals(1, result.size)
124     assertEquals(Timeslot(new Date(1), new Date(6)), result.head)
125
126     l = l ++ List(Timeslot(new Date(7), new Date(8)))
127     result = Timeslot.mergeOverlaps(l)
128     assertEquals(2, result.size)
129     assertEquals(Timeslot(new Date(1), new Date(6)), result.head)
130     assertEquals(Timeslot(new Date(7), new Date(8)), result.tail.head)
131
132     l = l ++ List(Timeslot(new Date(2), new Date(20)))
133     result = Timeslot.mergeOverlaps(l)
134     assertEquals(1, result.size)
135     assertEquals(Timeslot(new Date(1), new Date(20)), result.head)
136   }
137
138
139 }