tests/test-pathencode.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 24 Aug 2021 21:12:39 +0200
changeset 47915 51cd60c0a345
parent 45942 89a2afe31e82
child 48875 6000f5b25c9b
permissions -rw-r--r--
dirstate: drop the deprecated `drop` method eh ;-) Differential Revision: https://phab.mercurial-scm.org/D11350
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
     1
# This is a randomized test that generates different pathnames every
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
     2
# time it is invoked, and tests the encoding of those pathnames.
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
     3
#
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
     4
# It uses a simple probabilistic model to generate valid pathnames
26098
ce26928cbe41 spelling: behaviour -> behavior
timeless@mozdev.org
parents: 20938
diff changeset
     5
# that have proven likely to expose bugs and divergent behavior in
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
     6
# different encoding implementations.
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
     7
28928
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
     8
from __future__ import absolute_import, print_function
28918
72f683260f31 tests: make test-pathencode use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26849
diff changeset
     9
28928
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    10
import binascii
17935
9c888b945b65 test-pathencode: make a 2.4-safe import of collections
Bryan O'Sullivan <bryano@fb.com>
parents: 17934
diff changeset
    11
import collections
28928
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    12
import itertools
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    13
import math
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    14
import os
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    15
import random
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    16
import sys
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    17
import time
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    18
from mercurial import (
37880
1b230e19d044 tests: port test-pathencode.py to Python 3
Augie Fackler <augie@google.com>
parents: 36327
diff changeset
    19
    pycompat,
28928
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    20
    store,
59481bfdb7f3 tests: make test-pathencode use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28918
diff changeset
    21
)
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    22
34224
0f200e2310ca tests: add xrange alias for test-pathencode.py
Augie Fackler <raf@durin42.com>
parents: 28928
diff changeset
    23
try:
0f200e2310ca tests: add xrange alias for test-pathencode.py
Augie Fackler <raf@durin42.com>
parents: 28928
diff changeset
    24
    xrange
0f200e2310ca tests: add xrange alias for test-pathencode.py
Augie Fackler <raf@durin42.com>
parents: 28928
diff changeset
    25
except NameError:
0f200e2310ca tests: add xrange alias for test-pathencode.py
Augie Fackler <raf@durin42.com>
parents: 28928
diff changeset
    26
    xrange = range
0f200e2310ca tests: add xrange alias for test-pathencode.py
Augie Fackler <raf@durin42.com>
parents: 28928
diff changeset
    27
37880
1b230e19d044 tests: port test-pathencode.py to Python 3
Augie Fackler <augie@google.com>
parents: 36327
diff changeset
    28
validchars = set(map(pycompat.bytechr, range(0, 256)))
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    29
alphanum = range(ord('A'), ord('Z'))
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    30
37880
1b230e19d044 tests: port test-pathencode.py to Python 3
Augie Fackler <augie@google.com>
parents: 36327
diff changeset
    31
for c in (b'\0', b'/'):
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    32
    validchars.remove(c)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    33
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    34
winreserved = (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    35
    b'aux con prn nul'.split()
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    36
    + [b'com%d' % i for i in xrange(1, 10)]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    37
    + [b'lpt%d' % i for i in xrange(1, 10)]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    38
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    39
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    40
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    41
def casecombinations(names):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    42
    '''Build all case-diddled combinations of names.'''
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    43
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    44
    combos = set()
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    45
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    46
    for r in names:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    47
        for i in xrange(len(r) + 1):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    48
            for c in itertools.combinations(xrange(len(r)), i):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    49
                d = r
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    50
                for j in c:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    51
                    d = b''.join((d[:j], d[j : j + 1].upper(), d[j + 1 :]))
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    52
                combos.add(d)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    53
    return sorted(combos)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    54
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    55
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    56
def buildprobtable(fp, cmd='hg manifest tip'):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43104
diff changeset
    57
    """Construct and print a table of probabilities for path name
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43104
diff changeset
    58
    components.  The numbers are percentages."""
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    59
17935
9c888b945b65 test-pathencode: make a 2.4-safe import of collections
Bryan O'Sullivan <bryano@fb.com>
parents: 17934
diff changeset
    60
    counts = collections.defaultdict(lambda: 0)
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    61
    for line in os.popen(cmd).read().splitlines():
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    62
        if line[-2:] in ('.i', '.d'):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    63
            line = line[:-2]
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    64
        if line.startswith('data/'):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    65
            line = line[5:]
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    66
        for c in line:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    67
            counts[c] += 1
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    68
    for c in '\r/\n':
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    69
        counts.pop(c, None)
43104
74802979dd9d py3: define and use pycompat.itervalues()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43076
diff changeset
    70
    t = sum(pycompat.itervalues(counts)) / 100.0
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    71
    fp.write('probtable = (')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    72
    for i, (k, v) in enumerate(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    73
        sorted(counts.items(), key=lambda x: x[1], reverse=True)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    74
    ):
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    75
        if (i % 5) == 0:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    76
            fp.write('\n    ')
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    77
        vt = v / t
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    78
        if vt < 0.0005:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    79
            break
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    80
        fp.write('(%r, %.03f), ' % (k, vt))
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    81
    fp.write('\n    )\n')
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    82
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    83
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    84
# A table of character frequencies (as percentages), gleaned by
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    85
# looking at filelog names from a real-world, very large repo.
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    86
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
    87
probtable = (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    88
    (b't', 9.828),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    89
    (b'e', 9.042),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    90
    (b's', 8.011),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    91
    (b'a', 6.801),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    92
    (b'i', 6.618),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    93
    (b'g', 5.053),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    94
    (b'r', 5.030),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    95
    (b'o', 4.887),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    96
    (b'p', 4.363),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    97
    (b'n', 4.258),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    98
    (b'l', 3.830),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
    99
    (b'h', 3.693),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   100
    (b'_', 3.659),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   101
    (b'.', 3.377),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   102
    (b'm', 3.194),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   103
    (b'u', 2.364),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   104
    (b'd', 2.296),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   105
    (b'c', 2.163),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   106
    (b'b', 1.739),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   107
    (b'f', 1.625),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   108
    (b'6', 0.666),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   109
    (b'j', 0.610),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   110
    (b'y', 0.554),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   111
    (b'x', 0.487),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   112
    (b'w', 0.477),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   113
    (b'k', 0.476),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   114
    (b'v', 0.473),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   115
    (b'3', 0.336),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   116
    (b'1', 0.335),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   117
    (b'2', 0.326),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   118
    (b'4', 0.310),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   119
    (b'5', 0.305),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   120
    (b'9', 0.302),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   121
    (b'8', 0.300),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   122
    (b'7', 0.299),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   123
    (b'q', 0.298),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   124
    (b'0', 0.250),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   125
    (b'z', 0.223),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   126
    (b'-', 0.118),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   127
    (b'C', 0.095),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   128
    (b'T', 0.087),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   129
    (b'F', 0.085),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   130
    (b'B', 0.077),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   131
    (b'S', 0.076),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   132
    (b'P', 0.076),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   133
    (b'L', 0.059),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   134
    (b'A', 0.058),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   135
    (b'N', 0.051),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   136
    (b'D', 0.049),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   137
    (b'M', 0.046),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   138
    (b'E', 0.039),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   139
    (b'I', 0.035),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   140
    (b'R', 0.035),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   141
    (b'G', 0.028),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   142
    (b'U', 0.026),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   143
    (b'W', 0.025),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   144
    (b'O', 0.017),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   145
    (b'V', 0.015),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   146
    (b'H', 0.013),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   147
    (b'Q', 0.011),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   148
    (b'J', 0.007),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   149
    (b'K', 0.005),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   150
    (b'+', 0.004),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   151
    (b'X', 0.003),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   152
    (b'Y', 0.001),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   153
)
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   154
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   155
for c, _ in probtable:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   156
    validchars.remove(c)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   157
validchars = list(validchars)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   158
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   159
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   160
def pickfrom(rng, table):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   161
    c = 0
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   162
    r = rng.random() * sum(i[1] for i in table)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   163
    for i, p in table:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   164
        c += p
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   165
        if c >= r:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   166
            return i
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   167
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   168
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   169
reservedcombos = casecombinations(winreserved)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   170
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   171
# The first component of a name following a slash.
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   172
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   173
firsttable = (
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   174
    (lambda rng: pickfrom(rng, probtable), 90),
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   175
    (lambda rng: rng.choice(validchars), 5),
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   176
    (lambda rng: rng.choice(reservedcombos), 5),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   177
)
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   178
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   179
# Components of a name following the first.
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   180
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   181
resttable = firsttable[:-1]
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   182
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   183
# Special suffixes.
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   184
37880
1b230e19d044 tests: port test-pathencode.py to Python 3
Augie Fackler <augie@google.com>
parents: 36327
diff changeset
   185
internalsuffixcombos = casecombinations(b'.hg .i .d'.split())
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   186
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   187
# The last component of a path, before a slash or at the end of a name.
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   188
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   189
lasttable = resttable + (
37880
1b230e19d044 tests: port test-pathencode.py to Python 3
Augie Fackler <augie@google.com>
parents: 36327
diff changeset
   190
    (lambda rng: b'', 95),
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   191
    (lambda rng: rng.choice(internalsuffixcombos), 5),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   192
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   193
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   194
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   195
def makepart(rng, k):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   196
    '''Construct a part of a pathname, without slashes.'''
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   197
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   198
    p = pickfrom(rng, firsttable)(rng)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   199
    l = len(p)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   200
    ps = [p]
19319
ec17ddecdf64 test-pathencode: randomize length of each path component
Siddharth Agarwal <sid0@fb.com>
parents: 19318
diff changeset
   201
    maxl = rng.randint(1, k)
ec17ddecdf64 test-pathencode: randomize length of each path component
Siddharth Agarwal <sid0@fb.com>
parents: 19318
diff changeset
   202
    while l < maxl:
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   203
        p = pickfrom(rng, resttable)(rng)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   204
        l += len(p)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   205
        ps.append(p)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   206
    ps.append(pickfrom(rng, lasttable)(rng))
37880
1b230e19d044 tests: port test-pathencode.py to Python 3
Augie Fackler <augie@google.com>
parents: 36327
diff changeset
   207
    return b''.join(ps)
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   208
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   209
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   210
def makepath(rng, j, k):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   211
    '''Construct a complete pathname.'''
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   212
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   213
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   214
        b'data/'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   215
        + b'/'.join(makepart(rng, k) for _ in xrange(j))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   216
        + rng.choice([b'.d', b'.i'])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   217
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   218
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   219
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   220
def genpath(rng, count):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   221
    '''Generate random pathnames with gradually increasing lengths.'''
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   222
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   223
    mink, maxk = 1, 4096
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   224
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   225
    def steps():
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   226
        for i in xrange(count):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   227
            yield mink + int(round(math.sqrt((maxk - mink) * float(i) / count)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   228
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   229
    for k in steps():
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   230
        x = rng.randint(1, k)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   231
        y = rng.randint(1, k)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   232
        yield makepath(rng, x, y)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   233
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   234
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   235
def runtests(rng, seed, count):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   236
    nerrs = 0
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   237
    for p in genpath(rng, count):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   238
        h = store._pathencode(p)  # uses C implementation, if available
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   239
        r = store._hybridencode(p, True)  # reference implementation in Python
18094
8ceabb34f1cb test-pathencode: compare current pathencoding implementations
Adrian Buehlmann <adrian@cadifra.com>
parents: 17947
diff changeset
   240
        if h != r:
8ceabb34f1cb test-pathencode: compare current pathencoding implementations
Adrian Buehlmann <adrian@cadifra.com>
parents: 17947
diff changeset
   241
            if nerrs == 0:
28918
72f683260f31 tests: make test-pathencode use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26849
diff changeset
   242
                print('seed:', hex(seed)[:-1], file=sys.stderr)
72f683260f31 tests: make test-pathencode use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26849
diff changeset
   243
            print("\np: '%s'" % p.encode("string_escape"), file=sys.stderr)
72f683260f31 tests: make test-pathencode use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26849
diff changeset
   244
            print("h: '%s'" % h.encode("string_escape"), file=sys.stderr)
72f683260f31 tests: make test-pathencode use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 26849
diff changeset
   245
            print("r: '%s'" % r.encode("string_escape"), file=sys.stderr)
18094
8ceabb34f1cb test-pathencode: compare current pathencoding implementations
Adrian Buehlmann <adrian@cadifra.com>
parents: 17947
diff changeset
   246
            nerrs += 1
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   247
    return nerrs
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   248
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   249
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   250
def main():
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   251
    import getopt
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   252
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   253
    # Empirically observed to take about a second to run
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   254
    count = 100
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   255
    seed = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   256
    opts, args = getopt.getopt(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   257
        sys.argv[1:], 'c:s:', ['build', 'count=', 'seed=']
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   258
    )
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   259
    for o, a in opts:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   260
        if o in ('-c', '--count'):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   261
            count = int(a)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   262
        elif o in ('-s', '--seed'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   263
            seed = int(a, base=0)  # accepts base 10 or 16 strings
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   264
        elif o == '--build':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   265
            buildprobtable(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   266
                sys.stdout,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   267
                'find .hg/store/data -type f && '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   268
                'cat .hg/store/fncache 2>/dev/null',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   269
            )
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   270
            sys.exit(0)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   271
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   272
    if seed is None:
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   273
        try:
34225
d43340bec0f5 tests: use int() instead of long() in test-pathencode.py
Augie Fackler <raf@durin42.com>
parents: 34224
diff changeset
   274
            seed = int(binascii.hexlify(os.urandom(16)), 16)
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   275
        except AttributeError:
34225
d43340bec0f5 tests: use int() instead of long() in test-pathencode.py
Augie Fackler <raf@durin42.com>
parents: 34224
diff changeset
   276
            seed = int(time.time() * 1000)
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   277
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   278
    rng = random.Random(seed)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   279
    if runtests(rng, seed, count):
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   280
        sys.exit(1)
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   281
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 37880
diff changeset
   282
17947
f945caa5e963 test-pathencode: more aggressively check for python < 2.6
Bryan O'Sullivan <bryano@fb.com>
parents: 17935
diff changeset
   283
if __name__ == '__main__':
17934
736f1c09f321 tests: add a randomized test for pathencode
Bryan O'Sullivan <bryano@fb.com>
parents:
diff changeset
   284
    main()