Skip to content

Commit 2df3bb6

Browse files
authored
Merge pull request #9 from nomennescio/bugfix-#6
Fix dropping past the arguments stack causes test display to show wrong numbers (#6)
2 parents ca9496f + 61bd0c9 commit 2df3bb6

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

test/run-tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
passed, failed, verbose, quiet = 0, 0, 0, 0
77

88
def run (forthfile):
9-
return os.popen ("gforth ../ttester-codewars.4th " + forthfile + " -e bye").readlines ()
9+
return os.popen ("bash -c 'gforth ../ttester-codewars.4th " + forthfile + " -e bye 2> >(sed -E \"s/redefined [^ ]+ //g\" >&2)'").readlines ()
1010

1111
def timeless (lines):
1212
return [re.sub(r'\d+', '0', l) if "<COMPLETEDIN::>" in l else l for l in lines]

test/test-stack-underflow.4th

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
\ Copyright 2019 nomennescio
2+
s" stack underflow" describe#{
3+
s" single test" it#{
4+
<{ drop -> 0 }>
5+
}#
6+
s" double test" it#{
7+
<{ drop -> 0 }>
8+
<{ drop drop -> 0 }>
9+
}#
10+
s" double test" it#{
11+
<{ drop -> 0 0 }>
12+
<{ drop drop -> 0 0 }>
13+
}#
14+
s" double test" it#{
15+
<{ drop -> 0 0 }>
16+
<{ drop drop -> 0 }>
17+
}#
18+
}#

test/test-stack-underflow.expected

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
<DESCRIBE::>stack underflow
3+
4+
<IT::>single test
5+
6+
<FAILED::>Wrong number of results, expected 1 , got a 1 cell stack underflow
7+
8+
<COMPLETEDIN::>0.033 ms
9+
10+
<IT::>double test
11+
12+
<FAILED::>Wrong number of results, expected 1 , got a 1 cell stack underflow
13+
14+
<FAILED::>Wrong number of results, expected 1 , got a 2 cell stack underflow
15+
16+
<COMPLETEDIN::>1565296144893.672 ms
17+
18+
<IT::>double test
19+
20+
<FAILED::>Wrong number of results, expected 2 , got a 1 cell stack underflow
21+
22+
<FAILED::>Wrong number of results, expected 2 , got a 2 cell stack underflow
23+
24+
<COMPLETEDIN::>1565296144893.759 ms
25+
26+
<IT::>double test
27+
28+
<FAILED::>Wrong number of results, expected 2 , got a 1 cell stack underflow
29+
30+
<FAILED::>Wrong number of results, expected 1 , got a 2 cell stack underflow
31+
32+
<COMPLETEDIN::>1565296144893.836 ms
33+
34+
<COMPLETEDIN::>0.311 ms

ttester-codewars.4th

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
decimal
33
s" test/ttester.fs" included
44

5-
: #ms ( dmicroseconds -- len c-addr ) <# # # # [char] . hold #s #> ;
5+
: #ms ( dmicroseconds -- c-addr len ) <# # # # [char] . hold #s #> ;
66

7-
: describe#{ ( len c-addr -- ) cr ." <DESCRIBE::>" type cr utime ;
8-
: it#{ ( len c-addr -- ) cr ." <IT::>" type cr utime ;
7+
: describe#{ ( c-addr len -- ) cr ." <DESCRIBE::>" type cr utime ;
8+
: it#{ ( c-addr len -- ) cr ." <IT::>" type cr utime ;
99
: }# ( -- ) utime cr ." <COMPLETEDIN::>" 2swap d- #ms type ." ms" cr ;
1010

1111
: failed# ( -- ) cr ." <FAILED::>" ;
@@ -26,13 +26,20 @@ variable ^different
2626
RESULTS @ 0 +do ACTUAL-RESULTS i cells + @ . loop
2727
cr ;
2828
: nresults$ ." Wrong number of results, expected " depth START-DEPTH @ - .
29-
." , got " ACTUAL-DEPTH @ START-DEPTH @ - . cr ;
29+
." , got " ACTUAL-DEPTH @ START-DEPTH @ - dup 0< if negate ." a " . ." cell stack underflow" else . then cr ;
3030

3131
' passed$ ^passed !
3232
' nresults$ ^nresults !
3333
' different$ ^different !
3434

3535
: <{ T{ ;
36+
: -> depth dup ACTUAL-DEPTH !
37+
START-DEPTH @ >= if
38+
depth START-DEPTH @ - 0 +do ACTUAL-RESULTS i cells + ! loop
39+
else
40+
START-DEPTH @ depth - -1 +do 0 loop
41+
then
42+
F-> ;
3643
: }>
3744
depth ACTUAL-DEPTH @ = if
3845
depth START-DEPTH @ > if
@@ -56,4 +63,4 @@ variable ^different
5663
3037000493 constant #m \ prime number < sqrt (2^63-1)
5764
53 constant #p \ prime number
5865
: c# { hash pow c -- hash' pow' } c pow * hash + #m mod pow #p * #m mod ; \ polynomial rolling hash function, single char
59-
: s# { c-addr u -- hash } 0 1 c-addr u 0 +do { s } s c@ c# s char+ loop 2drop ; \ string hash
66+
: s# { c-addr len -- hash } 0 1 c-addr len 0 +do { s } s c@ c# s char+ loop 2drop ; \ string hash

0 commit comments

Comments
 (0)