Commit c907bd89 authored by Guido van Rossum's avatar Guido van Rossum

The error reporting here was a bit sparse. In verbose mode, the code

in run_test() referenced two non-existent variables, and in
non-verbose mode, the tests didn't report the actual number, when it
differed from the expected number.  Fixed this.

Also added an extra call to gc.collect() at the start of test_all().
This will be needed when I check in the changes to add GC to new-style
classes.
parent b855134a
...@@ -2,35 +2,35 @@ from test_support import verify, verbose, TestFailed ...@@ -2,35 +2,35 @@ from test_support import verify, verbose, TestFailed
import sys import sys
import gc import gc
def expect(actual, expected, name):
if actual != expected:
raise TestFailed, "test_%s: actual %d, expected %d" % (
name, actual, expected)
def expect_not(actual, expected, name):
if actual == expected:
raise TestFailed, "test_%s: actual %d unexpected" % (name, actual)
def run_test(name, thunk): def run_test(name, thunk):
if verbose: if verbose:
print "testing %s..." % name, print "testing %s..." % name,
try: thunk()
thunk() if verbose:
except TestFailed: print "ok"
if verbose:
print "failed (expected %s but got %s)" % (result,
test_result)
raise TestFailed, name
else:
if verbose:
print "ok"
def test_list(): def test_list():
l = [] l = []
l.append(l) l.append(l)
gc.collect() gc.collect()
del l del l
if gc.collect() != 1: expect(gc.collect(), 1, "list")
raise TestFailed
def test_dict(): def test_dict():
d = {} d = {}
d[1] = d d[1] = d
gc.collect() gc.collect()
del d del d
if gc.collect() != 1: expect(gc.collect(), 1, "dict")
raise TestFailed
def test_tuple(): def test_tuple():
# since tuples are immutable we close the loop with a list # since tuples are immutable we close the loop with a list
...@@ -40,8 +40,7 @@ def test_tuple(): ...@@ -40,8 +40,7 @@ def test_tuple():
gc.collect() gc.collect()
del t del t
del l del l
if gc.collect() != 2: expect(gc.collect(), 2, "tuple")
raise TestFailed
def test_class(): def test_class():
class A: class A:
...@@ -49,8 +48,7 @@ def test_class(): ...@@ -49,8 +48,7 @@ def test_class():
A.a = A A.a = A
gc.collect() gc.collect()
del A del A
if gc.collect() == 0: expect_not(gc.collect(), 0, "class")
raise TestFailed
def test_instance(): def test_instance():
class A: class A:
...@@ -59,8 +57,7 @@ def test_instance(): ...@@ -59,8 +57,7 @@ def test_instance():
a.a = a a.a = a
gc.collect() gc.collect()
del a del a
if gc.collect() == 0: expect_not(gc.collect(), 0, "instance")
raise TestFailed
def test_method(): def test_method():
# Tricky: self.__init__ is a bound method, it references the instance. # Tricky: self.__init__ is a bound method, it references the instance.
...@@ -70,8 +67,7 @@ def test_method(): ...@@ -70,8 +67,7 @@ def test_method():
a = A() a = A()
gc.collect() gc.collect()
del a del a
if gc.collect() == 0: expect_not(gc.collect(), 0, "method")
raise TestFailed
def test_finalizer(): def test_finalizer():
# A() is uncollectable if it is part of a cycle, make sure it shows up # A() is uncollectable if it is part of a cycle, make sure it shows up
...@@ -88,14 +84,13 @@ def test_finalizer(): ...@@ -88,14 +84,13 @@ def test_finalizer():
gc.collect() gc.collect()
del a del a
del b del b
if gc.collect() == 0: expect_not(gc.collect(), 0, "finalizer")
raise TestFailed
for obj in gc.garbage: for obj in gc.garbage:
if id(obj) == id_a: if id(obj) == id_a:
del obj.a del obj.a
break break
else: else:
raise TestFailed raise TestFailed, "didn't find obj in garbage (finalizer)"
gc.garbage.remove(obj) gc.garbage.remove(obj)
def test_function(): def test_function():
...@@ -105,16 +100,14 @@ def test_function(): ...@@ -105,16 +100,14 @@ def test_function():
exec("def f(): pass\n") in d exec("def f(): pass\n") in d
gc.collect() gc.collect()
del d del d
if gc.collect() != 2: expect(gc.collect(), 2, "function")
raise TestFailed
def test_frame(): def test_frame():
def f(): def f():
frame = sys._getframe() frame = sys._getframe()
gc.collect() gc.collect()
f() f()
if gc.collect() != 1: expect(gc.collect(), 1, "frame")
raise TestFailed
def test_saveall(): def test_saveall():
...@@ -133,7 +126,7 @@ def test_saveall(): ...@@ -133,7 +126,7 @@ def test_saveall():
del obj[:] del obj[:]
break break
else: else:
raise TestFailed raise TestFailed, "didn't find obj in garbage (saveall)"
gc.garbage.remove(obj) gc.garbage.remove(obj)
finally: finally:
gc.set_debug(debug) gc.set_debug(debug)
...@@ -155,6 +148,7 @@ def test_del(): ...@@ -155,6 +148,7 @@ def test_del():
def test_all(): def test_all():
gc.collect() # Delete 2nd generation garbage
run_test("lists", test_list) run_test("lists", test_list)
run_test("dicts", test_dict) run_test("dicts", test_dict)
run_test("tuples", test_tuple) run_test("tuples", test_tuple)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment