@@ -12,6 +12,36 @@ class SemispaceCacheTest extends ScalaCheckSuite {
1212
1313 val genEmpty : Gen [SemispaceCache [Int , String ]] =
1414 Gen .choose(- 1 , 10 ).map(SemispaceCache .empty(_, true ))
15+
16+ test(" eviction should never contain values in gen0/gen1" ) {
17+ val cache = SemispaceCache .empty(2 , true ).insert(" one" , 1 )
18+
19+ val i1 = cache.insert(" one" , 1 )
20+ // Two doesn't exist; space in gen0, insert
21+ val i2 = i1.lookup(" two" ).map(_._1).getOrElse(i1.insert(" two" , 2 ))
22+ assertEquals(i2.gen0, Map (" one" -> 1 , " two" -> 2 ))
23+ assertEquals(i2.gen1, Map .empty[String , Int ])
24+ assertEquals(i2.evicted.toList, Nil )
25+
26+ // Three doesn't exist, hit max; slide gen0 -> gen1 and add to gen0
27+ val i3 = i2.lookup(" three" ).map(_._1).getOrElse(i2.insert(" three" , 3 ))
28+ assertEquals(i3.gen0, Map (" three" -> 3 ))
29+ assertEquals(i3.gen1, Map (" one" -> 1 , " two" -> 2 ))
30+ assertEquals(i3.evicted.toList, Nil )
31+
32+ // One exists in gen1; pull up to gen0 and REMOVE from gen1
33+ val i4 = i3.lookup(" one" ).map(_._1).getOrElse(i3.insert(" one" , 1 ))
34+ assertEquals(i4.gen0, Map (" one" -> 1 , " three" -> 3 ))
35+ assertEquals(i4.gen1, Map (" two" -> 2 ))
36+ assertEquals(i4.evicted.toList, Nil )
37+
38+ // Four doesn't exist; gen0 is full so push to gen1
39+ // insert four to gen0 and evict gen1
40+ val i5 = i4.lookup(" four" ).map(_._1).getOrElse(i4.insert(" four" , 4 ))
41+ assertEquals(i5.gen0, Map (" four" -> 4 ))
42+ assertEquals(i5.gen1, Map (" one" -> 1 , " three" -> 3 ))
43+ assertEquals(i5.evicted.toList, List (2 ))
44+ }
1545
1646 test(" insert on empty cache results in eviction" ) {
1747 val cache = SemispaceCache .empty(0 , true ).insert(" one" , 1 )
@@ -73,7 +103,7 @@ class SemispaceCacheTest extends ScalaCheckSuite {
73103 val max = c.max
74104
75105 // Load up the cache such that it overflows by 1
76- val c ʹ = (0 to max).foldLeft(c) { case (c, n) => c.insert(n, " x " ) }
106+ val c ʹ = (0 to max).foldLeft(c) { case (c, n) => c.insert(n, n.toString ) }
77107 assertEquals(cʹ.gen0.size, 1 min max)
78108 assertEquals(cʹ.gen1.size, max)
79109
@@ -82,7 +112,9 @@ class SemispaceCacheTest extends ScalaCheckSuite {
82112 case None => assertEquals(max, 0 )
83113 case Some ((cʹʹ, _)) =>
84114 assertEquals(cʹʹ.gen0.size, 2 min max)
85- assertEquals(cʹʹ.gen1.size, max)
115+ // When we promote 0 to gen0, we remove it from gen1
116+ assertEquals(cʹʹ.gen1.size, max- 1 max 1 )
117+ assertEquals(cʹʹ.evicted.toList, Nil )
86118 }
87119
88120 }
0 commit comments