Skip to content

Commit

Permalink
Merge pull request #9 from nomennescio/bugfix-#6
Browse files Browse the repository at this point in the history
Fix dropping past the arguments stack causes test display to show wrong numbers (#6)
  • Loading branch information
kazk authored Aug 9, 2019
2 parents ca9496f + 61bd0c9 commit 2df3bb6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion test/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<COMPLETEDIN::>" in l else l for l in lines]
Expand Down
18 changes: 18 additions & 0 deletions test/test-stack-underflow.4th
Original file line number Diff line number Diff line change
@@ -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 }>
}#
}#
34 changes: 34 additions & 0 deletions test/test-stack-underflow.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

<DESCRIBE::>stack underflow

<IT::>single test

<FAILED::>Wrong number of results, expected 1 , got a 1 cell stack underflow

<COMPLETEDIN::>0.033 ms

<IT::>double test

<FAILED::>Wrong number of results, expected 1 , got a 1 cell stack underflow

<FAILED::>Wrong number of results, expected 1 , got a 2 cell stack underflow

<COMPLETEDIN::>1565296144893.672 ms

<IT::>double test

<FAILED::>Wrong number of results, expected 2 , got a 1 cell stack underflow

<FAILED::>Wrong number of results, expected 2 , got a 2 cell stack underflow

<COMPLETEDIN::>1565296144893.759 ms

<IT::>double test

<FAILED::>Wrong number of results, expected 2 , got a 1 cell stack underflow

<FAILED::>Wrong number of results, expected 1 , got a 2 cell stack underflow

<COMPLETEDIN::>1565296144893.836 ms

<COMPLETEDIN::>0.311 ms
17 changes: 12 additions & 5 deletions ttester-codewars.4th
Original file line number Diff line number Diff line change
Expand Up @@ -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 ." <DESCRIBE::>" type cr utime ;
: it#{ ( len c-addr -- ) cr ." <IT::>" type cr utime ;
: describe#{ ( c-addr len -- ) cr ." <DESCRIBE::>" type cr utime ;
: it#{ ( c-addr len -- ) cr ." <IT::>" type cr utime ;
: }# ( -- ) utime cr ." <COMPLETEDIN::>" 2swap d- #ms type ." ms" cr ;

: failed# ( -- ) cr ." <FAILED::>" ;
Expand All @@ -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
Expand All @@ -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
: s# { c-addr len -- hash } 0 1 c-addr len 0 +do { s } s c@ c# s char+ loop 2drop ; \ string hash

0 comments on commit 2df3bb6

Please # to comment.