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 Hash#shift when at capacity #5393

Merged
merged 3 commits into from Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions core/src/main/java/org/jruby/RubyHash.java
Expand Up @@ -1914,12 +1914,16 @@ public Throwable fillInStackTrace() {
@JRubyMethod(name = "shift")
public IRubyObject shift(ThreadContext context) {
modify();
IRubyObject key, value, lastKey;
IRubyObject key, value;

int start = this.start;
int end = this.end;
IRubyObject[] entries = this.entries;

key = entries[start * NUMBER_OF_ENTRIES];
value = entries[(start * NUMBER_OF_ENTRIES) + 1];

lastKey = entries[end * NUMBER_OF_ENTRIES];
if (key != lastKey) {
if (getLength() == end || key != entries[end * NUMBER_OF_ENTRIES]) {
RubyArray result = RubyArray.newArray(context.runtime, key, value);
internalDeleteEntry(key, value);
return result;
Expand Down
15 changes: 15 additions & 0 deletions spec/ruby/core/hash/shift_spec.rb
Expand Up @@ -61,4 +61,19 @@ def h.default(key)
lambda { HashSpecs.frozen_hash.shift }.should raise_error(frozen_error_class)
lambda { HashSpecs.empty_frozen_hash.shift }.should raise_error(frozen_error_class)
end

it "works when the hash is at capacity" do
# We try a wide range of sizes in hopes that this will cover all implementationss base Hash size.
results = []
1.upto(100) do |n|
h = {}
n.times do |i|
h[i] = i
end
h.shift
results << h.size
end

results.should == 0.upto(99).to_a
end
end