aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/test/test/luxc/analyser/procedure/common.lux
diff options
context:
space:
mode:
Diffstat (limited to 'new-luxc/test/test/luxc/analyser/procedure/common.lux')
-rw-r--r--new-luxc/test/test/luxc/analyser/procedure/common.lux396
1 files changed, 396 insertions, 0 deletions
diff --git a/new-luxc/test/test/luxc/analyser/procedure/common.lux b/new-luxc/test/test/luxc/analyser/procedure/common.lux
new file mode 100644
index 000000000..14edcf516
--- /dev/null
+++ b/new-luxc/test/test/luxc/analyser/procedure/common.lux
@@ -0,0 +1,396 @@
+(;module:
+ lux
+ (lux [io]
+ (control monad
+ pipe)
+ (concurrency [atom])
+ (data text/format
+ ["R" result]
+ [product]
+ (coll [array]))
+ ["r" math/random "r/" Monad<Random>]
+ [type "Type/" Eq<Type>]
+ [macro #+ Monad<Lux>]
+ (macro [code])
+ test)
+ (luxc ["&" base]
+ ["&;" env]
+ ["&;" module]
+ (lang ["~" analysis])
+ [analyser]
+ (analyser ["@" procedure]
+ ["@;" common]))
+ (../.. common))
+
+(do-template [<name> <success> <failure>]
+ [(def: (<name> procedure params output-type)
+ (-> Text (List Code) Type Bool)
+ (|> (&;with-expected-type output-type
+ (@;analyse-procedure analyse procedure params))
+ (macro;run init-compiler)
+ (case> (#R;Success _)
+ <success>
+
+ (#R;Error _)
+ <failure>)))]
+
+ [check-success+ true false]
+ [check-failure+ false true]
+ )
+
+(test: "Lux procedures"
+ [[primT primC] gen-simple-primitive
+ [antiT antiC] (|> gen-simple-primitive
+ (r;filter (|>. product;left (Type/= primT) not)))]
+ ($_ seq
+ (assert "Can test for reference equality."
+ (check-success+ "lux is" (list primC primC) Bool))
+ (assert "Reference equality must be done with elements of the same type."
+ (check-failure+ "lux is" (list primC antiC) Bool))
+ (assert "Can 'try' risky IO computations."
+ (check-success+ "lux try"
+ (list (` ((~' _lux_function) (~' _) (~' _) (~ primC))))
+ (type (Either Text primT))))
+ ))
+
+(test: "Bit procedures"
+ [subjectC (|> r;nat (:: @ map code;nat))
+ signedC (|> r;int (:: @ map code;int))
+ paramC (|> r;nat (:: @ map code;nat))]
+ ($_ seq
+ (assert "Can count the number of 1 bits in a bit pattern."
+ (check-success+ "bit count" (list subjectC) Nat))
+ (assert "Can perform bit 'and'."
+ (check-success+ "bit and" (list subjectC paramC) Nat))
+ (assert "Can perform bit 'or'."
+ (check-success+ "bit or" (list subjectC paramC) Nat))
+ (assert "Can perform bit 'xor'."
+ (check-success+ "bit xor" (list subjectC paramC) Nat))
+ (assert "Can shift bit pattern to the left."
+ (check-success+ "bit shift-left" (list subjectC paramC) Nat))
+ (assert "Can shift bit pattern to the right."
+ (check-success+ "bit unsigned-shift-right" (list subjectC paramC) Nat))
+ (assert "Can shift signed bit pattern to the right."
+ (check-success+ "bit shift-right" (list signedC paramC) Int))
+ ))
+
+(test: "Nat procedures"
+ [subjectC (|> r;nat (:: @ map code;nat))
+ paramC (|> r;nat (:: @ map code;nat))]
+ ($_ seq
+ (assert "Can add natural numbers."
+ (check-success+ "nat +" (list subjectC paramC) Nat))
+ (assert "Can subtract natural numbers."
+ (check-success+ "nat -" (list subjectC paramC) Nat))
+ (assert "Can multiply natural numbers."
+ (check-success+ "nat *" (list subjectC paramC) Nat))
+ (assert "Can divide natural numbers."
+ (check-success+ "nat /" (list subjectC paramC) Nat))
+ (assert "Can calculate remainder of natural numbers."
+ (check-success+ "nat %" (list subjectC paramC) Nat))
+ (assert "Can test equality of natural numbers."
+ (check-success+ "nat =" (list subjectC paramC) Bool))
+ (assert "Can compare natural numbers."
+ (check-success+ "nat <" (list subjectC paramC) Bool))
+ (assert "Can obtain minimum natural number."
+ (check-success+ "nat min" (list) Nat))
+ (assert "Can obtain maximum natural number."
+ (check-success+ "nat max" (list) Nat))
+ (assert "Can convert natural number to integer."
+ (check-success+ "nat to-int" (list subjectC) Int))
+ (assert "Can convert natural number to text."
+ (check-success+ "nat to-text" (list subjectC) Text))
+ ))
+
+(test: "Int procedures"
+ [subjectC (|> r;int (:: @ map code;int))
+ paramC (|> r;int (:: @ map code;int))]
+ ($_ seq
+ (assert "Can add integers."
+ (check-success+ "int +" (list subjectC paramC) Int))
+ (assert "Can subtract integers."
+ (check-success+ "int -" (list subjectC paramC) Int))
+ (assert "Can multiply integers."
+ (check-success+ "int *" (list subjectC paramC) Int))
+ (assert "Can divide integers."
+ (check-success+ "int /" (list subjectC paramC) Int))
+ (assert "Can calculate remainder of integers."
+ (check-success+ "int %" (list subjectC paramC) Int))
+ (assert "Can test equality of integers."
+ (check-success+ "int =" (list subjectC paramC) Bool))
+ (assert "Can compare integers."
+ (check-success+ "int <" (list subjectC paramC) Bool))
+ (assert "Can obtain minimum integer."
+ (check-success+ "int min" (list) Int))
+ (assert "Can obtain maximum integer."
+ (check-success+ "int max" (list) Int))
+ (assert "Can convert integer to natural number."
+ (check-success+ "int to-nat" (list subjectC) Nat))
+ (assert "Can convert integer to real number."
+ (check-success+ "int to-real" (list subjectC) Real))
+ ))
+
+(test: "Deg procedures"
+ [subjectC (|> r;deg (:: @ map code;deg))
+ paramC (|> r;deg (:: @ map code;deg))
+ natC (|> r;nat (:: @ map code;nat))]
+ ($_ seq
+ (assert "Can add degrees."
+ (check-success+ "deg +" (list subjectC paramC) Deg))
+ (assert "Can subtract degrees."
+ (check-success+ "deg -" (list subjectC paramC) Deg))
+ (assert "Can multiply degrees."
+ (check-success+ "deg *" (list subjectC paramC) Deg))
+ (assert "Can divide degrees."
+ (check-success+ "deg /" (list subjectC paramC) Deg))
+ (assert "Can calculate remainder of degrees."
+ (check-success+ "deg %" (list subjectC paramC) Deg))
+ (assert "Can test equality of degrees."
+ (check-success+ "deg =" (list subjectC paramC) Bool))
+ (assert "Can compare degrees."
+ (check-success+ "deg <" (list subjectC paramC) Bool))
+ (assert "Can obtain minimum degree."
+ (check-success+ "deg min" (list) Deg))
+ (assert "Can obtain maximum degree."
+ (check-success+ "deg max" (list) Deg))
+ (assert "Can convert degree to real number."
+ (check-success+ "deg to-real" (list subjectC) Real))
+ (assert "Can scale degree."
+ (check-success+ "deg scale" (list subjectC natC) Deg))
+ (assert "Can calculate the reciprocal of a natural number."
+ (check-success+ "deg reciprocal" (list natC) Deg))
+ ))
+
+(test: "Real procedures"
+ [subjectC (|> r;real (:: @ map code;real))
+ paramC (|> r;real (:: @ map code;real))
+ encodedC (|> (r;text +5) (:: @ map code;text))]
+ ($_ seq
+ (assert "Can add real numbers."
+ (check-success+ "real +" (list subjectC paramC) Real))
+ (assert "Can subtract real numbers."
+ (check-success+ "real -" (list subjectC paramC) Real))
+ (assert "Can multiply real numbers."
+ (check-success+ "real *" (list subjectC paramC) Real))
+ (assert "Can divide real numbers."
+ (check-success+ "real /" (list subjectC paramC) Real))
+ (assert "Can calculate remainder of real numbers."
+ (check-success+ "real %" (list subjectC paramC) Real))
+ (assert "Can test equality of real numbers."
+ (check-success+ "real =" (list subjectC paramC) Bool))
+ (assert "Can compare real numbers."
+ (check-success+ "real <" (list subjectC paramC) Bool))
+ (assert "Can obtain minimum real number."
+ (check-success+ "real min" (list) Real))
+ (assert "Can obtain maximum real number."
+ (check-success+ "real max" (list) Real))
+ (assert "Can obtain smallest real number."
+ (check-success+ "real smallest" (list) Real))
+ (assert "Can obtain not-a-number."
+ (check-success+ "real not-a-number" (list) Real))
+ (assert "Can obtain positive infinity."
+ (check-success+ "real positive-infinity" (list) Real))
+ (assert "Can obtain negative infinity."
+ (check-success+ "real negative-infinity" (list) Real))
+ (assert "Can convert real number to integer."
+ (check-success+ "real to-int" (list subjectC) Int))
+ (assert "Can convert real number to degree."
+ (check-success+ "real to-deg" (list subjectC) Deg))
+ (assert "Can convert real number to text."
+ (check-success+ "real to-text" (list subjectC) Text))
+ (assert "Can convert text to real number."
+ (check-success+ "real from-text" (list encodedC) (type (Maybe Real))))
+ ))
+
+(test: "Text procedures"
+ [subjectC (|> (r;text +5) (:: @ map code;text))
+ paramC (|> (r;text +5) (:: @ map code;text))
+ replacementC (|> (r;text +5) (:: @ map code;text))
+ fromC (|> r;nat (:: @ map code;nat))
+ toC (|> r;nat (:: @ map code;nat))]
+ ($_ seq
+ (assert "Can test text equality."
+ (check-success+ "text =" (list subjectC paramC) Bool))
+ (assert "Compare texts in lexicographical order."
+ (check-success+ "text <" (list subjectC paramC) Bool))
+ (assert "Can prepend one text to another."
+ (check-success+ "text prepend" (list subjectC paramC) Text))
+ (assert "Can find the index of a piece of text inside a larger one that (may) contain it."
+ (check-success+ "text index" (list subjectC paramC fromC) (type (Maybe Nat))))
+ (assert "Can query the size/length of a text."
+ (check-success+ "text size" (list subjectC) Nat))
+ (assert "Can calculate a hash code for text."
+ (check-success+ "text hash" (list subjectC) Nat))
+ (assert "Can replace a text inside of a larger one (once)."
+ (check-success+ "text replace-once" (list subjectC paramC replacementC) Text))
+ (assert "Can replace a text inside of a larger one (all times)."
+ (check-success+ "text replace-all" (list subjectC paramC replacementC) Text))
+ (assert "Can obtain the character code of a text at a given index."
+ (check-success+ "text char" (list subjectC fromC) Nat))
+ (assert "Can clip a piece of text between 2 indices."
+ (check-success+ "text clip" (list subjectC fromC toC) Text))
+ ))
+
+(test: "Array procedures"
+ [[elemT elemC] gen-simple-primitive
+ sizeC (|> r;nat (:: @ map code;nat))
+ idxC (|> r;nat (:: @ map code;nat))
+ var-name (r;text +5)
+ #let [arrayT (type (array;Array elemT))]]
+ ($_ seq
+ (assert "Can create arrays."
+ (check-success+ "array new" (list sizeC) arrayT))
+ (assert "Can get a value inside an array."
+ (|> (&env;with-scope ""
+ (&env;with-local [var-name arrayT]
+ (&;with-expected-type elemT
+ (@;analyse-procedure analyse "array get"
+ (list idxC
+ (code;symbol ["" var-name]))))))
+ (macro;run init-compiler)
+ (case> (#R;Success _)
+ true
+
+ (#R;Error _)
+ false)))
+ (assert "Can put a value inside an array."
+ (|> (&env;with-scope ""
+ (&env;with-local [var-name arrayT]
+ (&;with-expected-type arrayT
+ (@;analyse-procedure analyse "array put"
+ (list idxC
+ elemC
+ (code;symbol ["" var-name]))))))
+ (macro;run init-compiler)
+ (case> (#R;Success _)
+ true
+
+ (#R;Error _)
+ false)))
+ (assert "Can remove a value from an array."
+ (|> (&env;with-scope ""
+ (&env;with-local [var-name arrayT]
+ (&;with-expected-type arrayT
+ (@;analyse-procedure analyse "array remove"
+ (list idxC
+ (code;symbol ["" var-name]))))))
+ (macro;run init-compiler)
+ (case> (#R;Success _)
+ true
+
+ (#R;Error _)
+ false)))
+ (assert "Can query the size of an array."
+ (|> (&env;with-scope ""
+ (&env;with-local [var-name arrayT]
+ (&;with-expected-type Nat
+ (@;analyse-procedure analyse "array size"
+ (list (code;symbol ["" var-name]))))))
+ (macro;run init-compiler)
+ (case> (#R;Success _)
+ true
+
+ (#R;Error _)
+ false)))
+ ))
+
+(test: "Math procedures"
+ [subjectC (|> r;real (:: @ map code;real))
+ paramC (|> r;real (:: @ map code;real))]
+ (with-expansions [<unary> (do-template [<proc> <desc>]
+ [(assert (format "Can calculate " <desc> ".")
+ (check-success+ <proc> (list subjectC) Real))]
+
+ ["math cos" "cosine"]
+ ["math sin" "sine"]
+ ["math tan" "tangent"]
+ ["math acos" "inverse/arc cosine"]
+ ["math asin" "inverse/arc sine"]
+ ["math atan" "inverse/arc tangent"]
+ ["math cosh" "hyperbolic cosine"]
+ ["math sinh" "hyperbolic sine"]
+ ["math tanh" "hyperbolic tangent"]
+ ["math exp" "exponentiation"]
+ ["math log" "logarithm"]
+ ["math root2" "square root"]
+ ["math root3" "cubic root"]
+ ["math ceil" "ceiling"]
+ ["math floor" "floor"]
+ ["math round" "rounding"])
+ <binary> (do-template [<proc> <desc>]
+ [(assert (format "Can calculate " <desc> ".")
+ (check-success+ <proc> (list subjectC paramC) Real))]
+
+ ["math atan2" "inverse/arc tangent (with 2 arguments)"]
+ ["math pow" "power"])]
+ ($_ seq
+ <unary>
+ <binary>)))
+
+(test: "Atom procedures"
+ [[elemT elemC] gen-simple-primitive
+ sizeC (|> r;nat (:: @ map code;nat))
+ idxC (|> r;nat (:: @ map code;nat))
+ var-name (r;text +5)
+ #let [atomT (type (atom;Atom elemT))]]
+ ($_ seq
+ (assert "Can create atomic reference."
+ (check-success+ "atom new" (list elemC) atomT))
+ (assert "Can read the value of an atomic reference."
+ (|> (&env;with-scope ""
+ (&env;with-local [var-name atomT]
+ (&;with-expected-type elemT
+ (@;analyse-procedure analyse "atom read"
+ (list (code;symbol ["" var-name]))))))
+ (macro;run init-compiler)
+ (case> (#R;Success _)
+ true
+
+ (#R;Error _)
+ false)))
+ (assert "Can swap the value of an atomic reference."
+ (|> (&env;with-scope ""
+ (&env;with-local [var-name atomT]
+ (&;with-expected-type Bool
+ (@;analyse-procedure analyse "atom compare-and-swap"
+ (list elemC
+ elemC
+ (code;symbol ["" var-name]))))))
+ (macro;run init-compiler)
+ (case> (#R;Success _)
+ true
+
+ (#R;Error _)
+ false)))
+ ))
+
+(test: "Process procedures"
+ [[primT primC] gen-simple-primitive
+ timeC (|> r;nat (:: @ map code;nat))]
+ ($_ seq
+ (assert "Can query the level of concurrency."
+ (check-success+ "process concurrency-level" (list) Nat))
+ (assert "Can run an IO computation concurrently."
+ (check-success+ "process future"
+ (list (` ((~' _lux_function) (~' _) (~' _) (~ primC))))
+ Unit))
+ (assert "Can schedule an IO computation to run concurrently at some future time."
+ (check-success+ "process schedule"
+ (list timeC
+ (` ((~' _lux_function) (~' _) (~' _) (~ primC))))
+ Unit))
+ ))
+
+(test: "IO procedures"
+ [logC (|> (r;text +5) (:: @ map code;text))
+ exitC (|> r;nat (:: @ map code;nat))]
+ ($_ seq
+ (assert "Can log messages to standard output."
+ (check-success+ "io log" (list logC) Unit))
+ (assert "Can log messages to standard output."
+ (check-success+ "io error" (list logC) Bottom))
+ (assert "Can log messages to standard output."
+ (check-success+ "io exit" (list exitC) Bottom))
+ (assert "Can query the current time (as milliseconds since epoch)."
+ (check-success+ "io current-time" (list) Int))
+ ))