1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-17 06:04:49 +00:00
janet/gsttests/basic.gst

108 lines
3.1 KiB
Plaintext
Raw Normal View History

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")
2017-06-25 23:17:54 +00:00
(assert (= 4 (blshift 1 2)) "left shift")
(assert (= 1 (brshift 4 2)) "right shift")
(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")
(assert (> 6.0 5.0 4.0 3.0 2.0 1.0) "greater than reals")
(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")
2017-07-02 01:51:16 +00:00
(assert (< nil 1.0 1 false true "hi"
2017-07-02 21:24:33 +00:00
(quote hello)
(array 1 2 3)
(tuple 1 2 3)
(table "a" "b" "c" false)
(struct 1 2)
(thread (fn [x] x))
(buffer "hi")
(fn [x] (+ x x))
+) "type ordering")
(assert (not false) "false literal")
(assert true "true literal")
(assert (not nil) "nil literal")
(assert (= 7 (bor 3 4)) "bit or")
2017-07-02 01:51:16 +00:00
(assert (= 0 (band 3 4)) "bit and")
2017-06-25 23:17:54 +00:00
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-03 15:21:10 +00:00
(assert (= (symbol :banana) 'banana) "symbol function")
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)
(varset! accum (blshift accum 1))
(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)
(varset! accum (blshift accum 1))
(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
(assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter")
2017-07-01 16:47:57 +00:00
# Serialization tests
(def scheck (fn [x]
(def dat (serialize x))
(def deser (deserialize dat))
(assert (= x deser) (string "serialize " (description x)))
))
2017-06-25 23:29:38 +00:00
(scheck 1)
(scheck true)
(scheck false)
(scheck nil)
(scheck "asdasdasd")
(scheck (struct 1 2 3 4))
(scheck (tuple 1 2 3))
(scheck 123412.12)
(scheck (struct (struct 1 2 3 "a") (struct 1 2 3 "a") false 1 "asdasd" (tuple "a" "b")))
(scheck "qwertyuiopasdfghjklzxcvbnm123456789")
(scheck "qwertyuiopasdfghjklzxcvbnm1234567890!@#$%^&*()")
(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-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-01 16:47:57 +00:00
# report
2017-07-03 20:15:16 +00:00
(print "\n" num-tests-passed " of " num-tests-run " tests passed\n")
(if (not (= num-tests-passed num-tests-run)) (exit! 1))