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.

388 lines
4.4 KiB

  1. ;; -----------------------------------------------------
  2. ;; Testing list functions
  3. (list)
  4. ;=>()
  5. (list? (list))
  6. ;=>true
  7. (empty? (list))
  8. ;=>true
  9. (empty? (list 1))
  10. ;=>false
  11. (list 1 2 3)
  12. ;=>(1 2 3)
  13. (count (list 1 2 3))
  14. ;=>3
  15. (count (list))
  16. ;=>0
  17. (count nil)
  18. ;=>0
  19. (if (> (count (list 1 2 3)) 3) "yes" "no")
  20. ;=>"no"
  21. (if (>= (count (list 1 2 3)) 3) "yes" "no")
  22. ;=>"yes"
  23. ;; Testing if form
  24. (if true 7 8)
  25. ;=>7
  26. (if false 7 8)
  27. ;=>8
  28. (if true (+ 1 7) (+ 1 8))
  29. ;=>8
  30. (if false (+ 1 7) (+ 1 8))
  31. ;=>9
  32. (if nil 7 8)
  33. ;=>8
  34. (if 0 7 8)
  35. ;=>7
  36. (if "" 7 8)
  37. ;=>7
  38. (if (list) 7 8)
  39. ;=>7
  40. (if (list 1 2 3) 7 8)
  41. ;=>7
  42. (= (list) nil)
  43. ;=>false
  44. ;; Testing 1-way if form
  45. (if false (+ 1 7))
  46. ;=>nil
  47. (if nil 8 7)
  48. ;=>7
  49. (if true (+ 1 7))
  50. ;=>8
  51. ;; Testing basic conditionals
  52. (= 2 1)
  53. ;=>false
  54. (= 1 1)
  55. ;=>true
  56. (= 1 2)
  57. ;=>false
  58. (= 1 (+ 1 1))
  59. ;=>false
  60. (= 2 (+ 1 1))
  61. ;=>true
  62. (= nil 1)
  63. ;=>false
  64. (= nil nil)
  65. ;=>true
  66. (> 2 1)
  67. ;=>true
  68. (> 1 1)
  69. ;=>false
  70. (> 1 2)
  71. ;=>false
  72. (>= 2 1)
  73. ;=>true
  74. (>= 1 1)
  75. ;=>true
  76. (>= 1 2)
  77. ;=>false
  78. (< 2 1)
  79. ;=>false
  80. (< 1 1)
  81. ;=>false
  82. (< 1 2)
  83. ;=>true
  84. (<= 2 1)
  85. ;=>false
  86. (<= 1 1)
  87. ;=>true
  88. (<= 1 2)
  89. ;=>true
  90. ;; Testing equality
  91. (= 1 1)
  92. ;=>true
  93. (= 0 0)
  94. ;=>true
  95. (= 1 0)
  96. ;=>false
  97. (= "" "")
  98. ;=>true
  99. (= "abc" "")
  100. ;=>false
  101. (= "" "abc")
  102. ;=>false
  103. (= "abc" "def")
  104. ;=>false
  105. (= (list) (list))
  106. ;=>true
  107. (= (list 1 2) (list 1 2))
  108. ;=>true
  109. (= (list 1) (list))
  110. ;=>false
  111. (= (list) (list 1))
  112. ;=>false
  113. (= 0 (list))
  114. ;=>false
  115. (= (list) 0)
  116. ;=>false
  117. (= (list) "")
  118. ;=>false
  119. (= "" (list))
  120. ;=>false
  121. ;; Testing builtin and user defined functions
  122. (+ 1 2)
  123. ;=>3
  124. ( (fn* (a b) (+ b a)) 3 4)
  125. ;=>7
  126. ( (fn* () 4) )
  127. ;=>4
  128. ( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)
  129. ;=>8
  130. ;; Testing closures
  131. ( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)
  132. ;=>12
  133. (def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))
  134. (def! plus5 (gen-plus5))
  135. (plus5 7)
  136. ;=>12
  137. (def! gen-plusX (fn* (x) (fn* (b) (+ x b))))
  138. (def! plus7 (gen-plusX 7))
  139. (plus7 8)
  140. ;=>15
  141. ;; Testing variable length arguments
  142. ( (fn* (& more) (count more)) 1 2 3)
  143. ;=>3
  144. ( (fn* (& more) (count more)) 1)
  145. ;=>1
  146. ( (fn* (& more) (count more)) )
  147. ;=>0
  148. ( (fn* (a & more) (count more)) 1 2 3)
  149. ;=>2
  150. ( (fn* (a & more) (count more)) 1)
  151. ;=>0
  152. ;; Testing language defined not function
  153. (not false)
  154. ;=>true
  155. (not true)
  156. ;=>false
  157. (not "a")
  158. ;=>false
  159. (not 0)
  160. ;=>false
  161. ;; Testing do form
  162. (do (prn "prn output1"))
  163. ; "prn output1"
  164. ;=>nil
  165. (do (prn "prn output2") 7)
  166. ; "prn output2"
  167. ;=>7
  168. (do (prn "prn output1") (prn "prn output2") (+ 1 2))
  169. ; "prn output1"
  170. ; "prn output2"
  171. ;=>3
  172. (do (def! a 6) 7 (+ a 8))
  173. ;=>14
  174. a
  175. ;=>6
  176. ;; Testing recursive sumdown function
  177. (def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown (- N 1))) 0)))
  178. (sumdown 1)
  179. ;=>1
  180. (sumdown 2)
  181. ;=>3
  182. (sumdown 6)
  183. ;=>21
  184. ;; Testing recursive fibonacci function
  185. (def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))
  186. (fib 1)
  187. ;=>1
  188. (fib 2)
  189. ;=>2
  190. (fib 4)
  191. ;=>5
  192. (fib 10)
  193. ;=>89
  194. ;; -----------------------------------------------------
  195. ;; Testing string quoting
  196. ""
  197. ;=>""
  198. "abc"
  199. ;=>"abc"
  200. "abc def"
  201. ;=>"abc def"
  202. "\""
  203. ;=>"\""
  204. ;; Testing pr-str
  205. (pr-str)
  206. ;=>""
  207. (pr-str "")
  208. ;=>"\"\""
  209. (pr-str "abc")
  210. ;=>"\"abc\""
  211. (pr-str "abc def" "ghi jkl")
  212. ;=>"\"abc def\" \"ghi jkl\""
  213. (pr-str "\"")
  214. ;=>"\"\\\"\""
  215. (pr-str (list 1 2 "abc" "\"") "def")
  216. ;=>"(1 2 \"abc\" \"\\\"\") \"def\""
  217. ;; Testing str
  218. (str)
  219. ;=>""
  220. (str "")
  221. ;=>""
  222. (str "abc")
  223. ;=>"abc"
  224. (str "\"")
  225. ;=>"\""
  226. (str 1 "abc" 3)
  227. ;=>"1abc3"
  228. (str "abc def" "ghi jkl")
  229. ;=>"abc defghi jkl"
  230. ;;; TODO: get this working properly
  231. ;;;(str (list 1 2 "abc" "\"") "def")
  232. ;;;;=>"(1 2 \"abc\" \"\\\"\")def"
  233. ;; Testing prn
  234. (prn)
  235. ;
  236. ;=>nil
  237. (prn "")
  238. ; ""
  239. ;=>nil
  240. (prn "abc")
  241. ; "abc"
  242. ;=>nil
  243. (prn "abc def" "ghi jkl")
  244. ; "abc def" "ghi jkl"
  245. (prn "\"")
  246. ; "\""
  247. ;=>nil
  248. (prn (list 1 2 "abc" "\"") "def")
  249. ; (1 2 "abc" "\"") "def"
  250. ;=>nil
  251. ;; Testing println
  252. (println)
  253. ;
  254. ;=>nil
  255. (println "")
  256. ;
  257. ;=>nil
  258. (println "abc")
  259. ; abc
  260. ;=>nil
  261. (println "abc def" "ghi jkl")
  262. ; abc def ghi jkl
  263. (println "\"")
  264. ; "
  265. ;=>nil
  266. (println (list 1 2 "abc" "\"") "def")
  267. ; (1 2 abc ") def
  268. ;=>nil
  269. ;;
  270. ;; -------- Optional Functionality --------
  271. ;; Testing keywords
  272. (= :abc :abc)
  273. ;=>true
  274. (= :abc :def)
  275. ;=>false
  276. (= :abc ":abc")
  277. ;=>false
  278. ;; Testing vector truthiness
  279. (if [] 7 8)
  280. ;=>7
  281. ;; Testing vector functions
  282. (count [1 2 3])
  283. ;=>3
  284. (empty? [1 2 3])
  285. ;=>false
  286. (empty? [])
  287. ;=>true
  288. (list? [4 5 6])
  289. ;=>false
  290. ;; Testing vector equality
  291. (= [] (list))
  292. ;=>true
  293. (= (list 1 2) [1 2])
  294. ;=>true
  295. (= (list 1) [])
  296. ;=>false
  297. (= [] [1])
  298. ;=>false
  299. (= 0 [])
  300. ;=>false
  301. (= [] 0)
  302. ;=>false
  303. (= [] "")
  304. ;=>false
  305. (= "" [])
  306. ;=>false
  307. ;; Testing vector parameter lists
  308. ( (fn* [] 4) )
  309. ;=>4
  310. ( (fn* [f x] (f x)) (fn* [a] (+ 1 a)) 7)
  311. ;=>8