-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtutils.py
81 lines (66 loc) · 2.15 KB
/
tutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Some common test routines.
"""
def raises(exc, obj, *args, **kwargs):
"""
Assert that a callable raises a specified exception.
:exc An exception class or a string. If a class, assert that an
exception of this type is raised. If a string, assert that the string
occurs in the string representation of the exception, based on a
case-insenstivie match.
:obj A callable object.
:args Arguments to be passsed to the callable.
:kwargs Arguments to be passed to the callable.
"""
try:
apply(obj, args, kwargs)
except Exception, v:
if isinstance(exc, basestring):
if exc.lower() in str(v).lower():
return
else:
raise AssertionError(
"Expected %s, but caught %s"%(
repr(str(exc)), v
)
)
else:
if isinstance(v, exc):
return
else:
raise AssertionError(
"Expected %s, but caught %s %s"%(
exc.__name__, v.__class__.__name__, str(v)
)
)
raise AssertionError("No exception raised.")
def is_complete(lst):
"""
Does this list of points visit every vertex on the n-dimensional
cube once?
"""
lst = [tuple(i) for i in lst]
assert len(lst) == len(set(lst))
def is_traversal(lst):
"""
Does this list of points visit every vertex on the n-dimensional
cube once, with each value differing from the previous value by
exactly one bit?
"""
lst = [tuple(i) for i in lst]
assert len(lst) == len(set(lst))
prev = None
for i in lst:
if prev is not None:
diff = 0
for x, y in zip(i, prev):
if x != y:
if abs(x-y) != 1:
raise AssertionError("%s and %s differ by more than 1."%(i, prev))
diff += 1
assert diff == 1
prev = i
def symmetry(c):
l1 = list(c)
l2 = [c.index(i) for i in l1]
assert l2 == range(len(c))