forked from pjkundert/cpppo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc_test.py
More file actions
118 lines (95 loc) · 3.83 KB
/
misc_test.py
File metadata and controls
118 lines (95 loc) · 3.83 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import logging
import os
import sys
import types
import threading
from .misc import *
def test_centeraxis():
string='abc.123.xyz'
assert centeraxis( string, 20 ) \
== ' abc.123.xyz '
assert centeraxis( string, 20, reverse=True ) \
== ' abc.123.xyz '
assert centeraxis( string, 20, reverse=True, fillchar='x' ) \
== 'xxxxxxxabc.123.xyzxx'
string='----------abc.123.xyz++++++++++'
assert centeraxis( string, 20, reverse=True ) \
== '----------abc.123.xyz++++++++++'
assert centeraxis( string, 20, reverse=True, clip=True ) \
== '-------abc.123.xyz++'
assert centeraxis( string, 20, clip=True ) \
== '---abc.123.xyz++++++'
assert centeraxis( string, 20, clip=True ) \
== '---abc.123.xyz++++++'
# If no axis char, center around a fill char
string='abc'
assert centeraxis( string, 20, reverse=True ) \
== ' abc '
assert centeraxis( string, 20 ) \
== ' abc '
def test_natural():
assert natural('10th') == (' 10', 't', 'h')
for itm in [ None, [1], {'a':1} ]:
assert natural(itm) == ('', itm.__class__.__name__,
"%9d" % ( hash( itm )
if hasattr( itm, '__hash__' ) and itm.__hash__ is not None
else id( itm )))
l = ['10th', '1st', '9', 9, 'Z', 'a', 9.0, "9.1", None ]
ls1 = ['None', "'1st'", "'9'", "9", "9.0", "'9.1'", "'10th'", "'a'", "'Z'"]
ls2 = ['None', "'1st'", "9", "'9'", "9.0", "'9.1'", "'10th'", "'a'", "'Z'"]
s = sorted( l, key=natural )
rs = [ repr(i) for i in s ]
assert rs == ls1 or rs == ls2
def test_function_creation():
"""Creating functions with code containing a defined co_filename is required in
order to extend the logging module. Unfortunately, this module seeks up the
stack frame until it finds a function whose co_filename is not the logging
module... """
def func( boo ):
pass
assert func.__code__.co_filename == __file__
filename = "something/else.py"
change_function( func, co_filename=filename )
assert func.__code__.co_filename == filename
def test_mutexmethod():
class C( object ):
_cls_lock = threading.Lock()
def __init__( self ):
self._ins_lock = threading.Lock()
@classmethod
@mutexmethod( '_cls_lock', blocking=False )
def clsmethod_lock_cls( cls, f=None ):
if f:
return f()
@mutexmethod( '_cls_lock', blocking=False )
def insmethod_lock_cls( self, f=None ):
if f:
return f()
@mutexmethod( '_ins_lock', blocking=False )
def insmethod_lock_ins( self, f=None ):
if f:
return f()
c = C()
# Same lock; should raise Exception (since blocking=False used above)
assert c.insmethod_lock_cls() is None
try:
c.insmethod_lock_cls( c.insmethod_lock_cls )
assert False, "Should have raised recursive lock exception"
except Exception as exc:
assert "Lock is held" in str( exc )
assert c.clsmethod_lock_cls() is None
try:
c.clsmethod_lock_cls( c.clsmethod_lock_cls )
assert False, "Should have raised recursive lock exception"
except Exception as exc:
assert "Lock is held" in str( exc )
try:
c.clsmethod_lock_cls( c.insmethod_lock_cls )
assert False, "Should have raised recursive lock exception"
except Exception as exc:
assert "Lock is held" in str( exc )
# Two different locks; should not interfere
c.clsmethod_lock_cls( c.insmethod_lock_ins )