Login
7 branches 0 tags
Benjamin Vincent Schulenburg Renamed `time-milliseconds` -> `time/milliseconds` 0ce0121 4 years ago 22 Commits
nujel / stdlib / test.nuj
[def test-context "Nujel"]
[def test-list [cons [cons 1 1] #nil]]

[def test-add [λ [result @...expr]
	"Add a test where ...EXPR must eval to RESULT"
	[set! test-list [cons [cons result [cons 'begin @...expr]] test-list]]
]]

[def test-run [λ [] "Run through all automated Tests"
	[let [[nujel-start [msecs]]]
		[def test-count     0]
		[def success-count  0]
		[def error-count    0]
		[def test-errors  '[]]

		[def display-errors [λ [i]
			"Displays all the failed Tests"
			[map [λ [i] [when [pair? i] [error [cat [ansi-red "[Error] "] [ansi-red [caar i]] " != " [ansi-green [cdar i]] "\n" [str/write [cdr i]] "\n\n"]]]] test-errors]
		]]

		[def display-results [λ []
			"Prints the result Message"
			[error [cat test-context " [" [ansi-green success-count] " / " [ansi-red error-count] "] in "[- [msecs] nujel-start] "ms - " [if [zero? error-count] [ansi-rainbow "Everything is working, very nice!"] [ansi-red "Better fix those!"]] "\n"]]
		]]

		[def test-success [λ []
			"Should be called after a test has finished successfully"
			[set! success-count [++ success-count]]
		]]

		[def test-failure [λ [res-should res-is expr]
			"Should be called if EXPR does not equal RES"
			[set! test-errors [cons [cons [cons res-should res-is] expr] test-errors]]
			[set! error-count [++ error-count]]
		]]

		[def test [λ [result rawexpr]
			"Tests that RAWEXPR evaluates to RESULT"
			[let [[expr [eval rawexpr]]]
				[cond [[eq? result expr] [test-success]]
				      [#t [test-failure result expr rawexpr]]
				]
			]
		]]

		[def test-str [λ [result rawexpr]
			"Tests that RAWEXPR evaluates to RESULT"
			[let [[expr [str/write [eval rawexpr]]]]
				[cond [[eq? result expr] [test-success]]
				      [#t [test-failure result expr rawexpr]]
				]
			]
		]]
		[def test-run-iter [λ [list]
			"Recurse through LIST and runs eatch test"
			[cond [[nil? list] #t]
			      [#t [[if [string? [caar list]] test-str test] [caar list] [cdar list]] [test-run-iter [cdr list]]]
			]
		]]

		[test-run-iter test-list]
		[display-results]
		[when [> error-count 0] [display-errors] [display-results]]
		error-count
	]
]]

; [error"Evaluating comments is a terrible Idea!"] [newline] [quit 2]
[test-add 1073741824 [ash 1 30]]
[test-add 2147483647 [lognot [ash 1 31]]]
[test-add 39 [+ 42 [- 3]]]
[test-add 24 [* 4 [- [+ 1 [+ 1 1]] [- 3 3 3]]]]
[test-add 3 [div 9 3]]
[test-add 3 [let [[vier -4]] [+ [% 9 4] [/ -9 vier]]]]
[test-add 69 [+ [* 2 [/ 32 8] [- 16 8]] 5]]
[test-add 3 [define eins 1] [define zwei 2] [+ eins zwei]]
[test-add -3 [define eins 1] [define zwei 2] [define drei [+ eins zwei]] [set! eins [- drei drei drei]]]
[test-add 128 [define zahl 1_2_8] zahl]
[test-add 10 [let [[a 10]] a]]
[test-add 20 [define b 20] [let [[a b]] a]]
[test-add 10 [define b 20] [let [[a b]] [set! a 10] a]]
[test-add 20 [define b 20] [let [[a b]] [set! a 10] b]]
[test-add 42 [let [[a 12] [b 30]] [+ a b]]]
[test-add 16 [define square [lambda [a] [* a a]]] [square 4]]
[test-add 0 [- -1 -1]]
[test-add #t [or #f [and [> 5 1] [not [< 5 1]] [>= 5 5] [<= 5 5]]]]
[test-add #t [and [= #t #t] [= #f #f] [not [= #f #t]] [= 2 "2"]]]
[test-add 11 [length "Hallo, Welt"]]
[test-add #t [number? 0.1]]
[test-add 14 [define abs [lambda [a] [if [neg? a] [- 0 a] a]]] [+ [abs -7] [abs 7]]]
[test-add #t [and [or #f #t] [and #t #t]]]
[test-add #t [and [neg? -1] [neg? -0.01] [pos? 0] [pos? 0.01] [not [neg? 0]] [not [pos? -0.01]] [not [neg? #f]]]]
[test-add #t [and [number? 1] [number? -1] [number? 0] [number? 0.1]]]
[test-add #t [and [number? [vec 1]] [not [number? #f]] [not [number? "123"]]]]
[test-add #t [and [number? 1] [number? -1] [number? 0] [number? 0.1] [number? [vec 1]] [not [number? #f]] [not [number? "123"]]]]
[test-add 12340 [- [int [cat 12 "3" "45 Test"]] 5]]
[test-add 12340 [let [[a [cat 12 "3" 45]]] [- [int a] [length a]]]]
[test-add 123 [int [cat "123" "abc" 456]]]
[test-add 28 [+ [int 10] [int 10.23] [int "8"]]]
[test-add #t [and [not [< 3 2]] [zero? 0] [> 3.1 2.1] [> 3 2] [>= 4 "3"] [>= 3 3] [<= 3 3] [not [>= "2" 3]]]]
[test-add 1 [int [float [+ [vec 1] [vec 0 9 9]]]]]
[test-add 0 [- #nil]]
[test-add #f [and #nil #nil]]
[test-add #t [and [pair? [cons 1 '[2]]] [not [pair? 1]]]]
[test-add 1 [car [cons 1 '[2]]]]
[test-add 2 [+ [cadr '[1 2]] [cadr #nil] [cadr '[1]]]]
[test-add #t [string? [describe "++"]]]
[test-add 3 [++ [-- [length '[1 2 3]]]]]
[test-add #t [and [for-all? '[1 2 3] int?] [not [for-all? '[1 2 3.0] int?]]]]
[test-add #t [and [there-exists? '[1.0 2 3.0] int?] [not [there-exists? '[1.0 2.0 3.0] int?]]]]
[test-add #t [and [eq? "asd" "asd"] [not [eq? "asd" "bsd"]] [not [eq? "asd" "asdasd"]]]]
[test-add 23 [+ [reduce + [make-list 10 2]] [list-ref '[1 2 3 4] 1] [list-ref '[1 2] 3] [list-ref '[1 2] 0]]]
[test-add #t [and [nil? #nil] [not [nil? "NotNil"] [inf? #inf] [not [inf? "NotInf"] [vec? [vec 1]] [not [inf? "NotVec"]]]]]]
[test-add 11 [define count [let [[a 0]] [lambda [b] [set! a [+ a [cond [[number? b] b] [#t 1]]]]]]] [count 10] [count]]
[test-add 4 [let [[a 10]] [when [when #t [set! a [+ 2 "2"]] #f] [set! a -1]] a]]
[test-add 6 [eval '[+ 1 2 3]]]
[test-add 4 [arr-length #[1 2 3 4]]]
[test-add 2 [arr-ref #[1 2 3 4] 1]]
[test-add 3 [arr-length [arr-new 3]]]
[test-add 10 [+ [apply + '[1 2 3]] [apply ++ 3]]]
[test-add 0 [apply +]]
[test-add 0 [define cb '+] [apply cb]]
[test-add 1 [define cb '++] [apply cb]]
[test-add 5 [length "12345"]]
[test-add #f [or 0 0]]
[test-add #t [and 1 1]]
[test-add #f [bool 0]]
[test-add #t [bool 1]]
[test-add 6 [[lambda [a] [+ a 4]] 2]]
[test-add 2 [define test 1] [define test 2] test]
[test-add 0 [max]]
[test-add 0 [min]]
[test-add 1 [max 1]]
[test-add 4 [min 4]]
[test-add 4 [min 4 9]]
[test-add 9 [max 4 9]]
[test-add 25 [max 1 4.0 9 25]]
[test-add 25 [max 25 4.0 9 1]]
[test-add 1 [min 1 4.0 9 25]]
[test-add 1 [min 25 4.0 9 1]]
[test-add #t [even? 2]]
[test-add #f [even? 9]]
[test-add #t [odd? 7]]
[test-add #f [odd? 4]]
[test-add 256 [int [pow 2 8]]]
[test-add #f [undefine! testerle]]
[test-add 3 [define ein-test-arr #[1 2 3]] [ein-test-arr 2]]
[test-add 3 [define ein-test-arr #[1 2 3]] [ein-test-arr 2 9] [ein-test-arr 2]]
[test-add 123 [define i-assaultmegablaster 123] i-assaultmegablaster]
[test-add #t [int? [random]]]
[test-add #t [random-seed! 123] [define first-value [random]] [random-seed! 123] [= first-value [random]]]
[test-add 1 [define a 1] [unless [= 2 2] [set! a 4]] a]
[test-add 4 [define a 1] [unless [= 2 3] [set! a 4]] a]
[test-add 4 [define a 1] [when   [= 2 2] [set! a 4]] a]
[test-add 1 [define a 1] [when   [= 2 3] [set! a 4]] a]
[test-add 3 [define ein-test-arr #[1 2 3]] [ein-test-arr 2.2]]
[test-add 123 #d123]
[test-add 6 #b0110]
[test-add 10 #b1010]
[test-add 15 #b11-11]
[test-add 192 #b1100_0000]
[test-add 255 #xFF]
[test-add 255 #xFf]
[test-add 160 #xa0]
[test-add 31 #x1-F]
[test-add 31 #x1_F]
[test-add 50 #x32]
[test-add 256 #x100]
[test-add 0 #o]
[test-add 7 #o7]
[test-add 0 #o8]
[test-add 10 #o12]
[test-add 26 #o32]
[test-add -1 #b11111111_11111111_11111111_11111111]
[test-add -1 #xFFFFFFFF]
[test-add 2 [- [+ 1 #b10] 1]]
[test-add 8 [- [+ 1 #o10] 1]]
[test-add 16 [- [+ 1 #x10] 1]]
[test-add 32 [+ #x10#x10]]
[test-add 16 [+ #x10#f]]
[test-add 32 [+ #x10"16"]]
[test-add 32 [+ #x10[+ 0#x10]]]
[test-add 0 [logand #xf0 #x0F]]
[test-add 255 [logior #xf0 #x0F]]
[test-add 255 [logior #xfF #xFF]]
[test-add 255 [logxor #xf0 #x0F]]
[test-add 240 [logxor #xff #x0F]]
[test-add -1 [lognot 0]]
[test-add 0 [lognot -1]]
[test-add 16 [ash 1 4]]
[test-add 65536 [ash 1 16]]
[test-add -1 [ash -1 -1]]
[test-add -16 [lognand #b1111 #b1111]]
[test-add 6 [eval [read "[+ 1 2 3]"]]]
[test-add 3 [let [[test-string "3"]] [read test-string] [read test-string]]]
[test-add "[vec 1.0 2.0 3.0]" [abs [vec -1 -2 -3]]]
[test-add "3.33333" [+ 1.11111 2.22222]]
[test-add "\"H#Hallo\"" [let [[a "Hallo, Welt#"]] [cat [substr a 0 1] [substr a -1] [substr a 0 -7]]]]
[test-add "\"Test\"" [define a "Test"] a]
[test-add "11.6" [+ [+ 1.1 2.2] [+ 1.1 3] [+ 1 3.2]]]
[test-add "20.1" [+ [+ 1.1 3] [+ 1 3.3] [+ 3.3 4.1 4.3]]]
[test-add "15.54" [add [mul 3.2 3.2] [sub 5.5 1.1 1 1.1] [div 9.9 3.3]]]
[test-add "0.7" [% 10 3.1]]
[test-add #t [eq? #inf #inf]]
[test-add #inf [+ 1 [div 10 0]]]
[test-add "11.7" [add [+ 1.1 2.2] [+ 1.1 3] [+ 1 3.3]]]
[test-add "11.75" [+ [float 10] [int "10"] [float "-8.25"]]]
[test-add "30.3" [+ [abs "-10"] [int 8.2] 12.3]]
[test-add "[vec 12.0 12.0 12.0]" [+ [vec 1] 1 10]]
[test-add "[vec 3.0 5.0 6.0]" [+ [vec 1] [vec 1 2] [vec 1 2 3]]]
[test-add "[vec 3.0 3.0 3.0]" [+ 1 [vec 1] 1.0]]
[test-add "[vec 3.0 3.0 3.0]" [+ 1.0 [vec 1] "1"]]
[test-add "[vec -1.0 -1.0 -1.0]" [- [vec 1] [vec 1.0] [vec "1"]]]
[test-add "2.7" [+ [- 1 -1.0] [- 5 1.1 1] [- 1 1.1 1] [- 1 1.1 [int "1"]]]]
[test-add "[vec 3.0 3.0 3.0]" [+ 1 [vec 1] 1.0]]
[test-add "[vec -1.0 -1.0 -1.0]" [- 1 [vec 1] 1.0]]
[test-add "[vec 8.0 8.0 8.0]" [* [vec 2] "4"]]
[test-add "[vec 1.0 1.0 1.0]" [mod [vec 9] 2]]
[test-add "1.0" [float [int [+ [vec 1] [vec 0 9 9]]]]]
[test-add "[]" [cons]]
[test-add "[1]" [cons 1]]
[test-add "[1 . 2]" [cons 1 2]]
[test-add "[1 2]" [cons 1 '[2]]]
[test-add "[4 3 2 1]" [reverse '[1 2 3 4]]]
[test-add "[1 2.0 3 1 2.0 3]" [append '[1 2.0 3] '[1 2.0 3]]]
[test-add "[1 4]" [filter int?   '[1 2.0 #t 4]]]
[test-add "[2.0]" [filter float? '[1 2.0 #t 4]]]
[test-add "[2.0]" [filter float? '[1 2.0 #t 4]]]
[test-add "[#t]"  [filter bool?  '[1 2.0 #t 4]]]
[test-add "[2 3]" [sublist '[1 2 3 4 5 6] 1 3]]
[test-add "[1 2 3]" [list-head '[1 2 3 4 5 6] 3]]
[test-add "[4 5 6]" [list-tail [list 1 2 3 4 5 6] 3]]
[test-add "[3]" [last-pair [list 1 2 3]]]
[test-add "[1 2]" [except-last-pair '[1 2 3]]]
[test-add "[1.0 #t \"a\"]" [let [[l '[1.0 #t "a"]]] [append [except-last-pair l] [last-pair l]]]]
[test-add "[\"asd\" #t #f]" [member "asd" '[123 456 "asd" #t #f]]]
[test-add "[[vec 4.0 4.0 4.0] 9 16.0]" [map [λ [a] [* a a]] [cons [vec 2] '[3 4.0]]]]
[test-add "\"11.0\"" [cat 1 1.0]]
[test-add "[vec 9.0 9.0 9.0]" [div [vec 99] [cat 1 1.0]]]
[test-add "#[99 12 3 4]" [let [[cur-arr #[1 2 3 4]]] [arr-set! cur-arr 0 99 12] cur-arr]]
[test-add "#[42 42 42 42 42 42]" [arr-fill! [arr-new 6] 42]]
[test-add "#nil" [apply #nil]]
[test-add "[vec 1.0 3.0 9.0]" [define vs [λ [a] [vec [vec/z a] [vec/y a] [vec/x a]]]] [vs [vec 9 3.0 "1"]]]
[test-add "3" [define fib [λ [a] [cond [[zero? a] 0] [[= a 1] 1] [#t [+ [fib [- a 1]] [fib [- a 2]]]]]]] [fib 4]]
[test-add "21" [define fib [λ [a] [cond [[zero? a] 0] [[= a 1] 1] [#t [+ [fib [- a 1]] [fib [- a 2]]]]]]] [fib 8]]
[test-add "102334155" [define fib-iter [λ [a b count] [cond [[= count 0] b] [#t [fib-iter [+ a b] a [- count 1]]]]]] [define fib [λ [n] [fib-iter 1 0 n]]] [fib 40]]
[test-add 87654321 [let [[ret ""]] [[λ [a] [cond [[zero? a] [int ret]] [#t [set! ret [cat ret a]] [[cl-lambda 1] [-- a]]]]] 8]]]
[test-add "3.14159" [set! PI 3] PI]
[test-add "\"ASD123\"" [str/uppercase "asD123"]]
[test-add "\"asd123\"" [str/lowercase "aSD123"]]
[test-add "\"Asd123\"" [str/capitalize "aSD123"]]
[test-add "[vec 1.0 1.0 1.0]" [floor [vec 1.3 1.3 1.3]]]
[test-add "2.0" [ceil 1.3]]
[test-add "[vec 2.0 2.0 2.0]" [ceil [vec 1.3 1.3 1.3]]]
[test-add "1.0" [round 1.3]]
[test-add "2.0" [round 1.51]]
[test-add "3.0" [sqrt 9]]
[test-add "[vec 5.0 5.0 5.0]" [sqrt [vec 25 25 25]]]
[test-add "256.0" [pow 2.0 8]]
[test-add "[vec 4.0 8.0 16.0]" [pow 2.0 [vec 2.0 3.0 4.0]]]
[test-add "\"123\"" [str 123]]
[test-add "\"#t\"" [str #t]]
[test-add "testerle" testerle]
[test-add "[:asd qwerty]" [:asd qwerty]]
[test-add ":asd" [begin [def :asd #t] :asd]]
[test-add "testerle" [define testerle 123] [undefine! testerle] testerle]
[test-add "[1 . 2]" [define test [cons 1 2]] test]
[test-add "[3 . 2]" [define test [cons 1 2]] [set-car! test 3]]
[test-add "[3 . 4]" [define test [cons 1 2]] [set-car! test 3] [set-cdr! test 4]]
[test-add "\"Eins\"" [define eins [ω [] [define say [λ [] "Eins"]]]] [eins [say]]]
[test-add "\"Zwei\"" [define eins [ω [] [define say [λ [] "Zwei"]]]] [define zwei [eins [ω]]] [zwei [say]]]
[test-add "\"Polizei\"" [define eins [ω [] [define say [λ [] "Eins"]]]] [define zwei [eins [ω]]] [zwei [def say [λ [] "Polizei"]]] [zwei [say]]]
[test-add "\"asd\"" ["a" "s" "d"]]
[test-add "\"a\"" ["a"]]
[test-add "#nil" [define testerle #[1 2 3]] [testerle 4]]
[test-add "#nil" [define testerle #[1 2 3]] [testerle 40000]]
[test-add "#[1 2 3]" [define testerle #[1 2 3]] [testerle]]
[test-add "#nil" [define testerle #[1 2 3]] [testerle #t]]
[test-add "#nil" [define testerle #[1 2 3]] [testerle [vec 1 2 3]]]
[test-add "\"Trim Test\"" [trim "   Trim Test    \n"]]
[test-add "\"1,asd,3.0,#f\"" [join '[1 "asd" 3.0 #f] ","]]
[test-add "[1.0 2.0 3.0]" [map float [split "1,2,3" ","]]]
[test-add "[\"dies ist\" \"ein\" \"test\"]" [split "dies ist/ein/test" "/"]]
[test-add 1 [index-of "1,2,3" ","]]
[test-add #x123 [read [join [cons "#x" [split "123" ""]]]]]
[test-add 7 [char-at "\a" 0]]
[test-add 5 [char-at [from-char-code 5 10 20] 0]]
[test-add 2600 [int [from-char-code [char-at "2" 0] #\6 48 48]]]
[test-add #t [eq? 32 #\ ]]
[test-add #t [eq? #\Backspace 8]]
[test-add #t [eq? #\Tab [char-at "\t" 0]]]
[test-add #t [and [eq? #\cr 13] [eq? #\Return 13]]]
[test-add #t [and [eq? #\lf 10] [eq? 10 #\Linefeed]]]
[test-add #x07 [char-at "\a" 0]]
[test-add #x08 [char-at "\b" 0]]
[test-add #x1B [char-at "\e" 0]]
[test-add #x0c [char-at "\f" 0]]
[test-add #\lf [char-at "\n" 0]]
[test-add #\cr [char-at "\r" 0]]
[test-add #x09 [char-at "\t" 0]]
[test-add #x0B [char-at "\v" 0]]
[test-add #x27 [char-at "\'" 0]]
[test-add #x22 [char-at "\"" 0]]
[test-add #t [> [symbol-count] 200]] ; Probably not gonna shrink over time
[test-add #t [pair? [symbol-table]]]
[test-add #t [equal? + add]]
[test-add #t [eq? min min]]
[test-add #t [let [[some-value #f]] [not some-value]]]
[test-add 4 [>> 8 1]]
[test-add 15 [1 + 2 * [3 + 4]]]
[test-add "#inf" [% 4 0]]
[test-add 9 [10 - 1]]
[test-add 5 [10 / 2]]
[test-add 256 [1 << 8]]
[test-add 64 [512 >> 3]]
[test-add #t [eq? :asd :asd]]
[test-add #t [eq? :bool [type-of #f]]]
[test-add #t [eq? :int [type-of 123]]]
[test-add #f [eq? :int [type-of 123.123]]]
[test-add #t [eq? :float [type-of 123.123]]]
[test-add #t [eq? :vec [type-of [vec 1]]]]
[test-add #t [eq? :native-function [type-of +]]]
[test-add #t [eq? :lambda [type-of test-add]]]
[test-add #t [eq? :string [type-of "asd"]]]
[test-add 2 [getf [list :a 1 :b 2 :c 3] :b]]
[test-add "#nil" [getf [list :a 1 :b 2 :c 3] :d]]
[test-add "\"\\n\"" "\n"]
[test-add "\"\\n\"" [br]]
[test-add "\"\\n\\n\\n\"" [br 3]]
[test-add :dies-ist-ein-test-ob-lange-symbole-funktionieren :dies-ist-ein-test-ob-lange-symbole-funktionieren]
[test-add #t [eq? [+ 2 2] [2 + 2]]]
[test-add #t [eq? 4 [2 + 2]]]
[test-add #t [eq? 4 [+ 2 2]]]
[test-add :int [type-of [+ 2 2]]]
[test-add :int [type-of [2 + 2]]]
[test-add :float [type-of [+ 2.0 2.1]]]
[test-add :float [type-of [2.0 + 2.1]]]
[test-add :float [type-of [+ 2 2.1]]]
[test-add :float [type-of [2 + 2.1]]]
[test-add -1 [-1]]
[test-add -1 [- 1]]
[test-add -1 [let [[a 1]] [- a]]]
[test-add -1 [let [[a 1]] [-a]]]
[test-add -1 [let [[a 1]] -a]]
[test-add  0 [wrap-value 0 0 2]]
[test-add  1 [wrap-value 1 0 2]]
[test-add  0 [wrap-value 2 0 2]]
[test-add  1 [wrap-value 3 0 2]]
[test-add  0 [wrap-value 4 0 2]]
[test-add #t [zn? 0]]
[test-add #t [zn? -4.0]]
[test-add #f [zn? 0.1]]
[test-add  0 [let [[tmp [vec 0 0 0]]] [+ tmp [vec 1 1 1]] [vec/y tmp]]]
[test-add  0 [let [[tmp 0]] [+ tmp 1] tmp]]

[test-add  2 [wrap-value 2 2 4]]
[test-add  3 [wrap-value 3 2 4]]
[test-add  2 [wrap-value 4 2 4]]
[test-add  3 [wrap-value 5 2 4]]