本文整理汇总了Scala中collection.mutable.Stack类的典型用法代码示例。如果您正苦于以下问题:Scala Stack类的具体用法?Scala Stack怎么用?Scala Stack使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Stack类的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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))
}
}
开发者ID:MechaMagpie,项目名称:tn,代码行数:58,代码来源:TestBuiltins.scala
示例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 => {}
}
}
}
开发者ID:MechaMagpie,项目名称:tn,代码行数:41,代码来源:TestHelpers.scala
示例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
}
}
}
开发者ID:namelos,项目名称:play-slick-boilerplate,代码行数:31,代码来源:App.scala
示例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)
}
}
开发者ID:MalagaScala,项目名称:scala-tapas,代码行数:30,代码来源:FirstTapaSpec.scala
示例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
}
}
开发者ID:bunny-defense,项目名称:bunny_defense,代码行数:59,代码来源:path.scala
示例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()
}
}
}
开发者ID:Mooooony,项目名称:predi,代码行数:22,代码来源:ExampleSpec.scala
示例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()
}
}
}
开发者ID:jasonzou,项目名称:marc4scala,代码行数:20,代码来源:ExampleSpec.scala
示例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()
}
}
}
开发者ID:anancds,项目名称:scala-project,代码行数:27,代码来源:ExampleSpec.scala
示例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()
}
}
}
}
开发者ID:jordancoded1,项目名称:mechanicfm-reactivemongo-api,代码行数:22,代码来源:CategorySpec.scala
示例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]
}
}
开发者ID:ldeck,项目名称:churchscreen,代码行数:28,代码来源:AppTest.scala
示例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()
}
}
}
开发者ID:mat-hek,项目名称:akkaScalaClientServer,代码行数:21,代码来源:ExampleSpec.scala
示例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()
}
}
开发者ID:rrramiro,项目名称:sbt-linux-notifications,代码行数:21,代码来源:ExampleSpec.scala
示例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()
}
}
}
开发者ID:bijancn,项目名称:furry-chainsaw,代码行数:19,代码来源:ExampleSpec.scala
示例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))
}
}
开发者ID:sb-,项目名称:ScalaForth,代码行数:18,代码来源:FunctionTest.scala
示例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))
}
}
开发者ID:sb-,项目名称:ScalaForth,代码行数:52,代码来源:ArithmeticTest.scala
示例16: ForthSpec
//设置package包名称以及导入依赖的类
import collection.mutable.Stack
import org.scalatest._
import interpreter.ForthInterpreter
class ForthSpec extends FlatSpec with Matchers {
"Stack manipulations" should "initialize without crashing" in {
val forth = new ForthInterpreter(List("dup", "drop"))
}
it should "add three numbers to the stack" in {
val forth = new ForthInterpreter(List("1", "2", "3"))
forth.execute()
assert(forth.getStackState() == List(3, 2, 1))
}
it should "duplicate the top of the stack" in {
val forth = new ForthInterpreter(List("1", "2", "3", "dup"))
forth.execute()
assert(forth.getStackState() == List(3, 3, 2, 1))
}
it should "drop the top of the stack" in {
val forth = new ForthInterpreter(List("1", "2", "3", "drop"))
forth.execute()
assert(forth.getStackState() == List(2, 1))
}
it should "correctly reorder with the swap command" in {
val forth = new ForthInterpreter(List("1", "2", "3", "swap"))
forth.execute()
assert(forth.getStackState() == List(2, 3, 1))
}
it should "correctly duplicate the 2nd with the over command" in {
val forth = new ForthInterpreter(List("1", "2", "3", "over"))
forth.execute()
assert(forth.getStackState() == List(2, 3, 2, 1))
}
it should "correctly reorder the top 3 with the swap command" in {
val forth = new ForthInterpreter(List("99", "1", "2", "3", "rot"))
forth.execute()
assert(forth.getStackState() == List(1, 3, 2, 99))
}
}
开发者ID:sb-,项目名称:ScalaForth,代码行数:47,代码来源:ForthTest.scala
注:本文中的collection.mutable.Stack类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论