Advertisement
VladNitu

test objects Vlad

Apr 2nd, 2023
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 12.81 KB | None | 0 0
  1. //test: Test
  2.  
  3. import org.scalatest.FunSuite
  4. import Parser._
  5. import Desugar._
  6. import Interp._
  7. import Untyped._
  8.  
  9. class Test extends FunSuite with CatchErrorSuite {
  10.  
  11.   /**
  12.    * Tests for Desugaring
  13.    */
  14.  
  15.   test("Desugar 5") {
  16.     assertResult(
  17.       NumC(5)
  18.     ) {
  19.       desugar(NumExt(5))
  20.     }
  21.   }
  22.  
  23.   /**
  24.    * Tests for Interpreting
  25.    */
  26.  
  27.   test("Interp 5") {
  28.     assertResult(
  29.       NumV(5)
  30.     ) {
  31.       interp(NumC(5))
  32.     }
  33.   }
  34.  
  35.   test("Interp 5+true throws InterpException") {
  36.     intercept[InterpException] {
  37.       interp(PlusC(NumC(5), TrueC()))
  38.     }
  39.   }
  40.  
  41.    // My tests from 4-5: Mutation assignment
  42.  
  43.   test("easySet") {
  44.     assertResult(NumV(2)) {
  45.       interp("(let ((x 1)) (set x 2))")
  46.     }
  47.   }
  48.  
  49.     test("setInX") {
  50.     assertResult(NumV(3)) {
  51.       interp("(let ((x 1)) (set x 3))")
  52.     }
  53.   }
  54.  
  55.  
  56.   test("setBadUnbounded") {
  57.     intercept[InterpException] {
  58.       interp("(let ((y 1)) (set x 2))") // `x` unbounded error
  59.     }
  60.   }
  61.  
  62.  
  63.   test("setTooManyParams") {
  64.     intercept[ParseException] {
  65.       interp("(let ((y 1)) (set y 2 3))")
  66.     }
  67.   }
  68.  
  69.     test("setTooFewParams") {
  70.     intercept[ParseException] {
  71.       interp("(let ((y 1)) (set y))")
  72.     }
  73.   }
  74.  
  75.   test("setAssignVarToNumber") {
  76.     intercept[ParseException] {
  77.       interp("(let ((y 1)) (set 2 y))")
  78.     }
  79.  
  80.      intercept[ParseException] {
  81.       interp("(let ((y 1)) (set and y))")
  82.     }
  83.  
  84.     intercept[ParseException] {
  85.       interp("(let ((y 1)) (set y +))")
  86.     }
  87.   }
  88.  
  89.   test("setNotBound") {
  90.  
  91.     intercept[InterpException]{
  92.       interp("(set x (+ true 5))")
  93.     }
  94.   }
  95.  
  96.   test("setToYourself" ) {
  97.     assertResult(NumV(1)) {
  98.       interp("""
  99.      (let ((x 1)) (set x x)
  100.      )
  101.      """)
  102.     }
  103.   }
  104.  
  105.   test("letNoArgs") {
  106.     intercept[ParseException] {
  107.       interp("(let () (+ 1 2))")
  108.     }
  109.   }
  110.  
  111.   test("letUnboundedX") {
  112.     intercept[InterpException] {
  113.       interp("(let ((a x)) (+ x 1))")
  114.     }
  115.   }
  116.  
  117.   test("letDoesNotWorkLikeLetRec") {
  118.     intercept[InterpException] {
  119.       interp("(let ((x 1) (y x)) (+ x y))")
  120.     }
  121.   }
  122.     test("MultipleXBindings") {
  123.     intercept[ParseException] {
  124.       parse("(let ((x 1) (x 2)) (+ x 1))")
  125.     }
  126.   }
  127.  
  128.  
  129.   test("setShadowEnv") {
  130.     assertResult(
  131.       (NumV(3), List(Cell(1, NumV(3)), Cell(0, NumV(10)))
  132.       )) {
  133.       interp(desugar(parse("(let ((x 1)) (set x 3))")),
  134.       List(Pointer("x", 0)),
  135.       List(Cell(0, NumV(10)))
  136.       )
  137.     }
  138.   }
  139.  
  140.   test("setUseEnvVar") {
  141.     assertResult((NumV(10),List(Cell(2,NumV(3)), Cell(1,NumV(1)), Cell(0,NumV(10))))){
  142.       interp(desugar(parse("""
  143.      (let ((y 1) (z 2)) (seq (set z 3) x))
  144.      """)),
  145.       List(Pointer("x", 0)),
  146.       List(Cell(0, NumV(10))))
  147.     }
  148.  
  149.  
  150.     intercept[InterpException] { //`c` unbounded
  151.       interp(desugar(parse("""
  152.      (let ((y 1) (z 2)) (seq (set c z) x))
  153.      """)),
  154.        List(Pointer("x", 0)),
  155.        List(Cell(0, NumV(10))))
  156.     }
  157.   }
  158.  
  159.   test("seqFewParams") {
  160.     intercept[ParseException] {
  161.       parse("(seq 2)")
  162.     }
  163.   }
  164.  
  165.   test("seqTooManyParams") {
  166.     intercept[ParseException] {
  167.       parse("(seq 2 3 (+ 1 2))")
  168.     }
  169.   }
  170.  
  171.   test("seqMutateXTwoTimes") {
  172.     assertResult( (NumV(4), List(Cell(0, NumV(4)))) ) {
  173.       interp(desugar(parse("""
  174.      (seq
  175.        (set x 1)
  176.        (set x 4)
  177.      )
  178.      """)),
  179.        List(Pointer("x", 0)),
  180.        List(Cell(0, NumV(1)))
  181.       )
  182.     }
  183.   }
  184.  
  185.   test("seqMutateXThreeTimesNestedCorrectly") {
  186.     assertResult((NumV(4),
  187.     List(Cell(0,NumV(4)), Cell(1,NumV(600))))) {
  188.       interp(desugar(parse("""
  189.      (seq
  190.        (set x 1)
  191.        (seq
  192.          (set x 2)
  193.          (set x 4)
  194.        )
  195.      )
  196.      """)),
  197.        List(Pointer("x", 0), Pointer("y", 1)),
  198.        List(Cell(0, NumV(1)), Cell(1, NumV(600))))
  199.     }
  200.   }
  201.  
  202.     test("seqMutateXThreeTimesWRONG") {
  203.     intercept[ParseException] {
  204.       parse("""
  205.      (seq
  206.        (set x 1)
  207.        (set x 2)
  208.        (set x 4)
  209.      )
  210.      """)
  211.     }
  212.   }
  213.  
  214.   // Example assignment tests
  215.  
  216.   test("sumValuesImperatively") { // passes: `seq` and `application`
  217.     assertResult(NumV(15)) {
  218.       interp("""
  219.   (let
  220. ((sumto  (box 0))
  221.  (countv (box 0))
  222.  (sumv   (box 0))
  223.  (nop    0)
  224.  (runsum (box 0))
  225. )
  226. (seq
  227.  (setbox
  228.   runsum
  229.   (lambda ()
  230.     (if (num= (unbox countv) (unbox sumto))
  231.       nop
  232.       (seq (seq (setbox countv (+ (unbox countv) 1))
  233.                 (setbox sumv   (+ (unbox sumv  ) (unbox countv))))
  234.            ((unbox runsum)))
  235.     )
  236.   )
  237.  )
  238.  (seq (setbox sumto 5)
  239.       (seq ((unbox runsum))
  240.            (unbox sumv)
  241.       )
  242.  )
  243. )
  244. )
  245.   """)
  246.     }
  247.   }
  248.  
  249.   test("imperativeFibonacci") {
  250.     assertResult(NumV(5)) {
  251.       interp("""
  252.   (let ((a 0) (b 1) (sum 0))
  253.  (letrec
  254.    ((fib
  255.      (lambda (n)
  256.        (if (or (num= n 0) (num= n 1))
  257.          sum
  258.          (seq (set sum (+ a b))
  259.          (seq (set a b)
  260.          (seq (set b sum)
  261.              (fib (- n 1)))))))))
  262.      (fib 5)))
  263.   """)
  264.     }
  265.   }
  266.  
  267.     test("functionalFib") {
  268.       assertResult(NumV(5)) {
  269.         interp( """
  270.   (letrec
  271.  ((fib
  272.    (lambda (n)
  273.      (if (num= n 0)
  274.        0
  275.        (if (num= n 1)
  276.          1
  277.          (+ (fib (- n 1)) (fib (- n 2))))))))
  278.  (fib 5))
  279.   """)
  280.       }
  281.     }
  282.  
  283.     //More `let` tests
  284.     test("countThreeTimesBoxes") {
  285.        assertResult(NumV(3)) {
  286.          interp("""
  287.   (let ((counter
  288.        (lambda ()
  289.          (let ((n (box 0)))
  290.            (list
  291.              (lambda () (setbox n (+ (unbox n) 1)))
  292.              (lambda () (setbox n 0))
  293.            )
  294.          )
  295.        )
  296.     ))
  297.  (let (( c (counter) ))
  298.    (seq
  299.      ((head c))
  300.        (seq
  301.          ((head c))
  302.          ((head c))
  303.        )
  304.    )
  305.  )
  306. )
  307.   """)
  308.        }
  309.     }
  310.  
  311.     test("countThreeTimesVariables") {
  312.        assertResult(NumV(3)) {
  313.          interp( """
  314.   (let ((counter
  315.        (lambda ()
  316.          (let ((n 0))
  317.            (list
  318.              (lambda () (set n (+ n 1)))
  319.              (lambda () (set n 0))
  320.            )
  321.          )
  322.        )
  323.     ))
  324.  (let (( c (counter) ))
  325.    (seq
  326.      ((head c))
  327.      (seq
  328.        ((head c))
  329.        ((head c))
  330.      )
  331.    )
  332.  )
  333. )
  334.   """)
  335.        }
  336.      }
  337.  
  338.      test("countTwiceAlternativeSlides") { // passes `set` test
  339.        assertResult(NumV(2)) {
  340.          interp("""
  341.  (let
  342.    (
  343.     (inc (lambda (r) (setbox r (+ (unbox r) 1))))
  344.     (reset (lambda (r) (setbox r 0)))
  345.    )
  346.      (let ((counter (box 0)))
  347.        (seq
  348.          (set counter (box 1))
  349.          (inc counter)
  350.        )
  351.      )
  352.  )
  353.   """)
  354.        }
  355.      }
  356.  
  357.      test("letShadowNested") {
  358.        assertResult(NumV(10)) {
  359.         interp("""
  360.        (
  361.          let (
  362.            (x 1)
  363.            )
  364.            (
  365.              let (
  366.                (x (set x 5))
  367.              )
  368.              (+ x x)
  369.            )
  370.        )
  371.        """)
  372.        }
  373.      }
  374.  
  375.        test("Box cyclic datum slides") {
  376.     assertResult(NumV(1)) {
  377.       interp("""(let ((b (box 0)))
  378.                  (seq (setbox b b) (seq (unbox (unbox (unbox (unbox b)))) 1))
  379.                )""")
  380.     }
  381.   }
  382.  
  383.     test("letUnboundedG") {
  384.     intercept[InterpException] {
  385.       interp("(let ((f (lambda (x) (+ x 2)))) (g 3))")
  386.     }
  387.     }
  388.  
  389.     test("letUnboundedY") {
  390.     intercept[InterpException] {
  391.       interp("(let ((x y)) x)")
  392.     }
  393.     }
  394.  
  395.     test("letPropagateEnv") {
  396.       assertResult((NumV(10) ,List(Cell(1,NumV(10)), Cell(0,NumV(10))))) {
  397.         interp(desugar(parse("(let ((y x)) y)")),
  398.         List(Pointer("x", 0)),
  399.       List(Cell(0, NumV(10)))
  400.       )
  401.       }
  402.     }
  403.  
  404.     test("LetParseError") {
  405.       intercept[ParseException]{
  406.         interp(desugar(parse("(let ((2 x)) y)")),
  407.         List(Pointer("x", 0)),
  408.       List(Cell(0, NumV(10)))
  409.       )
  410.       }
  411.     }
  412.  
  413.     test("orderMatterComparison") {
  414.       assertResult( (BoolV(true), List(Cell(0, NumV(2))))) {
  415.         interp(desugar(parse("""
  416.        (let ((n 1))
  417. (num< n (set n 2))
  418. )
  419.        """)), Nil, Nil)
  420.       }
  421.     }
  422.  
  423.      test("orderMatterComparison2") {
  424.       assertResult( (BoolV(false), List(Cell(0, NumV(2))))) {
  425.         interp(desugar(parse("""
  426.        (let ((n 1))
  427. (num< (set n 2) n)
  428. )
  429.        """)), Nil, Nil)
  430.       }
  431.      }
  432.  
  433.        test("orderMatterComparison3") {
  434.       assertResult( (BoolV(false), List(Cell(0, NumV(2))))) {
  435.         interp(desugar(parse("""
  436.        (let ((n 1))
  437. (num< (set n 2) n)
  438. )
  439.        """)), Nil, Nil)
  440.       }
  441.     }
  442.  
  443.     test("orderMatterComparison4") {
  444.       assertResult( (BoolV(true), List(Cell(0, NumV(2))))) {
  445.         interp(desugar(parse("""(let ((n 1))
  446. (num< n (set n (+ n 1)))
  447. )""")), Nil, Nil)
  448.       }
  449.     }
  450.  
  451.  
  452.      test("miriMutation") {
  453.       assertResult((NumV(1001), List(Cell(1, NumV(1)), Cell(0, NumV(1000))))) {
  454.         interp(desugar(parse("""
  455.        (
  456.          let ((x 1))
  457.          (
  458.            
  459.            (lambda (y) (+ x y))
  460.            (seq (set x 1000) 1)
  461.            
  462.          )
  463.        )
  464.  
  465.        """)), Nil, Nil)
  466.       }
  467.     }
  468.  
  469.   // My tests for 7. Object assignment
  470.  
  471. //   test("desugarObjPoint") {
  472. //     val expr = """
  473. // (object
  474. // ((field x 0))
  475. // ((method get-x () x))
  476. // )
  477. // """
  478. //     assertResult(PointerClosV(FdC(List("msg"), )))
  479. //      {
  480. //       interp(expr)
  481. //     }
  482. //   }
  483.  
  484. test("interp obj 2") {
  485.     assertResult(
  486.       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())
  487.     ) {
  488.       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())))))
  489.       interp(exprc)
  490.     }
  491.   }
  492.  
  493. // test("interp obj") {
  494. //     assertResult(
  495. //       NumV(7)
  496. //     ) {
  497. //       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)))"
  498.  
  499. //       val s1 = desugar(parse(exp3))
  500.      
  501. //       interp(s1)
  502. //     }
  503. //   }
  504.  
  505.   test("doSeqTest") {
  506.     val expr = """(let ((x 0))
  507.  (do-seq
  508.    (set x 1)
  509.    (set x (+ x x))
  510.    x))"""
  511.  
  512.     assertResult(NumV(2)) {
  513.       interp(expr)
  514.     }
  515.   }
  516.  
  517.   test("LateBindingBasic") {
  518.  
  519.     assertResult(ConsV(NumV(4), ConsV(NumV(2), NilV()))) {
  520.       interp("""
  521.      (letrec ((vehicle-factory
  522.            (lambda ()
  523.              (object
  524.                ((field position 1))
  525.                ((method speed-factor () 1)
  526.                 (method get-position () position)
  527.                 (method move () (set position (* position
  528.                                                  (msg self speed-factor))))))))
  529.         (car
  530.            (object-del
  531.              (vehicle-factory)
  532.              ()
  533.              ((method speed-factor () 4))))
  534.         (bicycle
  535.            (object-del
  536.              (vehicle-factory)
  537.              ()
  538.              ((method speed-factor () 2)))))
  539.  (seq
  540.    (msg car move)
  541.    (seq
  542.      (msg bicycle move)
  543.      (cons
  544.        (msg car get-position)
  545.        (cons
  546.          (msg bicycle get-position)
  547.          nil)))))
  548.          """)
  549.     }
  550.  
  551.   }
  552.  
  553.  
  554.   test("LateBinding2") {
  555.  
  556.     assertResult(ConsV(NumV(8), ConsV(NumV(4), NilV()))) {
  557.       interp("""
  558.      (letrec ((vehicle-factory
  559.            (lambda ()
  560.              (object
  561.                ((field position 2))
  562.                ((method speed-factor () 5)
  563.                 (method get-position () position)
  564.                 (method move () (set position (* position
  565.                                                  (msg self speed-factor))))))))
  566.         (car
  567.            (object-del
  568.              (vehicle-factory)
  569.              ()
  570.              ((method speed-factor () 4))))
  571.         (bicycle
  572.            (object-del
  573.              (vehicle-factory)
  574.              ()
  575.              ((method speed-factor () 2)))))
  576.  (seq
  577.    (msg car move)
  578.    (seq
  579.      (msg bicycle move)
  580.      (cons
  581.        (msg car get-position)
  582.        (cons
  583.          (msg bicycle get-position)
  584.          nil)))))
  585.          """)
  586.     }
  587.  
  588.   }
  589.  
  590.  
  591.   /**
  592.    * Helpers
  593.    */
  594.  
  595.   def interp(expr: String): Value = Interp.interp(desugar(parse(expr)), Nil, Nil)._1
  596.   def interp(expr: ExprC): Value = Interp.interp(expr, Nil, Nil)._1
  597.   def interp(expr: ExprC, nv: PointerEnvironment, st: Store): (Value, Store) = Interp.interp(expr, nv, st)
  598.  
  599. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement