puts"Testing if object id's can be reused by #{klass}..."
i=0
n=0
id_to_ref={}
failures=0
iterations=100000ifiterations<=0
iterations.timesdo
i+=1
obj=Object.new
ifid_to_ref.key?(obj.object_id)
n+=1
ref=id_to_ref[obj.object_id]
ifref.weakref_alive?
STDOUT.write('x')
STDOUT.flush
failures+=1
end
end
ref=klass.new(obj)
id_to_ref[obj.object_id]=ref
raise"#{klass} did not return the correct object!"unless(klass.name=="WeakReference"?ref.object:ref.__getobj__)==obj
ifi%5000==0
STDOUT.write('.')
STDOUT.flush
end
end
STDOUT.write("\n")
collected=n
id_to_ref.values.eachdo|ref|
collected+=1ifref.weakref_alive?
end
puts"#{collected} instances out of #{iterations} (#{((collected.to_f/iterations)*100).round}%) objects refererenced by #{klass} were reclaimed by the garbage collector"
iffailures==0
puts"SUCCESS: even with #{n} object id's being reused in #{iterations} iterations"
else
puts"FAILURE: #{failures} instances of object ids being incorrectly referenced in #{iterations} iterations"
end
end
if$0==__FILE__
iterations=ARGV.first.to_i
ifdefined?(WeakReference)
# Compatibilty hack to make WeakReference duck type like a WeakRef for the test.