forked from codewars/ttester-codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttester-codewars.4th
66 lines (57 loc) · 1.9 KB
/
ttester-codewars.4th
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
\ ttester extension for Codewars
decimal
s" test/ttester.fs" included
: #ms ( dmicroseconds -- c-addr len ) <# # # # [char] . hold #s #> ;
: 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::>" ;
: passed# ( -- ) cr ." <PASSED::>" ;
create EXPECTED-RESULTS 32 cells allot
variable RESULTS
variable DIFFERENCES
variable ^passed
variable ^nresults
variable ^different
: passed$ ." Test Passed" cr ;
: different$ ." Expected "
0 RESULTS @ -do EXPECTED-RESULTS i 1- cells + @ . 1 -loop
." , got "
0 RESULTS @ -do ACTUAL-RESULTS i 1- cells + @ . 1 -loop
cr ;
: nresults$ ." Wrong number of results, expected " depth START-DEPTH @ - .
." , 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
0 DIFFERENCES !
depth START-DEPTH @ - dup RESULTS ! 0 +do
dup EXPECTED-RESULTS i cells + !
ACTUAL-RESULTS i cells + @ <> DIFFERENCES +!
loop
DIFFERENCES @ if
failed# ^different @ execute
else
passed# ^passed @ execute
then
then
else
failed# ^nresults @ execute
then
EMPTY-STACK
F} ;
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 len -- hash } 0 1 c-addr len 0 +do { s } s c@ c# s char+ loop 2drop ; \ string hash