Skip to content

Commit

Permalink
revert to test-words; merge sieve-test.8th and test.8th
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj committed Oct 11, 2023
1 parent 33ad8fa commit 8bf8246
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 266 deletions.
12 changes: 9 additions & 3 deletions bin/test-no-docker
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ make_test_dir() {
local key=$2
(
cd "$exercise_dir"
cp -t "$test_dir" test.8th test-words.8th "${slug}-tests.8th"
[[ -f test-words.8th ]] && cp test-words.8th "$test_dir"
[[ -d libs ]] && cp -r libs "$test_dir"
while IFS= read -r test; do
cp -r "$test" "$test_dir"
done < <(
jq -r '.files.test[]' .meta/config.json
)
while IFS= read -r solution; do
cp "$solution" "$test_dir/$slug.8th"
cp -r "$solution" "$test_dir/$slug.8th"
done < <(
jq -r --arg key "$key" '.files[$key][]' .meta/config.json
)
Expand All @@ -35,7 +41,7 @@ test_one() {
fi
(
cd "$test_dir" || die "cannot cd to $test_dir"
8th test.8th
8th -f test.8th
)
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/sieve/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"sieve.8th"
],
"test": [
"sieve-tests.8th"
"test.8th"
],
"example": [
".meta/example.8th"
Expand Down
31 changes: 17 additions & 14 deletions exercises/practice/sieve/.meta/example.8th
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,35 @@
;

: mark-multiples-of \ a i -- a
2dup a:@ !if 2drop ;; then
drop
dup dup n:* \ a i idx (initial idx is i^2)
repeat
third over \ a i idx a idx
false a:! \ a i idx a
drop over n:+
dup r@ n:>
until!
2dup a:@ if
drop
dup dup n:* \ a i idx (initial idx is i^2)
repeat
third over \ a i idx a idx
false a:! \ a i idx a
drop over n:+
dup r@ n:> \ quit loop when idx > n (from r-stack)
until!
then
2drop
;

\ from 2 to √n, mark multiples of primes as non-prime
: mark-multiples \ a -- a
' mark-multiples-of 2 r@ n:sqrt loop
;

: extract-primes \ a1 -- a2
a:new >r
( if r> a:push >r else drop then ) a:each
drop r>
a:new swap
\ a:each removes the array from the stack while it's running
( if a:push else drop then ) a:each
drop
;

: primes \ n -- a
>r
>r \ store `n` on r-stack
make-flag-array
mark-multiples
extract-primes
rdrop
rdrop \ clear `n` from r-stack
;
90 changes: 0 additions & 90 deletions exercises/practice/sieve/harness.8th

This file was deleted.

123 changes: 123 additions & 0 deletions exercises/practice/sieve/libs/exercism/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
needs console/loaded

\ -----------------------------------------------------------------

ns: test

-1 var, test-count
var tests-passed
var tests-failed
var tests-skipped
true var, run-test

\ Some utility words
: test-passed \ s --
1 tests-passed n:+!
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
;

: test-skipped \ s --
1 tests-skipped n:+!
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
;

: test-failed \ s --
1 tests-failed n:+!
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
;

: isword? \ x -- x f
dup >kind ns:w n:=
;

: run-test? \ -- T
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
;

\ Num passed + num skipped + num failed should == num tests
: all-tests-run? \ -- T
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
test-count @ n:=
;

\ adapted from 8th forum
: eq? \ x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then

\ same kind: try different comparators
number? if n:= ;then
string? if s:= ;then
array? if ' eq? a:= 2nip ;then
map? if ' eq? m:= 2nip ;then

\ otherwise fall back to 'lazy evaluation'
l: =
;

\ -----------------------------------------------------------------

\ status report at end of run
( all-tests-run?
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
) onexit

\ Print a summary of the tests run
( con:white con:onBlack
test-count @ . space "tests planned - " .
tests-passed @ . space "passed - " .
tests-skipped @ . space "skipped - " .
tests-failed @ . space "failed" . cr
) onexit

\ -----------------------------------------------------------------
\ The public-facing words
\ -----------------------------------------------------------------

: equal? \ s x w -- | s w x --
run-test? !if 2drop test-skipped ;; then
isword? !if swap then
w:exec
eq? if test-passed else test-failed then
;

: true? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
if test-passed else test-failed then
;

: false? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
!if test-passed else test-failed then
;

: null? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
G:null? nip if test-passed else test-failed then
;

: SKIP-REST-OF-TESTS false run-test ! ;

: tests \ n --
test-count !
;

\ Set the exit status:
\ 0 = all OK
\ 1 = not all tests were run (some error occurred)
\ 2 = some tests failed
: end-of-tests \ --
all-tests-run?
if
tests-failed @ 0 n:= if 0 else 2 then
else
1
then
die
;

with: test
Loading

0 comments on commit 8bf8246

Please sign in to comment.