| 1 |
# Randomized torture test generation for bdiff |
| 2 |
|
| 3 |
import random |
| 4 |
import sys |
| 5 |
|
| 6 |
from mercurial import ( |
| 7 |
mdiff, |
| 8 |
pycompat, |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
def reducetest(a, b): |
| 13 |
tries = 0 |
| 14 |
reductions = 0 |
| 15 |
print("reducing...") |
| 16 |
while tries < 1000: |
| 17 |
a2 = ( |
| 18 |
"\n".join(l for l in a.splitlines() if random.randint(0, 100) > 0) |
| 19 |
+ "\n" |
| 20 |
) |
| 21 |
b2 = ( |
| 22 |
"\n".join(l for l in b.splitlines() if random.randint(0, 100) > 0) |
| 23 |
+ "\n" |
| 24 |
) |
| 25 |
if a2 == a and b2 == b: |
| 26 |
continue |
| 27 |
if a2 == b2: |
| 28 |
continue |
| 29 |
tries += 1 |
| 30 |
|
| 31 |
try: |
| 32 |
test1(a, b) |
| 33 |
except Exception: |
| 34 |
reductions += 1 |
| 35 |
tries = 0 |
| 36 |
a = a2 |
| 37 |
b = b2 |
| 38 |
|
| 39 |
print("reduced:", reductions, len(a) + len(b), repr(a), repr(b)) |
| 40 |
try: |
| 41 |
test1(a, b) |
| 42 |
except Exception as inst: |
| 43 |
print("failed:", inst) |
| 44 |
|
| 45 |
sys.exit(0) |
| 46 |
|
| 47 |
|
| 48 |
def test1(a, b): |
| 49 |
d = mdiff.textdiff(a, b) |
| 50 |
if not d: |
| 51 |
raise ValueError("empty") |
| 52 |
c = mdiff.patches(a, [d]) |
| 53 |
if c != b: |
| 54 |
raise ValueError("bad") |
| 55 |
|
| 56 |
|
| 57 |
def testwrap(a, b): |
| 58 |
try: |
| 59 |
test1(a, b) |
| 60 |
return |
| 61 |
except Exception as inst: |
| 62 |
print("exception:", inst) |
| 63 |
reducetest(a, b) |
| 64 |
|
| 65 |
|
| 66 |
def test(a, b): |
| 67 |
testwrap(a, b) |
| 68 |
testwrap(b, a) |
| 69 |
|
| 70 |
|
| 71 |
def rndtest(size, noise): |
| 72 |
a = [] |
| 73 |
src = " aaaaaaaabbbbccd" |
| 74 |
for x in pycompat.xrange(size): |
| 75 |
a.append(src[random.randint(0, len(src) - 1)]) |
| 76 |
|
| 77 |
while True: |
| 78 |
b = [c for c in a if random.randint(0, 99) > noise] |
| 79 |
b2 = [] |
| 80 |
for c in b: |
| 81 |
b2.append(c) |
| 82 |
while random.randint(0, 99) < noise: |
| 83 |
b2.append(src[random.randint(0, len(src) - 1)]) |
| 84 |
if b2 != a: |
| 85 |
break |
| 86 |
|
| 87 |
a = "\n".join(a) + "\n" |
| 88 |
b = "\n".join(b2) + "\n" |
| 89 |
|
| 90 |
test(a, b) |
| 91 |
|
| 92 |
|
| 93 |
maxvol = 10000 |
| 94 |
startsize = 2 |
| 95 |
while True: |
| 96 |
size = startsize |
| 97 |
count = 0 |
| 98 |
while size < maxvol: |
| 99 |
print(size) |
| 100 |
volume = 0 |
| 101 |
while volume < maxvol: |
| 102 |
rndtest(size, 2) |
| 103 |
volume += size |
| 104 |
count += 2 |
| 105 |
size *= 2 |
| 106 |
maxvol *= 4 |
| 107 |
startsize *= 4 |