Permalink
Browse files

Improved testing framework for sets and maps.

Switched to JUnit testing framework for sets and maps.  They now test
broadly against each other for consistency.  Tests for mutable.AnyRefMap
and mutable.LongMap are folded in here (originals removed).  There is still
lots of redundancy with other tests that has not been removed.

This framework is also designed to enable more robust testing of changes to
implementations of sets and maps; although it's still quite possible to get
a broken implementation through, these tests should make it harder to get
the fundamentals wrong.
  • Loading branch information...
Ichoran committed Dec 26, 2013
1 parent 9f0594c commit 5cc01766a6b1496e575e13ce64381a6986998027
@@ -1,91 +0,0 @@
-object Test extends App {
-
- import scala.collection.mutable.HashMap;
- import scala.collection.mutable.AnyRefMap;
-
- val keys = Array(
- null, "perch", "herring", "salmon", "pike", "cod", ""
- )
-
- val rn = new scala.util.Random(42L)
- var arm = AnyRefMap.empty[String, Int]
- val hm = HashMap.empty[String, Int]
-
- def checkConsistent = hm.forall{ case (k,v) => arm.get(k).exists(_ == v) }
-
- assert {
- (0 to 10000).forall{ i =>
- val k = keys(rn.nextInt(keys.length))
- if (rn.nextInt(100) < 2) arm = arm.clone()
- if (rn.nextInt(100) < 5) arm.repack()
- if (rn.nextBoolean) {
- hm += ((k, i))
- rn.nextInt(6) match {
- case 0 => arm += ((k, i))
- case 1 => arm += (k, i)
- case 2 => arm(k) = i
- case 3 => arm.put(k,i)
- case 4 => arm ++= List((k,i))
- case _ => if (!arm.contains(k)) arm.getOrElseUpdate(k,i)
- else arm += (k,i)
- }
- }
- else {
- hm -= k
- rn.nextInt(2) match {
- case 0 => arm -= k
- case _ => arm --= List(k)
- }
- }
- checkConsistent
- }
- }
-
- assert {
- val mapped =
- arm.map{ case (k,v) => (if (k==null) "" else k+k) -> v.toString }
- mapped.getClass == arm.getClass
- }
-
- assert {
- val arm2 = new AnyRefMap[java.lang.Integer,Unit](2000000)
- for (i <- 0 until 1000000) arm2(java.lang.Integer.valueOf(i)) = ()
-
- arm2.size == 1000000 &&
- (0 to 1100000 by 100000).map(java.lang.Integer.valueOf).forall(i => (arm2 contains i) == i < 1000000)
- }
-
- arm = AnyRefMap("heron" -> 22, "dove" -> 5, "budgie" -> 0)
-
- assert{
- var s = ""
- arm.foreachKey(s += _)
-
- s.length == "herondovebudgie".length &&
- s.contains("heron") &&
- s.contains("dove") &&
- s.contains("budgie")
- }
-
- assert{ var s = 0L; arm.foreachValue(s += _); s == 27L }
-
- assert {
- val m2 = arm.mapValuesNow(_+2)
- arm.transformValues(_+2)
- m2 == arm && !(m2 eq arm) && (for ((_,v) <- arm) yield v).sum == 33L
- }
-
- assert {
- val arm2 = new AnyRefMap[String, String](x => if (x==null) "null" else x)
- arm2 += ("cod" -> "fish", "Rarity" -> "unicorn")
- val hm2 = (new HashMap[String,String]) ++= arm2
-
- List(null, "cod", "sparrow", "Rarity").forall(i =>
- arm2.get(i) == hm2.get(i) &&
- arm2.getOrElse(i, "") == hm2.getOrElse(i, "") &&
- arm2(i) == hm2.get(i).getOrElse(if (i==null) "null" else i.toString) &&
- arm2.getOrNull(i) == hm2.get(i).orNull
- )
- }
-}
-
@@ -1,79 +0,0 @@
-object Test extends App {
-
- import scala.collection.mutable.HashMap;
- import scala.collection.mutable.LongMap;
-
- val keys = Array(
- Long.MinValue, Int.MinValue - 1L, Int.MinValue, -9127, -1,
- 0, 1, 9127, Int.MaxValue, Long.MaxValue
- )
-
- val rn = new scala.util.Random(42L)
- var lm = LongMap.empty[Long]
- val hm = HashMap.empty[Long,Long]
-
- def checkConsistent = hm.forall{ case (k,v) => lm.get(k).exists(_ == v) }
-
- assert {
- (0 to 10000).forall{ i =>
- val k = keys(rn.nextInt(keys.length))
- if (rn.nextInt(100) < 2) lm = lm.clone()
- if (rn.nextInt(100) < 5) lm.repack()
- if (rn.nextBoolean) {
- hm += ((k, i))
- rn.nextInt(6) match {
- case 0 => lm += ((k, i))
- case 1 => lm += (k, i)
- case 2 => lm(k) = i
- case 3 => lm.put(k,i)
- case 4 => lm ++= List((k,i))
- case _ => if (!lm.contains(k)) lm.getOrElseUpdate(k,i)
- else lm += (k,i)
- }
- }
- else {
- hm -= k
- rn.nextInt(2) match {
- case 0 => lm -= k
- case _ => lm --= List(k)
- }
- }
- checkConsistent
- }
- }
-
- assert {
- lm.map{ case (k,v) => -k*k -> v.toString }.getClass == lm.getClass
- }
-
- assert {
- val lm2 = new LongMap[Unit](2000000)
- for (i <- 0 until 1000000) lm2(i) = ()
-
- lm2.size == 1000000 &&
- (0 to 1100000 by 100000).forall(i => (lm2 contains i) == i < 1000000)
- }
-
- lm = LongMap(8L -> 22L, -5L -> 5L, Long.MinValue -> 0L)
-
- assert{ var s = 0L; lm.foreachKey(s += _); s == Long.MinValue + 3 }
- assert{ var s = 0L; lm.foreachValue(s += _); s == 27L }
- assert {
- val m2 = lm.mapValuesNow(_+2)
- lm.transformValues(_+2)
- m2 == lm && !(m2 eq lm) && (for ((_,v) <- lm) yield v).sum == 33L
- }
-
- assert {
- val lm2 = new LongMap[String](_.toString)
- lm2 += (5L -> "fish", 0L -> "unicorn")
- val hm2 = (new HashMap[Long,String]) ++= lm2
-
- List(Long.MinValue, 0L, 1L, 5L).forall(i =>
- lm2.get(i) == hm2.get(i) &&
- lm2.getOrElse(i, "") == hm2.getOrElse(i, "") &&
- lm2(i) == hm2.get(i).getOrElse(i.toString) &&
- lm2.getOrNull(i) == hm2.get(i).orNull
- )
- }
-}
Oops, something went wrong.

0 comments on commit 5cc0176

Please sign in to comment.