-
Notifications
You must be signed in to change notification settings - Fork 320
add test case for empty array #1504
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
add test case for empty array #1504
Conversation
Hey! So I'm not sure I agree with this... I think partitioning around a valid index (i.e.,
|
Oh I do think we should test it though, so thank you for that! |
I can't find any with a quick search, which makes me think that this hasn't been a major issue for users (and that we should therefore opt to use NumPy's behavior). One more argument: it seems odd to me that we'd want the behavior of a function to change when the user alters the length of a different axis from the one that they're operating on. It feels like the error would be a little "far away" from the function itself. There's a policy laid out by one of the original library authors somewhere (I can't find it right now) that says a function can only panic as a result of bad arguments, not as a result of the contents of an array (i.e., you can't panic if you encounter a certain value in an array, you'd have to return a |
@akern40 Hi! Thanks for the comments. How about modifying it so that if the input array is empty, the result array is returned immediately before performing the bound check? |
@NewBornRustacean I was on vacation but these look good! I just added some documentation, once the tests pass I'll merge this in. |
Add test cases for empty arrays in partition()
@akern40 what's up! thanks to follow up #1502 😃
I realize that you add check to empty array but if empty array comes in,
partition
goes panic for now.this PR is adding test cases for that cases(empty array).
if we prefer return
result
with empty array, then we need to change the bound check logic imo.Changes
test_partition_empty()
test case to validate behavior ofpartition()
with empty arraysArray1::zeros(0)
)Array2::zeros((0, 3))
)Array2::zeros((2, 0))
)Why Panic?
The
partition()
method should panic when called on empty arrays for several reasons:Logical Consistency: Partitioning requires a
kth
element to partition around, but empty arrays have no elements to use as a pivot. This makes the operation undefined for empty arrays.Mathematical Correctness: The partition operation's postcondition requires that:
kth
index are ≤ thekth
elementkth
index are ≥ thekth
elementThis condition cannot be satisfied with an empty array since there is no
kth
element.Safety: Allowing partition on empty arrays could lead to undefined behavior or incorrect results in algorithms that rely on the partitioning postcondition.
Consistency with Other Operations: Many array operations (like indexing, slicing) panic on empty arrays when the operation is undefined, making this behavior consistent with the rest of the API.