main_marking5/bf_test6.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:01:53 +0000
changeset 463 0315d9983cd0
parent 424 daf561a83ba6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
     1
import M5b._
392
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     3
import io.Source
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     4
import scala.util._
392
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     6
def urban_load_bff(name: String) : String = 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     7
  Try(Source.fromFile(name)("ISO-8859-1").mkString).getOrElse("")
392
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
     9
import scala.concurrent._
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    10
import scala.concurrent.duration._
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    11
import ExecutionContext.Implicits.global
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    12
import scala.language.postfixOps 
392
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    14
lazy val f = Future {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    15
  assert(optimise(urban_load_bff("benchmark.bf")).length == 181)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    16
  assert(optimise(urban_load_bff("sierpinski.bf")).length == 110)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    17
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    18
  assert(run3("[-]", Map(0 -> 100)) == Map(0 -> 0))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    19
  assert(run3("[->+<]", Map(0 -> 10)) == Map(0 -> 0, 1 -> 10))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    20
  assert(run3("[>>+>>+<<<<-]", Map(0 -> 42)) == Map(0 -> 0, 2 -> 42, 4 -> 42))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    21
  val hw_urban2 = """+++++[->++++++++++<]>--<+++[->>++++++++++
392
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
                  <<]>>++<<----------[+>.>.<+<]"""
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    23
  assert(run3(hw_urban2) == Map(0 -> 0, 1 -> 58, 2 -> 32))
392
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    25
  val hw_urban3 = """++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.
392
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
                   +++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."""
97ecdc8cb61b marking 5
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    28
  assert(run3(hw_urban3) == Map(0 -> 0, 5 -> 33, 1 -> 0, 6 -> 10, 2 -> 72, 3 -> 100, 4 -> 87))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    29
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    30
}
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    31
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    32
Await.result(f, 32 second)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    33