Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //test: Test
- import org.scalatest.FunSuite
- import Parser._
- import Desugar._
- import Interp._
- import Untyped._
- class Test extends FunSuite with CatchErrorSuite {
- /**
- * Tests for Desugaring
- */
- test("Desugar 5") {
- assertResult(
- NumC(5)
- ) {
- desugar(NumExt(5))
- }
- }
- /**
- * Tests for Interpreting
- */
- test("Interp 5") {
- assertResult(
- NumV(5)
- ) {
- interp(NumC(5))
- }
- }
- test("Interp 5+true throws InterpException") {
- intercept[InterpException] {
- interp(PlusC(NumC(5), TrueC()))
- }
- }
- // My tests from 4-5: Mutation assignment
- test("easySet") {
- assertResult(NumV(2)) {
- interp("(let ((x 1)) (set x 2))")
- }
- }
- test("setInX") {
- assertResult(NumV(3)) {
- interp("(let ((x 1)) (set x 3))")
- }
- }
- test("setBadUnbounded") {
- intercept[InterpException] {
- interp("(let ((y 1)) (set x 2))") // `x` unbounded error
- }
- }
- test("setTooManyParams") {
- intercept[ParseException] {
- interp("(let ((y 1)) (set y 2 3))")
- }
- }
- test("setTooFewParams") {
- intercept[ParseException] {
- interp("(let ((y 1)) (set y))")
- }
- }
- test("setAssignVarToNumber") {
- intercept[ParseException] {
- interp("(let ((y 1)) (set 2 y))")
- }
- intercept[ParseException] {
- interp("(let ((y 1)) (set and y))")
- }
- intercept[ParseException] {
- interp("(let ((y 1)) (set y +))")
- }
- }
- test("setNotBound") {
- intercept[InterpException]{
- interp("(set x (+ true 5))")
- }
- }
- test("setToYourself" ) {
- assertResult(NumV(1)) {
- interp("""
- (let ((x 1)) (set x x)
- )
- """)
- }
- }
- test("letNoArgs") {
- intercept[ParseException] {
- interp("(let () (+ 1 2))")
- }
- }
- test("letUnboundedX") {
- intercept[InterpException] {
- interp("(let ((a x)) (+ x 1))")
- }
- }
- test("letDoesNotWorkLikeLetRec") {
- intercept[InterpException] {
- interp("(let ((x 1) (y x)) (+ x y))")
- }
- }
- test("MultipleXBindings") {
- intercept[ParseException] {
- parse("(let ((x 1) (x 2)) (+ x 1))")
- }
- }
- test("setShadowEnv") {
- assertResult(
- (NumV(3), List(Cell(1, NumV(3)), Cell(0, NumV(10)))
- )) {
- interp(desugar(parse("(let ((x 1)) (set x 3))")),
- List(Pointer("x", 0)),
- List(Cell(0, NumV(10)))
- )
- }
- }
- test("setUseEnvVar") {
- assertResult((NumV(10),List(Cell(2,NumV(3)), Cell(1,NumV(1)), Cell(0,NumV(10))))){
- interp(desugar(parse("""
- (let ((y 1) (z 2)) (seq (set z 3) x))
- """)),
- List(Pointer("x", 0)),
- List(Cell(0, NumV(10))))
- }
- intercept[InterpException] { //`c` unbounded
- interp(desugar(parse("""
- (let ((y 1) (z 2)) (seq (set c z) x))
- """)),
- List(Pointer("x", 0)),
- List(Cell(0, NumV(10))))
- }
- }
- test("seqFewParams") {
- intercept[ParseException] {
- parse("(seq 2)")
- }
- }
- test("seqTooManyParams") {
- intercept[ParseException] {
- parse("(seq 2 3 (+ 1 2))")
- }
- }
- test("seqMutateXTwoTimes") {
- assertResult( (NumV(4), List(Cell(0, NumV(4)))) ) {
- interp(desugar(parse("""
- (seq
- (set x 1)
- (set x 4)
- )
- """)),
- List(Pointer("x", 0)),
- List(Cell(0, NumV(1)))
- )
- }
- }
- test("seqMutateXThreeTimesNestedCorrectly") {
- assertResult((NumV(4),
- List(Cell(0,NumV(4)), Cell(1,NumV(600))))) {
- interp(desugar(parse("""
- (seq
- (set x 1)
- (seq
- (set x 2)
- (set x 4)
- )
- )
- """)),
- List(Pointer("x", 0), Pointer("y", 1)),
- List(Cell(0, NumV(1)), Cell(1, NumV(600))))
- }
- }
- test("seqMutateXThreeTimesWRONG") {
- intercept[ParseException] {
- parse("""
- (seq
- (set x 1)
- (set x 2)
- (set x 4)
- )
- """)
- }
- }
- // Example assignment tests
- test("sumValuesImperatively") { // passes: `seq` and `application`
- assertResult(NumV(15)) {
- interp("""
- (let
- ((sumto (box 0))
- (countv (box 0))
- (sumv (box 0))
- (nop 0)
- (runsum (box 0))
- )
- (seq
- (setbox
- runsum
- (lambda ()
- (if (num= (unbox countv) (unbox sumto))
- nop
- (seq (seq (setbox countv (+ (unbox countv) 1))
- (setbox sumv (+ (unbox sumv ) (unbox countv))))
- ((unbox runsum)))
- )
- )
- )
- (seq (setbox sumto 5)
- (seq ((unbox runsum))
- (unbox sumv)
- )
- )
- )
- )
- """)
- }
- }
- test("imperativeFibonacci") {
- assertResult(NumV(5)) {
- interp("""
- (let ((a 0) (b 1) (sum 0))
- (letrec
- ((fib
- (lambda (n)
- (if (or (num= n 0) (num= n 1))
- sum
- (seq (set sum (+ a b))
- (seq (set a b)
- (seq (set b sum)
- (fib (- n 1)))))))))
- (fib 5)))
- """)
- }
- }
- test("functionalFib") {
- assertResult(NumV(5)) {
- interp( """
- (letrec
- ((fib
- (lambda (n)
- (if (num= n 0)
- 0
- (if (num= n 1)
- 1
- (+ (fib (- n 1)) (fib (- n 2))))))))
- (fib 5))
- """)
- }
- }
- //More `let` tests
- test("countThreeTimesBoxes") {
- assertResult(NumV(3)) {
- interp("""
- (let ((counter
- (lambda ()
- (let ((n (box 0)))
- (list
- (lambda () (setbox n (+ (unbox n) 1)))
- (lambda () (setbox n 0))
- )
- )
- )
- ))
- (let (( c (counter) ))
- (seq
- ((head c))
- (seq
- ((head c))
- ((head c))
- )
- )
- )
- )
- """)
- }
- }
- test("countThreeTimesVariables") {
- assertResult(NumV(3)) {
- interp( """
- (let ((counter
- (lambda ()
- (let ((n 0))
- (list
- (lambda () (set n (+ n 1)))
- (lambda () (set n 0))
- )
- )
- )
- ))
- (let (( c (counter) ))
- (seq
- ((head c))
- (seq
- ((head c))
- ((head c))
- )
- )
- )
- )
- """)
- }
- }
- test("countTwiceAlternativeSlides") { // passes `set` test
- assertResult(NumV(2)) {
- interp("""
- (let
- (
- (inc (lambda (r) (setbox r (+ (unbox r) 1))))
- (reset (lambda (r) (setbox r 0)))
- )
- (let ((counter (box 0)))
- (seq
- (set counter (box 1))
- (inc counter)
- )
- )
- )
- """)
- }
- }
- test("letShadowNested") {
- assertResult(NumV(10)) {
- interp("""
- (
- let (
- (x 1)
- )
- (
- let (
- (x (set x 5))
- )
- (+ x x)
- )
- )
- """)
- }
- }
- test("Box cyclic datum slides") {
- assertResult(NumV(1)) {
- interp("""(let ((b (box 0)))
- (seq (setbox b b) (seq (unbox (unbox (unbox (unbox b)))) 1))
- )""")
- }
- }
- test("letUnboundedG") {
- intercept[InterpException] {
- interp("(let ((f (lambda (x) (+ x 2)))) (g 3))")
- }
- }
- test("letUnboundedY") {
- intercept[InterpException] {
- interp("(let ((x y)) x)")
- }
- }
- test("letPropagateEnv") {
- assertResult((NumV(10) ,List(Cell(1,NumV(10)), Cell(0,NumV(10))))) {
- interp(desugar(parse("(let ((y x)) y)")),
- List(Pointer("x", 0)),
- List(Cell(0, NumV(10)))
- )
- }
- }
- test("LetParseError") {
- intercept[ParseException]{
- interp(desugar(parse("(let ((2 x)) y)")),
- List(Pointer("x", 0)),
- List(Cell(0, NumV(10)))
- )
- }
- }
- test("orderMatterComparison") {
- assertResult( (BoolV(true), List(Cell(0, NumV(2))))) {
- interp(desugar(parse("""
- (let ((n 1))
- (num< n (set n 2))
- )
- """)), Nil, Nil)
- }
- }
- test("orderMatterComparison2") {
- assertResult( (BoolV(false), List(Cell(0, NumV(2))))) {
- interp(desugar(parse("""
- (let ((n 1))
- (num< (set n 2) n)
- )
- """)), Nil, Nil)
- }
- }
- test("orderMatterComparison3") {
- assertResult( (BoolV(false), List(Cell(0, NumV(2))))) {
- interp(desugar(parse("""
- (let ((n 1))
- (num< (set n 2) n)
- )
- """)), Nil, Nil)
- }
- }
- test("orderMatterComparison4") {
- assertResult( (BoolV(true), List(Cell(0, NumV(2))))) {
- interp(desugar(parse("""(let ((n 1))
- (num< n (set n (+ n 1)))
- )""")), Nil, Nil)
- }
- }
- test("miriMutation") {
- assertResult((NumV(1001), List(Cell(1, NumV(1)), Cell(0, NumV(1000))))) {
- interp(desugar(parse("""
- (
- let ((x 1))
- (
- (lambda (y) (+ x y))
- (seq (set x 1000) 1)
- )
- )
- """)), Nil, Nil)
- }
- }
- // My tests for 7. Object assignment
- // test("desugarObjPoint") {
- // val expr = """
- // (object
- // ((field x 0))
- // ((method get-x () x))
- // )
- // """
- // assertResult(PointerClosV(FdC(List("msg"), )))
- // {
- // interp(expr)
- // }
- // }
- test("interp obj 2") {
- assertResult(
- PointerClosV(FdC(List("msg"),IfC(EqStrC(StringC("msg"),StringC("get-x")),FdC(List(),IdC("x")),IfC(EqStrC(StringC("msg"),StringC("set-x")),FdC(List("nx"),SetC("x",IdC("nx"))),IfC(EqStrC(StringC("msg"),StringC("get-y")),FdC(List(),IdC("y")),IfC(EqStrC(StringC("msg"),StringC("set-y")),FdC(List("ny"),SetC("y",IdC("ny"))),UndefinedC()))))),List())
- ) {
- val exprc = FdC(List("msg"),IfC(EqStrC(StringC("msg"),StringC("get-x")),FdC(List(),IdC("x")),IfC(EqStrC(StringC("msg"),StringC("set-x")),FdC(List("nx"),SetC("x",IdC("nx"))),IfC(EqStrC(StringC("msg"),StringC("get-y")),FdC(List(),IdC("y")),IfC(EqStrC(StringC("msg"),StringC("set-y")),FdC(List("ny"),SetC("y",IdC("ny"))),UndefinedC())))))
- interp(exprc)
- }
- }
- // test("interp obj") {
- // assertResult(
- // NumV(7)
- // ) {
- // val exp3 = "(let ((point(object((field x 3)(field y (msg self set-x 7)))((method get-x () x)(method get-y () y)(method set-x (nx) (set x nx))(method set-y (ny) (set y ny))))))(seq (msg point set-y 42)(msg point get-x)))"
- // val s1 = desugar(parse(exp3))
- // interp(s1)
- // }
- // }
- test("doSeqTest") {
- val expr = """(let ((x 0))
- (do-seq
- (set x 1)
- (set x (+ x x))
- x))"""
- assertResult(NumV(2)) {
- interp(expr)
- }
- }
- test("LateBindingBasic") {
- assertResult(ConsV(NumV(4), ConsV(NumV(2), NilV()))) {
- interp("""
- (letrec ((vehicle-factory
- (lambda ()
- (object
- ((field position 1))
- ((method speed-factor () 1)
- (method get-position () position)
- (method move () (set position (* position
- (msg self speed-factor))))))))
- (car
- (object-del
- (vehicle-factory)
- ()
- ((method speed-factor () 4))))
- (bicycle
- (object-del
- (vehicle-factory)
- ()
- ((method speed-factor () 2)))))
- (seq
- (msg car move)
- (seq
- (msg bicycle move)
- (cons
- (msg car get-position)
- (cons
- (msg bicycle get-position)
- nil)))))
- """)
- }
- }
- test("LateBinding2") {
- assertResult(ConsV(NumV(8), ConsV(NumV(4), NilV()))) {
- interp("""
- (letrec ((vehicle-factory
- (lambda ()
- (object
- ((field position 2))
- ((method speed-factor () 5)
- (method get-position () position)
- (method move () (set position (* position
- (msg self speed-factor))))))))
- (car
- (object-del
- (vehicle-factory)
- ()
- ((method speed-factor () 4))))
- (bicycle
- (object-del
- (vehicle-factory)
- ()
- ((method speed-factor () 2)))))
- (seq
- (msg car move)
- (seq
- (msg bicycle move)
- (cons
- (msg car get-position)
- (cons
- (msg bicycle get-position)
- nil)))))
- """)
- }
- }
- /**
- * Helpers
- */
- def interp(expr: String): Value = Interp.interp(desugar(parse(expr)), Nil, Nil)._1
- def interp(expr: ExprC): Value = Interp.interp(expr, Nil, Nil)._1
- def interp(expr: ExprC, nv: PointerEnvironment, st: Store): (Value, Store) = Interp.interp(expr, nv, st)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement