Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed a bug in new_strings #45

Merged
merged 3 commits into from
Jul 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/searchers/bfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ pub fn bfs(input: &str) -> Option<String> {
None // short-circuits the iterator
}
MyResults::Continue(results_vec) => {
new_strings = results_vec
.into_iter()
.flat_map(|r| r.unencrypted_text)
.filter(|s| seen_strings.insert(s.clone()))
.collect();
new_strings.extend(
results_vec
.into_iter()
.flat_map(|r| r.unencrypted_text)
.filter(|s| seen_strings.insert(s.clone())),
);
Some(()) // indicate we want to continue processing
}
});
Expand Down Expand Up @@ -72,4 +73,21 @@ mod tests {
assert!(result.is_some());
assert!(result.unwrap() == "hello");
}

// Vector storing the strings to perform decoding in next iteraion
// had strings only from result of last decoding it performed.
// This was due to reassignment in try_for_each block
// which lead to unintended behaviour.
// We want strings from all results, so to fix it,
// we call .extend() to extend the vector.
// Link to forum https://discord.com/channels/754001738184392704/1002135076034859068
#[test]
fn non_deterministic_like_behaviour_regression_test() {
// text was too long, so we put \ to escape the \n
// and put the rest of string on next line.
let result = bfs("UFRCRVRWVkNiRlZMTVVkYVVFWjZVbFZPU0\
dGMU1WVlpZV2d4VkRVNWJWWnJjRzFVUlhCc1pYSlNWbHBPY0VaV1ZXeHJWRWd4TUZWdlZsWlg=");
assert!(result.is_some());
assert_eq!(result.unwrap(), "https://www.google.com");
}
}