A fork of Crisp for HARP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
3.2 KiB

  1. ;;;
  2. ;;; See IMPL/tests/stepA_mal.mal for implementation specific
  3. ;;; interop tests.
  4. ;;;
  5. ;;
  6. ;; Testing readline
  7. (readline "mal-user> ")
  8. "hello"
  9. ;=>"\"hello\""
  10. ;;
  11. ;; Testing *host-language*
  12. ;;; each impl is different, but this should return false
  13. ;;; rather than throwing an exception
  14. (= "something bogus" *host-language*)
  15. ;=>false
  16. ;;
  17. ;; ------- Optional Functionality ----------
  18. ;; ------- (Needed for self-hosting) -------
  19. ;;
  20. ;; Testing metadata on functions
  21. ;;
  22. ;; Testing metadata on mal functions
  23. (meta (fn* (a) a))
  24. ;=>nil
  25. (meta (with-meta (fn* (a) a) {"b" 1}))
  26. ;=>{"b" 1}
  27. (meta (with-meta (fn* (a) a) "abc"))
  28. ;=>"abc"
  29. (def! l-wm (with-meta (fn* (a) a) {"b" 2}))
  30. (meta l-wm)
  31. ;=>{"b" 2}
  32. (meta (with-meta l-wm {"new_meta" 123}))
  33. ;=>{"new_meta" 123}
  34. (meta l-wm)
  35. ;=>{"b" 2}
  36. (def! f-wm (with-meta (fn* [a] (+ 1 a)) {"abc" 1}))
  37. (meta f-wm)
  38. ;=>{"abc" 1}
  39. (meta (with-meta f-wm {"new_meta" 123}))
  40. ;=>{"new_meta" 123}
  41. (meta f-wm)
  42. ;=>{"abc" 1}
  43. (def! f-wm2 ^{"abc" 1} (fn* [a] (+ 1 a)))
  44. (meta f-wm2)
  45. ;=>{"abc" 1}
  46. ;; Meta of native functions should return nil (not fail)
  47. (meta +)
  48. ;=>nil
  49. ;;
  50. ;; Make sure closures and metadata co-exist
  51. (def! gen-plusX (fn* (x) (with-meta (fn* (b) (+ x b)) {"meta" 1})))
  52. (def! plus7 (gen-plusX 7))
  53. (def! plus8 (gen-plusX 8))
  54. (plus7 8)
  55. ;=>15
  56. (meta plus7)
  57. ;=>{"meta" 1}
  58. (meta plus8)
  59. ;=>{"meta" 1}
  60. (meta (with-meta plus7 {"meta" 2}))
  61. ;=>{"meta" 2}
  62. (meta plus8)
  63. ;=>{"meta" 1}
  64. ;;
  65. ;; Testing atoms
  66. (def! inc3 (fn* (a) (+ 3 a)))
  67. (def! a (atom 2))
  68. ;=>(atom 2)
  69. ;;;(type a)
  70. ;;;;=>"atom"
  71. (deref a)
  72. ;=>2
  73. @a
  74. ;=>2
  75. (reset! a 3)
  76. ;=>3
  77. @a
  78. ;=>3
  79. (swap! a inc3)
  80. ;=>6
  81. @a
  82. ;=>6
  83. (swap! a (fn* (a) a))
  84. ;=>6
  85. (swap! a (fn* (a) (* 2 a)))
  86. ;=>12
  87. (swap! a (fn* (a b) (* a b)) 10)
  88. ;=>120
  89. (swap! a + 3)
  90. ;=>123
  91. ;; Testing swap!/closure interaction
  92. (def! inc-it (fn* (a) (+ 1 a)))
  93. (def! atm (atom 7))
  94. (def! f (fn* [] (swap! atm inc-it)))
  95. (f)
  96. ;=>8
  97. (f)
  98. ;=>9
  99. ;; Testing hash-map evaluation and atoms (i.e. an env)
  100. (def! e (atom {"+" +}))
  101. (swap! e assoc "-" -)
  102. ( (get @e "+") 7 8)
  103. ;=>15
  104. ( (get @e "-") 11 8)
  105. ;=>3
  106. ;;
  107. ;; ------- Optional Functionality --------------
  108. ;; ------- (Not needed for self-hosting) -------
  109. ;;
  110. ;; Testing conj function
  111. (conj (list) 1)
  112. ;=>(1)
  113. (conj (list 1) 2)
  114. ;=>(2 1)
  115. (conj (list 2 3) 4)
  116. ;=>(4 2 3)
  117. (conj (list 2 3) 4 5 6)
  118. ;=>(6 5 4 2 3)
  119. (conj (list 1) (list 2 3))
  120. ;=>((2 3) 1)
  121. (conj [] 1)
  122. ;=>[1]
  123. (conj [1] 2)
  124. ;=>[1 2]
  125. (conj [2 3] 4)
  126. ;=>[2 3 4]
  127. (conj [2 3] 4 5 6)
  128. ;=>[2 3 4 5 6]
  129. (conj [1] [2 3])
  130. ;=>[1 [2 3]]
  131. ;;
  132. ;; Testing metadata on collections
  133. (meta [1 2 3])
  134. ;=>nil
  135. (with-meta [1 2 3] {"a" 1})
  136. ;=>[1 2 3]
  137. (meta (with-meta [1 2 3] {"a" 1}))
  138. ;=>{"a" 1}
  139. (vector? (with-meta [1 2 3] {"a" 1}))
  140. ;=>true
  141. (meta (with-meta [1 2 3] "abc"))
  142. ;=>"abc"
  143. (meta (with-meta (list 1 2 3) {"a" 1}))
  144. ;=>{"a" 1}
  145. (list? (with-meta (list 1 2 3) {"a" 1}))
  146. ;=>true
  147. (meta (with-meta {"abc" 123} {"a" 1}))
  148. ;=>{"a" 1}
  149. (map? (with-meta {"abc" 123} {"a" 1}))
  150. ;=>true
  151. ;;; Not actually supported by Clojure
  152. ;;;(meta (with-meta (atom 7) {"a" 1}))
  153. ;;;;=>{"a" 1}
  154. (def! l-wm (with-meta [4 5 6] {"b" 2}))
  155. ;=>[4 5 6]
  156. (meta l-wm)
  157. ;=>{"b" 2}
  158. (meta (with-meta l-wm {"new_meta" 123}))
  159. ;=>{"new_meta" 123}
  160. (meta l-wm)
  161. ;=>{"b" 2}
  162. ;;
  163. ;; Testing metadata on builtin functions
  164. (meta +)
  165. ;=>nil
  166. (def! f-wm3 ^{"def" 2} +)
  167. (meta f-wm3)
  168. ;=>{"def" 2}
  169. (meta +)
  170. ;=>nil