mirror of
https://github.com/python/cpython.git
synced 2026-01-03 14:02:21 +00:00
Used sets.Set() to compare unordered sequences.
Improves clarity and brevity.
This commit is contained in:
parent
40006e9f7a
commit
91bbd9a7b9
6 changed files with 17 additions and 40 deletions
|
|
@ -2,6 +2,7 @@
|
|||
from test import test_support
|
||||
|
||||
from test.test_support import verify, verbose
|
||||
from sets import Set
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
|
|
@ -42,10 +43,8 @@ def check_all(self, modname):
|
|||
exec "from %s import *" % modname in names
|
||||
if names.has_key("__builtins__"):
|
||||
del names["__builtins__"]
|
||||
keys = names.keys()
|
||||
keys.sort()
|
||||
all = list(sys.modules[modname].__all__) # in case it's a tuple
|
||||
all.sort()
|
||||
keys = Set(names)
|
||||
all = Set(sys.modules[modname].__all__)
|
||||
verify(keys==all, "%s != %s" % (keys, all))
|
||||
|
||||
def test_all(self):
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import test.test_support, unittest
|
||||
from test.test_support import fcmp, have_unicode, TESTFN, unlink
|
||||
from sets import Set
|
||||
|
||||
import sys, warnings, cStringIO
|
||||
warnings.filterwarnings("ignore", "hex../oct.. of negative int",
|
||||
|
|
@ -1159,18 +1160,9 @@ def get_vars_f2():
|
|||
get_vars_f2 = staticmethod(get_vars_f2)
|
||||
|
||||
def test_vars(self):
|
||||
a = b = None
|
||||
a = vars().keys()
|
||||
b = dir()
|
||||
a.sort()
|
||||
b.sort()
|
||||
self.assertEqual(a, b)
|
||||
self.assertEqual(Set(vars()), Set(dir()))
|
||||
import sys
|
||||
a = vars(sys).keys()
|
||||
b = dir(sys)
|
||||
a.sort()
|
||||
b.sort()
|
||||
self.assertEqual(a, b)
|
||||
self.assertEqual(Set(vars(sys)), Set(dir(sys)))
|
||||
self.assertEqual(self.get_vars_f0(), {})
|
||||
self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2})
|
||||
self.assertRaises(TypeError, vars, 42, 42)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
from test.test_support import run_unittest, TESTFN
|
||||
import glob
|
||||
import os
|
||||
from sets import Set
|
||||
|
||||
def mkdirs(fname):
|
||||
if os.path.exists(fname) or fname == '':
|
||||
|
|
@ -61,11 +62,7 @@ def glob(self, *parts):
|
|||
return glob.glob(p)
|
||||
|
||||
def assertSequencesEqual_noorder(self, l1, l2):
|
||||
l1 = list(l1)
|
||||
l2 = list(l2)
|
||||
l1.sort()
|
||||
l2.sort()
|
||||
self.assertEqual(l1, l2)
|
||||
self.assertEqual(Set(l1), Set(l2))
|
||||
|
||||
def test_glob_literal(self):
|
||||
eq = self.assertSequencesEqual_noorder
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
from types import ClassType, FunctionType, MethodType
|
||||
import pyclbr
|
||||
from unittest import TestCase
|
||||
from sets import Set
|
||||
|
||||
# This next line triggers an error on old versions of pyclbr.
|
||||
|
||||
|
|
@ -23,17 +24,10 @@ class PyclbrTest(TestCase):
|
|||
|
||||
def assertListEq(self, l1, l2, ignore):
|
||||
''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
|
||||
l1.sort()
|
||||
l2.sort()
|
||||
try:
|
||||
for p1, p2 in (l1, l2), (l2, l1):
|
||||
for item in p1:
|
||||
ok = (item in p2) or (item in ignore)
|
||||
if not ok:
|
||||
self.fail("%r missing" % item)
|
||||
except:
|
||||
missing = (Set(l1) ^ Set(l2)) - Set(ignore)
|
||||
if missing:
|
||||
print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore)
|
||||
raise
|
||||
self.fail("%r missing" % missing.pop())
|
||||
|
||||
def assertHasattr(self, obj, attr, ignore):
|
||||
''' succeed iff hasattr(obj,attr) or attr in ignore. '''
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
# Check every path through every method of UserDict
|
||||
|
||||
import test.test_support, unittest
|
||||
from sets import Set
|
||||
|
||||
import UserDict
|
||||
|
||||
|
|
@ -68,10 +69,7 @@ def check_iterandlist(iter, lst, ref):
|
|||
self.assert_(hasattr(iter, 'next'))
|
||||
self.assert_(hasattr(iter, '__iter__'))
|
||||
x = list(iter)
|
||||
x.sort()
|
||||
lst.sort()
|
||||
ref.sort()
|
||||
self.assert_(x==lst==ref)
|
||||
self.assert_(Set(x)==Set(lst)==Set(ref))
|
||||
check_iterandlist(d.iterkeys(), d.keys(), self.reference.keys())
|
||||
check_iterandlist(iter(d), d.keys(), self.reference.keys())
|
||||
check_iterandlist(d.itervalues(), d.values(), self.reference.values())
|
||||
|
|
@ -243,10 +241,8 @@ def items(self):
|
|||
ikeys = []
|
||||
for k in u2:
|
||||
ikeys.append(k)
|
||||
ikeys.sort()
|
||||
keys = u2.keys()
|
||||
keys.sort()
|
||||
self.assertEqual(ikeys, keys)
|
||||
self.assertEqual(Set(ikeys), Set(keys))
|
||||
|
||||
# Test setdefault
|
||||
t = UserDict.UserDict()
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
import weakref
|
||||
|
||||
from test import test_support
|
||||
from sets import Set
|
||||
|
||||
|
||||
class C:
|
||||
|
|
@ -340,9 +341,7 @@ def test_weak_keys(self):
|
|||
"wrong object returned by weak dict!")
|
||||
items1 = dict.items()
|
||||
items2 = dict.copy().items()
|
||||
items1.sort()
|
||||
items2.sort()
|
||||
self.assert_(items1 == items2,
|
||||
self.assert_(Set(items1) == Set(items2),
|
||||
"cloning of weak-keyed dictionary did not work!")
|
||||
del items1, items2
|
||||
self.assert_(len(dict) == self.COUNT)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue