From 9d14341c7d7444df9f8eb3fcc968fb6687b39671 Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Mon, 28 Jul 2008 16:42:17 -0700
Subject: [PATCH] test: add testutil.flip_one_bit which flips a randomly chosen
 bit of the input string

---
 src/allmydata/util/testutil.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/allmydata/util/testutil.py b/src/allmydata/util/testutil.py
index 2242de65..ec6afdc1 100644
--- a/src/allmydata/util/testutil.py
+++ b/src/allmydata/util/testutil.py
@@ -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
-- 
2.45.2