test: add testutil.flip_one_bit which flips a randomly chosen bit of the input string

This commit is contained in:
Zooko O'Whielacronx 2008-07-28 16:42:17 -07:00
parent fe522e31a4
commit 9d14341c7d

View File

@ -1,4 +1,5 @@
import os, signal, time
from random import randrange
from twisted.internet import reactor, defer, task
from twisted.python import failure
@ -12,6 +13,13 @@ def flip_bit(good, which):
pieces = good[:which], good[which:which+1], good[which+1:]
return pieces[0] + chr(ord(pieces[1]) ^ 0x01) + pieces[2]
def flip_one_bit(s):
# flip one random bit of the string s
i = randrange(0, len(s))
result = s[:i] + chr(ord(s[i])^(0x01<<randrange(0, 8))) + s[i+1:]
assert result != s, "Internal error -- flip_one_bit() produced the same string as its input: %s == %s" % (result, s)
return result
class SignalMixin:
# This class is necessary for any code which wants to use Processes
# outside the usual reactor.run() environment. It is copied from