2006-03-04 01:28:51 -05:00
|
|
|
require 'test/unit'
|
2008-06-05 10:57:05 -04:00
|
|
|
require_relative 'envutil'
|
2006-03-04 01:28:51 -05:00
|
|
|
|
|
|
|
class TestObjectSpace < Test::Unit::TestCase
|
|
|
|
def self.deftest_id2ref(obj)
|
|
|
|
/:(\d+)/ =~ caller[0]
|
|
|
|
file = $`
|
|
|
|
line = $1.to_i
|
2006-03-04 01:39:25 -05:00
|
|
|
code = <<"End"
|
2006-03-04 01:28:51 -05:00
|
|
|
define_method("test_id2ref_#{line}") {\
|
|
|
|
o = ObjectSpace._id2ref(obj.object_id);\
|
2006-09-16 11:30:49 -04:00
|
|
|
assert_same(obj, o, "didn't round trip: \#{obj.inspect}");\
|
2006-03-04 01:28:51 -05:00
|
|
|
}
|
|
|
|
End
|
2006-03-04 01:39:25 -05:00
|
|
|
eval code, binding, file, line
|
2006-03-04 01:28:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
deftest_id2ref(-0x4000000000000001)
|
|
|
|
deftest_id2ref(-0x4000000000000000)
|
|
|
|
deftest_id2ref(-0x40000001)
|
|
|
|
deftest_id2ref(-0x40000000)
|
|
|
|
deftest_id2ref(-1)
|
|
|
|
deftest_id2ref(0)
|
|
|
|
deftest_id2ref(1)
|
|
|
|
deftest_id2ref(0x3fffffff)
|
|
|
|
deftest_id2ref(0x40000000)
|
|
|
|
deftest_id2ref(0x3fffffffffffffff)
|
|
|
|
deftest_id2ref(0x4000000000000000)
|
|
|
|
deftest_id2ref(:a)
|
|
|
|
deftest_id2ref(:abcdefghijilkjl)
|
|
|
|
deftest_id2ref(:==)
|
|
|
|
deftest_id2ref(Object.new)
|
|
|
|
deftest_id2ref(self)
|
|
|
|
deftest_id2ref(true)
|
|
|
|
deftest_id2ref(false)
|
|
|
|
deftest_id2ref(nil)
|
2008-04-14 10:26:51 -04:00
|
|
|
|
|
|
|
def test_count_objects
|
|
|
|
h = {}
|
|
|
|
ObjectSpace.count_objects(h)
|
|
|
|
assert_kind_of(Hash, h)
|
|
|
|
assert(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
|
|
|
|
assert(h.values.all? {|x| x.is_a?(Integer) })
|
|
|
|
|
|
|
|
h = ObjectSpace.count_objects
|
|
|
|
assert_kind_of(Hash, h)
|
|
|
|
assert(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
|
|
|
|
assert(h.values.all? {|x| x.is_a?(Integer) })
|
|
|
|
|
|
|
|
assert_raise(TypeError) { ObjectSpace.count_objects(1) }
|
2008-05-31 10:03:23 -04:00
|
|
|
|
|
|
|
h0 = {:T_FOO=>1000}
|
|
|
|
h = ObjectSpace.count_objects(h0)
|
|
|
|
assert_same(h0, h)
|
|
|
|
assert_equal(0, h0[:T_FOO])
|
2008-04-14 10:26:51 -04:00
|
|
|
end
|
2008-06-05 10:57:05 -04:00
|
|
|
|
|
|
|
def test_finalizer
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(["-e", <<-END], "", %w(:ok :ok :ok :ok), [])
|
2008-06-05 10:57:05 -04:00
|
|
|
a = []
|
|
|
|
ObjectSpace.define_finalizer(a) { p :ok }
|
|
|
|
b = a.dup
|
|
|
|
ObjectSpace.define_finalizer(a) { p :ok }
|
2010-06-23 01:32:46 -04:00
|
|
|
!b
|
2008-06-05 10:57:05 -04:00
|
|
|
END
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_raise(ArgumentError) { ObjectSpace.define_finalizer([], Object.new) }
|
2008-06-05 10:57:05 -04:00
|
|
|
end
|
2012-12-03 05:03:25 -05:00
|
|
|
|
|
|
|
def test_each_object
|
|
|
|
GC.disable
|
|
|
|
eval('begin; 1.times{}; rescue; ensure; end')
|
|
|
|
arys = []
|
|
|
|
ObjectSpace.each_object(Array){|ary|
|
|
|
|
arys << ary
|
|
|
|
}
|
|
|
|
GC.enable
|
|
|
|
arys.each{|ary|
|
|
|
|
assert_equal(String, ary.inspect.class) # should not cause SEGV
|
|
|
|
}
|
|
|
|
end
|
2006-03-04 01:28:51 -05:00
|
|
|
end
|