summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/util.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-07-19 21:01:52 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-07-19 21:01:52 (GMT)
commitbed7d04fedd800a8d3bd8a7c9c3ff823e365c236 (patch)
treef7e20f085f4c4a6a4cc0227ab16aa73d1dd7dc8e /Lib/unittest/util.py
parentc4296d1edc1f05f96cc7b6b3e1c3df9dd596d5f4 (diff)
downloadcpython-bed7d04fedd800a8d3bd8a7c9c3ff823e365c236.zip
cpython-bed7d04fedd800a8d3bd8a7c9c3ff823e365c236.tar.gz
cpython-bed7d04fedd800a8d3bd8a7c9c3ff823e365c236.tar.bz2
Merged revisions 74095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r74095 | benjamin.peterson | 2009-07-19 15:18:21 -0500 (Sun, 19 Jul 2009) | 1 line split unittest.py into a package ........
Diffstat (limited to 'Lib/unittest/util.py')
-rw-r--r--Lib/unittest/util.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/Lib/unittest/util.py b/Lib/unittest/util.py
new file mode 100644
index 0000000..26805de
--- /dev/null
+++ b/Lib/unittest/util.py
@@ -0,0 +1,75 @@
+"""Various utility functions."""
+
+def strclass(cls):
+ return "%s.%s" % (cls.__module__, cls.__name__)
+
+def sorted_list_difference(expected, actual):
+ """Finds elements in only one or the other of two, sorted input lists.
+
+ Returns a two-element tuple of lists. The first list contains those
+ elements in the "expected" list but not in the "actual" list, and the
+ second contains those elements in the "actual" list but not in the
+ "expected" list. Duplicate elements in either input list are ignored.
+ """
+ i = j = 0
+ missing = []
+ unexpected = []
+ while True:
+ try:
+ e = expected[i]
+ a = actual[j]
+ if e < a:
+ missing.append(e)
+ i += 1
+ while expected[i] == e:
+ i += 1
+ elif e > a:
+ unexpected.append(a)
+ j += 1
+ while actual[j] == a:
+ j += 1
+ else:
+ i += 1
+ try:
+ while expected[i] == e:
+ i += 1
+ finally:
+ j += 1
+ while actual[j] == a:
+ j += 1
+ except IndexError:
+ missing.extend(expected[i:])
+ unexpected.extend(actual[j:])
+ break
+ return missing, unexpected
+
+
+def unorderable_list_difference(expected, actual):
+ """Same behavior as sorted_list_difference but
+ for lists of unorderable items (like dicts).
+
+ As it does a linear search per item (remove) it
+ has O(n*n) performance."""
+ missing = []
+ while expected:
+ item = expected.pop()
+ try:
+ actual.remove(item)
+ except ValueError:
+ missing.append(item)
+
+ # anything left in actual is unexpected
+ return missing, actual
+
+def CmpToKey(mycmp):
+ 'Convert a cmp= function into a key= function'
+ class K(object):
+ def __init__(self, obj, *args):
+ self.obj = obj
+ def __lt__(self, other):
+ return mycmp(self.obj, other.obj) == -1
+ return K
+
+def three_way_cmp(x, y):
+ """Return -1 if x < y, 0 if x == y and 1 if x > y"""
+ return (x > y) - (x < y)