本文整理汇总了Scala中collection.mutable.Stack类的典型用法代码示例。如果您正苦于以下问题:Scala Stack类的具体用法?Scala Stack怎么用?Scala Stack使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Stack类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TestBuiltins
//设置package包名称以及导入依赖的类
package interpreter
import java.nio.file.{Files, Paths}
import main.scala.interpreter._
import collection.mutable.Stack
import collection.JavaConversions._
import TestHelpers._
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSuite}
class TestBuiltins extends FunSuite with BeforeAndAfterAll with BeforeAndAfter {
val baseDir = "src/test/resources/"
val basePath = Paths.get(baseDir)
def provideFile(name: String, content: String) = {
val path = basePath.resolve(name)
Files.createFile(path)
Files.write(path, content.map(_.toByte).toArray)
}
override def beforeAll() = {
Files.createDirectories(basePath)
}
override def afterAll() = {
for(file <- Files.newDirectoryStream(basePath))
Files.deleteIfExists(file)
Files.deleteIfExists(basePath)
}
after {
State.files.clear
State.files.push(StdInReader)
}
test("Input must close files after using them") {
provideFile("empty.tn", "")
giveLine(" ")
leavesStack(TnList(Input))(baseDir + "empty.tn")()
Parser.parseNext()
Parser.parseNext()
assert(State.files === Stack(DummyFile(" "), StdInReader))
}
test("Input should make the parser parse given file") {
provideFile("test1.tn", "[]null?")
leavesStack(TnList(Input, Pull, Pop, Uncons, Pop, Pull, Pop, Uncons, Pop))(baseDir + "test1.tn")(IsEmpty, NullList)
}
test("Output should allow writing to a file") {
val path = baseDir + "testtemp.txt"
leavesStack(TnList(Output, "testing", State.table.map("print"), I))(path)()
assert(Files.readAllBytes(Paths.get(path)).map(_.toChar).mkString("") === "testing")
Files.delete(Paths.get(path))
}
}
示例2: TestHelpers
//设置package包名称以及导入依赖的类
package interpreter
import main.scala.interpreter._
import org.scalatest.Assertions._
import collection.mutable.Stack
object TestHelpers {
def leavesString(fun: TnList*)(before: TnObj*)(after: TnList): Unit = {
val stk = Stack[TnObj](before:_*)
fun.foreach(_.getList.foreach(_(stk)))
assert(stk.pop.toString === after.toString)
}
def leavesStack(fun: TnList*)(before: TnObj*)(after: TnObj*): Unit = {
val stk = Stack(before:_*)
fun.foreach(_.eval(stk))
assert(stk === Stack(after:_*))
}
implicit class withDirectEval(ls: TnList) {
def eval(stk: Stack[TnObj]) = ls.getList.foreach(_(stk))
}
case class DummyFile(str: String) extends InputQueue {
for(chr <- str) queue += chr
override def block(): Unit = ???
override def finished(): Boolean = throw new IllegalStateException()
}
def giveLine(str: String) = State.files.push(DummyFile(str));
def testLine(str: String)(implicit stack: Stack[TnObj]) = {
giveLine(str)
try {
while(true) Parser.parseNext().get.eval(stack)
} catch {
case _: Throwable => {}
}
}
}
示例3: ExampleSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
"An empty Set" should "have size 0" in {
assert(Set.empty.isEmpty)
}
it should "produce NoSuchElementException when head is invoked" in {
intercept[NoSuchElementException] {
Set.empty.head
}
}
}
示例4: FirstTapaSpec
//设置package包名称以及导入依赖的类
import org.scalatest._
import collection.mutable.Stack
// source: http://www.scalatest.org/quick_start
class FirstTapaSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
// assertions example
"Assertions" should "be used" in {
assert(1 == 1)
assert(Some(2).isDefined)
//assert(Some(2).isEmpty)
}
}
示例5: Path
//设置package包名称以及导入依赖的类
package game_mechanics.path
import collection.mutable.Stack
class Path {
def at(i: Int): Waypoint = {
return waypoints.apply(i)
}
def length(): Int = {
return waypoints.length
}
def last(): Waypoint = {
return waypoints.last
}
def pop(): Waypoint = {
return waypoints.pop
}
def head(): Waypoint = {
return waypoints.head
}
def apply(i: Int): Waypoint = {
return waypoints.apply(i)
}
def push(wp: Waypoint): Unit = {
waypoints.push(wp)
}
def pushAll(p: Path): Unit = {
waypoints.pushAll(p.waypoints)
}
def takeRight(i: Int): Stack[Waypoint] =
return this.waypoints.takeRight(i)
def reverse(): Path = {
val npath = new Path()
npath.waypoints.pushAll( this.waypoints)
return npath
}
def exists(p:(Waypoint) => Boolean) : Boolean = {
this.waypoints.exists(p)
}
override def toString() : String = {
this.waypoints.toString
}
def isEmpty(): Boolean = {
return waypoints.isEmpty
}
}
示例6: ExampleSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
// emptyStack.push(5)
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
示例7: ExampleSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
示例8: ExampleSpec
//设置package包名称以及导入依赖的类
package com.cds.learn
import collection.mutable.Stack
import org.scalatest._
import scala.collection.mutable
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new mutable.Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be(2)
stack.pop() should be(1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new mutable.Stack[Int]
a[NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
示例9: CategorySpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatestplus.play._
class CategorySpec extends PlaySpec {
"A Stack" must {
"pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() mustBe 2
stack.pop() mustBe 1
}
"throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] must be thrownBy {
emptyStack.pop()
}
}
}
}
示例10: AppSpec
//设置package包名称以及导入依赖的类
package churchscreen.app
import collection.mutable.Stack
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.junit.ShouldMatchersForJUnit
@RunWith(classOf[JUnitRunner])
class AppSpec extends FlatSpec with ShouldMatchersForJUnit {
behavior of "A Stack"
it should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should equal (2)
stack.pop() should equal (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[String]
evaluating { emptyStack.pop() } should produce [NoSuchElementException]
}
}
示例11: ExampleSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
示例12: ExampleSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
emptyStack.pop()
}
}
示例13: ExampleSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest.{Matchers,FlatSpec}
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a[NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
示例14: FunctionSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
import interpreter.ForthInterpreter
class FunctionSpec extends FlatSpec with Matchers {
"Functions" should "define and execute correctly" in {
var forth = new ForthInterpreter(List("defn", "addone", "1", "add", "enddef", "6"))
forth.execute()
// Function shouldn't be called
assert(forth.getStackState() == List(6))
forth = new ForthInterpreter(List("defn", "addone", "1", "add", "enddef", "6", "addone"))
forth.execute()
assert(forth.getStackState() == List(7))
}
}
示例15: ArithmeticSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
import interpreter.ForthInterpreter
class ArithmeticSpec extends FlatSpec with Matchers {
"A ForthInterpreter" should "add two numbers correctly" in {
val forth = new ForthInterpreter(List("1", "2", "add"))
forth.execute()
assert(forth.getStackState() == List(3))
}
it should "interpret negative numbers correctly" in {
val forth = new ForthInterpreter(List("5", "-10", "add"))
forth.execute()
assert(forth.getStackState() == List(-5))
}
it should "subtract two numbers correctly" in {
val forth = new ForthInterpreter(List("5", "4", "sub"))
forth.execute()
assert(forth.getStackState() == List(1))
}
it should "do integer division correctly" in {
var forth = new ForthInterpreter(List("4", "2", "div"))
forth.execute()
assert(forth.getStackState() == List(2))
forth = new ForthInterpreter(List("4", "3", "div"))
forth.execute()
assert(forth.getStackState() == List(1))
}
it should "multiply correctly" in {
val forth = new ForthInterpreter(List("5", "3", "mul"))
forth.execute()
assert(forth.getStackState() == List(15))
}
it should "do remainder correctly" in {
var forth = new ForthInterpreter(List("4", "2", "mod"))
forth.execute()
assert(forth.getStackState() == List(0))
forth = new ForthInterpreter(List("5", "3", "mod"))
forth.execute()
assert(forth.getStackState() == List(2))
}
}