tests/test-pathencode.py
branchstable
changeset 49366 288de6f5d724
parent 49285 56f98406831b
equal deleted inserted replaced
49364:e8ea403b1c46 49366:288de6f5d724
     3 #
     3 #
     4 # It uses a simple probabilistic model to generate valid pathnames
     4 # It uses a simple probabilistic model to generate valid pathnames
     5 # that have proven likely to expose bugs and divergent behavior in
     5 # that have proven likely to expose bugs and divergent behavior in
     6 # different encoding implementations.
     6 # different encoding implementations.
     7 
     7 
     8 from __future__ import absolute_import, print_function
       
     9 
     8 
    10 import binascii
     9 import binascii
    11 import collections
    10 import collections
    12 import itertools
    11 import itertools
    13 import math
    12 import math
    18 from mercurial import (
    17 from mercurial import (
    19     pycompat,
    18     pycompat,
    20     store,
    19     store,
    21 )
    20 )
    22 
    21 
    23 try:
       
    24     xrange
       
    25 except NameError:
       
    26     xrange = range
       
    27 
       
    28 validchars = set(map(pycompat.bytechr, range(0, 256)))
    22 validchars = set(map(pycompat.bytechr, range(0, 256)))
    29 alphanum = range(ord('A'), ord('Z'))
    23 alphanum = range(ord('A'), ord('Z'))
    30 
    24 
    31 for c in (b'\0', b'/'):
    25 for c in (b'\0', b'/'):
    32     validchars.remove(c)
    26     validchars.remove(c)
    33 
    27 
    34 winreserved = (
    28 winreserved = (
    35     b'aux con prn nul'.split()
    29     b'aux con prn nul'.split()
    36     + [b'com%d' % i for i in xrange(1, 10)]
    30     + [b'com%d' % i for i in range(1, 10)]
    37     + [b'lpt%d' % i for i in xrange(1, 10)]
    31     + [b'lpt%d' % i for i in range(1, 10)]
    38 )
    32 )
    39 
    33 
    40 
    34 
    41 def casecombinations(names):
    35 def casecombinations(names):
    42     '''Build all case-diddled combinations of names.'''
    36     '''Build all case-diddled combinations of names.'''
    43 
    37 
    44     combos = set()
    38     combos = set()
    45 
    39 
    46     for r in names:
    40     for r in names:
    47         for i in xrange(len(r) + 1):
    41         for i in range(len(r) + 1):
    48             for c in itertools.combinations(xrange(len(r)), i):
    42             for c in itertools.combinations(range(len(r)), i):
    49                 d = r
    43                 d = r
    50                 for j in c:
    44                 for j in c:
    51                     d = b''.join((d[:j], d[j : j + 1].upper(), d[j + 1 :]))
    45                     d = b''.join((d[:j], d[j : j + 1].upper(), d[j + 1 :]))
    52                 combos.add(d)
    46                 combos.add(d)
    53     return sorted(combos)
    47     return sorted(combos)
    65             line = line[5:]
    59             line = line[5:]
    66         for c in line:
    60         for c in line:
    67             counts[c] += 1
    61             counts[c] += 1
    68     for c in '\r/\n':
    62     for c in '\r/\n':
    69         counts.pop(c, None)
    63         counts.pop(c, None)
    70     t = sum(pycompat.itervalues(counts)) / 100.0
    64     t = sum(counts.values()) / 100.0
    71     fp.write('probtable = (')
    65     fp.write('probtable = (')
    72     for i, (k, v) in enumerate(
    66     for i, (k, v) in enumerate(
    73         sorted(counts.items(), key=lambda x: x[1], reverse=True)
    67         sorted(counts.items(), key=lambda x: x[1], reverse=True)
    74     ):
    68     ):
    75         if (i % 5) == 0:
    69         if (i % 5) == 0:
   210 def makepath(rng, j, k):
   204 def makepath(rng, j, k):
   211     '''Construct a complete pathname.'''
   205     '''Construct a complete pathname.'''
   212 
   206 
   213     return (
   207     return (
   214         b'data/'
   208         b'data/'
   215         + b'/'.join(makepart(rng, k) for _ in xrange(j))
   209         + b'/'.join(makepart(rng, k) for _ in range(j))
   216         + rng.choice([b'.d', b'.i'])
   210         + rng.choice([b'.d', b'.i'])
   217     )
   211     )
   218 
   212 
   219 
   213 
   220 def genpath(rng, count):
   214 def genpath(rng, count):
   221     '''Generate random pathnames with gradually increasing lengths.'''
   215     '''Generate random pathnames with gradually increasing lengths.'''
   222 
   216 
   223     mink, maxk = 1, 4096
   217     mink, maxk = 1, 4096
   224 
   218 
   225     def steps():
   219     def steps():
   226         for i in xrange(count):
   220         for i in range(count):
   227             yield mink + int(round(math.sqrt((maxk - mink) * float(i) / count)))
   221             yield mink + int(round(math.sqrt((maxk - mink) * float(i) / count)))
   228 
   222 
   229     for k in steps():
   223     for k in steps():
   230         x = rng.randint(1, k)
   224         x = rng.randint(1, k)
   231         y = rng.randint(1, k)
   225         y = rng.randint(1, k)