aboutsummaryrefslogtreecommitdiff
path: root/tests/run/lists-run.scala
blob: 713b19659687dca903994615b34547eded6a8ee5 (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
/** Test the Scala implementation of class <code>scala.List</code>.
 *
 *  @author Stephane Micheloud
 */
import scala.language.postfixOps

object Test {
  def main(args: Array[String]): Unit = {
    Test_multiset.run() // multiset operations: union, intersect, diff
    Test1.run() //count, exists, filter, ..
    Test2.run() //#468
    Test3.run() //#1691
    Test4.run()  //#1721
    Test5.run()
  }
}

object Test_multiset {
  def run(): Unit = {
    def isSubListOf[A](thiz: List[A], that: List[A]): Boolean =
      thiz forall (that contains _)
    val xs = List(1, 1, 2)
    val ys = List(1, 2, 2, 3)
    assert(List(1, 1, 2, 1, 2, 2, 3) == (xs union ys), "xs_union_ys")
    assert(List(1, 2, 2, 3, 1, 1, 2) == (ys union xs), "ys_union_xs")
    assert(List(1, 2) == (xs intersect ys), "xs_intersect_ys")
    assert(List(1, 2) == (ys intersect xs), "ys_intersect_xs")
    assert(List(1) == (xs diff ys), "xs_diff_ys")
    assert(List(2, 3) == (ys diff xs), "ys_diff_xs")
    assert(isSubListOf(xs filterNot (ys contains), xs diff ys), "xs_subset_ys")

    val zs = List(0, 1, 1, 2, 2, 2)
    assert(List(0, 1, 1, 2, 2, 2, 1, 2, 2, 3) == (zs union ys), "zs_union_ys")
    assert(List(1, 2, 2, 3, 0, 1, 1, 2, 2, 2) == (ys union zs), "ys_union_zs")
    assert(List(1, 2, 2) == (zs intersect ys), "zs_intersect_ys")
    assert(List(1, 2, 2) == (ys intersect zs), "ys_intersect_zs")
    assert(List(0, 1, 2) == (zs diff ys), "zs_diff_ys")
    assert(List(3) == (ys diff zs), "ys_diff_zs")
    assert(isSubListOf(zs filterNot (ys contains), zs diff ys), "xs_subset_ys")

    val ws = List(2)
    assert(List(2, 1, 2, 2, 3) == (ws union ys), "ws_union_ys")
    assert(List(1, 2, 2, 3, 2) == (ys union ws), "ys_union_ws")
    assert(List(2) == (ws intersect ys), "ws_intersect_ys")
    assert(List(2) == (ys intersect ws), "ys_intersect_ws")
    assert(List() == (ws diff ys), "ws_diff_ys")
    assert(List(1, 2, 3) == (ys diff ws), "ys_diff_ws")
    assert(isSubListOf(ws filterNot (ys contains), ws diff ys), "ws_subset_ys")

    val vs = List(3, 2, 2, 1)
    assert(List(1, 1, 2, 3, 2, 2, 1) == (xs union vs), "xs_union_vs")
    assert(List(3, 2, 2, 1, 1, 1, 2) == (vs union xs), "vs_union_xs")
    assert(List(1, 2) == (xs intersect vs), "xs_intersect_vs")
    assert(List(2, 1) == (vs intersect xs), "vs_intersect_xs")
    assert(List(1) == (xs diff vs), "xs_diff_vs")
    assert(List(3, 2) == (vs diff xs), "vs_diff_xs")
    assert(isSubListOf(xs filterNot (vs contains), xs diff vs), "xs_subset_vs")

    // tests adapted from Thomas Jung
    assert({
        def sort(zs: List[Int]) = zs sortWith ( _ > _ )
        sort(xs intersect ys) == sort(ys intersect xs)
      }, "be symmetric after sorting")
    assert({
        def cardinality[A](zs: List[A], e: A): Int = zs count (e == _)
        val intersection = xs intersect ys
        xs forall (e => cardinality(intersection, e) == (cardinality(xs, e)
min cardinality(ys, e)))
      }, "obey min cardinality")
    assert({
        val intersection = xs intersect ys
		val unconsumed = xs.foldLeft(intersection){(rest, e) =>
		  if (! rest.isEmpty && e == rest.head) rest.tail else rest
		}
		unconsumed.isEmpty
      }, "maintain order")
    assert(xs == (xs intersect xs),
      "has the list as again intersection")
  }
}

object Test1 {
  def run(): Unit = {
    val xs1 = List(1, 2, 3)
    val xs2 = List('a', 'b')
    val xs3 = List(List(1, 2), List(4, 5))
    val xs4 = List(2, 4, 6, 8)
    val xs5 = List(List(3, 4), List(3), List(4, 5))

  {
    val n1 = xs1 count { e => e % 2 != 0 }
    val n2 = xs4 count { e => e < 5 }
    assert(4 == (n1 + n2), "check_count")
  }
  {
    val b1 = xs1 exists { e => e % 2 == 0 }
    val b2 = xs4 exists { e => e == 5 }
    assert(!(b1 & b2), "check_exists")
  }
  {
    val ys1 = xs1 filter { e => e % 2 == 0 }
    val ys2 = xs4 filter { e => e < 5 }
    assert(3 == ys1.length + ys2.length, "check_filter")
  }
  {
    val n1 = xs1.foldLeft(0)((e1, e2) => e1 + e2)
    val ys1 = xs4.foldLeft(List[Int]())((e1, e2) => e2 :: e1)
    assert(10 == n1 + ys1.length, "check_foldLeft")
  }
  {
    val b1 = xs1 forall { e => e < 10}
    val b2 = xs4 forall { e => e % 2 == 0 }
    assert(b1 & b2, "check_forall")
  }
  {
    val ys1 = xs1 filterNot { e => e % 2 != 0 }
    val ys2 = xs4 filterNot { e => e < 5 }
    assert(3 == ys1.length + ys2.length, "check_remove")
  }
  {
    val ys1 = xs1 zip xs2
    val ys2 = xs1 zip xs3
    assert(4 == ys1.length + ys2.length, "check_zip")
  }
  {
    val ys1 = xs1.zipAll(xs2, 0, '_')
    val ys2 = xs2.zipAll(xs1, '_', 0)
    val ys3 = xs1.zipAll(xs3, 0, List(-1))
    assert(9 == ys1.length + ys2.length + ys3.length, "check_zipAll")
  }
  }
}

object Test2 {
  def run(): Unit = {
    val xs1 = List(1, 2, 3)
    val xs2 = List(0)

    val ys1 = xs1 ::: List(4)
    assert(List(1, 2, 3, 4) == ys1, "check_:::")

    val ys2 = ys1 filterNot (_ == 4)
    assert(xs1 == ys2, "check_-")

    val n2 = (xs1 ++ ys1).length
    val n3 = (xs1 ++ Nil).length
    val n4 = (xs1 ++ ((new collection.mutable.ArrayBuffer[Int]) += 0)).length
    assert(14 == n2 + n3 + n4, "check_++")
  }
}

object Test3 {
  def run(): Unit = {
    try {
      List.range(1, 10, 0)
    } catch {
      case e: IllegalArgumentException => ()
      case _: Throwable => throw new Error("List.range(1, 10, 0)")
    }
    assert(List.range(10, 0, -2) == List(10, 8, 6, 4, 2))
  }
}

object Test4 {
  def run(): Unit = {
    assert(List(1,2,3).endsWith(List(2,3)))
    assert(!List(1,2,3).endsWith(List(1,3)))
    assert(List(1,2,3).endsWith(List()))
    assert(!List(1,2,3).endsWith(List(0,1,2,3)))
    assert(List(1,2,3).endsWith(List(1,2,3)))
    assert(!List().endsWith(List(1,2,3)))
    assert(List().endsWith(List()))
  }
}

object Test5 {
  def show(xs: List[String]) = xs match {
    case "foo" :: args => args.toString
    case List(x) => x.toString
    case Nil => "Nil"
  }
  def run(): Unit = {
    assert(show(List()) == "Nil")
    assert(show(List("a")) == "a")
    assert(show(List("foo", "b")) == "List(b)")
  }
}