samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala
changeset 402 e25dbfce40e9
child 403 ebe08056c60c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala	Fri Aug 31 20:16:57 2012 +0200
     1.3 @@ -0,0 +1,85 @@
     1.4 +package org.apidesign.effectivelist
     1.5 +
     1.6 +import org.junit._
     1.7 +import Assert._
     1.8 +
     1.9 +class ListTest {
    1.10 +  case class Person(name : String, age : Int) extends Listable[Person]
    1.11 +  
    1.12 +  var list : List[Person] = _
    1.13 +  var p1, p2, p3, p4 : Person = _
    1.14 +  
    1.15 +  @Before def initializeListAndValues: Unit = {
    1.16 +    list = new List[Person]
    1.17 +    p1 = new Person("Jarda", 39)
    1.18 +    p2 = new Person("Sona", 37)
    1.19 +    p3 = new Person("Anna", 7)
    1.20 +    p4 = new Person("Ondra", 6)
    1.21 +  }
    1.22 +
    1.23 +  @Test def tryFewAdditions = {
    1.24 +    assertTrue(list.add(p1))
    1.25 +    assertTrue(list.add(p2))
    1.26 +    assertTrue(list.add(p3))
    1.27 +    assertTrue(list.add(p4))
    1.28 +    
    1.29 +    assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
    1.30 +    assertEquals("Sona was inserted 2nd", 37, list.get(1).age)
    1.31 +    assertEquals("Anna was inserted 3rd", "Anna", list.get(2).name)
    1.32 +    assertEquals("Anna was inserted 4th", 6, list.get(3).age)
    1.33 +  }
    1.34 +
    1.35 +  @Test def addFewAndThenTryToRemoveHead() = {
    1.36 +    assertTrue(list.add(p1))
    1.37 +    assertTrue(list.add(p2))
    1.38 +    assertTrue(list.add(p3))
    1.39 +    
    1.40 +    assertEquals("Size it three", 3, list.size())
    1.41 +    assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
    1.42 +
    1.43 +    assertFalse("Can't remove not added element", list.remove(p4))
    1.44 +    assertTrue("OK to remove head", list.remove(p1))
    1.45 +    
    1.46 +    assertEquals("Sona is now first", "Sona", list.get(0).name)
    1.47 +    assertEquals("Anna is now 2nd", "Anna", list.get(1).name)
    1.48 +    
    1.49 +    try {
    1.50 +      list.get(2)
    1.51 +      fail("Previous call should throw exception")
    1.52 +    } catch {
    1.53 +      case e : IndexOutOfBoundsException => ; // OK
    1.54 +    }
    1.55 +  }
    1.56 +  
    1.57 +  @Test def addFewAndThenTryToRemoveSecond() = {
    1.58 +    assertEquals(0, list.size())
    1.59 +    assertTrue(list.add(p1))
    1.60 +    assertEquals(1, list.size())
    1.61 +    assertTrue(list.add(p2))
    1.62 +    assertEquals(2, list.size())
    1.63 +    assertTrue(list.add(p3))
    1.64 +    assertEquals(3, list.size())
    1.65 +    
    1.66 +    assertEquals("Size it three", 3, list.size())
    1.67 +    assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
    1.68 +
    1.69 +    assertFalse("Can't remove not added element", list.remove(p4))
    1.70 +    assertTrue("OK to remove head", list.remove(p2))
    1.71 +    
    1.72 +    assertEquals("Jarda is still first", "Jarda", list.get(0).name)
    1.73 +    assertEquals("Anna is now 2nd", "Anna", list.get(1).name)
    1.74 +    
    1.75 +    try {
    1.76 +      list.get(2);
    1.77 +      fail("Previous call should throw exception")
    1.78 +    } catch {
    1.79 +      case e : IndexOutOfBoundsException => ; // OK
    1.80 +    }
    1.81 +    assertEquals(2, list.size());
    1.82 +    
    1.83 +    list.remove(p1)
    1.84 +    list.remove(p3)
    1.85 +    
    1.86 +    assertEquals("Not it is empty", 0, list.size())
    1.87 +  }
    1.88 +}