jtulach@402
|
1 |
package org.apidesign.effectivelist
|
jtulach@402
|
2 |
|
jtulach@402
|
3 |
import org.junit._
|
jtulach@402
|
4 |
import Assert._
|
jtulach@402
|
5 |
|
jtulach@402
|
6 |
class ListTest {
|
jtulach@402
|
7 |
case class Person(name : String, age : Int) extends Listable[Person]
|
jtulach@402
|
8 |
|
jtulach@402
|
9 |
var list : List[Person] = _
|
jtulach@402
|
10 |
var p1, p2, p3, p4 : Person = _
|
jtulach@402
|
11 |
|
jtulach@402
|
12 |
@Before def initializeListAndValues: Unit = {
|
jtulach@402
|
13 |
list = new List[Person]
|
jtulach@402
|
14 |
p1 = new Person("Jarda", 39)
|
jtulach@402
|
15 |
p2 = new Person("Sona", 37)
|
jtulach@402
|
16 |
p3 = new Person("Anna", 7)
|
jtulach@402
|
17 |
p4 = new Person("Ondra", 6)
|
jtulach@402
|
18 |
}
|
jtulach@402
|
19 |
|
jtulach@402
|
20 |
@Test def tryFewAdditions = {
|
jtulach@402
|
21 |
assertTrue(list.add(p1))
|
jtulach@402
|
22 |
assertTrue(list.add(p2))
|
jtulach@402
|
23 |
assertTrue(list.add(p3))
|
jtulach@402
|
24 |
assertTrue(list.add(p4))
|
jtulach@402
|
25 |
|
jtulach@402
|
26 |
assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
|
jtulach@402
|
27 |
assertEquals("Sona was inserted 2nd", 37, list.get(1).age)
|
jtulach@402
|
28 |
assertEquals("Anna was inserted 3rd", "Anna", list.get(2).name)
|
jtulach@402
|
29 |
assertEquals("Anna was inserted 4th", 6, list.get(3).age)
|
jtulach@402
|
30 |
}
|
jtulach@402
|
31 |
|
jtulach@402
|
32 |
@Test def addFewAndThenTryToRemoveHead() = {
|
jtulach@402
|
33 |
assertTrue(list.add(p1))
|
jtulach@402
|
34 |
assertTrue(list.add(p2))
|
jtulach@402
|
35 |
assertTrue(list.add(p3))
|
jtulach@402
|
36 |
|
jtulach@402
|
37 |
assertEquals("Size it three", 3, list.size())
|
jtulach@402
|
38 |
assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
|
jtulach@402
|
39 |
|
jtulach@402
|
40 |
assertFalse("Can't remove not added element", list.remove(p4))
|
jtulach@402
|
41 |
assertTrue("OK to remove head", list.remove(p1))
|
jtulach@402
|
42 |
|
jtulach@402
|
43 |
assertEquals("Sona is now first", "Sona", list.get(0).name)
|
jtulach@402
|
44 |
assertEquals("Anna is now 2nd", "Anna", list.get(1).name)
|
jtulach@402
|
45 |
|
jtulach@402
|
46 |
try {
|
jtulach@402
|
47 |
list.get(2)
|
jtulach@402
|
48 |
fail("Previous call should throw exception")
|
jtulach@402
|
49 |
} catch {
|
jtulach@402
|
50 |
case e : IndexOutOfBoundsException => ; // OK
|
jtulach@402
|
51 |
}
|
jtulach@402
|
52 |
}
|
jtulach@402
|
53 |
|
jtulach@402
|
54 |
@Test def addFewAndThenTryToRemoveSecond() = {
|
jtulach@402
|
55 |
assertEquals(0, list.size())
|
jtulach@402
|
56 |
assertTrue(list.add(p1))
|
jtulach@402
|
57 |
assertEquals(1, list.size())
|
jtulach@402
|
58 |
assertTrue(list.add(p2))
|
jtulach@402
|
59 |
assertEquals(2, list.size())
|
jtulach@402
|
60 |
assertTrue(list.add(p3))
|
jtulach@402
|
61 |
assertEquals(3, list.size())
|
jtulach@402
|
62 |
|
jtulach@402
|
63 |
assertEquals("Size it three", 3, list.size())
|
jtulach@402
|
64 |
assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
|
jtulach@402
|
65 |
|
jtulach@402
|
66 |
assertFalse("Can't remove not added element", list.remove(p4))
|
jtulach@402
|
67 |
assertTrue("OK to remove head", list.remove(p2))
|
jtulach@402
|
68 |
|
jtulach@402
|
69 |
assertEquals("Jarda is still first", "Jarda", list.get(0).name)
|
jtulach@402
|
70 |
assertEquals("Anna is now 2nd", "Anna", list.get(1).name)
|
jtulach@402
|
71 |
|
jtulach@402
|
72 |
try {
|
jtulach@402
|
73 |
list.get(2);
|
jtulach@402
|
74 |
fail("Previous call should throw exception")
|
jtulach@402
|
75 |
} catch {
|
jtulach@402
|
76 |
case e : IndexOutOfBoundsException => ; // OK
|
jtulach@402
|
77 |
}
|
jtulach@402
|
78 |
assertEquals(2, list.size());
|
jtulach@402
|
79 |
|
jtulach@402
|
80 |
list.remove(p1)
|
jtulach@402
|
81 |
list.remove(p3)
|
jtulach@402
|
82 |
|
jtulach@402
|
83 |
assertEquals("Not it is empty", 0, list.size())
|
jtulach@402
|
84 |
}
|
jtulach@402
|
85 |
}
|