Implemented ObjectSpace.count_objects to count the number of allocated objects for each type

This commit is contained in:
Ryan Scott
2013-05-12 18:42:21 +10:00
parent 37e3643f8f
commit 0d4227ac04
5 changed files with 146 additions and 2 deletions
+21
View File
@@ -86,6 +86,19 @@ def assert_true(ret, msg = nil, diff = nil)
ret
end
def assert_false(ret, msg = nil, diff = nil)
if $mrbtest_assert
$mrbtest_assert_idx += 1
if ret
msg = "Expected #{ret.inspect} to be false" unless msg
diff = assertion_diff(false, ret) unless diff
$mrbtest_assert.push([$mrbtest_assert_idx, msg, diff])
end
end
!ret
end
def assert_equal(exp, act, msg = nil)
msg = "Expected to be equal" unless msg
diff = assertion_diff(exp, act)
@@ -135,6 +148,14 @@ def assert_raise(*exp)
ret
end
##
# Fails unless +obj+ is a kind of +cls+.
def assert_kind_of(cls, obj, msg = nil)
msg = "Expected #{obj.inspect} to be a kind of #{cls}, not #{obj.class}" unless msg
diff = assertion_diff(cls, obj.class)
assert_true(obj.kind_of?(cls), msg, diff)
end
##
# Report the test result and print all assertions
# which were reported broken.
+19
View File
@@ -0,0 +1,19 @@
assert('ObjectSpace.count_objects') do
h = {}
ObjectSpace.count_objects(h)
assert_kind_of(Hash, h)
assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
assert_true(h.values.all? {|x| x.is_a?(Integer) })
h = ObjectSpace.count_objects
assert_kind_of(Hash, h)
assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
assert_true(h.values.all? {|x| x.is_a?(Integer) })
assert_raise(TypeError) { ObjectSpace.count_objects(1) }
h0 = {:MRB_TT_FOO=>1000}
h = ObjectSpace.count_objects(h0)
assert_false(h0.has_key?(:MRB_TT_FOO))
end