summaryrefslogtreecommitdiff
path: root/spec/ruby/library/objectspace
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2019-05-28 22:41:48 +0200
committerBenoit Daloze <eregontp@gmail.com>2019-05-28 22:41:48 +0200
commita66bc2c01194a9c017c874a30db5b3b6bd95e966 (patch)
tree598d6375b44fd86f90c3477c73086f6fcf08d76c /spec/ruby/library/objectspace
parentd070523e7be4b95914adeef9a10401fba7718c5a (diff)
Update to ruby/spec@9a501a8
Diffstat (limited to 'spec/ruby/library/objectspace')
-rw-r--r--spec/ruby/library/objectspace/memsize_of_spec.rb30
-rw-r--r--spec/ruby/library/objectspace/reachable_objects_from_spec.rb61
2 files changed, 91 insertions, 0 deletions
diff --git a/spec/ruby/library/objectspace/memsize_of_spec.rb b/spec/ruby/library/objectspace/memsize_of_spec.rb
new file mode 100644
index 0000000..06a80d9
--- /dev/null
+++ b/spec/ruby/library/objectspace/memsize_of_spec.rb
@@ -0,0 +1,30 @@
+require_relative '../../spec_helper'
+require 'objspace'
+
+describe "ObjectSpace.memsize_of" do
+ it "returns 0 for true, false and nil" do
+ ObjectSpace.memsize_of(true).should == 0
+ ObjectSpace.memsize_of(false).should == 0
+ ObjectSpace.memsize_of(nil).should == 0
+ end
+
+ it "returns 0 for small Integers" do
+ ObjectSpace.memsize_of(42).should == 0
+ end
+
+ it "returns an Integer for an Object" do
+ obj = Object.new
+ ObjectSpace.memsize_of(obj).should be_kind_of(Integer)
+ ObjectSpace.memsize_of(obj).should > 0
+ end
+
+ it "is larger if the Object has more instance variables" do
+ obj = Object.new
+ before = ObjectSpace.memsize_of(obj)
+ 100.times do |i|
+ obj.instance_variable_set(:"@foo#{i}", nil)
+ end
+ after = ObjectSpace.memsize_of(obj)
+ after.should > before
+ end
+end
diff --git a/spec/ruby/library/objectspace/reachable_objects_from_spec.rb b/spec/ruby/library/objectspace/reachable_objects_from_spec.rb
new file mode 100644
index 0000000..7e70bc8
--- /dev/null
+++ b/spec/ruby/library/objectspace/reachable_objects_from_spec.rb
@@ -0,0 +1,61 @@
+require_relative '../../spec_helper'
+require 'objspace'
+
+describe "ObjectSpace.reachable_objects_from" do
+ it "returns nil for true and false" do
+ ObjectSpace.reachable_objects_from(true).should == nil
+ ObjectSpace.reachable_objects_from(false).should == nil
+ end
+
+ it "returns nil for nil" do
+ ObjectSpace.reachable_objects_from(nil).should == nil
+ end
+
+ it "returns nil for small Integers" do
+ ObjectSpace.reachable_objects_from(42).should == nil
+ end
+
+ it "enumerates objects directly reachable from a given object" do
+ ObjectSpace.reachable_objects_from(['a', 'b', 'c']).should include(Array, 'a', 'b', 'c')
+ ObjectSpace.reachable_objects_from(Object.new).should == [Object]
+ end
+
+ it "finds an object stored in an Array" do
+ obj = Object.new
+ ary = [obj]
+ reachable = ObjectSpace.reachable_objects_from(ary)
+ reachable.should include(obj)
+ end
+
+ it "finds an object stored in a copy-on-write Array" do
+ removed = Object.new
+ obj = Object.new
+ ary = [removed, obj]
+ ary.shift
+ reachable = ObjectSpace.reachable_objects_from(ary)
+ reachable.should include(obj)
+ reachable.should_not include(removed)
+ end
+
+ it "finds an object stored in a Queue" do
+ require 'thread'
+ o = Object.new
+ q = Queue.new
+ q << o
+
+ reachable = ObjectSpace.reachable_objects_from(q)
+ reachable = reachable + reachable.flat_map { |r| ObjectSpace.reachable_objects_from(r) }
+ reachable.should include(o)
+ end
+
+ it "finds an object stored in a SizedQueue" do
+ require 'thread'
+ o = Object.new
+ q = SizedQueue.new(3)
+ q << o
+
+ reachable = ObjectSpace.reachable_objects_from(q)
+ reachable = reachable + reachable.flat_map { |r| ObjectSpace.reachable_objects_from(r) }
+ reachable.should include(o)
+ end
+end