Skip to content

Add some missing tests #392

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Add some missing tests #392

wants to merge 4 commits into from

Conversation

AbeZbm
Copy link

@AbeZbm AbeZbm commented May 24, 2025

Hi,

Thanks for your time to review this PR.

By examining the existing code, we found that some tests can be added to improve the repo's overall test coverage. We've added some tests to cover missing unit tests in slice.rs, map.rs and set.rs. The tests we submitted have been carefully curated by us to ensure their behavior and effectiveness.

Thanks again for reviewing.

@AbeZbm
Copy link
Author

AbeZbm commented May 28, 2025

@cuviper Hello! I was wondering if there's any feedback on this PR. I've ensured all checks are passing and I'm happy to address any concerns or make changes as needed. Thank you!

Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few places where I suggest changes that would apply elsewhere as well, so please do. (manually or by LLM is up to you)

@AbeZbm
Copy link
Author

AbeZbm commented Jun 3, 2025

@cuviper Thanks for your correction and thanks for your time to review my PR!😊 All feedback has been addressed, and I've marked the relevant comments as resolved. Please let me know if there are any further questions!

@AbeZbm AbeZbm requested a review from cuviper June 4, 2025 12:59
@cuviper
Copy link
Member

cuviper commented Jun 4, 2025

I will review again when I get a chance, but please stop pinging so much. You've repeated nearly the same comment now 4 times, which I guess you deleted and reposted. I see no urgency just for test coverage.

assert_eq!(result, Some((3, 4)));
assert_eq!(map.len(), 4);

map.swap_remove(&5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This swap is a bad choice, because it makes the other shifts irrelevant. That is, I can change all of those other shifts to swap (e.g. as if the shift was implemented incorrectly), and the expected values here still pass!

Just forget this line, and the rest could be more reasonable.

  • shift_remove_index(1) -> (3, 4)
  • shift_remove_index(1) -> (5, 6) -- because it shifted to fill the gap)
  • shift_remove_index(2) -> (9, 10) -- because the last was never swapped, only shifted down twice, and it also tests that shift-removing the last entry is fine
  • shift_remove_index(2) -> None

assert_eq!(map.len(), 4);

map.swap_remove(&5);
assert_eq!(map.as_slice(), &[(1, 2), (9, 10), (7, 8)]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, the swap_remove makes it so we can't tell whether the first one really shifted or swapped. Remove that and you can compare the full shifted order instead.

assert_eq!(result, Some((1, 3, 4)));
assert_eq!(map.len(), 4);

map.swap_remove(&5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, don't use swap when testing shifts.

assert_eq!(slice, &mut [(3, 30), (4, 40)]);

for i in 0..slice.len() {
slice.entries[i].value += 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slice.entries[i].value += 1;
slice[i] += 1;

index_set.insert(10);
index_set.insert(20);
index_set.insert(30);
assert_eq!(index_set.capacity(), 3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too dependent on the growth strategy and load factor, which are implementation details of both hashbrown and this crate combined, but it would be ok if the test calls shrink_to_fit() first. We still can't be sure indices table won't have higher capacity, but the entries vec will shrink to the exact capacity, and we report the lesser.

The rest of the test is ok for the same reason -- the vec will grow exactly even if the indices table doesn't.

set.insert(3);

let drain = set.drain(0..2);
assert_eq!(drain.as_slice(), &[1, 2]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be worth testing the output of the drain iterator, and the state of the set afterward.

Comment on lines +693 to +694
let mut set: IndexSet<i32> = IndexSet::new();
set.extend(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need a vec - just convert a direct array.

Suggested change
let mut set: IndexSet<i32> = IndexSet::new();
set.extend(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let mut set: IndexSet<i32> = IndexSet::from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

This or similar can be applied in a few of the new tests.

Comment on lines +722 to +724
let mut set: IndexSet<i32> = IndexSet::new();
set.extend(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
set.truncate(5);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut set: IndexSet<i32> = IndexSet::new();
set.extend(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
set.truncate(5);
let mut set: IndexSet<i32> = IndexSet::from([1, 2, 3, 4, 5]);

set.truncate(5);
let split_set: IndexSet<i32> = set.split_off(3);
assert_eq!(split_set.len(), 2);
assert_eq!(set.len(), 3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about checking the contents of each as well.

Comment on lines +801 to +803
set.insert(6);

set.pop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems irrelevant to insert and immediately pop here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants