File: collection-stacks.scala

package info (click to toggle)
scala 2.7.7.dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,804 kB
  • ctags: 1,852
  • sloc: java: 7,762; xml: 6,608; sh: 1,723; cs: 158; makefile: 9; ansic: 6
file content (38 lines) | stat: -rw-r--r-- 1,034 bytes parent folder | download
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
import scala.collection._

object Test extends Application {
  def mutableStack[T](xs: T*): mutable.Stack[T] = {
    val s = new mutable.Stack[T]
    s.push(xs: _*)
    s
  }
  
  def immutableStack[T](xs: T*): immutable.Stack[T] = {
    immutable.Stack.Empty push xs
  }
  
  def check[T](expected: T, got: T) {
    println(got + ": " + (expected == got))
  }
  
  // check #957
  check("1-2-3", immutableStack(1, 2, 3).elements.mkString("-"))
  check("1-2-3", mutableStack(1, 2, 3).elements.mkString("-"))
  
  println("apply")
  check(1, immutableStack(1, 2, 3).apply(0))
  check(1, mutableStack(1, 2, 3).apply(0))
  check(3, immutableStack(1, 2, 3).apply(2))
  check(3, mutableStack(1, 2, 3).apply(2))
  
  println("top")
  check(3, immutableStack(1, 2, 3).top)
  check(3, mutableStack(1, 2, 3).top)
  
  println("pop")
  check("1-2", immutableStack(1, 2, 3).pop.mkString("-"))
  check(3, mutableStack(1, 2, 3).pop())
  check("1-2", { val s = mutableStack(1, 2, 3); s.pop(); s.toList.mkString("-") })
}

// vim: set ts=2 sw=2 et: