From 0b2276805b2a618756289585c238aaef5e60c951 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Thu, 1 Aug 2019 23:50:22 +0200 Subject: [PATCH 1/9] Use string hash for comparing strings --- test/test-hash.4th | 11 +++++++++++ test/test-hash.expected | 20 ++++++++++++++++++++ ttester-codewars.4th | 2 ++ 3 files changed, 33 insertions(+) create mode 100644 test/test-hash.4th create mode 100644 test/test-hash.expected diff --git a/test/test-hash.4th b/test/test-hash.4th new file mode 100644 index 0000000..f3d0f13 --- /dev/null +++ b/test/test-hash.4th @@ -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# }> + }# +}# diff --git a/test/test-hash.expected b/test/test-hash.expected new file mode 100644 index 0000000..1c61140 --- /dev/null +++ b/test/test-hash.expected @@ -0,0 +1,20 @@ + +string hash + +short strings + +Test Passed + +Test Passed + +0.098 ms + +failing compares + +Expected 431880812 , got 390772129 + +Expected 143064460 , got 431880812 + +0.118 ms + +0.258 ms diff --git a/ttester-codewars.4th b/ttester-codewars.4th index 77bcd04..5605276 100644 --- a/ttester-codewars.4th +++ b/ttester-codewars.4th @@ -38,3 +38,5 @@ variable DIFFERENCES then EMPTY-STACK F} ; + +: s# { c-addr u -- hash } 1000000009 { m } 0 1 c-addr u 0 +do { hash p s } s c@ p * hash + m mod p 53 * m mod s char+ loop 2drop ; \ No newline at end of file From bf1d7f88bf19eb9393dce57dd461137cab6b4bc9 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Thu, 1 Aug 2019 23:50:22 +0200 Subject: [PATCH 2/9] Custom messages in string hash tests --- test/test-hash.4th | 4 ++++ test/test-hash.expected | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/test/test-hash.4th b/test/test-hash.4th index f3d0f13..1192e0c 100644 --- a/test/test-hash.4th +++ b/test/test-hash.4th @@ -8,4 +8,8 @@ s" string hash" describe#{ <{ s" Hello World!" s# -> s" Hello Worlds!" s# }> <{ s" Hello Worlds!" s# -> s" Hello Worlds! " s# }> }# + s" failing compares with custom messages" it#{ + <{ s" Hello World!" 2dup ." Actual: '" type ." '" s# -> s" Hello Worlds!" 2dup ." , Expected: '" type ." '" s# }> + <{ s" Hello Worlds!" 2dup ." Actual: '" type ." '" s# -> s" Hello Worlds! " 2dup ." , Expected: '" type ." '" s# }> + }# }# diff --git a/test/test-hash.expected b/test/test-hash.expected index 1c61140..261bef3 100644 --- a/test/test-hash.expected +++ b/test/test-hash.expected @@ -7,7 +7,7 @@ Test Passed -0.098 ms +0.077 ms failing compares @@ -15,6 +15,14 @@ Expected 143064460 , got 431880812 -0.118 ms +0.093 ms -0.258 ms +failing compares with custom messages +Actual: 'Hello World!', Expected: 'Hello Worlds!' +Expected 431880812 , got 390772129 +Actual: 'Hello Worlds!', Expected: 'Hello Worlds! ' +Expected 143064460 , got 431880812 + +0.167 ms + +0.386 ms From 5273cef5fdd56d4a0d15db93c6e19c85ef55df57 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Fri, 2 Aug 2019 10:46:24 +0200 Subject: [PATCH 3/9] Refactor and comment --- ttester-codewars.4th | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ttester-codewars.4th b/ttester-codewars.4th index 5605276..8d9741a 100644 --- a/ttester-codewars.4th +++ b/ttester-codewars.4th @@ -39,4 +39,7 @@ variable DIFFERENCES EMPTY-STACK F} ; -: s# { c-addr u -- hash } 1000000009 { m } 0 1 c-addr u 0 +do { hash p s } s c@ p * hash + m mod p 53 * m mod s char+ loop 2drop ; \ No newline at end of file +1000000009 constant #m \ prime number < 2^32 +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 \ No newline at end of file From 9e7149346ba6c771a2d71295033ac2a353317a1b Mon Sep 17 00:00:00 2001 From: nomennescio Date: Fri, 2 Aug 2019 11:03:46 +0200 Subject: [PATCH 4/9] Use larger prime --- test/test-hash.expected | 16 ++++++++-------- ttester-codewars.4th | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test-hash.expected b/test/test-hash.expected index 261bef3..bdc716f 100644 --- a/test/test-hash.expected +++ b/test/test-hash.expected @@ -7,22 +7,22 @@ Test Passed -0.077 ms +0.081 ms failing compares -Expected 431880812 , got 390772129 +Expected 1138466699 , got 297433350 -Expected 143064460 , got 431880812 +Expected 1662069178 , got 1138466699 -0.093 ms +0.118 ms failing compares with custom messages Actual: 'Hello World!', Expected: 'Hello Worlds!' -Expected 431880812 , got 390772129 +Expected 1138466699 , got 297433350 Actual: 'Hello Worlds!', Expected: 'Hello Worlds! ' -Expected 143064460 , got 431880812 +Expected 1662069178 , got 1138466699 -0.167 ms +0.151 ms -0.386 ms +0.397 ms diff --git a/ttester-codewars.4th b/ttester-codewars.4th index 8d9741a..ab73429 100644 --- a/ttester-codewars.4th +++ b/ttester-codewars.4th @@ -39,7 +39,7 @@ variable DIFFERENCES EMPTY-STACK F} ; -1000000009 constant #m \ prime number < 2^32 +2147483659 constant #m \ prime number < 2^32 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 \ No newline at end of file From ec3d0cde4cd491d5ef3d9884b282a64725806b39 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Fri, 2 Aug 2019 11:16:26 +0200 Subject: [PATCH 5/9] Squeeze in some extra bits of information --- test/test-hash.expected | 16 ++++++++-------- ttester-codewars.4th | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test-hash.expected b/test/test-hash.expected index bdc716f..24d1087 100644 --- a/test/test-hash.expected +++ b/test/test-hash.expected @@ -7,22 +7,22 @@ Test Passed -0.081 ms +0.211 ms failing compares -Expected 1138466699 , got 297433350 +Expected 1266463317 , got 610024131 -Expected 1662069178 , got 1138466699 +Expected 1589968543 , got 1266463317 -0.118 ms +0.206 ms failing compares with custom messages Actual: 'Hello World!', Expected: 'Hello Worlds!' -Expected 1138466699 , got 297433350 +Expected 1266463317 , got 610024131 Actual: 'Hello Worlds!', Expected: 'Hello Worlds! ' -Expected 1662069178 , got 1138466699 +Expected 1589968543 , got 1266463317 -0.151 ms +0.344 ms -0.397 ms +0.864 ms diff --git a/ttester-codewars.4th b/ttester-codewars.4th index ab73429..e4dad28 100644 --- a/ttester-codewars.4th +++ b/ttester-codewars.4th @@ -39,7 +39,7 @@ variable DIFFERENCES EMPTY-STACK F} ; -2147483659 constant #m \ prime number < 2^32 +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 \ No newline at end of file From 5e7c6a90d8e30932d5a99f631dae10eb8fd3d298 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Fri, 2 Aug 2019 22:18:37 +0200 Subject: [PATCH 6/9] Refactor messages --- ttester-codewars.4th | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/ttester-codewars.4th b/ttester-codewars.4th index e4dad28..a57b227 100644 --- a/ttester-codewars.4th +++ b/ttester-codewars.4th @@ -15,26 +15,39 @@ 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 + 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} ; From 48f4475726acee0f7cc645dc5c89809e59eeb9a8 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Fri, 2 Aug 2019 23:39:18 +0200 Subject: [PATCH 7/9] Fix --- ttester-codewars.4th | 1 + 1 file changed, 1 insertion(+) diff --git a/ttester-codewars.4th b/ttester-codewars.4th index a57b227..8553ffb 100644 --- a/ttester-codewars.4th +++ b/ttester-codewars.4th @@ -36,6 +36,7 @@ variable ^different : }> 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 +! From 5bb197292cf9b5a34b22dfc7a6fd9086cd554105 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Fri, 2 Aug 2019 23:39:51 +0200 Subject: [PATCH 8/9] Only test string hash --- test/test-hash.4th | 4 ---- test/test-hash.expected | 14 +++----------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/test/test-hash.4th b/test/test-hash.4th index 1192e0c..f3d0f13 100644 --- a/test/test-hash.4th +++ b/test/test-hash.4th @@ -8,8 +8,4 @@ s" string hash" describe#{ <{ s" Hello World!" s# -> s" Hello Worlds!" s# }> <{ s" Hello Worlds!" s# -> s" Hello Worlds! " s# }> }# - s" failing compares with custom messages" it#{ - <{ s" Hello World!" 2dup ." Actual: '" type ." '" s# -> s" Hello Worlds!" 2dup ." , Expected: '" type ." '" s# }> - <{ s" Hello Worlds!" 2dup ." Actual: '" type ." '" s# -> s" Hello Worlds! " 2dup ." , Expected: '" type ." '" s# }> - }# }# diff --git a/test/test-hash.expected b/test/test-hash.expected index 24d1087..4f6fc88 100644 --- a/test/test-hash.expected +++ b/test/test-hash.expected @@ -7,7 +7,7 @@ Test Passed -0.211 ms +0.125 ms failing compares @@ -15,14 +15,6 @@ Expected 1589968543 , got 1266463317 -0.206 ms +0.124 ms -failing compares with custom messages -Actual: 'Hello World!', Expected: 'Hello Worlds!' -Expected 1266463317 , got 610024131 -Actual: 'Hello Worlds!', Expected: 'Hello Worlds! ' -Expected 1589968543 , got 1266463317 - -0.344 ms - -0.864 ms +0.292 ms From 61c244fc67bb7cbf816dd09c2745091ad7972275 Mon Sep 17 00:00:00 2001 From: nomennescio Date: Fri, 2 Aug 2019 23:42:31 +0200 Subject: [PATCH 9/9] Test custom messages --- test/test-custom.4th | 35 +++++++++++++++++++++++++++++++++++ test/test-custom.expected | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 test/test-custom.4th create mode 100644 test/test-custom.expected diff --git a/test/test-custom.4th b/test/test-custom.4th new file mode 100644 index 0000000..0ec04b1 --- /dev/null +++ b/test/test-custom.4th @@ -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# }> + }# + +}# diff --git a/test/test-custom.expected b/test/test-custom.expected new file mode 100644 index 0000000..c966c57 --- /dev/null +++ b/test/test-custom.expected @@ -0,0 +1,36 @@ + +custom messages + +short strings + +Just passed + +Just passed + +0.139 ms + +failing compares + +Just failed + +Just failed + +0.162 ms + +short strings + +Got 'Hello World!' as expected + +Got 'Hello Worlds!' as expected + +0.149 ms + +failing compares + +Expected 'Hello Worlds!', got 'Hello World!' + +Expected 'Hello Worlds! ', got 'Hello Worlds!' + +0.150 ms + +1.365 ms