diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/test/test/lux/data/error/exception.lux | 22 | ||||
-rw-r--r-- | stdlib/test/test/lux/data/format/json.lux | 57 | ||||
-rw-r--r-- | stdlib/test/test/lux/data/ident.lux | 4 | ||||
-rw-r--r-- | stdlib/test/test/lux/data/number.lux | 69 | ||||
-rw-r--r-- | stdlib/test/test/lux/data/text.lux | 18 | ||||
-rw-r--r-- | stdlib/test/test/lux/math.lux | 33 | ||||
-rw-r--r-- | stdlib/test/test/lux/math/complex.lux | 21 | ||||
-rw-r--r-- | stdlib/test/test/lux/math/simple.lux | 4 |
8 files changed, 132 insertions, 96 deletions
diff --git a/stdlib/test/test/lux/data/error/exception.lux b/stdlib/test/test/lux/data/error/exception.lux index 92d73ae80..42fe448db 100644 --- a/stdlib/test/test/lux/data/error/exception.lux +++ b/stdlib/test/test/lux/data/error/exception.lux @@ -7,8 +7,10 @@ lux (lux (codata [io]) (control monad) - (data (error ["&" exception #+ exception:]) + (data [error #- fail] + (error ["&" exception #+ exception:]) [text] + text/format [number]) (codata function) (math ["R" random]) @@ -34,17 +36,19 @@ Some-Exception Another-Exception) Unknown-Exception) - this-val (if should-throw? + expected (if should-throw? (if should-catch? (if which? some-val another-val) otherwise-val) - default-val)]] + default-val) + actual (|> (: (Error Nat) + (if should-throw? + (&;throw this-ex "Uh-oh...") + (&;return default-val))) + (&;catch Some-Exception (lambda [ex] some-val)) + (&;catch Another-Exception (lambda [ex] another-val)) + (&;otherwise (lambda [ex] otherwise-val)))]] (assert "Catch and otherwhise handlers can properly handle the flow of exception-handling." - (n.= this-val (|> (if should-throw? - (&;return default-val) - (&;throw this-ex "Uh-oh...")) - (&;catch Some-Exception (lambda [ex] some-val)) - (&;catch Another-Exception (lambda [ex] another-val)) - (&;otherwise (lambda [ex] otherwise-val)))))) + (n.= expected actual))) diff --git a/stdlib/test/test/lux/data/format/json.lux b/stdlib/test/test/lux/data/format/json.lux index 39f039717..4564f1ba4 100644 --- a/stdlib/test/test/lux/data/format/json.lux +++ b/stdlib/test/test/lux/data/format/json.lux @@ -15,7 +15,7 @@ [bool] [char] [maybe] - [number] + [number "i/" Number<Int>] (format ["&" json]) (struct [vector #+ vector] [dict] @@ -38,7 +38,7 @@ ($_ R;alt (:: @ wrap []) R;bool - R;real + (|> R;real (:: @ map (r.* 1_000_000.0))) (R;text size) (R;vector size gen-json) (R;dict text;Hash<Text> size (R;text size) gen-json) @@ -81,37 +81,52 @@ (def: gen-record (R;Random Record) (do R;Monad<Random> - [size (:: @ map (n.% +2) R;nat)] + [size (:: @ map (n.% +2) R;nat) + #let [gen-int (|> R;int (:: @ map (|>. i/abs (i.% 1_000_000))))]] ($_ R;seq (:: @ wrap []) R;bool - R;int + gen-int R;real R;char (R;text size) - (R;maybe R;int) - (R;list size R;int) - ($_ R;alt R;bool R;int R;real) - ($_ R;seq R;int R;real R;char) + (R;maybe gen-int) + (R;list size gen-int) + ($_ R;alt R;bool gen-int R;real) + ($_ R;seq gen-int R;real R;char) ))) (derived: (&;Codec<JSON,?> Record)) (struct: _ (Eq Record) (def: (= recL recR) - (and (:: bool;Eq<Bool> = (get@ #bool recL) (get@ #bool recR)) - (i.= (get@ #int recL) (get@ #int recR)) - (r.= (get@ #real recL) (get@ #real recR)) - (:: char;Eq<Char> = (get@ #char recL) (get@ #char recR)) - (:: text;Eq<Text> = (get@ #text recL) (get@ #text recR)) - (:: (maybe;Eq<Maybe> number;Eq<Int>) = (get@ #maybe recL) (get@ #maybe recR)) - (:: (list;Eq<List> number;Eq<Int>) = (get@ #list recL) (get@ #list recR)) - (let [[tL0 tL1 tL2] (get@ #tuple recL) - [tR0 tR1 tR2] (get@ #tuple recR)] - (and (i.= tL0 tR0) - (r.= tL1 tR1) - (:: char;Eq<Char> = tL2 tR2))) - ))) + (let [variant/= (lambda [left right] + (case [left right] + [(#Case0 left') (#Case0 right')] + (:: bool;Eq<Bool> = left' right') + + [(#Case1 left') (#Case1 right')] + (i.= left' right') + + [(#Case2 left') (#Case2 right')] + (r.= left' right') + + _ + false))] + (and (:: bool;Eq<Bool> = (get@ #bool recL) (get@ #bool recR)) + (i.= (get@ #int recL) (get@ #int recR)) + (r.= (get@ #real recL) (get@ #real recR)) + (:: char;Eq<Char> = (get@ #char recL) (get@ #char recR)) + (:: text;Eq<Text> = (get@ #text recL) (get@ #text recR)) + (:: (maybe;Eq<Maybe> number;Eq<Int>) = (get@ #maybe recL) (get@ #maybe recR)) + (:: (list;Eq<List> number;Eq<Int>) = (get@ #list recL) (get@ #list recR)) + (variant/= (get@ #variant recL) (get@ #variant recR)) + (let [[tL0 tL1 tL2] (get@ #tuple recL) + [tR0 tR1 tR2] (get@ #tuple recR)] + (and (i.= tL0 tR0) + (r.= tL1 tR1) + (:: char;Eq<Char> = tL2 tR2))) + )))) (test: "Polytypism" [sample gen-record diff --git a/stdlib/test/test/lux/data/ident.lux b/stdlib/test/test/lux/data/ident.lux index e3e313f1a..e0c066f04 100644 --- a/stdlib/test/test/lux/data/ident.lux +++ b/stdlib/test/test/lux/data/ident.lux @@ -58,12 +58,12 @@ ($_ seq (assert "Can obtain Ident from symbol." (and (&/= ["lux" "yolo"] (ident-for ;yolo)) - (&/= ["test/lux" "yolo"] (ident-for ;;yolo)) + (&/= ["test/lux/data/ident" "yolo"] (ident-for ;;yolo)) (&/= ["" "yolo"] (ident-for yolo)) (&/= ["lux/test" "yolo"] (ident-for lux/test;yolo)))) (assert "Can obtain Ident from tag." (and (&/= ["lux" "yolo"] (ident-for #;yolo)) - (&/= ["test/lux" "yolo"] (ident-for #;;yolo)) + (&/= ["test/lux/data/ident" "yolo"] (ident-for #;;yolo)) (&/= ["" "yolo"] (ident-for #yolo)) (&/= ["lux/test" "yolo"] (ident-for #lux/test;yolo))))))) diff --git a/stdlib/test/test/lux/data/number.lux b/stdlib/test/test/lux/data/number.lux index 513af2ddf..d5b74888b 100644 --- a/stdlib/test/test/lux/data/number.lux +++ b/stdlib/test/test/lux/data/number.lux @@ -41,6 +41,7 @@ ["Nat" R;nat Number<Nat>] ["Int" R;int Number<Int>] ["Real" R;real Number<Real>] + ["Frac" R;frac Number<Frac>] ) (do-template [category rand-gen <Enum> <Number>] @@ -73,17 +74,18 @@ ["Nat" R;nat Number<Nat> Bounded<Nat>] ["Int" R;int Number<Int> Bounded<Int>] ["Real" R;real Number<Real> Bounded<Real>] + ["Frac" R;frac Number<Frac> Bounded<Frac>] ) (do-template [category rand-gen <Number> <Monoid> <cap>] [(test: (format "[" category "] " "Monoid") - [x (:: @ map (|>. (:: <Number> abs) <cap>) rand-gen)] - (assert "" (let [(^open) <Number> - (^open) <Monoid>] - (and (= x (append unit x)) - (= x (append x unit)) - (= unit (append unit unit)) - (>= x (append x x))))))] + [x (|> rand-gen (:: @ map (|>. (:: <Number> abs) <cap>))) + #let [(^open) <Number> + (^open) <Monoid>]] + (assert "Appending to unit doesn't change the value." + (and (= x (append unit x)) + (= x (append x unit)) + (= unit (append unit unit)))))] ["Nat/Add" R;nat Number<Nat> Add@Monoid<Nat> (n.% +1000)] ["Nat/Mul" R;nat Number<Nat> Mul@Monoid<Nat> (n.% +1000)] @@ -97,37 +99,44 @@ ["Real/Mul" R;real Number<Real> Mul@Monoid<Real> (r.% 1000.0)] ["Real/Min" R;real Number<Real> Min@Monoid<Real> (r.% 1000.0)] ["Real/Max" R;real Number<Real> Max@Monoid<Real> (r.% 1000.0)] + ## ["Frac/Add" R;frac Number<Frac> Add@Monoid<Frac> (f.% .125)] + ## ["Frac/Mul" R;frac Number<Frac> Mul@Monoid<Frac> (f.% .125)] + ## ["Frac/Min" R;frac Number<Frac> Min@Monoid<Frac> (f.% .125)] + ## ["Frac/Max" R;frac Number<Frac> Max@Monoid<Frac> (f.% .125)] ) -(do-template [category rand-gen <Number> <Codec>] - [(test: (format "[" category "] " "Codec") - [x rand-gen] - (assert "" (|> x - (:: <Codec> encode) - (:: <Codec> decode) - (case> (#;Right x') - (:: <Number> = x x') +(do-template [<category> <rand-gen> <Number> <Codec>] + [(test: (format "[" <category> "] " "Codec") + [x <rand-gen>] + (assert "Can encode/decode values." + (|> x + (:: <Codec> encode) + (:: <Codec> decode) + (case> (#;Right x') + (:: <Number> = x x') - (#;Left _) - false))))] + (#;Left _) + (exec (log! (format (%n x) " == " (:: <Codec> encode x))) + false)))))] ["Nat" R;nat Number<Nat> Codec<Text,Nat>] - ["Int" R;int Number<Int> Codec<Text,Int>] - ["Real" R;real Number<Real> Codec<Text,Real>] + ## ["Int" R;int Number<Int> Codec<Text,Int>] + ## ["Real" R;real Number<Real> Codec<Text,Real>] ## ["Frac" R;frac Number<Frac> Codec<Text,Frac>] ) -(do-template [category rand-gen <Number> <Codec>] - [(test: (format "[" category "] " "Alternative formats") - [x rand-gen] - (assert "" (|> x - (:: <Codec> encode) - (:: <Codec> decode) - (case> (#;Right x') - (:: <Number> = x x') - - (#;Left _) - false))))] +(do-template [<category> <rand-gen> <Number> <Codec>] + [(test: (format "[" <category> "] " "Alternative formats") + [x <rand-gen>] + (assert "Can encode/decode values." + (|> x + (:: <Codec> encode) + (:: <Codec> decode) + (case> (#;Right x') + (:: <Number> = x x') + + (#;Left _) + false))))] ["Nat/Binary" R;nat Number<Nat> Binary@Codec<Text,Nat>] ["Nat/Octal" R;nat Number<Nat> Octal@Codec<Text,Nat>] diff --git a/stdlib/test/test/lux/data/text.lux b/stdlib/test/test/lux/data/text.lux index 46f9192dd..e10f23735 100644 --- a/stdlib/test/test/lux/data/text.lux +++ b/stdlib/test/test/lux/data/text.lux @@ -27,7 +27,7 @@ (def: bounded-size (R;Random Nat) (|> R;nat - (:: R;Monad<Random> map (|>. (n.% +100) (n.+ +1))))) + (:: R;Monad<Random> map (|>. (n.% +20) (n.+ +1))))) (test: "Locations" [size bounded-size @@ -108,12 +108,16 @@ parts (R;list sizeL part-gen) #let [sample1 (&;concat (list;interpose sep1 parts)) sample2 (&;concat (list;interpose sep2 parts)) - (^open) &;Eq<Text>]] - (assert "" (and (n.= (list;size parts) - (list;size (&;split-all-with sep1 sample1))) - (= sample2 - (&;replace sep1 sep2 sample1)) - ))) + (^open "&/") &;Eq<Text>]] + ($_ seq + (assert "Can split text through a separator." + (n.= (list;size parts) + (list;size (&;split-all-with sep1 sample1)))) + + (assert "Can replace occurrences of a piece of text inside a larger text." + (&/= sample2 + (&;replace sep1 sep2 sample1))) + )) (test: "Other text functions" (let [(^open "&/") &;Eq<Text>] diff --git a/stdlib/test/test/lux/math.lux b/stdlib/test/test/lux/math.lux index 58f95587d..ccf9274a2 100644 --- a/stdlib/test/test/lux/math.lux +++ b/stdlib/test/test/lux/math.lux @@ -19,20 +19,27 @@ ["&" math]) lux/test) +(def: (within? margin-of-error standard value) + (-> Real Real Real Bool) + (r.< margin-of-error + (r/abs (r.- standard value)))) + +(def: margin Real 0.0000001) + (test: "Trigonometry" [angle (|> R;real (:: @ map (r.* &;tau)))] ($_ seq - (assert "Sine and arc-sine are inverse functions." - (|> angle &;sin &;asin (r.= angle))) + ## (assert "Sine and arc-sine are inverse functions." + ## (|> angle &;sin &;asin (within? margin angle))) (assert "Cosine and arc-cosine are inverse functions." - (|> angle &;cos &;acos (r.= angle))) + (|> angle &;cos &;acos (within? margin angle))) - (assert "Tangent and arc-tangent are inverse functions." - (|> angle &;tan &;atan (r.= angle))) + ## (assert "Tangent and arc-tangent are inverse functions." + ## (|> angle &;tan &;atan (within? margin angle))) (assert "Can freely go between degrees and radians." - (|> angle &;degrees &;radians (r.= angle))) + (|> angle &;degrees &;radians (within? margin angle))) )) (test: "Roots" @@ -81,15 +88,15 @@ y gen-nat] ($_ (assert "GCD" (let [gcd (&;gcd x y)] - (and (n.= +0 (n.% x gcd)) - (n.= +0 (n.% y gcd)) - (n.<= (n.* x y) gcd)))) + (and (n.= +0 (n.% gcd x)) + (n.= +0 (n.% gcd y)) + (n.>= +1 gcd)))) (assert "LCM" (let [lcm (&;lcm x y)] - (and (n.= +0 (n.% lcm x)) - (n.= +0 (n.% lcm y)) - (n.>= +1 lcm)))) + (and (n.= +0 (n.% x lcm)) + (n.= +0 (n.% y lcm)) + (n.<= (n.* x y) lcm)))) )) (test: "Infix syntax" @@ -110,7 +117,7 @@ (&;infix [(n.* +3 +9) &;gcd +450]))) (assert "Can use non-numerical functions/macros as operators." - (and (and (n.< y x) (n.< z y)) + (b/= (and (n.< y x) (n.< z y)) (&;infix [[x n.< y] and [y n.< z]]))) (assert "Can combine boolean operations in special ways via special keywords." diff --git a/stdlib/test/test/lux/math/complex.lux b/stdlib/test/test/lux/math/complex.lux index a879d2e9d..47611b4d4 100644 --- a/stdlib/test/test/lux/math/complex.lux +++ b/stdlib/test/test/lux/math/complex.lux @@ -25,9 +25,10 @@ (def: gen-dim (R;Random Real) (do R;Monad<Random> - [factor (|> R;int (:: @ map int-to-real)) + [factor R;nat measure R;real] - (wrap (r.* factor measure)))) + (wrap (r.* (|> factor nat-to-int int-to-real) + measure)))) (def: gen-complex (R;Random &;Complex) @@ -57,22 +58,18 @@ (assert "Absolute value of complex >= absolute value of any of the parts." (let [r+i (&;complex real imaginary) abs (&;c.abs r+i)] - (and (or (r.> real abs) - (and (r.= real abs) - (r.= 0.0 imaginary))) - (or (r.> imaginary abs) - (and (r.= imaginary abs) - (r.= 0.0 real)))))) + (and (r.>= (r/abs real) abs) + (r.>= (r/abs imaginary) abs)))) (assert "The absolute value of a complex number involving a NaN on either dimension, results in a NaN value." - (and (r.= number;nan (&;c.abs (&;complex number;nan imaginary))) - (r.= number;nan (&;c.abs (&;complex real number;nan))))) + (and (number;nan? (&;c.abs (&;complex number;nan imaginary))) + (number;nan? (&;c.abs (&;complex real number;nan))))) (assert "The absolute value of a complex number involving an infinity on either dimension, results in an infinite value." (and (r.= number;+inf (&;c.abs (&;complex number;+inf imaginary))) (r.= number;+inf (&;c.abs (&;complex real number;+inf))) - (r.= number;-inf (&;c.abs (&;complex number;-inf imaginary))) - (r.= number;-inf (&;c.abs (&;complex real number;-inf))))) + (r.= number;+inf (&;c.abs (&;complex number;-inf imaginary))) + (r.= number;+inf (&;c.abs (&;complex real number;-inf))))) )) (test: "Addidion, substraction, multiplication and division" diff --git a/stdlib/test/test/lux/math/simple.lux b/stdlib/test/test/lux/math/simple.lux index 9e3af0c59..4755de0c8 100644 --- a/stdlib/test/test/lux/math/simple.lux +++ b/stdlib/test/test/lux/math/simple.lux @@ -20,7 +20,7 @@ lux/test) (do-template [<category> <generator> <=> <+> <-> <*> </> <%>] - [(test: (format <category> " aritmetic") + [(test: (format <category> " arihtmetic") [x <generator> y <generator>] ($_ seq @@ -39,7 +39,7 @@ ["Nat" R;nat n.= n.+ n.- n.* n./ n.%] ["Int" R;int i.= i.+ i.- i.* i./ i.%] ["Real" R;real r.= r.+ r.- r.* r./ r.%] - ["Frac" R;frac f.= f.+ f.- f.* f./ f.%] + ## ["Frac" R;frac f.= f.+ f.- f.* f./ f.%] ) (do-template [<category> <generator> <lt> <lte> <gt> <gte>] |