2017-12-21 04:03:34 +00:00
|
|
|
# Copyright (c) 2017 Calvin Rose
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2017-07-02 01:51:16 +00:00
|
|
|
(print "\nRunning basic tests...\n")
|
2017-06-25 23:29:38 +00:00
|
|
|
|
2017-07-01 15:17:29 +00:00
|
|
|
(var num-tests-passed 0)
|
|
|
|
(var num-tests-run 0)
|
|
|
|
(def assert (fn [x e]
|
2017-07-02 01:51:16 +00:00
|
|
|
(varset! num-tests-run (+ 1 num-tests-run))
|
|
|
|
(if x
|
|
|
|
(do
|
2017-07-03 15:21:10 +00:00
|
|
|
(print " \e[32m✔\e[0m " e)
|
2017-07-01 15:17:29 +00:00
|
|
|
(varset! num-tests-passed (+ 1 num-tests-passed))
|
|
|
|
x)
|
2017-07-02 01:51:16 +00:00
|
|
|
(do
|
2017-07-03 15:21:10 +00:00
|
|
|
(print " \e[31m✘\e[0m " e)
|
2017-07-01 15:17:29 +00:00
|
|
|
x))))
|
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-01-05 21:17:55 +00:00
|
|
|
(assert (< nil false true
|
|
|
|
(fiber (fn [x] x))
|
|
|
|
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))
|
|
|
|
+) "type ordering")
|
|
|
|
|
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")
|
2017-06-25 23:17:54 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
# Set global variables to prevent some compiler optimizations that defeat point of the test
|
|
|
|
(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")
|
|
|
|
|
|
|
|
# Mcarthy's 91 function
|
|
|
|
(var f91 nil)
|
|
|
|
(varset! f91 (fn [n] (if (> n 100) (- n 10) (f91 (f91 (+ n 11))))))
|
|
|
|
(assert (= 91 (f91 10)), "f91(10) = 91")
|
|
|
|
(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")
|
|
|
|
|
2017-07-02 21:24:33 +00:00
|
|
|
(assert (= "hello" :hello) "keyword syntax for strings")
|
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-01-04 02:36:10 +00:00
|
|
|
(varset! accum (<< accum 1))
|
2017-07-02 21:17:24 +00:00
|
|
|
(varset! count (+ 1 count)))
|
|
|
|
(assert (= accum 65536) "loop in closure")))
|
|
|
|
|
2017-06-25 23:17:54 +00:00
|
|
|
(var accum 1)
|
|
|
|
(var count 0)
|
|
|
|
(while (< count 16)
|
2018-01-04 02:36:10 +00:00
|
|
|
(varset! accum (<< accum 1))
|
2017-06-29 02:51:24 +00:00
|
|
|
(varset! count (+ 1 count)))
|
2017-07-02 21:17:24 +00:00
|
|
|
(assert (= accum 65536) "loop globally")
|
2017-06-25 23:17:54 +00:00
|
|
|
|
2017-06-30 01:57:09 +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")
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
# Fiber tests
|
2017-06-30 01:57:09 +00:00
|
|
|
|
|
|
|
(def athread (thread (fn [x]
|
|
|
|
(error (string "hello, " x)))))
|
|
|
|
|
|
|
|
(def athread-result (tran athread "world!"))
|
|
|
|
|
|
|
|
(assert (= athread-result "hello, world!") "thread error result")
|
|
|
|
(assert (= (status athread) "error") "thread 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
|
|
|
|
|
|
|
(def t (thread (fn [] (tran nil 1) (tran nil 2) 3)))
|
|
|
|
|
|
|
|
(assert (= 1 (tran t)) "initial transfer to new thread")
|
|
|
|
(assert (= 2 (tran t)) "second transfer to thread")
|
|
|
|
(assert (= 3 (tran t)) "return from thread")
|
|
|
|
(assert (= (status t) "dead") "finished thread is dead")
|
|
|
|
|
2017-07-15 16:32:24 +00:00
|
|
|
# Var arg tests
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
(def vargf (fn [x & more] (apply + (if x x 0) 100 more)))
|
2017-07-15 16:32:24 +00:00
|
|
|
(assert (= 100 (vargf)) "var arg no arguments")
|
|
|
|
(assert (= 101 (vargf 1)) "var arg no packed arguments")
|
|
|
|
(assert (= 103 (vargf 1 2)) "var arg tuple size 1")
|
|
|
|
(assert (= 110 (vargf 1 2 3 4)) "var arg tuple size 3")
|
|
|
|
(assert (= 210 (vargf 1 2 3 4 10 10 10 10 10 10 10 10 10 10)) "var arg large tuple")
|
|
|
|
|
2017-07-16 15:00:20 +00:00
|
|
|
# Gensym tests
|
|
|
|
|
|
|
|
(assert (not= (gensym) (gensym)) "two gensyms not equal")
|
|
|
|
(assert (not= (gensym 'abc) (gensym 'abc)) "two gensyms with arg not equal")
|
|
|
|
((fn []
|
|
|
|
(def syms (table))
|
|
|
|
(var count 0)
|
|
|
|
(while (< count 128)
|
|
|
|
(set! syms (gensym 'beep) true)
|
|
|
|
(varset! count (+ 1 count)))
|
|
|
|
(assert (= (length syms) 128) "many symbols")))
|
|
|
|
|
2017-07-01 16:47:57 +00:00
|
|
|
# report
|
2017-06-29 02:51:24 +00:00
|
|
|
|
2017-07-03 20:15:16 +00:00
|
|
|
(print "\n" num-tests-passed " of " num-tests-run " tests passed\n")
|
2017-12-21 04:03:34 +00:00
|
|
|
(if (not (= num-tests-passed num-tests-run)) (exit! 1))
|