aboutsummaryrefslogtreecommitdiff
path: root/tests/run/t5986.scala
blob: b05d488f206d817202e6f9a0775987f7259f54e8 (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
import scala.collection._



/** A sorted set should not replace elements when adding
 *  and the element already exists in the set.
 */
object Test {

  class Foo(val name: String, val n: Int) {
    override def equals(obj: Any): Boolean = obj match { case other: Foo => name == other.name; case _ => false }
    override def hashCode = name.##
    override def toString = "Foo(" + name + ", " + n + ")"
  }

  implicit val ordering: Ordering[Foo] = Ordering.fromLessThan[Foo] { (a, b) => a.name.compareTo(b.name) < 0 }

  def check[S <: Set[Foo]](set: S): Unit = {
    def output(s: Set[Foo]) = println(s.toList.sorted.mkString(","))
    output(set + new Foo("bar", 2))
    output(set ++ List(new Foo("bar", 2), new Foo("bar", 3), new Foo("bar", 4)))
    output(set union Set(new Foo("bar", 2), new Foo("baz", 3), new Foo("bazz", 4)))
  }

  def main(args: Array[String]): Unit = {
    check(Set(new Foo("bar", 1)))
    check(immutable.Set(new Foo("bar", 1)))
    check(mutable.Set(new Foo("bar", 1)))
    check(immutable.SortedSet(new Foo("bar", 1)))
    check(mutable.SortedSet(new Foo("bar", 1)))
  }

}