1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
import scala.collection.mutable.TreeSet
object Test extends App {
val list = List(6,5,4,3,2,1,1,2,3,4,5,6,6,5,4,3,2,1)
val distinct = list.distinct
val sorted = distinct.sorted
// sublist stuff for a single level of slicing
val min = list.min
val max = list.max
val nonlist = ((min - 10) until (max + 20) filterNot list.contains).toList
val sublist = list filter {x => x >=(min + 1) && x < max}
val distinctSublist = sublist.distinct
val subnonlist = min :: max :: nonlist
val subsorted = distinctSublist.sorted
// subsublist for a 2nd level of slicing
val almostmin = sublist.min
val almostmax = sublist.max
val subsublist = sublist filter {x => x >=(almostmin + 1) && x < almostmax}
val distinctSubsublist = subsublist.distinct
val subsubnonlist = almostmin :: almostmax :: subnonlist
val subsubsorted = distinctSubsublist.sorted
def testSize {
def check(set : TreeSet[Int], list: List[Int]) {
assert(set.size == list.size, s"$set had size ${set.size} while $list had size ${list.size}")
}
check(TreeSet[Int](), List[Int]())
val set = TreeSet(list:_*)
check(set, distinct)
check(set.clone, distinct)
val subset = set from (min + 1) until max
check(subset, distinctSublist)
check(subset.clone, distinctSublist)
val subsubset = subset from (almostmin + 1) until almostmax
check(subsubset, distinctSubsublist)
check(subsubset.clone, distinctSubsublist)
}
def testContains {
def check(set : TreeSet[Int], list: List[Int], nonlist: List[Int]) {
assert(list forall set.apply, s"$set did not contain all elements of $list using apply")
assert(list forall set.contains, s"$set did not contain all elements of $list using contains")
assert(!(nonlist exists set.apply), s"$set had an element from $nonlist using apply")
assert(!(nonlist exists set.contains), s"$set had an element from $nonlist using contains")
}
val set = TreeSet(list:_*)
check(set, list, nonlist)
check(set.clone, list, nonlist)
val subset = set from (min + 1) until max
check(subset, sublist, subnonlist)
check(subset.clone, sublist, subnonlist)
val subsubset = subset from (almostmin + 1) until almostmax
check(subsubset, subsublist, subsubnonlist)
check(subsubset.clone, subsublist, subsubnonlist)
}
def testAdd {
def check(set : TreeSet[Int], list: List[Int], nonlist: List[Int]) {
var builtList = List[Int]()
for (x <- list) {
set += x
builtList = (builtList :+ x).distinct.sorted filterNot nonlist.contains
assert(builtList forall set.apply, s"$set did not contain all elements of $builtList using apply")
assert(builtList.size == set.size, s"$set had size ${set.size} while $builtList had size ${builtList.size}")
}
assert(!(nonlist exists set.apply), s"$set had an element from $nonlist using apply")
assert(!(nonlist exists set.contains), s"$set had an element from $nonlist using contains")
}
val set = TreeSet[Int]()
val clone = set.clone
val subset = set.clone from (min + 1) until max
val subclone = subset.clone
val subsubset = subset.clone from (almostmin + 1) until almostmax
val subsubclone = subsubset.clone
check(set, list, nonlist)
check(clone, list, nonlist)
check(subset, list, subnonlist)
check(subclone, list, subnonlist)
check(subsubset, list, subsubnonlist)
check(subsubclone, list, subsubnonlist)
}
def testRemove {
def check(set: TreeSet[Int], sorted: List[Int]) {
var builtList = sorted
for (x <- list) {
set remove x
builtList = builtList filterNot (_ == x)
assert(builtList forall set.apply, s"$set did not contain all elements of $builtList using apply")
assert(builtList.size == set.size, s"$set had size $set.size while $builtList had size $builtList.size")
}
}
val set = TreeSet(list:_*)
val clone = set.clone
val subset = set.clone from (min + 1) until max
val subclone = subset.clone
val subsubset = subset.clone from (almostmin + 1) until almostmax
val subsubclone = subsubset.clone
check(set, sorted)
check(clone, sorted)
check(subset, subsorted)
check(subclone, subsorted)
check(subsubset, subsubsorted)
check(subsubclone, subsubsorted)
}
def testIterator {
def check(set: TreeSet[Int], list: List[Int]) {
val it = set.iterator.toList
assert(it == list, s"$it did not equal $list")
}
val set = TreeSet(list: _*)
check(set, sorted)
check(set.clone, sorted)
val subset = set from (min + 1) until max
check(subset, subsorted)
check(subset.clone, subsorted)
val subsubset = subset from (almostmin + 1) until almostmax
check(subsubset, subsubsorted)
check(subsubset.clone, subsubsorted)
}
testSize
testContains
testAdd
testRemove
testIterator
}
|