Skip to content

Commit da1b2f5

Browse files
committed
Add tests for capacity of new arrays.
1 parent 8b38efa commit da1b2f5

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

spec/truffle/array/new_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 1.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "Array.new" do
12+
def storage(ary)
13+
Truffle::Debug.array_storage(ary)
14+
end
15+
16+
def capacity(ary)
17+
Truffle::Debug.array_capacity(ary)
18+
end
19+
20+
before :each do
21+
@long = 1 << 52
22+
end
23+
24+
it "creates arrays with the requested capacity" do
25+
a = Array.new(17)
26+
capacity(a).should == 17
27+
end
28+
29+
it "creates arrays with the requested capacity with a default value" do
30+
a = Array.new(17, 1)
31+
capacity(a).should == 17
32+
end
33+
34+
it "creates arrays with the requested capacity with a value block" do
35+
a = Array.new(17) { 1 }
36+
capacity(a).should == 17
37+
end
38+
end
39+

src/main/java/org/truffleruby/debug/TruffleDebugNodes.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,17 @@ public DynamicObject arrayStorage(DynamicObject array) {
295295

296296
}
297297

298+
@CoreMethod(names = "array_capacity", onSingleton = true, required = 1)
299+
public abstract static class ArrayCapacityNode extends CoreMethodArrayArgumentsNode {
300+
301+
@TruffleBoundary
302+
@Specialization(guards = "isRubyArray(array)")
303+
public int arrayStorage(DynamicObject array) {
304+
return ArrayStrategy.of(array).capacityNode().execute(Layouts.ARRAY.getStore(array));
305+
}
306+
307+
}
308+
298309
@CoreMethod(names = "hash_storage", onSingleton = true, required = 1)
299310
public abstract static class HashStorageNode extends CoreMethodArrayArgumentsNode {
300311

0 commit comments

Comments
 (0)