##// END OF EJS Templates
py3: make contrib/bdiff-torture.py conform to our import style
Yuya Nishihara -
r29209:eccfd650 default
parent child Browse files
Show More
@@ -1,98 +1,100 b''
1 # Randomized torture test generation for bdiff
1 # Randomized torture test generation for bdiff
2
2
3 from __future__ import absolute_import, print_function
3 from __future__ import absolute_import, print_function
4 import random, sys
4 import random
5 import sys
6
5 from mercurial import (
7 from mercurial import (
6 bdiff,
8 bdiff,
7 mpatch,
9 mpatch,
8 )
10 )
9
11
10 def reducetest(a, b):
12 def reducetest(a, b):
11 tries = 0
13 tries = 0
12 reductions = 0
14 reductions = 0
13 print("reducing...")
15 print("reducing...")
14 while tries < 1000:
16 while tries < 1000:
15 a2 = "\n".join(l for l in a.splitlines()
17 a2 = "\n".join(l for l in a.splitlines()
16 if random.randint(0, 100) > 0) + "\n"
18 if random.randint(0, 100) > 0) + "\n"
17 b2 = "\n".join(l for l in b.splitlines()
19 b2 = "\n".join(l for l in b.splitlines()
18 if random.randint(0, 100) > 0) + "\n"
20 if random.randint(0, 100) > 0) + "\n"
19 if a2 == a and b2 == b:
21 if a2 == a and b2 == b:
20 continue
22 continue
21 if a2 == b2:
23 if a2 == b2:
22 continue
24 continue
23 tries += 1
25 tries += 1
24
26
25 try:
27 try:
26 test1(a, b)
28 test1(a, b)
27 except Exception as inst:
29 except Exception as inst:
28 reductions += 1
30 reductions += 1
29 tries = 0
31 tries = 0
30 a = a2
32 a = a2
31 b = b2
33 b = b2
32
34
33 print("reduced:", reductions, len(a) + len(b),
35 print("reduced:", reductions, len(a) + len(b),
34 repr(a), repr(b))
36 repr(a), repr(b))
35 try:
37 try:
36 test1(a, b)
38 test1(a, b)
37 except Exception as inst:
39 except Exception as inst:
38 print("failed:", inst)
40 print("failed:", inst)
39
41
40 sys.exit(0)
42 sys.exit(0)
41
43
42 def test1(a, b):
44 def test1(a, b):
43 d = bdiff.bdiff(a, b)
45 d = bdiff.bdiff(a, b)
44 if not d:
46 if not d:
45 raise ValueError("empty")
47 raise ValueError("empty")
46 c = mpatch.patches(a, [d])
48 c = mpatch.patches(a, [d])
47 if c != b:
49 if c != b:
48 raise ValueError("bad")
50 raise ValueError("bad")
49
51
50 def testwrap(a, b):
52 def testwrap(a, b):
51 try:
53 try:
52 test1(a, b)
54 test1(a, b)
53 return
55 return
54 except Exception as inst:
56 except Exception as inst:
55 pass
57 pass
56 print("exception:", inst)
58 print("exception:", inst)
57 reducetest(a, b)
59 reducetest(a, b)
58
60
59 def test(a, b):
61 def test(a, b):
60 testwrap(a, b)
62 testwrap(a, b)
61 testwrap(b, a)
63 testwrap(b, a)
62
64
63 def rndtest(size, noise):
65 def rndtest(size, noise):
64 a = []
66 a = []
65 src = " aaaaaaaabbbbccd"
67 src = " aaaaaaaabbbbccd"
66 for x in xrange(size):
68 for x in xrange(size):
67 a.append(src[random.randint(0, len(src) - 1)])
69 a.append(src[random.randint(0, len(src) - 1)])
68
70
69 while True:
71 while True:
70 b = [c for c in a if random.randint(0, 99) > noise]
72 b = [c for c in a if random.randint(0, 99) > noise]
71 b2 = []
73 b2 = []
72 for c in b:
74 for c in b:
73 b2.append(c)
75 b2.append(c)
74 while random.randint(0, 99) < noise:
76 while random.randint(0, 99) < noise:
75 b2.append(src[random.randint(0, len(src) - 1)])
77 b2.append(src[random.randint(0, len(src) - 1)])
76 if b2 != a:
78 if b2 != a:
77 break
79 break
78
80
79 a = "\n".join(a) + "\n"
81 a = "\n".join(a) + "\n"
80 b = "\n".join(b2) + "\n"
82 b = "\n".join(b2) + "\n"
81
83
82 test(a, b)
84 test(a, b)
83
85
84 maxvol = 10000
86 maxvol = 10000
85 startsize = 2
87 startsize = 2
86 while True:
88 while True:
87 size = startsize
89 size = startsize
88 count = 0
90 count = 0
89 while size < maxvol:
91 while size < maxvol:
90 print(size)
92 print(size)
91 volume = 0
93 volume = 0
92 while volume < maxvol:
94 while volume < maxvol:
93 rndtest(size, 2)
95 rndtest(size, 2)
94 volume += size
96 volume += size
95 count += 2
97 count += 2
96 size *= 2
98 size *= 2
97 maxvol *= 4
99 maxvol *= 4
98 startsize *= 4
100 startsize *= 4
General Comments 0
You need to be logged in to leave comments. Login now