diff --git a/test/run-tests.py b/test/run-tests.py index 6f77f53..ebb8a9b 100755 --- a/test/run-tests.py +++ b/test/run-tests.py @@ -6,7 +6,7 @@ passed, failed, verbose, quiet = 0, 0, 0, 0 def run (forthfile): - return os.popen ("gforth ../ttester-codewars.4th " + forthfile + " -e bye").readlines () + return os.popen ("bash -c 'gforth ../ttester-codewars.4th " + forthfile + " -e bye 2> >(sed -E \"s/redefined [^ ]+ //g\" >&2)'").readlines () def timeless (lines): return [re.sub(r'\d+', '0', l) if "" in l else l for l in lines] diff --git a/test/test-stack-underflow.4th b/test/test-stack-underflow.4th new file mode 100644 index 0000000..1b3aa54 --- /dev/null +++ b/test/test-stack-underflow.4th @@ -0,0 +1,18 @@ +\ Copyright 2019 nomennescio +s" stack underflow" describe#{ + s" single test" it#{ + <{ drop -> 0 }> + }# + s" double test" it#{ + <{ drop -> 0 }> + <{ drop drop -> 0 }> + }# + s" double test" it#{ + <{ drop -> 0 0 }> + <{ drop drop -> 0 0 }> + }# + s" double test" it#{ + <{ drop -> 0 0 }> + <{ drop drop -> 0 }> + }# +}# diff --git a/test/test-stack-underflow.expected b/test/test-stack-underflow.expected new file mode 100644 index 0000000..aeab6a5 --- /dev/null +++ b/test/test-stack-underflow.expected @@ -0,0 +1,34 @@ + +stack underflow + +single test + +Wrong number of results, expected 1 , got a 1 cell stack underflow + +0.033 ms + +double test + +Wrong number of results, expected 1 , got a 1 cell stack underflow + +Wrong number of results, expected 1 , got a 2 cell stack underflow + +1565296144893.672 ms + +double test + +Wrong number of results, expected 2 , got a 1 cell stack underflow + +Wrong number of results, expected 2 , got a 2 cell stack underflow + +1565296144893.759 ms + +double test + +Wrong number of results, expected 2 , got a 1 cell stack underflow + +Wrong number of results, expected 1 , got a 2 cell stack underflow + +1565296144893.836 ms + +0.311 ms diff --git a/ttester-codewars.4th b/ttester-codewars.4th index 8553ffb..3ad39b4 100644 --- a/ttester-codewars.4th +++ b/ttester-codewars.4th @@ -2,10 +2,10 @@ decimal s" test/ttester.fs" included -: #ms ( dmicroseconds -- len c-addr ) <# # # # [char] . hold #s #> ; +: #ms ( dmicroseconds -- c-addr len ) <# # # # [char] . hold #s #> ; -: describe#{ ( len c-addr -- ) cr ." " type cr utime ; -: it#{ ( len c-addr -- ) cr ." " type cr utime ; +: describe#{ ( c-addr len -- ) cr ." " type cr utime ; +: it#{ ( c-addr len -- ) cr ." " type cr utime ; : }# ( -- ) utime cr ." " 2swap d- #ms type ." ms" cr ; : failed# ( -- ) cr ." " ; @@ -26,13 +26,20 @@ variable ^different RESULTS @ 0 +do ACTUAL-RESULTS i cells + @ . loop cr ; : nresults$ ." Wrong number of results, expected " depth START-DEPTH @ - . - ." , got " ACTUAL-DEPTH @ START-DEPTH @ - . cr ; + ." , got " ACTUAL-DEPTH @ START-DEPTH @ - dup 0< if negate ." a " . ." cell stack underflow" else . then cr ; ' passed$ ^passed ! ' nresults$ ^nresults ! ' different$ ^different ! : <{ T{ ; +: -> depth dup ACTUAL-DEPTH ! + START-DEPTH @ >= if + depth START-DEPTH @ - 0 +do ACTUAL-RESULTS i cells + ! loop + else + START-DEPTH @ depth - -1 +do 0 loop + then + F-> ; : }> depth ACTUAL-DEPTH @ = if depth START-DEPTH @ > if @@ -56,4 +63,4 @@ variable ^different 3037000493 constant #m \ prime number < sqrt (2^63-1) 53 constant #p \ prime number : c# { hash pow c -- hash' pow' } c pow * hash + #m mod pow #p * #m mod ; \ polynomial rolling hash function, single char -: s# { c-addr u -- hash } 0 1 c-addr u 0 +do { s } s c@ c# s char+ loop 2drop ; \ string hash \ No newline at end of file +: s# { c-addr len -- hash } 0 1 c-addr len 0 +do { s } s c@ c# s char+ loop 2drop ; \ string hash \ No newline at end of file