Advertisement
VladNitu

letrecteste

Mar 30th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. //5V2 - let-rec
  2.  
  3. test("letRecSameVarDiffTypes") {
  4. intercept[ParseException] {
  5. typeOf("""
  6. (letrec
  7. (
  8. ((x : Num) 1)
  9. ((x : Num) 2)
  10. )
  11.  
  12. (* 2 x)
  13. )
  14. """)
  15. }
  16. }
  17.  
  18. test("letRecBadDiffTypes") {
  19. intercept[TypeException] {
  20. typeOf("""
  21. (letrec
  22. (
  23. ((x : Num) 1)
  24. ((y : Bool) x)
  25. )
  26.  
  27. (* 2 x)
  28. )
  29. """)
  30. }
  31. }
  32.  
  33. test("letRecGood"){
  34. assertResult(NumT()) {
  35. typeOf("""
  36. (letrec
  37. (
  38. ((x : Num) 2)
  39. ((y : Num) (* 2 x))
  40. )
  41.  
  42. (* 2 y)
  43. )
  44. """)
  45. }
  46. }
  47.  
  48. test("let test - recursion") {
  49. val eq = """
  50. (letrec (((f : ((Num) -> Num)) (lambda ((x : Num))
  51. (if (num= x 0)
  52. 1
  53. (* x (f (- x 1))))))
  54. ((g : ((Num) -> Num)) (lambda ((y : Num))
  55. (f y))))
  56. (g 5))
  57. """
  58. assertResult(NumT()) {
  59. typeOf(eq)
  60. }
  61. }
  62. test("letrec parse err") {
  63. val eq = "(letrec ((f (lambda ((x : Bool)) (if (num= x 0) 1 (* x (f (- x 1)))))) (g (lambda ((y : Num)) (f y)))) (g 5))"
  64. assertThrows[ParseException] {
  65. typeOf(eq)
  66. }
  67. }
  68.  
  69. test("letRecBadUnbound"){
  70. intercept[TypeException] {
  71. typeOf("""
  72. (letrec
  73. (
  74. ((x : Num) 2)
  75. ((y : Num) (* 2 x))
  76. )
  77.  
  78. z
  79. )
  80. """)
  81. }
  82. }
  83.  
  84. test("letRecForwardRefGOOD") {
  85. assertResult(NumT()) {
  86. typeOf("""
  87. (letrec
  88. (
  89. ((x : Num) y)
  90. ((y : Num) 2)
  91. )
  92.  
  93. (+ x y)
  94. )
  95. """)
  96. }
  97.  
  98. intercept[InterpException] {
  99. safeInterp("""
  100. (letrec
  101. (
  102. ((x : Num) y)
  103. ((y : Num) 2)
  104. )
  105.  
  106. (+ x y)
  107. )
  108. """)
  109. }
  110. }
  111.  
  112. test("letRecBackwarddRefGOOD") {
  113. assertResult(NumT()) {
  114. typeOf("""
  115. (letrec
  116. (
  117. ((x : Num) 2)
  118. ((y : Num) x)
  119. )
  120.  
  121. (+ x y)
  122. )
  123. """)
  124. }
  125.  
  126. assertResult(NumV(4)) {
  127. safeInterp("""
  128. (letrec
  129. (
  130. ((x : Num) 2)
  131. ((y : Num) x)
  132. )
  133.  
  134. (+ x y)
  135. )
  136. """)
  137. }
  138. }
  139.  
  140. test("letrecBadDiffTypes") {
  141. intercept[TypeException] {
  142. typeOf("""
  143. (letrec
  144. (
  145. ((x : Num) y)
  146. ((y : Bool) true)
  147. )
  148.  
  149. (+ x y)
  150. )
  151. """)
  152. }
  153. }
  154.  
  155. test("letRecDoesNotType") {
  156. intercept[TypeException] {
  157. typeOf("""
  158. (letrec (((x : Bool) 1)) x)
  159. """)
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement