Kaydet (Commit) 5e056bbb authored tarafından Barry Warsaw's avatar Barry Warsaw

test_rotor.py: New test of the rotor module.

test_*: converted to the new test harness.  GvR note!  test_signal.py
works interatively (i.e. when verbose=1) but does not work inside the
test harness.  It must be a timing issue, but I haven't figured it out
yet.
üst aeb207c6
from test_support import verbose
import sys import sys
import new import new
...@@ -5,7 +6,10 @@ class Eggs: ...@@ -5,7 +6,10 @@ class Eggs:
def get_yolks(self): def get_yolks(self):
return self.yolks return self.yolks
print 'new.module()'
m = new.module('Spam') m = new.module('Spam')
if verbose:
print m
m.Eggs = Eggs m.Eggs = Eggs
sys.modules['Spam'] = m sys.modules['Spam'] = m
import Spam import Spam
...@@ -13,12 +17,21 @@ import Spam ...@@ -13,12 +17,21 @@ import Spam
def get_more_yolks(self): def get_more_yolks(self):
return self.yolks + 3 return self.yolks + 3
print 'new.classobj()'
C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks}) C = new.classobj('Spam', (Spam.Eggs,), {'get_more_yolks': get_more_yolks})
if verbose:
print C
print 'new.instance()'
c = new.instance(C, {'yolks': 3}) c = new.instance(C, {'yolks': 3})
if verbose:
print c
def break_yolks(self): def break_yolks(self):
self.yolks = self.yolks - 2 self.yolks = self.yolks - 2
print 'new.instancemethod()'
im = new.instancemethod(break_yolks, c, C) im = new.instancemethod(break_yolks, c, C)
if verbose:
print im
if c.get_yolks() <> 3 and c.get_more_yolks() <> 6: if c.get_yolks() <> 3 and c.get_more_yolks() <> 6:
print 'Broken call of hand-crafted class instance' print 'Broken call of hand-crafted class instance'
...@@ -35,10 +48,16 @@ c = a + b ...@@ -35,10 +48,16 @@ c = a + b
ccode = compile(codestr, '<string>', 'exec') ccode = compile(codestr, '<string>', 'exec')
g = {'c': 0, '__builtins__': __builtins__} g = {'c': 0, '__builtins__': __builtins__}
# this test could be more robust # this test could be more robust
print 'new.function()'
func = new.function(ccode, g) func = new.function(ccode, g)
if verbose:
print func
func() func()
if g['c'] <> 3: if g['c'] <> 3:
print 'Could not create a proper function object' print 'Could not create a proper function object'
# bogus test of new.code() # bogus test of new.code()
new.code(3, 3, 3, codestr, (), (), (), "<string>", "<name>") print 'new.code()'
d = new.code(3, 3, 3, codestr, (), (), (), "<string>", "<name>")
if verbose:
print d
from test_support import verbose
import nis import nis
verbose = 0 print 'nis.maps()'
if __name__ == '__main__':
verbose = 1
maps = nis.maps() maps = nis.maps()
done = 0 done = 0
for nismap in maps: for nismap in maps:
......
...@@ -2,6 +2,7 @@ import operator ...@@ -2,6 +2,7 @@ import operator
import sys import sys
def test(name, input, output, *args): def test(name, input, output, *args):
print 'testing:', name
f = getattr(operator, name) f = getattr(operator, name)
params = (input,) + args params = (input,) + args
try: try:
......
from test_support import verbose
import pwd import pwd
import string import string
verbose = 0 print 'pwd.getpwall()'
if __name__ == '__main__':
verbose = 1
entries = pwd.getpwall() entries = pwd.getpwall()
for e in entries: for e in entries:
...@@ -12,12 +10,16 @@ for e in entries: ...@@ -12,12 +10,16 @@ for e in entries:
uid = e[2] uid = e[2]
if verbose: if verbose:
print name, uid print name, uid
print 'pwd.getpwuid()'
dbuid = pwd.getpwuid(uid) dbuid = pwd.getpwuid(uid)
if dbuid[0] <> name: if dbuid[0] <> name:
print 'Mismatch in pwd.getpwuid()' print 'Mismatch in pwd.getpwuid()'
print 'pwd.getpwnam()'
dbname = pwd.getpwnam(name) dbname = pwd.getpwnam(name)
if dbname[2] <> uid: if dbname[2] <> uid:
print 'Mismatch in pwd.getpwnam()' print 'Mismatch in pwd.getpwnam()'
else:
print 'name matches uid'
break break
# try to get some errors # try to get some errors
...@@ -53,7 +55,7 @@ while bynames.has_key(fakename): ...@@ -53,7 +55,7 @@ while bynames.has_key(fakename):
try: try:
pwd.getpwnam(fakename) pwd.getpwnam(fakename)
except KeyError: except KeyError:
pass print 'caught expected exception'
else: else:
print 'fakename', fakename, 'did not except pwd.getpwnam()' print 'fakename', fakename, 'did not except pwd.getpwnam()'
...@@ -65,6 +67,6 @@ fakeuid = uids[0] + 1 ...@@ -65,6 +67,6 @@ fakeuid = uids[0] + 1
try: try:
pwd.getpwuid(fakeuid) pwd.getpwuid(fakeuid)
except KeyError: except KeyError:
pass print 'caught expected exception'
else: else:
print 'fakeuid', fakeuid, 'did not except pwd.getpwuid()' print 'fakeuid', fakeuid, 'did not except pwd.getpwuid()'
import rotor
r = rotor.newrotor("you'll never guess this")
r = rotor.newrotor("you'll never guess this", 12)
A = 'spam and eggs'
B = 'cheese shop'
a = r.encrypt(A)
print a
b = r.encryptmore(B)
print b
A1 = r.decrypt(a)
print A1
if A1 <> A:
print 'decrypt failed'
B1 = r.decryptmore(b)
print B1
if B1 <> B:
print 'decryptmore failed'
r.setkey()
r.setkey('you guessed it!')
# Test the signal module # Test the signal module
from test_support import verbose
import signal import signal
import os import os
if verbose:
x = '-x'
else:
x = '+x'
pid = os.getpid() pid = os.getpid()
# Shell script that will send us asynchronous signals # Shell script that will send us asynchronous signals
script = """ script = """
( (
set -x set %(x)s
sleep 2 sleep 2
kill -5 %(pid)d kill -5 %(pid)d
sleep 2 sleep 2
kill -2 %(pid)d kill -2 %(pid)d
sleep 2 sleep 2
kill -3 %(pid)d kill -3 %(pid)d
) & ) &
""" % vars() """ % vars()
def handlerA(*args): def handlerA(*args):
print "handlerA", args if verbose:
print "handlerA", args
HandlerBCalled = "HandlerBCalled" # Exception HandlerBCalled = "HandlerBCalled" # Exception
def handlerB(*args): def handlerB(*args):
print "handlerB", args if verbose:
print "handlerB", args
raise HandlerBCalled, args raise HandlerBCalled, args
signal.alarm(20) # Entire test lasts at most 20 sec. signal.alarm(20) # Entire test lasts at most 20 sec.
...@@ -40,11 +46,18 @@ print "starting pause() loop..." ...@@ -40,11 +46,18 @@ print "starting pause() loop..."
try: try:
while 1: while 1:
print "call pause()..." if verbose:
print "call pause()..."
try: try:
signal.pause() signal.pause()
print "pause() returned" if verbose:
print "pause() returned"
except HandlerBCalled: except HandlerBCalled:
print "HandlerBCalled exception caught" if verbose:
print "HandlerBCalled exception caught"
else:
pass
except KeyboardInterrupt: except KeyboardInterrupt:
print "KeyboardInterrupt (assume the alarm() went off)" if verbose:
print "KeyboardInterrupt (assume the alarm() went off)"
from test_support import verbose
import _xdr import _xdr
verbose = 0
if __name__ == '__main__':
verbose = 1
fd = 8.01 fd = 8.01
print '_xdr.pack_float()'
s = _xdr.pack_float(fd) s = _xdr.pack_float(fd)
if verbose:
print `s`
print '_xdr.unpack_float()'
f = _xdr.unpack_float(s) f = _xdr.unpack_float(s)
if verbose: if verbose:
print f print f
if int(100*f) <> int(100*fd): if int(100*f) <> int(100*fd):
print 'pack_float() <> unpack_float()' print 'pack_float() <> unpack_float()'
fd = 9900000.9 fd = 9900000.9
print '_xdr.pack_double()'
s = _xdr.pack_double(fd) s = _xdr.pack_double(fd)
if verbose:
print `s`
print '_xdr.unpack_double()'
f = _xdr.unpack_double(s) f = _xdr.unpack_double(s)
if verbose: if verbose:
print f print f
......
...@@ -33,6 +33,7 @@ tests = ['test_grammar', ...@@ -33,6 +33,7 @@ tests = ['test_grammar',
'test_imageop', 'test_imageop',
'test_imgfile', 'test_imgfile',
'test_regex', 'test_regex',
'test_rotor',
] ]
if __name__ == '__main__': if __name__ == '__main__':
......
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