Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Improve StringShrinker algorithm#377

Merged
sksamuel merged 1 commit intokotest:masterfromajalt:bisect-string-shrink
Jul 15, 2018

Conversation

ajalt
Copy link
Contributor

The current string shrink algorithm generates candidates by dropping single characters from the end of the input. This doesn't produce the smallest case, since it doesn't drop characters from the start of the string. It is also linear on the size of the input, so it requires a potentially large number of tries to reach the result it does produce.

This PR changes to algorithm to bisect the input string from both directions, producing a minimal output in log n tries.

As an example of the current behavior, the test:

forAll { it: String -> !it.contains("#") }

produces output like this:

Attempting to shrink failed arg DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!) e|Gr+ tri7jw{Shrink #1: <empty string> passShrink #2: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!) e|Gr+ tri7 failShrink #3: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!) e|Gr+ failShrink #4: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!)  failShrink #5: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQx failShrink #6: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k failShrink #7: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x, failShrink #8: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B* failShrink #9: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn failShrink #10: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2 failShrink #11: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M# failShrink #12: DPn CPQ7hBAY&LP;7MxPtN^Oy\$ passShrink #13: DPn CPQ7hBAY&LP;7MxPtN^Oy\$F passShrink #14: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz passShrink #15: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz; passShrink #16: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M passShrink #17: aaaaaDPn CPQ7hBAY&LP;7MxPtN^Oy\$ passShrink #18: aaaaDPn CPQ7hBAY&LP;7MxPtN^Oy\$F passShrink #19: aaaDPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz passShrink #20: aaDPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz; passShrink #21: aDPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M passShrink result => DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#

This is 21 attempts to produce a string 32x longer than optimal.

The new algorithm produces output like this:

Attempting to shrink failed arg t ^j>t\o,x3?eb9#F'>g>vGQ-N}nkxShrink #1: <empty string> passShrink #2: t ^j>t\o,x3?eb9 passShrink #3: #F'>g>vGQ-N}nkx failShrink #4: #F'>g>vG failShrink #5: #F'> failShrink #6: #F failShrink #7: # failShrink result => #

This is only 7 tries, and produces the correct output of"#"

The current string shrink algorithm generates candidates by dropping single characters from the end of the input. This doesn't produce the smallest case, since it doesn't drop characters from the start of the string. It is also linear on the size of the input, so it requires a potentially large number of tries to reach the result it does produce.This PR changes to algorithm to bisect the input string from both directions, producing a minimal output in log n tries.As an example of the current behavior, the test:`forAll { it: String -> !it.contains("#") }`produces output like this:```Attempting to shrink failed arg DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!) e|Gr+ tri7jw{Shrink#1: <empty string> passShrink#2: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!) e|Gr+ tri7 failShrink#3: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!) e|Gr+ failShrink#4: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQxU+!)  failShrink#5: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k<}QQx failShrink#6: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x,L;6&k failShrink#7: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B*w,1x, failShrink#8: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn18+B* failShrink#9: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2yEcNn failShrink#10: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#EC\M2 failShrink#11: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M# failShrink#12: DPn CPQ7hBAY&LP;7MxPtN^Oy\$ passShrink#13: DPn CPQ7hBAY&LP;7MxPtN^Oy\$F passShrink#14: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz passShrink#15: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz; passShrink#16: DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M passShrink#17: aaaaaDPn CPQ7hBAY&LP;7MxPtN^Oy\$ passShrink#18: aaaaDPn CPQ7hBAY&LP;7MxPtN^Oy\$F passShrink#19: aaaDPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz passShrink#20: aaDPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz; passShrink#21: aDPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M passShrink result => DPn CPQ7hBAY&LP;7MxPtN^Oy\$Fz;M#```This is 21 attempts to produce a string 32x longer than optimal.The new algorithm produces output like this:```Attempting to shrink failed arg t ^j>t\o,x3?eb9#F'>g>vGQ-N}nkxShrink#1: <empty string> passShrink#2: t ^j>t\o,x3?eb9 passShrink#3: #F'>g>vGQ-N}nkx failShrink#4: #F'>g>vG failShrink#5: #F'> failShrink#6: #F failShrink#7: # failShrink result => #```This is only 7 tries, and produces the correct output of `"#"`
@sksamuel
Copy link
Member

Brilliant.

@sksamuelsksamuel merged commit477a293 intokotest:masterJul 15, 2018
@ajaltajalt deleted the bisect-string-shrink branchJuly 15, 2018 16:17
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@ajalt@sksamuel

[8]ページ先頭

©2009-2025 Movatter.jp