From a1805b96af85e1f184b01794748334b2b083eb51 Mon Sep 17 00:00:00 2001 From: Nicolas Boulenguez Date: Wed, 15 May 2019 16:49:30 +0200 Subject: [PATCH] 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. --- examples/memoize.mal | 22 ----------- examples/pprint.mal | 7 ---- tests/lib/folds.mal | 31 +++++++++++++++ tests/lib/memoize.mal | 17 +++++++++ tests/lib/pprint.mal | 37 ++++++++++++++++++ {examples => tests/lib}/protocols.mal | 11 +++--- tests/lib/test_cascade.mal | 26 +++++++++++++ tests/step8_macros.mal | 54 +-------------------------- tests/stepA_mal.mal | 4 +- 9 files changed, 120 insertions(+), 89 deletions(-) delete mode 100644 examples/memoize.mal delete mode 100644 examples/pprint.mal create mode 100644 tests/lib/folds.mal create mode 100644 tests/lib/memoize.mal create mode 100644 tests/lib/pprint.mal rename {examples => tests/lib}/protocols.mal (74%) create mode 100644 tests/lib/test_cascade.mal diff --git a/examples/memoize.mal b/examples/memoize.mal deleted file mode 100644 index 31648ba2..00000000 --- a/examples/memoize.mal +++ /dev/null @@ -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" diff --git a/examples/pprint.mal b/examples/pprint.mal deleted file mode 100644 index 9b5ff7e0..00000000 --- a/examples/pprint.mal +++ /dev/null @@ -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)) diff --git a/tests/lib/folds.mal b/tests/lib/folds.mal new file mode 100644 index 00000000..1317d49a --- /dev/null +++ b/tests/lib/folds.mal @@ -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) diff --git a/tests/lib/memoize.mal b/tests/lib/memoize.mal new file mode 100644 index 00000000..4df8fbfa --- /dev/null +++ b/tests/lib/memoize.mal @@ -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 diff --git a/tests/lib/pprint.mal b/tests/lib/pprint.mal new file mode 100644 index 00000000..1f513efc --- /dev/null +++ b/tests/lib/pprint.mal @@ -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 diff --git a/examples/protocols.mal b/tests/lib/protocols.mal similarity index 74% rename from examples/protocols.mal rename to tests/lib/protocols.mal index 44d059e7..4154e5d2 100644 --- a/examples/protocols.mal +++ b/tests/lib/protocols.mal @@ -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 diff --git a/tests/lib/test_cascade.mal b/tests/lib/test_cascade.mal new file mode 100644 index 00000000..c1f540fd --- /dev/null +++ b/tests/lib/test_cascade.mal @@ -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 diff --git a/tests/step8_macros.mal b/tests/step8_macros.mal index b8866513..05ac19a9 100644 --- a/tests/step8_macros.mal +++ b/tests/step8_macros.mal @@ -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) diff --git a/tests/stepA_mal.mal b/tests/stepA_mal.mal index e797d25f..86a74a63 100644 --- a/tests/stepA_mal.mal +++ b/tests/stepA_mal.mal @@ -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