2018-05-18 03:41:20 +00:00
|
|
|
# Copyright (c) 2018 Calvin Rose
|
2017-12-21 04:03:34 +00:00
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to
|
|
|
|
# deal in the Software without restriction, including without limitation the
|
|
|
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
# IN THE SOFTWARE.
|
|
|
|
|
2018-06-08 19:58:23 +00:00
|
|
|
(import test.helper :prefix "" :exit true)
|
2018-03-26 01:25:33 +00:00
|
|
|
(start-suite 0)
|
2017-06-25 23:17:54 +00:00
|
|
|
|
2017-06-20 03:01:34 +00:00
|
|
|
(assert (= 10 (+ 1 2 3 4)) "addition")
|
|
|
|
(assert (= -8 (- 1 2 3 4)) "subtraction")
|
|
|
|
(assert (= 24 (* 1 2 3 4)) "multiplication")
|
2018-01-04 02:36:10 +00:00
|
|
|
(assert (= 4 (<< 1 2)) "left shift")
|
|
|
|
(assert (= 1 (>> 4 2)) "right shift")
|
2017-06-29 02:51:24 +00:00
|
|
|
(assert (< 1 2 3 4 5 6) "less than integers")
|
|
|
|
(assert (< 1.0 2.0 3.0 4.0 5.0 6.0) "less than reals")
|
|
|
|
(assert (> 6 5 4 3 2 1) "greater than integers")
|
2017-07-02 18:16:37 +00:00
|
|
|
(assert (> 6.0 5.0 4.0 3.0 2.0 1.0) "greater than reals")
|
2017-06-29 02:51:24 +00:00
|
|
|
(assert (<= 1 2 3 3 4 5 6) "less than or equal to integers")
|
|
|
|
(assert (<= 1.0 2.0 3.0 3.0 4.0 5.0 6.0) "less than or equal to reals")
|
|
|
|
(assert (>= 6 5 4 4 3 2 1) "greater than or equal to integers")
|
|
|
|
(assert (>= 6.0 5.0 4.0 4.0 3.0 2.0 1.0) "greater than or equal to reals")
|
2018-09-30 00:01:57 +00:00
|
|
|
(assert (= 7 (% 20 13)) "modulo 1")
|
|
|
|
(assert (= -7 (% -20 13)) "modulo 2")
|
2017-06-29 02:51:24 +00:00
|
|
|
|
2018-02-09 16:57:58 +00:00
|
|
|
(assert (order< nil false true
|
2018-05-09 21:01:58 +00:00
|
|
|
(fiber.new (fn [x] x))
|
2018-01-05 21:17:55 +00:00
|
|
|
1 1.0 "hi"
|
2017-07-02 21:24:33 +00:00
|
|
|
(quote hello)
|
2017-06-29 02:51:24 +00:00
|
|
|
(array 1 2 3)
|
|
|
|
(tuple 1 2 3)
|
2018-01-05 21:17:55 +00:00
|
|
|
(table "a" "b" "c" "d")
|
|
|
|
(struct 1 2 3 4)
|
2017-06-29 02:51:24 +00:00
|
|
|
(buffer "hi")
|
|
|
|
(fn [x] (+ x x))
|
2018-07-02 04:12:36 +00:00
|
|
|
print) "type ordering")
|
2017-06-29 02:51:24 +00:00
|
|
|
|
2018-01-28 20:29:47 +00:00
|
|
|
(assert (= (string (buffer "123" "456")) (string @"123456")) "buffer literal")
|
2018-01-05 21:17:55 +00:00
|
|
|
(assert (= (get {} 1) nil) "get nil from empty struct")
|
|
|
|
(assert (= (get @{} 1) nil) "get nil from empty table")
|
|
|
|
(assert (= (get {:boop :bap} :boop) :bap) "get non nil from struct")
|
|
|
|
(assert (= (get @{:boop :bap} :boop) :bap) "get non nil from table")
|
|
|
|
(assert (put @{} :boop :bap) "can add to empty table")
|
|
|
|
(assert (put @{1 3} :boop :bap) "can add to non-empty table")
|
|
|
|
|
2017-06-29 02:51:24 +00:00
|
|
|
(assert (not false) "false literal")
|
|
|
|
(assert true "true literal")
|
|
|
|
(assert (not nil) "nil literal")
|
2018-01-04 02:36:10 +00:00
|
|
|
(assert (= 7 (| 3 4)) "bit or")
|
|
|
|
(assert (= 0 (& 3 4)) "bit and")
|
2018-09-30 00:01:57 +00:00
|
|
|
(assert (= 0xFF (^ 0x0F 0xF0)) "bit xor")
|
|
|
|
(assert (= 0xF0 (^ 0xFF 0x0F)) "bit xor 2")
|
2017-06-25 23:17:54 +00:00
|
|
|
|
2018-01-13 15:34:59 +00:00
|
|
|
# Set global variables to prevent some possible compiler optimizations that defeat point of the test
|
2018-01-05 21:17:55 +00:00
|
|
|
(var zero 0)
|
|
|
|
(var one 1)
|
|
|
|
(var two 2)
|
|
|
|
(var three 3)
|
|
|
|
(var plus +)
|
|
|
|
(assert (= 22 (plus one (plus 1 2 two) (plus 8 (plus zero 1) 4 three))) "nested function calls")
|
|
|
|
|
2018-08-23 14:27:42 +00:00
|
|
|
# String literals
|
|
|
|
(assert (= "abcd" "\x61\x62\x63\x64") "hex escapes")
|
|
|
|
(assert (= "\e" "\x1B") "escape character")
|
|
|
|
(assert (= "\x09" "\t") "tab character")
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
# Mcarthy's 91 function
|
|
|
|
(var f91 nil)
|
2018-03-16 22:31:03 +00:00
|
|
|
(:= f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11))))))
|
2018-01-05 21:17:55 +00:00
|
|
|
(assert (= 91 (f91 10)), "f91(10) = 91")
|
2018-01-13 15:34:59 +00:00
|
|
|
(assert (= 91 (f91 11)), "f91(11) = 91")
|
|
|
|
(assert (= 91 (f91 20)), "f91(20) = 91")
|
|
|
|
(assert (= 91 (f91 31)), "f91(31) = 91")
|
2018-01-05 21:17:55 +00:00
|
|
|
(assert (= 91 (f91 100)), "f91(100) = 91")
|
|
|
|
(assert (= 91 (f91 101)), "f91(101) = 91")
|
|
|
|
(assert (= 92 (f91 102)), "f91(102) = 92")
|
|
|
|
(assert (= 93 (f91 103)), "f91(103) = 93")
|
|
|
|
(assert (= 94 (f91 104)), "f91(104) = 94")
|
|
|
|
|
2018-01-16 04:31:39 +00:00
|
|
|
# Fibonacci
|
2018-03-16 22:31:03 +00:00
|
|
|
(def fib (do (var fib nil) (:= fib (fn [n] (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))))
|
2018-01-21 19:39:32 +00:00
|
|
|
(def fib2 (fn fib2 [n] (if (< n 2) n (+ (fib2 (- n 1)) (fib2 (- n 2))))))
|
|
|
|
|
|
|
|
(assert (= (fib 0) (fib2 0) 0) "fib(0)")
|
|
|
|
(assert (= (fib 1) (fib2 1) 1) "fib(1)")
|
|
|
|
(assert (= (fib 2) (fib2 2) 1) "fib(2)")
|
|
|
|
(assert (= (fib 3) (fib2 3) 2) "fib(3)")
|
|
|
|
(assert (= (fib 4) (fib2 4) 3) "fib(4)")
|
|
|
|
(assert (= (fib 5) (fib2 5) 5) "fib(5)")
|
|
|
|
(assert (= (fib 6) (fib2 6) 8) "fib(6)")
|
|
|
|
(assert (= (fib 7) (fib2 7) 13) "fib(7)")
|
|
|
|
(assert (= (fib 8) (fib2 8) 21) "fib(8)")
|
|
|
|
(assert (= (fib 9) (fib2 9) 34) "fib(9)")
|
|
|
|
(assert (= (fib 10) (fib2 10) 55) "fib(10)")
|
|
|
|
|
|
|
|
# Closure in non function scope
|
|
|
|
(def outerfun (fn [x y]
|
|
|
|
(def c (do
|
|
|
|
(def someval (+ 10 y))
|
|
|
|
(def ctemp (if x (fn [] someval) (fn [] y)))
|
|
|
|
ctemp
|
|
|
|
))
|
|
|
|
(+ 1 2 3 4 5 6 7)
|
|
|
|
c))
|
|
|
|
|
|
|
|
(assert (= ((outerfun 1 2)) 12) "inner closure 1")
|
|
|
|
(assert (= ((outerfun nil 2)) 2) "inner closure 2")
|
|
|
|
(assert (= ((outerfun false 3)) 3) "inner closure 3")
|
2018-01-16 04:31:39 +00:00
|
|
|
|
2017-07-02 21:35:38 +00:00
|
|
|
(assert (= '(1 2 3) (quote (1 2 3)) (tuple 1 2 3)) "quote shorthand")
|
2017-07-02 21:24:33 +00:00
|
|
|
|
2017-07-02 21:17:24 +00:00
|
|
|
((fn []
|
|
|
|
(var accum 1)
|
|
|
|
(var count 0)
|
|
|
|
(while (< count 16)
|
2018-03-16 22:31:03 +00:00
|
|
|
(:= accum (<< accum 1))
|
|
|
|
(:= count (+ 1 count)))
|
2017-07-02 21:17:24 +00:00
|
|
|
(assert (= accum 65536) "loop in closure")))
|
|
|
|
|
2017-06-25 23:17:54 +00:00
|
|
|
(var accum 1)
|
|
|
|
(var count 0)
|
|
|
|
(while (< count 16)
|
2018-03-16 22:31:03 +00:00
|
|
|
(:= accum (<< accum 1))
|
|
|
|
(:= count (+ 1 count)))
|
2017-07-02 21:17:24 +00:00
|
|
|
(assert (= accum 65536) "loop globally")
|
2017-06-25 23:17:54 +00:00
|
|
|
|
2018-01-21 21:41:15 +00:00
|
|
|
(assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter 1")
|
|
|
|
(assert (= (struct
|
|
|
|
:apple 1
|
|
|
|
6 :bork
|
|
|
|
'(1 2 3) 5)
|
|
|
|
(struct
|
|
|
|
6 :bork
|
|
|
|
'(1 2 3) 5
|
|
|
|
:apple 1)) "struct order does not matter 2")
|
2017-06-30 01:57:09 +00:00
|
|
|
|
2018-01-24 22:59:00 +00:00
|
|
|
# Symbol function
|
|
|
|
|
|
|
|
(assert (= (symbol "abc" 1 2 3) 'abc123) "symbol function")
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
# Fiber tests
|
2017-06-30 01:57:09 +00:00
|
|
|
|
2018-05-09 21:01:58 +00:00
|
|
|
(def afiber (fiber.new (fn [x]
|
2018-05-17 02:09:36 +00:00
|
|
|
(error (string "hello, " x))) :e))
|
2017-06-30 01:57:09 +00:00
|
|
|
|
2018-06-09 17:08:30 +00:00
|
|
|
(def afiber-result (resume afiber "world!"))
|
2017-06-30 01:57:09 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
(assert (= afiber-result "hello, world!") "fiber error result")
|
2018-05-08 23:40:28 +00:00
|
|
|
(assert (= (fiber.status afiber) :error) "fiber error status")
|
2017-06-29 02:51:24 +00:00
|
|
|
|
2017-07-01 16:47:57 +00:00
|
|
|
# yield tests
|
2017-07-01 15:22:10 +00:00
|
|
|
|
2018-08-03 17:41:44 +00:00
|
|
|
(def t (fiber.new (fn @[] (yield 1) (yield 2) 3)))
|
2017-07-01 15:22:10 +00:00
|
|
|
|
2018-06-09 17:08:30 +00:00
|
|
|
(assert (= 1 (resume t)) "initial transfer to new fiber")
|
|
|
|
(assert (= 2 (resume t)) "second transfer to fiber")
|
|
|
|
(assert (= 3 (resume t)) "return from fiber")
|
2018-05-08 23:40:28 +00:00
|
|
|
(assert (= (fiber.status t) :dead) "finished fiber is dead")
|
2017-07-01 15:22:10 +00:00
|
|
|
|
2017-07-15 16:32:24 +00:00
|
|
|
# Var arg tests
|
|
|
|
|
2018-01-12 18:54:37 +00:00
|
|
|
(def vargf (fn [more] (apply + more)))
|
|
|
|
|
2018-03-24 16:48:42 +00:00
|
|
|
(assert (= 0 (vargf @[])) "var arg no arguments")
|
|
|
|
(assert (= 1 (vargf @[1])) "var arg no packed arguments")
|
|
|
|
(assert (= 3 (vargf @[1 2])) "var arg tuple size 1")
|
|
|
|
(assert (= 10 (vargf @[1 2 3 4])) "var arg tuple size 2, 2 normal args")
|
|
|
|
(assert (= 110 (vargf @[1 2 3 4 10 10 10 10 10 10 10 10 10 10])) "var arg large tuple")
|
2017-07-15 16:32:24 +00:00
|
|
|
|
2018-01-13 19:08:42 +00:00
|
|
|
# Higher order functions
|
|
|
|
|
|
|
|
(def compose (fn [f g] (fn [& xs] (f (apply g xs)))))
|
|
|
|
|
|
|
|
(def -+ (compose - +))
|
|
|
|
(def +- (compose + -))
|
|
|
|
|
|
|
|
(assert (= (-+ 1 2 3 4) -10) "compose - +")
|
|
|
|
(assert (= (+- 1 2 3 4) -8) "compose + -")
|
|
|
|
(assert (= ((compose -+ +-) 1 2 3 4) 8) "compose -+ +-")
|
|
|
|
(assert (= ((compose +- -+) 1 2 3 4) 10) "compose +- -+")
|
|
|
|
|
|
|
|
# UTF-8
|
|
|
|
|
|
|
|
#🐙🐙🐙🐙
|
|
|
|
|
|
|
|
(def 🦊 :fox)
|
|
|
|
(def 🐮 :cow)
|
2018-02-03 18:55:55 +00:00
|
|
|
(assert (= (string "🐼" 🦊 🐮) "🐼:fox:cow") "emojis 🙉 :)")
|
2018-06-09 17:08:30 +00:00
|
|
|
(assert (not= 🦊 "🦊") "utf8 strings are not symbols and vice versa")
|
2018-01-13 19:08:42 +00:00
|
|
|
|
2018-03-24 16:48:42 +00:00
|
|
|
# Symbols with @ character
|
2018-01-20 22:19:47 +00:00
|
|
|
|
|
|
|
(def @ 1)
|
|
|
|
(assert (= @ 1) "@ symbol")
|
|
|
|
(def @@ 2)
|
|
|
|
(assert (= @@ 2) "@@ symbol")
|
|
|
|
(def @hey 3)
|
|
|
|
(assert (= @hey 3) "@hey symbol")
|
|
|
|
|
2018-01-14 15:17:18 +00:00
|
|
|
# Merge sort
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
# Imperative merge sort merge
|
2018-01-14 15:17:18 +00:00
|
|
|
(def merge (fn [xs ys]
|
2018-03-24 16:48:42 +00:00
|
|
|
(def ret @[])
|
2018-01-14 15:17:18 +00:00
|
|
|
(def xlen (length xs))
|
|
|
|
(def ylen (length ys))
|
|
|
|
(var i 0)
|
|
|
|
(var j 0)
|
|
|
|
# Main merge
|
|
|
|
(while (if (< i xlen) (< j ylen))
|
|
|
|
(def xi (get xs i))
|
|
|
|
(def yj (get ys j))
|
|
|
|
(if (< xi yj)
|
2018-05-08 23:40:28 +00:00
|
|
|
(do (array.push ret xi) (:= i (+ i 1)))
|
|
|
|
(do (array.push ret yj) (:= j (+ j 1)))))
|
2018-01-14 15:17:18 +00:00
|
|
|
# Push rest of xs
|
|
|
|
(while (< i xlen)
|
|
|
|
(def xi (get xs i))
|
2018-05-08 23:40:28 +00:00
|
|
|
(array.push ret xi)
|
2018-03-16 22:31:03 +00:00
|
|
|
(:= i (+ i 1)))
|
2018-01-14 15:17:18 +00:00
|
|
|
# Push rest of ys
|
|
|
|
(while (< j ylen)
|
|
|
|
(def yj (get ys j))
|
2018-05-08 23:40:28 +00:00
|
|
|
(array.push ret yj)
|
2018-03-16 22:31:03 +00:00
|
|
|
(:= j (+ j 1)))
|
2018-01-14 15:17:18 +00:00
|
|
|
ret))
|
|
|
|
|
2018-03-24 16:48:42 +00:00
|
|
|
(assert (apply <= (merge @[1 3 5] @[2 4 6])) "merge sort merge 1")
|
|
|
|
(assert (apply <= (merge @[1 2 3] @[4 5 6])) "merge sort merge 2")
|
|
|
|
(assert (apply <= (merge @[1 3 5] @[2 4 6 6 6 9])) "merge sort merge 3")
|
|
|
|
(assert (apply <= (merge '(1 3 5) @[2 4 6 6 6 9])) "merge sort merge 4")
|
2018-01-14 15:23:24 +00:00
|
|
|
|
2017-07-16 15:00:20 +00:00
|
|
|
# Gensym tests
|
|
|
|
|
|
|
|
(assert (not= (gensym) (gensym)) "two gensyms not equal")
|
|
|
|
((fn []
|
|
|
|
(def syms (table))
|
|
|
|
(var count 0)
|
|
|
|
(while (< count 128)
|
2018-07-01 19:49:33 +00:00
|
|
|
(put syms (gensym) true)
|
2018-03-16 22:31:03 +00:00
|
|
|
(:= count (+ 1 count)))
|
2017-07-16 15:00:20 +00:00
|
|
|
(assert (= (length syms) 128) "many symbols")))
|
|
|
|
|
2018-03-11 20:30:38 +00:00
|
|
|
# Let
|
|
|
|
|
|
|
|
(assert (= (let [a 1 b 2] (+ a b)) 3), "simple let")
|
2018-03-24 16:48:42 +00:00
|
|
|
(assert (= (let [[a b] @[1 2]] (+ a b)) 3), "destructured let")
|
|
|
|
(assert (= (let [[a [c d] b] @[1 (tuple 4 3) 2]] (+ a b c d)) 10), "double destructured let")
|
2018-03-11 20:30:38 +00:00
|
|
|
|
2018-01-28 20:29:47 +00:00
|
|
|
# Macros
|
|
|
|
|
|
|
|
(defn dub [x] (+ x x))
|
|
|
|
(assert (= 2 (dub 1)) "defn macro")
|
|
|
|
(do
|
|
|
|
(defn trip [x] (+ x x x))
|
|
|
|
(assert (= 3 (trip 1)) "defn macro triple"))
|
|
|
|
(do
|
|
|
|
(var i 0)
|
|
|
|
(when true
|
2018-03-24 16:48:42 +00:00
|
|
|
(++ i)
|
|
|
|
(++ i)
|
|
|
|
(++ i)
|
|
|
|
(++ i)
|
|
|
|
(++ i)
|
|
|
|
(++ i))
|
2018-01-28 20:29:47 +00:00
|
|
|
(assert (= i 6) "when macro"))
|
|
|
|
|
2018-03-26 01:25:33 +00:00
|
|
|
(end-suite)
|
2018-03-24 16:48:42 +00:00
|
|
|
|