1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-16 17:20:23 +03:00

Move tests of lib/ and new tests of core.mal to tests/lib/.

Partial translation of examples from lib/ to new tests.

Correct test of memoize.

`reduce`, `foldr`, `every?`, `some` and `and` were previously not
tested in tests/step*.mal. Tests them in `tests/lib/` instead.
This commit is contained in:
Nicolas Boulenguez 2019-05-15 16:49:30 +02:00
parent bf6647fbb3
commit a1805b96af
9 changed files with 120 additions and 89 deletions

View File

@ -1,22 +0,0 @@
;; Benchmarks for memoize.mal
(load-file "../lib/heavy_computations.mal") ; fib
(load-file "../lib/memoize.mal") ; memoize
(load-file "../lib/perf.mal") ; time
(def! N 32)
;; Benchmark naive 'fib'
(println "fib N=" N ": without memoization:")
(time (fib N))
;; "Elapsed time: 14402 msecs"
;; Benchmark memoized 'fib'
(def! fib (memoize fib))
(println "fib N=" N ": with memoization:")
(time (fib N))
;; "Elapsed time: 1 msecs"

View File

@ -1,7 +0,0 @@
;; Examples of the pretty printer.
(load-file "../lib/pprint.mal") ; pprint
(pprint '(7 8 9 "ten" [11 12 [13 14]] 15 16))
(pprint '{:abc 123 :def {:ghi 456 :jkl [789 "ten eleven twelve"]}})
(pprint '(7 8 {:abc 123 :def {:ghi 456 :jkl 789}} 9 10 [11 12 [13 14]] 15 16))

31
tests/lib/folds.mal Normal file
View File

@ -0,0 +1,31 @@
(load-file "../lib/folds.mal")
;; Testing reduce
(reduce + 7 [])
;=>7
(reduce + 7 [1])
;=>8
(reduce + 7 [1 2])
;=>10
(reduce * 7 [-1 2])
;=>-14
(reduce concat [1] [[2] [3]])
;=>(1 2 3)
(reduce str "a" ["b" "c"])
;=>"abc"
;; Testing foldr
(foldr + 7 [])
;=>7
(foldr + 7 [1])
;=>8
(foldr + 7 [1 2])
;=>10
(reduce * 7 [-1 2])
;=>-14
(foldr concat [1] [[2] [3]])
;=>(2 3 1)
(foldr str "a" ["b" "c"])
;=>"bca"
(foldr cons [4 5] [2 3])
;=>(2 3 4 5)

17
tests/lib/memoize.mal Normal file
View File

@ -0,0 +1,17 @@
(load-file "../lib/heavy_computations.mal") ; fib
(load-file "../lib/memoize.mal") ; memoize
(def! N 32)
;; Benchmark naive 'fib'
(def! r1 (fib N)) ; Should be slow
;; Benchmark memoized 'fib'
(def! fib ((memoize fib)))
(def! r2 (fib N)) ; Should be quick
(= r1 r2)
;=>true

37
tests/lib/pprint.mal Normal file
View File

@ -0,0 +1,37 @@
(load-file "../lib/pprint.mal") ; pprint
(pprint '(7 8 9 "ten" [11 12 [13 14]] 15 16))
;/(7
;/ 8
;/ 9
;/ "ten"
;/ [11
;/ 12
;/ [13
;/ 14]]
;/ 15
;/ 16)
;=>nil
(pprint '{:abc 123 :def {:ghi 456 :jkl [789 "ten eleven twelve"]}})
;/{:abc 123
;/ :def {:ghi 456
;/ :jkl [789
;/ "ten eleven twelve"]}}
;=>nil
(pprint '(7 8 {:abc 123 :def {:ghi 456 :jkl 789}} 9 10 [11 12 [13 14]] 15 16))
;/(7
;/ 8
;/ {:abc 123
;/ :def {:ghi 456
;/ :jkl 789}}
;/ 9
;/ 10
;/ [11
;/ 12
;/ [13
;/ 14]]
;/ 15
;/ 16)
;=>nil

View File

@ -1,5 +1,3 @@
;; Examples for protocols.
(load-file "../lib/protocols.mal") ; defprotocol extend satisfies
(def! make-triangle (fn* [o a]
@ -12,7 +10,8 @@
(area [this])
(draw [this]))
(prn :false-> (satisfies? IDraw (make-triangle 5 5))) ;=> false
(prn :false-> (satisfies? IDraw (make-triangle 5 5)))
;=>false
(extend :shape/rectangle
IDraw
@ -24,6 +23,8 @@
{:area (fn* [obj] (/ (* (get obj :opposite) (get obj :adjacent)) 2))
:draw (fn* [obj] (println " .\n.."))})
(prn :true-> (satisfies? IDraw (make-triangle 5 5))) ;=> true
(prn :true-> (satisfies? IDraw (make-triangle 5 5)))
;=>true
(prn :area-> (area (make-triangle 5 4))) ;=> 10
(prn :area-> (area (make-triangle 5 4)))
;=>10

View File

@ -0,0 +1,26 @@
(load-file "../lib/test_cascade.mal")
;; Testing every?
(every? first [])
;=>true
(every? first [[1] [2]])
;=>true
(every? first [[1] [nil] []])
;=>false
;; Testing some
(some first [])
;=>nil
(some first [[nil] [1] []])
;=>1
(and)
;=>true
(and 1)
;=>1
(and 1 2 3 4)
;=>4
(and false 2)
;=>false
(and true 1 nil false)
;=>nil

View File

@ -150,60 +150,8 @@ x
;=>"yes"
;;
(load-file "../lib/folds.mal")
;=>nil
;; Testing reduce
(reduce + 7 [])
;=>7
(reduce + 7 [1])
;=>8
(reduce + 7 [1 2])
;=>10
(reduce * 7 [-1 2])
;=>-14
(reduce concat [1] [[2] [3]])
;=>(1 2 3)
(reduce str "a" ["b" "c"])
;=>"abc"
;; Testing foldr
(foldr + 7 [])
;=>7
(foldr + 7 [1])
;=>8
(foldr + 7 [1 2])
;=>10
(reduce * 7 [-1 2])
;=>-14
(foldr concat [1] [[2] [3]])
;=>(2 3 1)
(foldr str "a" ["b" "c"])
;=>"bca"
(foldr cons [4 5] [2 3])
;=>(2 3 4 5)
;;
(load-file "../lib/test_cascade.mal")
;=>nil
;; Testing every?
(every? first [])
;=>true
(every? first [[1] [2]])
;=>true
(every? first [[1] [nil] []])
;=>false
;; Testing some
(some first [])
;=>nil
(some first [[nil] [1] []])
;=>1
;;
;; Loading -> and ->> from lib/composition.mal
(load-file "../lib/composition.mal")
;=>nil
;; Testing -> macro
(-> 7)

View File

@ -286,11 +286,11 @@
(let* [or_FIXME 23] (or false (+ or_FIXME 100)))
;=>123
;; Loading sumdown from lib/heavy_computations.mal
(load-file "../lib/heavy_computations.mal")
;;
;; Testing time-ms function
(load-file "../lib/heavy_computations.mal")
;=>nil
(def! start-time (time-ms))
(= start-time 0)
;=>false