Skip to content

Commit

Permalink
Use string hash for comparing strings (#4)
Browse files Browse the repository at this point in the history
Use string hash for comparing strings
  • Loading branch information
kazk authored Aug 2, 2019
2 parents 297c2cb + 61c244f commit ca9496f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 8 deletions.
35 changes: 35 additions & 0 deletions test/test-custom.4th
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
\ Copyright 2019 nomennescio

s" custom messages" describe#{

:noname ." Just passed" cr ; ^passed !
:noname ." Just failed" cr ; ^different !

s" short strings" it#{
<{ s" Hello World!" s# -> s" Hello World!" s# }>
<{ s" Hello Worlds!" s# -> s" Hello Worlds!" s# }>
}#
s" failing compares" it#{
<{ s" Hello World!" s# -> s" Hello Worlds!" s# }>
<{ s" Hello Worlds!" s# -> s" Hello Worlds! " s# }>
}#

2variable actual$
2variable expected$

:noname ." Got '" actual$ 2@ type ." ' as expected" cr ; ^passed !
:noname ." Expected '" expected$ 2@ type ." ', got '" actual$ 2@ type ." '" cr ; ^different !

: &actual 2dup actual$ 2! ;
: &expected 2dup expected$ 2! ;

s" short strings" it#{
<{ s" Hello World!" &actual s# -> s" Hello World!" &expected s# }>
<{ s" Hello Worlds!" &actual s# -> s" Hello Worlds!" &expected s# }>
}#
s" failing compares" it#{
<{ s" Hello World!" &actual s# -> s" Hello Worlds!" &expected s# }>
<{ s" Hello Worlds!" &actual s# -> s" Hello Worlds! " &expected s# }>
}#

}#
36 changes: 36 additions & 0 deletions test/test-custom.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

<DESCRIBE::>custom messages

<IT::>short strings

<PASSED::>Just passed

<PASSED::>Just passed

<COMPLETEDIN::>0.139 ms

<IT::>failing compares

<FAILED::>Just failed

<FAILED::>Just failed

<COMPLETEDIN::>0.162 ms

<IT::>short strings

<PASSED::>Got 'Hello World!' as expected

<PASSED::>Got 'Hello Worlds!' as expected

<COMPLETEDIN::>0.149 ms

<IT::>failing compares

<FAILED::>Expected 'Hello Worlds!', got 'Hello World!'

<FAILED::>Expected 'Hello Worlds! ', got 'Hello Worlds!'

<COMPLETEDIN::>0.150 ms

<COMPLETEDIN::>1.365 ms
11 changes: 11 additions & 0 deletions test/test-hash.4th
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
\ Copyright 2019 nomennescio
s" string hash" describe#{
s" short strings" it#{
<{ s" Hello World!" s# -> s" Hello World!" s# }>
<{ s" Hello Worlds!" s# -> s" Hello Worlds!" s# }>
}#
s" failing compares" it#{
<{ s" Hello World!" s# -> s" Hello Worlds!" s# }>
<{ s" Hello Worlds!" s# -> s" Hello Worlds! " s# }>
}#
}#
20 changes: 20 additions & 0 deletions test/test-hash.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<DESCRIBE::>string hash

<IT::>short strings

<PASSED::>Test Passed

<PASSED::>Test Passed

<COMPLETEDIN::>0.125 ms

<IT::>failing compares

<FAILED::>Expected 1266463317 , got 610024131

<FAILED::>Expected 1589968543 , got 1266463317

<COMPLETEDIN::>0.124 ms

<COMPLETEDIN::>0.292 ms
35 changes: 27 additions & 8 deletions ttester-codewars.4th
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,45 @@ create EXPECTED-RESULTS 32 cells allot
variable RESULTS
variable DIFFERENCES

variable ^passed
variable ^nresults
variable ^different

: passed$ ." Test Passed" cr ;
: different$ ." Expected "
RESULTS @ 0 +do EXPECTED-RESULTS i cells + @ . loop
." , got "
RESULTS @ 0 +do ACTUAL-RESULTS i cells + @ . loop
cr ;
: nresults$ ." Wrong number of results, expected " depth START-DEPTH @ - .
." , got " ACTUAL-DEPTH @ START-DEPTH @ - . cr ;

' passed$ ^passed !
' nresults$ ^nresults !
' different$ ^different !

: <{ T{ ;
: }>
depth ACTUAL-DEPTH @ = if
depth START-DEPTH @ > if
depth START-DEPTH @ - dup RESULTS ! 0 do
0 DIFFERENCES !
depth START-DEPTH @ - dup RESULTS ! 0 +do
dup EXPECTED-RESULTS i cells + !
ACTUAL-RESULTS i cells + @ <> DIFFERENCES +!
loop
DIFFERENCES @ if
failed# ." Expected "
RESULTS @ 0 do EXPECTED-RESULTS i cells + @ . loop
." , got "
RESULTS @ 0 do ACTUAL-RESULTS i cells + @ . loop
cr
failed# ^different @ execute
else
passed# ." Test Passed" cr
passed# ^passed @ execute
then
then
else
failed# ." Wrong number of results, expected " depth START-DEPTH @ - . ." , got " ACTUAL-DEPTH @ START-DEPTH @ - . cr
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 u -- hash } 0 1 c-addr u 0 +do { s } s c@ c# s char+ loop 2drop ; \ string hash

0 comments on commit ca9496f

Please sign in to comment.