diff options
| author | s-ol <s+removethis@s-ol.nu> | 2022-01-27 13:58:29 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | 141d45a8ba34fce1f8710ab5cf917eb5d679bf4e (patch) | |
| tree | 0ecd226f4266ef1689bc663430fee7472099e481 | |
| parent | docs: show output samples on dark background (diff) | |
| download | alive-141d45a8ba34fce1f8710ab5cf917eb5d679bf4e.tar.gz alive-141d45a8ba34fce1f8710ab5cf917eb5d679bf4e.zip | |
test logic module
| -rw-r--r-- | alv-lib/logic.moon | 10 | ||||
| -rw-r--r-- | spec/lang/logic_spec.moon | 264 |
2 files changed, 268 insertions, 6 deletions
diff --git a/alv-lib/logic.moon b/alv-lib/logic.moon index 763638c..d66f53a 100644 --- a/alv-lib/logic.moon +++ b/alv-lib/logic.moon @@ -1,4 +1,4 @@ -import PureOp, Constant, T, sig, evt from require 'alv.base' +import PureOp, Constant, T, any from require 'alv.base' all_same = (first, list) -> for v in *list @@ -14,10 +14,8 @@ tobool = (val) -> else true -any = sig! / evt! - class ReduceOp extends PureOp - pattern: any\rep 2, nil + pattern: any!\rep 2, nil type: T.bool tick: => @@ -107,7 +105,7 @@ not_ = Constant.meta examples: { '(not a)' } value: class extends PureOp - pattern: any\rep 1, 1 + pattern: any!\rep 1, 1 type: T.bool tick: => @out\set not tobool @inputs[1]! @@ -119,7 +117,7 @@ bool = Constant.meta description: "`false` if a is `false`, `nil` or `0`, `true` otherwise." value: class extends PureOp - pattern: any\rep 1, 1 + pattern: any!\rep 1, 1 type: T.bool tick: => @out\set tobool @inputs[1]! diff --git a/spec/lang/logic_spec.moon b/spec/lang/logic_spec.moon new file mode 100644 index 0000000..f3d0610 --- /dev/null +++ b/spec/lang/logic_spec.moon @@ -0,0 +1,264 @@ +import TestPilot from require 'spec.test_setup' +import T, Array, Constant from require 'alv' + +describe "logic", -> + test = TestPilot '', '(import* logic)\n' + TRUE = T.bool\mk_const true + FALSE = T.bool\mk_const false + + describe "==", -> + it "can compare any type", -> + with COPILOT\eval_once '(== 1 1)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(== 1 2)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== 1 "hello")' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== "hello" "hello")' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(== (array 1 2 3) (array 1 2 3))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(== (array 1 2 3) (array 1 2 1))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== (array 1 2 3) (array 1 2))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== (array 1 2 3) (array 1 2 3 4))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== + (struct "a" 1 "b" true "c" (array "test")) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(== + (struct "a" 1 "b" false "c" (array "test")) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== + (struct "a" 1 "b" true "c" (array "test" "toast")) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== + (struct "a" 1 "b" true) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== + (struct "a" 1 "b" true) + (struct "a" 1))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(== print print)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(== print ==)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + it "is aliased as eq", -> + with COPILOT\eval_once '(== eq ==)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + describe "!=", -> + it "can compare any type", -> + with COPILOT\eval_once '(!= 1 1)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(!= 1 2)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= 1 "hello")' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= "hello" "hello")' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2 3))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2 1))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= (array 1 2 3) (array 1 2 3 4))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= + (struct "a" 1 "b" true "c" (array "test")) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(!= + (struct "a" 1 "b" false "c" (array "test")) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= + (struct "a" 1 "b" true "c" (array "test" "toast")) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= + (struct "a" 1 "b" true) + (struct "a" 1 "b" true "c" (array "test")))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= + (struct "a" 1 "b" true) + (struct "a" 1))' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(!= print print)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(!= print ==)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + it "is aliased as note-eq", -> + with COPILOT\eval_once '(== not-eq !=)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + describe "bool", -> + it "coerces numbers", -> + with COPILOT\eval_once '(bool 0)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(bool 1)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(bool -1)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(bool 1024)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + it "accepts booleans", -> + with COPILOT\eval_once '(bool false)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(bool true)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + describe "not", -> + it "accepts booleans", -> + with COPILOT\eval_once '(not false)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(not true)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + it "coerces numbers", -> + with COPILOT\eval_once '(not 0)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(not 1)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(not -1)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(not 1024)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + + describe "or", -> + it "accepts any number of mixed arguments", -> + with COPILOT\eval_once '(or false 0)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(or 1 0)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(or 0 false 0 0 0 0)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(or 0 0 0 true 0 0)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(or 0 true 0 1 0 0)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + describe "and", -> + it "accepts any number of mixed arguments", -> + with COPILOT\eval_once '(and false 1)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(and 1 true)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(and false 0)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(and 1 1 true 0)' + assert.is.true \is_const! + assert.is.equal FALSE, .result + + with COPILOT\eval_once '(and 1 1 true true 1)' + assert.is.true \is_const! + assert.is.equal TRUE, .result + + with COPILOT\eval_once '(and 1 1 1)' + assert.is.true \is_const! + assert.is.equal TRUE, .result |
