1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-13 17:06:49 +00:00

Fix or with zero arguments.

The value is nil to be consistent for and/or and all/some.
Also add some tests for and/or.
This commit is contained in:
Leah Neukirchen 2021-02-16 19:52:07 +01:00
parent f1819c916a
commit c16a9d8463
2 changed files with 23 additions and 1 deletions

View File

@ -277,7 +277,8 @@
[& forms]
(def len (length forms))
(var i (- len 1))
(var ret (in forms i))
(var ret (if (= 0 len) nil
(in forms i)))
(while (> i 0)
(-- i)
(def fi (in forms i))

View File

@ -294,4 +294,25 @@
(sort (mapcat (fn [[x y z]] [z y x]) (partition 3 (range 99))))) "sort 5")
(assert (<= ;(sort (map (fn [x] (math/random)) (range 1000)))) "sort 6")
# And and or
(assert (= (and true true) true) "and true true")
(assert (= (and true false) false) "and true false")
(assert (= (and false true) false) "and false true")
(assert (= (and true true true) true) "and true true true")
(assert (= (and 0 1 2) 2) "and 0 1 2")
(assert (= (and 0 1 nil) nil) "and 0 1 nil")
(assert (= (and 1) 1) "and 1")
(assert (= (and) true) "and with no arguments")
(assert (= (or true true) true) "or true true")
(assert (= (or true false) true) "or true false")
(assert (= (or false true) true) "or false true")
(assert (= (or false false) false) "or false true")
(assert (= (or true true false) true) "or true true false")
(assert (= (or 0 1 2) 0) "or 0 1 2")
(assert (= (or nil 1 2) 1) "or nil 1 2")
(assert (= (or 1) 1) "or 1")
(assert (= (or) nil) "or with no arguments")
(end-suite)