-
Notifications
You must be signed in to change notification settings - Fork 194
Implement rb_hash_bulk_insert #3715
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,61 @@ | |
end | ||
end | ||
|
||
describe "rb_hash_bulk_insert" do | ||
it 'inserts key-value pairs into the hash' do | ||
arr = [:a, 1, :b, 2, :c, 3] | ||
hash = {} | ||
|
||
@s.rb_hash_bulk_insert(arr.length, arr, hash) | ||
|
||
hash.should == {a: 1, b: 2, c: 3} | ||
end | ||
|
||
it 'overwrites existing keys' do | ||
arr = [:a, 4, :b, 5, :c, 6] | ||
hash = {a: 1, b: 2} | ||
|
||
@s.rb_hash_bulk_insert(arr.length, arr, hash) | ||
|
||
hash.should == {a: 4, b: 5, c: 6} | ||
end | ||
|
||
it 'uses the last key in the array if it appears multiple times' do | ||
arr = [:a, 1, :b, 2, :a, 3] | ||
hash = {} | ||
|
||
@s.rb_hash_bulk_insert(arr.length, arr, hash) | ||
|
||
hash.should == {a: 3, b: 2} | ||
end | ||
|
||
it 'allows the array to be NULL if the length is zero' do | ||
hash = {} | ||
|
||
@s.rb_hash_bulk_insert(0, nil, hash) | ||
|
||
hash.should == {} | ||
end | ||
|
||
it 'does not include any keys after the given length' do | ||
arr = [:a, 1, :b, 2, :c, 3, :d, 4] | ||
hash = {} | ||
|
||
@s.rb_hash_bulk_insert(arr.length - 2, arr, hash) | ||
|
||
hash.should == {a: 1, b: 2, c: 3} | ||
end | ||
|
||
it 'does not modify the hash if the length is zero' do | ||
arr = [] | ||
hash = {a: 1, b: 2} | ||
|
||
@s.rb_hash_bulk_insert(arr.length, arr, hash) | ||
|
||
hash.should == {a: 1, b: 2} | ||
end | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add the following cases:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add tests for the other cases. As for when the length isn't even, it appears to be that if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right. Yes, it makes sense to do the same and to allow passing odd length. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't add a spec for that odd case, because specs should also pass with We could file an issue at https://bugs.ruby-lang.org/, I think it would make sense, it doesn't seem OK the C API reads past the length it's given. |
||
describe "rb_hash_size" do | ||
it "returns the size of the hash" do | ||
hsh = {fast: 'car', good: 'music'} | ||
|
Uh oh!
There was an error while loading. Please reload this page.