diff options
| author | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
| commit | 3169070063b2cb877200117ebb384269d73bcb93 (patch) | |
| tree | 23de3db1ea5c37afd21a45b6ed7771f56a08c0c4 /make/tests | |
| download | mal-3169070063b2cb877200117ebb384269d73bcb93.tar.gz mal-3169070063b2cb877200117ebb384269d73bcb93.zip | |
Current state of mal for Clojure West lighting talk.
Diffstat (limited to 'make/tests')
| -rw-r--r-- | make/tests/common.mk | 18 | ||||
| -rw-r--r-- | make/tests/reader.mk | 76 | ||||
| -rw-r--r-- | make/tests/step9_interop.mk | 14 | ||||
| -rw-r--r-- | make/tests/types.mk | 304 |
4 files changed, 412 insertions, 0 deletions
diff --git a/make/tests/common.mk b/make/tests/common.mk new file mode 100644 index 0000000..55b931d --- /dev/null +++ b/make/tests/common.mk @@ -0,0 +1,18 @@ + +# assert macros +assert = $(if $1,,$(error assert failure: $2)) +assert_not = $(if $1,$(error assert_not: $2),) +assert_eq = $(if $(call _EQ,$(1),$(2)),,$(error assert_eq failure: $(1) != $(2): $(3))) +# With debug: +#assert_eq = $(info 1: $(1))$(info 2: $(2))$(info 3: $(3))$(if $(call _EQ,$(1),$(2)),,$(error assert_eq failure: $(3))) + + +# REPL related wrappers +test_read = $(call READ_STR,$(1)) +ifndef MACROEXPAND +define MACROEXPAND +$(1) +endef +endif +test_re = $(strip $(call EVAL,$(call MACROEXPAND,$(strip $(call test_read,$(1))),$(REPL_ENV)),$(REPL_ENV))) +test_rep = $(call PRINT,$(strip $(call EVAL,$(call MACROEXPAND,$(strip $(call test_read,$(1))),$(REPL_ENV)),$(REPL_ENV)))) diff --git a/make/tests/reader.mk b/make/tests/reader.mk new file mode 100644 index 0000000..672d27b --- /dev/null +++ b/make/tests/reader.mk @@ -0,0 +1,76 @@ +INTERACTIVE = no + +include tests/common.mk +include reader.mk + +_tonum = $(call int_decode,$($(1)_value)) + +$(info Testing READ_STR of numbers) +$(call assert_eq,2,$(call _tonum,$(call READ_STR,2))) +$(call assert_eq,12345,$(call _tonum,$(call READ_STR,12345))) +$(call assert_eq,12345,$(call _tonum,$(call READ_STR,12345 "abc"))) +$(call assert_eq,12345,$(call _tonum,$(call READ_STR,12345"abc"))) +$(call assert_eq,12345,$(call _tonum,$(call READ_STR,12345(1)))) + +$(info Testing READ_STR of symbols) +$(call assert_eq,abc,$($(call READ_STR,abc)_value)) +$(call assert_eq,abc,$($(call READ_STR,abc )_value)) +$(call assert_eq,abc,$($(call READ_STR,abc"a str")_value)) +$(call assert_eq,abc,$($(call READ_STR,abc(2 3))_value)) + +$(info Testing READ_STR of strings) +$(call assert_eq,a string,$(call str_decode,$($(call READ_STR,"a string")_value))) +$(call assert_eq,a string (with parens),$(call str_decode,$($(call READ_STR,"a string (with parens)")_value))) +$(call assert_eq,a string,$(call str_decode,$($(call READ_STR,"a string"())_value))) +$(call assert_eq,a string,$(call str_decode,$($(call READ_STR,"a string"123)_value))) +$(call assert_eq,a string,$(call str_decode,$($(call READ_STR,"a string"abc)_value))) +$(call assert_eq,,$(call str_decode,$($(call READ_STR,"")_value))) +$(call assert_eq,abc ,$(call str_decode,$($(call READ_STR,"abc ")_value))) +$(call assert_eq, abc,$(call str_decode,$($(call READ_STR," abc")_value))) +$(call assert_eq,$$abc,$(call str_decode,$($(call READ_STR,"$$abc")_value))) +$(call assert_eq,abc$$(),$(call str_decode,$($(call READ_STR,"abc$$()")_value))) +$(call assert_eq,"xyz",$(call str_decode,$($(call READ_STR,"\"xyz\"")_value))) + +$(info Testing READ_STR of lists) +$(call assert_eq,2,$(call _count,$(call READ_STR,(2 3)))) +$(call assert_eq,2,$(call _tonum,$(call sfirst,$(call READ_STR,(2 3))))) +$(call assert_eq,3,$(call _tonum,$(call sfirst,$(call srest,$(call READ_STR,(2 3)))))) +L := $(strip $(call READ_STR,(+ 1 2 "str1" "string (with parens) and 'single quotes'"))) +$(call assert_eq,5,$(call _count,$(L))) +$(call assert_eq,str1,$(call str_decode,$($(call _nth,$(L),3)_value))) +$(call assert_eq,string (with parens) and 'single quotes',$(call str_decode,$($(call _nth,$(L),4)_value))) + +$(info Testing READ_STR of vectors) +$(call assert_eq,2,$(call _count,$(call READ_STR,[2 3]))) +$(call assert_eq,2,$(call _tonum,$(call sfirst,$(call READ_STR,[2 3])))) +$(call assert_eq,3,$(call _tonum,$(call sfirst,$(call srest,$(call READ_STR,[2 3]))))) +L := $(strip $(call READ_STR,[+ 1 2 "str1" "string (with parens) and 'single quotes'"])) +$(call assert_eq,5,$(call _count,$(L))) +$(call assert_eq,str1,$(call str_decode,$($(call _nth,$(L),3)_value))) +$(call assert_eq,string (with parens) and 'single quotes',$(call str_decode,$($(call _nth,$(L),4)_value))) + +$(info Testing READ_STR of quote/quasiquote) +$(call assert_eq,quote,$($(call _nth,$(call READ_STR,'1),0)_value)) #' +$(call assert_eq,1,$(call _tonum,$(call _nth,$(call READ_STR,'1),1))) #' +$(call assert_eq,quote,$($(call _nth,$(call READ_STR,'(1 2 3)),0)_value)) #' +$(call assert_eq,3,$(call _tonum,$(call _nth,$(call _nth,$(call READ_STR,'(1 2 3)),1),2))) #' + +$(call assert_eq,quasiquote,$($(call _nth,$(call READ_STR,`1),0)_value)) +$(call assert_eq,1,$(call _tonum,$(call _nth,$(call READ_STR,`1),1))) +$(call assert_eq,quasiquote,$($(call _nth,$(call READ_STR,`(1 2 3)),0)_value)) +$(call assert_eq,3,$(call _tonum,$(call _nth,$(call _nth,$(call READ_STR,`(1 2 3)),1),2))) + +$(call assert_eq,unquote,$($(call _nth,$(call READ_STR,~1),0)_value)) +$(call assert_eq,1,$(call _tonum,$(call _nth,$(call READ_STR,~1),1))) +$(call assert_eq,unquote,$($(call _nth,$(call READ_STR,~(1 2 3)),0)_value)) +$(call assert_eq,3,$(call _tonum,$(call _nth,$(call _nth,$(call READ_STR,~(1 2 3)),1),2))) + +$(call assert_eq,splice-unquote,$($(call _nth,$(call READ_STR,~@1),0)_value)) +$(call assert_eq,1,$(call _tonum,$(call _nth,$(call READ_STR,~@1),1))) +$(call assert_eq,splice-unquote,$($(call _nth,$(call READ_STR,~@(1 2 3)),0)_value)) +$(call assert_eq,3,$(call _tonum,$(call _nth,$(call _nth,$(call READ_STR,~@(1 2 3)),1),2))) + + +.PHONY: all +all: + @echo "All tests completed" diff --git a/make/tests/step9_interop.mk b/make/tests/step9_interop.mk new file mode 100644 index 0000000..0e44a88 --- /dev/null +++ b/make/tests/step9_interop.mk @@ -0,0 +1,14 @@ +INTERACTIVE = + +include tests/common.mk +include step9_interop.mk + +$(info Testing trivial macros) +$(call assert_eq,7,$(call test_rep, (make* "7") )) +$(call assert_eq,"XaY XbY XcY",$(call test_rep, (make* "\"$(foreach v,a b c,X$(v)Y)\"") )) +$(call assert_eq,(2 3 4),$(call test_rep, (make* "($(foreach v,1 2 3,$(call gmsl_plus,1,$(v))))") )) + + +.PHONY: all +all: + @echo "All tests completed" diff --git a/make/tests/types.mk b/make/tests/types.mk new file mode 100644 index 0000000..c1c1849 --- /dev/null +++ b/make/tests/types.mk @@ -0,0 +1,304 @@ +include tests/common.mk +include types.mk + +# treat an expression as a statement +do = $(eval __tmp := $(1)) + + +$(info Testing foreach as a let form) + +$(call assert_eq,XbX,$(foreach local_var,b,X$(local_var)X),\ + Using foreach as 'let' failed) + + +$(info Testing type function) +$(call assert_eq,make,$(call _obj_type,xyz),\ + (type xyz) is not 'make') +$(call assert_eq,nil,$(call _obj_type,$(__nil)),\ + (type $$(__nil)) is not 'nil') +$(call assert_eq,true,$(call _obj_type,$(__true)),\ + (type $$(__true)) is not 'true') +$(call assert_eq,false,$(call _obj_type,$(__false)),\ + (type $$(__false)) is not 'false') + + +$(info Testing number? function) + +$(call assert_eq,number,$(call _obj_type,$(call number,1))) +$(call assert_eq,number,$(call _obj_type,$(call number,10))) +$(call assert_eq,number,$(call _obj_type,$(call number,12345))) + + +$(info Testing symbols) + +$(call assert_eq,symbol,$(call _obj_type,$(call symbol,abc)),\ + (type (symbol abc)) is not 'symbol') +SYM1 := $(call symbol,a sym value) +$(call assert_eq,a sym value,$($(SYM1)_value)) +$(call assert_eq,$(__true),$(call symbol?,$(SYM1))) + + +$(info Testing strings) + +$(call assert_eq,string,$(call _obj_type,$(call string,abc)),\ + (type (string abc)) is not string) + +STR1 := $(call string,a string value) +$(call assert_eq,a string value,$(call str_decode,$($(STR1)_value))) +$(call assert_eq,$(__true),$(call string?,$(STR1))) +$(call assert_eq,14,$(call _count,$(STR1))) + +STR2 := $(call string,a string (with parens)) +$(call assert_eq,a string (with parens),$(call str_decode,$($(STR2)_value))) +$(call assert_eq,$(__true),$(call string?,$(STR2))) +$(call assert_eq,22,$(call _count,$(STR2))) + +$(info Testing strings (subs)) +$(call assert_eq,a string (with parens),$(call str_decode,$($(call subs,$(STR2),$(call number,2))_value))) +$(call assert_eq,a string,$(call str_decode,$($(call subs,$(STR2),$(call number,0),$(call number,8))_value))) + +$(info Testing strings (str)) +$(call assert_eq,a string value - a string (with parens),$(call str_decode,$($(call str,$(STR1) $(call string, - ) $(STR2))_value))) + + +$(info Testing function objects) + +$(call assert_eq,function,$(call _obj_type,$(call function,abc)),\ + (type (function abc)) is not 'function') +FN1 := $(call function,arg1:'$$(word 1,$$(1))' arg2:'$$(word 2,$$(1))') +$(call assert_eq,$(__true),$(call function?,$(FN1))) +$(call assert_eq,arg1:'A' arg2:'B',$(call apply,$(FN1),$(call list,A B))) + + +$(info Testing lists) + +$(call assert_eq,list,$(call _obj_type,$(call list)),\ + (type (list)) is not 'list') + +$(info Testing lists (cons)) +L1 := $(call cons,P $(call list)) +L2 := $(call cons,Q $(L1)) +$(call assert_eq,$(__true),$(call list?,$(L1))) +$(call assert_eq,$(__true),$(call list?,$(L2))) +$(call assert_eq,P,$(call sfirst,$(L1))) +$(call assert_eq,2,$(call _count,$(L2))) +$(call assert_eq,Q,$(call sfirst,$(L2))) +$(call assert_eq,P,$(call _nth,$(L2),1)) +$(call assert_eq,$(__true),$(call equal?,$(L1) $(call srest,$(L2)))) + +$(info Testing lists (concat)) +L1_2 := $(call concat,$(L1) $(L2)) +$(call assert_eq,3,$(call _count,$(L1_2))) +$(call assert_eq,P,$(call sfirst,$(L1_2))) +$(call assert_eq,Q,$(call _nth,$(L1_2),1)) +$(call assert_eq,P,$(call _nth,$(L1_2),2)) +$(call assert_eq,$(__true),$(call equal?,$(L2) $(call srest,$(L1_2)))) + +$(info Testing lists (conj)) +L3 := $(call _conj!,$(call list),A B) +L4 := $(call _conj!,$(call list),X $(L3)) +$(call assert_eq,$(__true),$(call list?,$(L3)),\ + (list? $$(L3))) +$(call assert_eq,$(__true),$(call list?,$(L4)),\ + (list? $$(L3))) +$(call assert_eq,A,$(call sfirst,$(L3)),\ + (sfirst $$(L3)) is not 'A') +$(call assert_eq,X,$(call sfirst,$(L4)),\ + (sfirst $$(L4)) is not 'X') +$(call assert_eq,$(__true),$(call list?,$(call _nth,$(L4),1)),\ + (_nth $$(L4),1) is not a list) +$(call assert_eq,A,$(call sfirst,$(call _nth,$(L4),1)),\ + (first (_nth $$(L4),1)) is not 'A') + + +$(info Testing hash_maps) + +X := $(call hash_map) +$(call assert_eq,$(__true),$(call hash_map?,$(X)),\ + (hash_map? $$(X))) +$(call assert_eq,$(__false),$(call vector?,$(X)),\ + (vector? $$(X))) + +mykey := $(call _string,a) +$(call assert_not,$(call _get,$(X),a),\ + (get $$(X),a)) +$(call assert_eq,$(__false),$(call contains?,$(X),$(mykey)),\ + (contains? $$(X),a)) +$(call do,$(call _assoc!,$(X),a,value of X a)) +$(call assert_eq,value of X a,$(call _get,$(X),a),\ + (get $$(X),a) is not 'value of Xa') +$(call assert_eq,$(__true),$(call contains?,$(X) $(mykey)),\ + (contains? $$(X),a)) + +Y := $(call hash_map) +$(call assert_eq,0,$(call _count,$(Y)),\ + (_count $$(Y))) +$(call do,$(call _assoc!,$(Y),a,value of Y a)) +$(call assert_eq,1,$(call _count,$(Y)),\ + (_count $$(Y))) +$(call do,$(call _assoc!,$(Y),b,value of Y b)) +$(call assert_eq,2,$(call _count,$(Y)),\ + (_count $$(Y))) +$(call assert_eq,value of Y a,$(call _get,$(Y),a),\ + (get $$(Y),a) is not 'value of Y a') +$(call assert_eq,value of Y b,$(call _get,$(Y),b),\ + (get $$(Y),b) is not 'value of Y b') +$(call assert_eq,value of Y a value of Y b,$(call raw_flat,$(Y),b),\ + (raw_flat $(Y)) is not 'value of Y a value of Y b') + +$(call do,$(call _assoc!,$(X),b,$(Y))) +$(call assert_eq,2,$(call _count,$(Y),a),\ + (_count $$(Y)) should still be 2) + +$(call assert_eq,$(__true),$(call hash_map?,$(call _get,$(X),b)),\ + (hash_map? (get $$(X),b))) + +$(call assert_eq,$(call _get,$(call _get,$(X),b),a),value of Y a,\ + (get (get $(X),b),a) is not 'value of Y a') +$(call assert_eq,$(call _get,$(call _get,$(X),b),b),value of Y b,\ + (get (get $(X),b),b) is not 'value of Y b') + +$(call do,$(call _dissoc!,$(Y),a)) +$(call assert_eq,1,$(call _count,$(Y)),\ + (_count $$(Y)) should now be 1) +$(call assert_not,$(call _get,$(Y),a),\ + (get $$(Y),a)) +$(call do,$(call _dissoc!,$(Y),b)) +$(call assert_eq,0,$(call _count,$(Y)),\ + (_count $$(Y)) should now be 0) +$(call assert_not,$(call _get,$(Y),b),\ + (get $$(Y),b)) + + +$(info Testing vectors) + +V1 := $(call _conj!,$(call vector),first.vector.value second.vector.value third.vector.value) +$(call assert_eq,$(__true),$(call vector?,$(V1)),\ + (vector? $$(V1))) +$(call assert_eq,first.vector.value,$(call _nth,$(V1),0)) +$(call assert_eq,second.vector.value,$(call _nth,$(V1),1)) +$(call assert_eq,third.vector.value,$(call _nth,$(V1),2)) +$(call assert_eq,third.vector.value,$(call slast,$(V1))) +$(call assert_eq,3,$(call _count,$(V1))) + +V2 := $(call _conj!,$(call vector),A B C) +$(call assert_eq,3,$(call _count,$(V2)),\ + (_count $$(V2)) is not 3) +$(call assert_eq,A B C,$($(V2)_value)) +$(call assert_eq,A,$(call sfirst,$(V2)),\ + (first $$(V2)) is not 'A') +$(call assert_eq,$(__true),$(call list?,$(call srest,$(V2))),\ + (rest $$(V2)) is not a vector) +$(call assert_eq,B C,$($(call srest,$(V2))_value)) +$(call assert_eq,B,$(call sfirst,$(call srest,$(V2))),\ + (first (rest $$(V2))) is not 'B') +$(call assert_eq,C,$(call sfirst,$(call srest,$(call srest,$(V2)))),\ + (first (rest (rest $$(V2)))) is not 'C') +$(call assert_eq,C,$(call _nth,$(V2),2),\ + (_nth $$(V2),2) is not 'C') + +V2_1 := $(call _conj!,$(V2),$(V1)) +$(call assert_eq,4,$(call _count,$(V2_1)),\ + (_count $$(V2_1)) is not 4) +$(call assert_eq,C,$(call _nth,$(V2_1),2),\ + (_nth $$(V2_1),2) is no longer 'C') +$(call assert_eq,$(__true),$(call vector?,$(call _nth,$(V2_1),3)),\ + (_nth $$(V2_1),3) is not a vector) +$(call assert_eq,second.vector.value,$(call _nth,$(call _nth,$(V2_1),3),1),\ + (_nth (_nth $$(V2_1),3),1) is not 'second.vector.value') + +$(info Testing vectors (rest)) + +V3 := $(call srest,$(V2_1)) +$(call assert_eq,3,$(call _count,$(V3)),\ + (_count $$(V3)) is not 3) +$(call assert_eq,B,$(call sfirst,$(V3)),\ + (first $$(V3)) is not 'B') +$(call assert_eq,$(__true),$(call vector?,$(call _nth,$(V3),2)),\ + (_nth $$(V3),2) is not a vector) +$(call assert_eq,second.vector.value,$(call _nth,$(call _nth,$(V3),2),1),\ + (_nth (_nth $$(V3),2),1) is not 'second.vector.value') + +$(info Testing vectors (contains?)) + +$(call assert_eq,$(__true),$(call _contains?,$(V2_1),0),\ + (contains? $$(V2_1),0)) +$(call assert_eq,,$(call _contains?,$(V2_1),7),\ + (contains? $$(V2_1),7)) + + +$(info Testing _apply function) + +label_args = $(word 1,$(1))$(word 2,$(1))$(word 3,$(1))$(word 4,$(1)) +$(call assert_eq,,$(call _apply,label_args,$(call list))) +$(call assert_eq,A,$(call _apply,label_args,$(call list,A))) +$(call assert_eq,AB,$(call _apply,label_args,$(call list,A B))) +$(call assert_eq,ABCD,$(call _apply,label_args,$(call list,A B C D))) + + +$(info Testing smap function) + +L5 := $(call _conj!,$(call list),$(call number,1) $(call number,2) $(call number,3)) +inc = $(call number_plus,$(call number,1) $(1)) +$(call assert_eq,(2 3 4),$(call _pr_str,$(call _smap,inc,$(L5)))) +inc_func := $(call function,$$(call number_plus,$$(call number,1) $$(1))) +$(call assert_eq,(2 3 4),$(call _pr_str,$(call smap,$(inc_func) $(L5)))) + + +$(info Testing equal? function) +$(call assert_eq,$(__true),$(call equal?,2 2)) +$(call assert_eq,$(__false),$(call equal?,2 3)) +$(call assert_eq,$(__false),$(call equal?,2 3)) +$(call assert_eq,$(__true),$(call equal?,abc abc)) +$(call assert_eq,$(__false),$(call equal?,abc abz)) +$(call assert_eq,$(__false),$(call equal?,zbc abc)) +$(call assert_eq,$(__true),$(call equal?,$(call string,abc) $(call string,abc))) +$(call assert_eq,$(__false),$(call equal?,$(call string,abc) $(call string,abz))) +$(call assert_eq,$(__false),$(call equal?,$(call string,zbc) $(call string,abc))) +$(call assert_eq,$(__true),$(call equal?,$(call symbol,abc) $(call symbol,abc))) +$(call assert_eq,$(__false),$(call equal?,$(call symbol,abc) $(call symbol,abz))) +$(call assert_eq,$(__false),$(call equal?,$(call symbol,zbc) $(call symbol,abc))) +L6 := $(call _conj!,$(call list),1 2 3) +L7 := $(call _conj!,$(call list),1 2 3) +L8 := $(call _conj!,$(call list),1 2 Z) +L9 := $(call _conj!,$(call list),Z 2 3) +L10 := $(call _conj!,$(call list),1 2) +$(call assert_eq,$(__true),$(call equal?,$(L6) $(L7))) +$(call assert_eq,$(__false),$(call equal?,$(L6) $(L8))) +$(call assert_eq,$(__false),$(call equal?,$(L6) $(L9))) +$(call assert_eq,$(__false),$(call equal?,$(L6) $(L10))) +$(call assert_eq,$(__false),$(call equal?,$(L10) $(L6))) + + +$(info Testing empty? function) +$(call assert_eq,$(__true),$(call empty?,$(call list))) +$(call assert_eq,$(__false),$(call empty?,$(call list,1))) + + +$(info Testing ENV (1 level)) +env1 := $(call ENV) +$(call assert_eq,,$(call ENV_GET,$(env1),a)) +$(call assert_eq,$(env1),$(call ENV_SET,$(env1),a,val_a)) +$(call assert_eq,$(env1),$(call ENV_SET,$(env1),b,val_b)) +$(call assert_eq,$(env1),$(call ENV_SET,$(env1),=,val_eq)) +$(call assert_eq,val_a,$(call ENV_GET,$(env1),a)) +$(call assert_eq,val_b,$(call ENV_GET,$(env1),b)) +$(call assert_eq,val_eq,$(call ENV_GET,$(env1),=)) +$(call assert_eq,hash_map,$(call _obj_type,$(call ENV_FIND,$(env1),a))) +$(call assert_eq,val_a,$(call _get,$(call ENV_FIND,$(env1),a),a)) + +$(info Testing ENV (2 levels)) +env2 := $(call ENV,$(env1)) +$(call assert_eq,$(env2),$(call ENV_SET,$(env2),b,val_b2)) +$(call assert_eq,$(env2),$(call ENV_SET,$(env2),c,val_c)) +$(call assert_eq,$(env1),$(call ENV_FIND,$(env2),a)) +$(call assert_eq,$(env2),$(call ENV_FIND,$(env2),b)) +$(call assert_eq,$(env2),$(call ENV_FIND,$(env2),c)) +$(call assert_eq,val_a,$(call ENV_GET,$(env2),a)) +$(call assert_eq,val_b2,$(call ENV_GET,$(env2),b)) +$(call assert_eq,val_c,$(call ENV_GET,$(env2),c)) + + +.PHONY: all +all: + @echo "All tests completed" |
