-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Refactor Heap Sort Implementation #705
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
Conversation
- Implement generic heap sort that can sort array in both ascending and descending order. - Parametrized tests using macros - Add file and function docstrings.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #705 +/- ##
==========================================
- Coverage 94.79% 94.78% -0.01%
==========================================
Files 298 298
Lines 22177 22161 -16
==========================================
- Hits 21022 21006 -16
Misses 1155 1155 ☔ View full report in Codecov by Sentry. |
- Test cases are defined as tuples containing the input vector and the expected output vector - The `run_test_case` function runs the sorting algorithm on the input vector twice: once in ascending order and once in descending order, and then asserts that the results match the expected output vectors - The `test_heap_sort` function iterates over each test case and runs the run_test_case function for each of them
- Use two pre-defined methods `is_sorted` and `is_descending_sorted` to assert sorted arrays in ascending and descending orders respectively - Remove predefined outputs in tests cases
Can you please have a look at this PR, @vil02? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about expressing test like that:
#[cfg(test)]
mod tests {
use crate::sorting::{have_same_elements, heap_sort, is_descending_sorted, is_sorted};
macro_rules! test_heap_sort {
($($name:ident: $input:expr,)*) => {
$(
#[test]
fn $name() {
let input_array = $input;
let mut arr_asc = input_array.clone();
heap_sort(&mut arr_asc, true);
assert!(is_sorted(&arr_asc) && have_same_elements(&arr_asc, &input_array));
let mut arr_dsc = input_array.clone();
heap_sort(&mut arr_dsc, false);
assert!(is_descending_sorted(&arr_dsc) && have_same_elements(&arr_dsc, &input_array));
}
)*
}
}
test_heap_sort! {
empty_array: Vec::<i32>::new(),
single_element_array: vec![5],
sorted: vec![1, 2, 3, 4, 5],
sorted_desc: vec![5, 4, 3, 2, 1, 0],
basic_0: vec![9, 8, 7, 6, 5],
basic_1: vec![8, 3, 1, 5, 7],
basic_2: vec![4, 5, 7, 1, 2, 3, 2, 8, 5, 4, 9, 9, 100, 1, 2, 3, 6, 4, 3],
duplicated_elements: vec![5, 5, 5, 5, 5],
strings: vec!["aa", "a", "ba", "ab"],
}
}
Please note that the current tests would pass if the input array would be always changed to an empty array.
Thanks! Your suggestion is more concise and definite. |
Description
Type of change
Checklist:
cargo clippy --all -- -D warnings
just before my last commit and fixed any issue that was found.cargo fmt
just before my last commit.cargo test
just before my last commit and all tests passed.mod.rs
file within its own folder, and in any parent folder(s).DIRECTORY.md
with the correct link.COUNTRIBUTING.md
and my code follows its guidelines.