summaryrefslogtreecommitdiff
path: root/test/scalacheck/scala/collection/mutable/MutableTreeSet.scala
blob: d2f5a238c05ce785780c32e806da7d61073519fb (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package scala.collection.mutable

import java.io._

import org.scalacheck._
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop.forAll

import scala.collection.generic.CanBuildFrom
import scala.collection.immutable
import scala.collection.mutable
import scala.util.Try

object MutableTreeSetProperties extends Properties("mutable.TreeSet") {
  type K = String

  property("size, isEmpty") = forAll { (elems: Set[K]) =>
    val set = mutable.TreeSet[K]()
    set ++= elems
    set.size == elems.size && set.isEmpty == elems.isEmpty
  }

  property("+=") = forAll { (set: mutable.TreeSet[K], k: K) =>
    val oldSize = set.size
    val containedKeyBefore = set.contains(k)
    val newExpectedSize = if(containedKeyBefore) oldSize else oldSize + 1

    set += k
    set.contains(k) && set.size == newExpectedSize
  }

  property("++=") = forAll { (set: mutable.TreeSet[K], ks: Seq[K]) =>
    val oldElems = set.toList
    set ++= ks
    (oldElems ++ ks).forall(set.contains)
  }

  property("-=") = forAll { (set: mutable.TreeSet[K], k: K) =>
    val oldSize = set.size
    val containedKeyBefore = set.contains(k)
    val newExpectedSize = if(containedKeyBefore) oldSize - 1 else oldSize

    set -= k
    !set.contains(k) && set.size == newExpectedSize
  }

  property("--=") = forAll { (set: mutable.TreeSet[K], ks: Seq[K]) =>
    val oldElems = set.toList
    set --= ks
    val deletedElems = ks.toSet
    oldElems.forall { e => set.contains(e) == !deletedElems(e) }
  }

  property("iterator") = forAll { (ks: Set[K]) =>
    val set = mutable.TreeSet[K]()
    set ++= ks

    set.iterator.toSeq == ks.toSeq.sorted
  }

  property("iteratorFrom, keysIteratorFrom") = forAll { (ks: Set[K], k: K) =>
    val set = mutable.TreeSet[K]()
    set ++= ks

    set.iteratorFrom(k).toSeq == ks.filter(_ >= k).toSeq.sorted
    set.keysIteratorFrom(k).toSeq == ks.filter(_ >= k).toSeq.sorted
  }

  property("headOption") = forAll { (set: mutable.TreeSet[K]) =>
    set.headOption == Try(set.iterator.next()).toOption
  }

  property("lastOption") = forAll { (set: mutable.TreeSet[K]) =>
    set.lastOption == Try(set.iterator.max).toOption
  }

  property("clear") = forAll { (set: mutable.TreeSet[K]) =>
    set.clear()
    set.isEmpty && set.size == 0
  }

  property("serializable") = forAll { (set: mutable.TreeSet[K]) =>
    val bytesOut = new ByteArrayOutputStream()
    val out = new ObjectOutputStream(bytesOut)
    out.writeObject(set)
    val bytes = bytesOut.toByteArray

    val in = new ObjectInputStream(new ByteArrayInputStream(bytes))
    val sameSet = in.readObject().asInstanceOf[mutable.TreeSet[K]]
    set.iterator.toSeq == sameSet.iterator.toSeq
  }

  property("same behavior as immutable.TreeMap") = forAll { ops: Seq[Either[K, K]] =>
    var iset = immutable.TreeSet[K]()
    val mset = mutable.TreeSet[K]()

    ops.foreach {
      case Left(k) => iset += k; mset += k
      case Right(k) => iset -= k; mset -= k
    }

    iset.toList == mset.toList
  }
}

object MutableTreeSetViewProperties extends Properties("mutable.TreeSetView") {
  type K = String

  implicit val ord = implicitly[Ordering[K]]

  def in(key: K, from: Option[K], until: Option[K]) =
    from.fold(true)(_ <= key) && until.fold(true)(_ > key)

  def keysInView[This <: TraversableOnce[K], That](keys: This, from: Option[K], until: Option[K])(implicit bf: CanBuildFrom[This, K, That]) = {
    (bf.apply(keys) ++= keys.filter(in(_, from, until))).result()
  }

  property("size, isEmpty") = forAll { (keys: Set[K], from: Option[K], until: Option[K]) =>
    val map = mutable.TreeSet[K]()
    map ++= keys

    val mapView = map.rangeImpl(from, until)
    mapView.size == keysInView(keys, from, until).size &&
      mapView.isEmpty == !keys.exists(in(_, from, until))
  }

  property("+=") = forAll { (set: mutable.TreeSet[K], k: K, from: Option[K], until: Option[K]) =>
    val oldSize = set.size
    val containedKeyBefore = set.contains(k)
    val newExpectedSize = if(containedKeyBefore) oldSize else oldSize + 1
    val isInRange = in(k, from, until)

    val setView = set.rangeImpl(from, until)
    setView += k

    set.contains(k) && set.size == newExpectedSize && setView.contains(k) == isInRange
  }

  property("++=") = forAll { (set: mutable.TreeSet[K], ks: Seq[K], from: Option[K], until: Option[K]) =>
    val setView = set.rangeImpl(from, until)
    setView ++= ks
    ks.toSet.forall { k =>
      set.contains(k) && setView.contains(k) == in(k, from, until)
    }
  }

  property("-=") = forAll { (set: mutable.TreeSet[K], k: K, from: Option[K], until: Option[K]) =>
    val oldSize = set.size
    val containedKeyBefore = set.contains(k)
    val newExpectedSize = if(containedKeyBefore) oldSize - 1 else oldSize

    val setView = set.rangeImpl(from, until)
    setView -= k

    !set.contains(k) && set.size == newExpectedSize && !setView.contains(k)
  }

  property("--=") = forAll { (set: mutable.TreeSet[K], ks: Seq[K], from: Option[K], until: Option[K]) =>
    val setView = set.rangeImpl(from, until)
    setView --= ks
    ks.toSet.forall { k => !set.contains(k) && !setView.contains(k) }
  }

  property("iterator") = forAll { (ks: Set[K], from: Option[K], until: Option[K]) =>
    val set = mutable.TreeSet[K]()
    set ++= ks

    val setView = set.rangeImpl(from, until)
    setView.iterator.toSeq == keysInView(ks, from, until).toSeq.sorted
  }

  property("iteratorFrom, keysIteratorFrom") = forAll { (ks: Set[K], k: K, from: Option[K], until: Option[K]) =>
    val set = mutable.TreeSet[K]()
    set ++= ks

    val setView = set.rangeImpl(from, until)
    val newLower = Some(from.fold(k)(ord.max(_, k)))
    setView.iteratorFrom(k).toSeq == keysInView(ks, newLower, until).toSeq.sorted
  }

  property("headOption") = forAll { (set: mutable.TreeSet[K], from: Option[K], until: Option[K]) =>
    val setView = set.rangeImpl(from, until)
    setView.headOption == Try(keysInView(set.iterator, from, until).next()).toOption
  }

  property("lastOption") = forAll { (set: mutable.TreeSet[K], from: Option[K], until: Option[K]) =>
    val setView = set.rangeImpl(from, until)
    setView.lastOption == Try(keysInView(set.iterator, from, until).max).toOption
  }

  property("clear") = forAll { (set: mutable.TreeSet[K], from: Option[K], until: Option[K]) =>
    val setView = set.rangeImpl(from, until)
    setView.clear()
    set.isEmpty && setView.isEmpty && set.size == 0 && setView.size == 0
  }

  property("serializable") = forAll { (set: mutable.TreeSet[K], from: Option[K], until: Option[K]) =>
    val setView = set.rangeImpl(from, until)

    val bytesOut = new ByteArrayOutputStream()
    val out = new ObjectOutputStream(bytesOut)
    out.writeObject(setView)
    val bytes = bytesOut.toByteArray

    val in = new ObjectInputStream(new ByteArrayInputStream(bytes))
    val sameSetView = in.readObject().asInstanceOf[mutable.TreeSet[K]]
    setView.iterator.toSeq == sameSetView.iterator.toSeq
  }
}