PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test/mri/ruby/test_objectspace.rb

http://github.com/jruby/jruby
Ruby | 160 lines | 154 code | 5 blank | 1 comment | 2 complexity | ebd14ca3360be6bd5c55991979d99fd5 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. class TestObjectSpace < Test::Unit::TestCase
  4. def self.deftest_id2ref(obj)
  5. /:(\d+)/ =~ caller[0]
  6. file = $`
  7. line = $1.to_i
  8. code = <<"End"
  9. define_method("test_id2ref_#{line}") {\
  10. o = ObjectSpace._id2ref(obj.object_id);\
  11. assert_same(obj, o, "didn't round trip: \#{obj.inspect}");\
  12. }
  13. End
  14. eval code, binding, file, line
  15. end
  16. deftest_id2ref(-0x4000000000000001)
  17. deftest_id2ref(-0x4000000000000000)
  18. deftest_id2ref(-0x40000001)
  19. deftest_id2ref(-0x40000000)
  20. deftest_id2ref(-1)
  21. deftest_id2ref(0)
  22. deftest_id2ref(1)
  23. deftest_id2ref(0x3fffffff)
  24. deftest_id2ref(0x40000000)
  25. deftest_id2ref(0x3fffffffffffffff)
  26. deftest_id2ref(0x4000000000000000)
  27. deftest_id2ref(:a)
  28. deftest_id2ref(:abcdefghijilkjl)
  29. deftest_id2ref(:==)
  30. deftest_id2ref(Object.new)
  31. deftest_id2ref(self)
  32. deftest_id2ref(true)
  33. deftest_id2ref(false)
  34. deftest_id2ref(nil)
  35. def test_count_objects
  36. h = {}
  37. ObjectSpace.count_objects(h)
  38. assert_kind_of(Hash, h)
  39. assert_empty(h.keys.delete_if {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
  40. assert_empty(h.values.delete_if {|x| x.is_a?(Integer) })
  41. h = ObjectSpace.count_objects
  42. assert_kind_of(Hash, h)
  43. assert_empty(h.keys.delete_if {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
  44. assert_empty(h.values.delete_if {|x| x.is_a?(Integer) })
  45. assert_raise(TypeError) { ObjectSpace.count_objects(1) }
  46. h0 = {:T_FOO=>1000}
  47. h = ObjectSpace.count_objects(h0)
  48. assert_same(h0, h)
  49. assert_equal(0, h0[:T_FOO])
  50. end
  51. def test_finalizer
  52. assert_in_out_err(["-e", <<-END], "", %w(:ok :ok :ok :ok), [])
  53. a = []
  54. ObjectSpace.define_finalizer(a) { p :ok }
  55. b = a.dup
  56. ObjectSpace.define_finalizer(a) { p :ok }
  57. !b
  58. END
  59. assert_raise(ArgumentError) { ObjectSpace.define_finalizer([], Object.new) }
  60. code = proc do |priv|
  61. <<-"CODE"
  62. fin = Object.new
  63. class << fin
  64. #{priv}def call(id)
  65. puts "finalized"
  66. end
  67. end
  68. ObjectSpace.define_finalizer([], fin)
  69. CODE
  70. end
  71. assert_in_out_err([], code[""], ["finalized"])
  72. assert_in_out_err([], code["private "], ["finalized"])
  73. c = EnvUtil.labeled_class("C\u{3042}").new
  74. o = Object.new
  75. assert_raise_with_message(ArgumentError, /C\u{3042}/) {
  76. ObjectSpace.define_finalizer(o, c)
  77. }
  78. end
  79. def test_each_object
  80. klass = Class.new
  81. new_obj = klass.new
  82. found = []
  83. count = ObjectSpace.each_object(klass) do |obj|
  84. found << obj
  85. end
  86. assert_equal(1, count)
  87. assert_equal(1, found.size)
  88. assert_same(new_obj, found[0])
  89. end
  90. def test_each_object_enumerator
  91. klass = Class.new
  92. new_obj = klass.new
  93. found = []
  94. counter = ObjectSpace.each_object(klass)
  95. assert_equal(1, counter.each {|obj| found << obj})
  96. assert_equal(1, found.size)
  97. assert_same(new_obj, found[0])
  98. end
  99. def test_each_object_no_gabage
  100. assert_separately([], <<-End)
  101. GC.disable
  102. eval('begin; 1.times{}; rescue; ensure; end')
  103. arys = []
  104. ObjectSpace.each_object(Array){|ary|
  105. arys << ary
  106. }
  107. GC.enable
  108. arys.each{|ary|
  109. begin
  110. assert_equal(String, ary.inspect.class) # should not cause SEGV
  111. rescue RuntimeError
  112. # rescue "can't modify frozen File" error.
  113. end
  114. }
  115. End
  116. end
  117. def test_each_object_recursive_key
  118. assert_normal_exit(<<-'end;', '[ruby-core:66742] [Bug #10579]')
  119. h = {["foo"]=>nil}
  120. p Thread.current[:__recursive_key__]
  121. end;
  122. end
  123. def test_each_object_singleton_class
  124. assert_separately([], <<-End)
  125. class C
  126. class << self
  127. $c = self
  128. end
  129. end
  130. exist = false
  131. ObjectSpace.each_object(Class){|o|
  132. exist = true if $c == o
  133. }
  134. assert(exist, 'Bug #11360')
  135. End
  136. klass = Class.new
  137. instance = klass.new
  138. sclass = instance.singleton_class
  139. meta = klass.singleton_class
  140. assert_kind_of(meta, sclass)
  141. assert_include(ObjectSpace.each_object(meta).to_a, sclass)
  142. end
  143. end