tests/test-ancestor.py
author Siddharth Agarwal <sid0@fb.com>
Fri, 14 Nov 2014 23:44:38 -0800
changeset 23334 59e6e5dd3605
parent 23331 3b1b8f25443e
child 23335 3f28e8cb3066
permissions -rw-r--r--
ancestor.missingancestors: turn into a state-keeping class This allows multiple efficient missing ancestor queries against the same set of bases. In upcoming patches we'll also define ways to grow the set of bases. The fact that the test output hasn't changed establishes this patch's correctness.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19504
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
     1
from mercurial import ancestor, commands, hg, ui, util
23331
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     2
from mercurial.node import nullrev
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     3
import binascii, getopt, math, os, random, sys, time
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     4
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     5
def buildgraph(rng, nodes=100, rootprob=0.05, mergeprob=0.2, prevprob=0.7):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     6
    '''nodes: total number of nodes in the graph
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     7
    rootprob: probability that a new node (not 0) will be a root
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     8
    mergeprob: probability that, excluding a root a node will be a merge
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
     9
    prevprob: probability that p1 will be the previous node
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    10
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    11
    return value is a graph represented as an adjacency list.
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    12
    '''
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    13
    graph = [None] * nodes
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    14
    for i in xrange(nodes):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    15
        if i == 0 or rng.random() < rootprob:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    16
            graph[i] = [nullrev]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    17
        elif i == 1:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    18
            graph[i] = [0]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    19
        elif rng.random() < mergeprob:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    20
            if i == 2 or rng.random() < prevprob:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    21
                # p1 is prev
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    22
                p1 = i - 1
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    23
            else:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    24
                p1 = rng.randrange(i - 1)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    25
            p2 = rng.choice(range(0, p1) + range(p1 + 1, i))
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    26
            graph[i] = [p1, p2]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    27
        elif rng.random() < prevprob:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    28
            graph[i] = [i - 1]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    29
        else:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    30
            graph[i] = [rng.randrange(i - 1)]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    31
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    32
    return graph
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    33
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    34
def buildancestorsets(graph):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    35
    ancs = [None] * len(graph)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    36
    for i in xrange(len(graph)):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    37
        ancs[i] = set([i])
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    38
        if graph[i] == [nullrev]:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    39
            continue
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    40
        for p in graph[i]:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    41
            ancs[i].update(ancs[p])
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    42
    return ancs
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    43
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    44
def naivemissingancestors(ancs, revs, bases):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    45
    res = set()
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    46
    for rev in revs:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    47
        if rev != nullrev:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    48
            res.update(ancs[rev])
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    49
    for base in bases:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    50
        if base != nullrev:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    51
            res.difference_update(ancs[base])
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    52
    return sorted(res)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    53
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    54
def test_missingancestors(seed, rng):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    55
    # empirically observed to take around 1 second
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    56
    graphcount = 100
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    57
    testcount = 100
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    58
    nerrs = [0]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    59
    # the default mu and sigma give us a nice distribution of mostly
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    60
    # single-digit counts (including 0) with some higher ones
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    61
    def lognormrandom(mu, sigma):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    62
        return int(math.floor(rng.lognormvariate(mu, sigma)))
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    63
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    64
    def samplerevs(nodes, mu=1.1, sigma=0.8):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    65
        count = min(lognormrandom(mu, sigma), len(nodes))
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    66
        return rng.sample(nodes, count)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    67
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    68
    def err(seed, graph, bases, revs, output, expected):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    69
        if nerrs[0] == 0:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    70
            print >> sys.stderr, 'seed:', hex(seed)[:-1]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    71
        if gerrs[0] == 0:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    72
            print >> sys.stderr, 'graph:', graph
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    73
        print >> sys.stderr, '* bases:', bases
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    74
        print >> sys.stderr, '* revs: ', revs
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    75
        print >> sys.stderr, '*  output:  ', output
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    76
        print >> sys.stderr, '*  expected:', expected
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    77
        nerrs[0] += 1
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    78
        gerrs[0] += 1
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    79
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    80
    for g in xrange(graphcount):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    81
        graph = buildgraph(rng)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    82
        ancs = buildancestorsets(graph)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    83
        gerrs = [0]
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    84
        for _ in xrange(testcount):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    85
            # start from nullrev to include it as a possibility
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    86
            graphnodes = range(nullrev, len(graph))
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    87
            bases = samplerevs(graphnodes)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    88
            revs = samplerevs(graphnodes)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    89
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    90
            # fast algorithm
23334
59e6e5dd3605 ancestor.missingancestors: turn into a state-keeping class
Siddharth Agarwal <sid0@fb.com>
parents: 23331
diff changeset
    91
            inc = ancestor.incrementalmissingancestors(graph.__getitem__, bases)
59e6e5dd3605 ancestor.missingancestors: turn into a state-keeping class
Siddharth Agarwal <sid0@fb.com>
parents: 23331
diff changeset
    92
            h = inc.missingancestors(revs)
23331
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    93
            # reference slow algorithm
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    94
            r = naivemissingancestors(ancs, revs, bases)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    95
            if h != r:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
    96
                err(seed, graph, bases, revs, h, r)
18079
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    97
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    98
# graph is a dict of child->parent adjacency lists for this graph:
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    99
# o  13
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   100
# |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   101
# | o  12
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   102
# | |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   103
# | | o    11
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   104
# | | |\
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   105
# | | | | o  10
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   106
# | | | | |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   107
# | o---+ |  9
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   108
# | | | | |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   109
# o | | | |  8
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   110
#  / / / /
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   111
# | | o |  7
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   112
# | | | |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   113
# o---+ |  6
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   114
#  / / /
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   115
# | | o  5
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   116
# | |/
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   117
# | o  4
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   118
# | |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   119
# o |  3
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   120
# | |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   121
# | o  2
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   122
# |/
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   123
# o  1
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   124
# |
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   125
# o  0
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   126
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   127
graph = {0: [-1], 1: [0], 2: [1], 3: [1], 4: [2], 5: [4], 6: [4],
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   128
         7: [4], 8: [-1], 9: [6, 7], 10: [5], 11: [3, 7], 12: [9],
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   129
         13: [8]}
b3ba69692f8a ancestor: move missingancestors doctest out into a separate file
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   130
18091
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   131
def genlazyancestors(revs, stoprev=0, inclusive=False):
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   132
    print ("%% lazy ancestor set for %s, stoprev = %s, inclusive = %s" %
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   133
           (revs, stoprev, inclusive))
23328
3a7d9c0c57a5 ancestor.lazyancestors: take parentrevs function rather than changelog
Siddharth Agarwal <sid0@fb.com>
parents: 22355
diff changeset
   134
    return ancestor.lazyancestors(graph.get, revs, stoprev=stoprev,
18091
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   135
                                  inclusive=inclusive)
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   136
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   137
def printlazyancestors(s, l):
23329
c6cd4b8b76f8 test-ancestor: test iteration for lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23328
diff changeset
   138
    print 'membership: %r' % [n for n in l if n in s]
c6cd4b8b76f8 test-ancestor: test iteration for lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23328
diff changeset
   139
    print 'iteration:  %r' % list(s)
18091
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   140
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   141
def test_lazyancestors():
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   142
    # Empty revs
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   143
    s = genlazyancestors([])
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   144
    printlazyancestors(s, [3, 0, -1])
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   145
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   146
    # Standard example
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   147
    s = genlazyancestors([11, 13])
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   148
    printlazyancestors(s, [11, 13, 7, 9, 8, 3, 6, 4, 1, -1, 0])
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   149
22355
731b2a90983b test-ancestor: add a test for `ancestor` with ancestry within the initset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21024
diff changeset
   150
    # Standard with ancestry in the initial set (1 is ancestor of 3)
731b2a90983b test-ancestor: add a test for `ancestor` with ancestry within the initset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21024
diff changeset
   151
    s = genlazyancestors([1, 3])
731b2a90983b test-ancestor: add a test for `ancestor` with ancestry within the initset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21024
diff changeset
   152
    printlazyancestors(s, [1, -1, 0])
731b2a90983b test-ancestor: add a test for `ancestor` with ancestry within the initset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21024
diff changeset
   153
18091
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   154
    # Including revs
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   155
    s = genlazyancestors([11, 13], inclusive=True)
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   156
    printlazyancestors(s, [11, 13, 7, 9, 8, 3, 6, 4, 1, -1, 0])
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   157
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   158
    # Test with stoprev
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   159
    s = genlazyancestors([11, 13], stoprev=6)
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   160
    printlazyancestors(s, [11, 13, 7, 9, 8, 3, 6, 4, 1, -1, 0])
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   161
    s = genlazyancestors([11, 13], stoprev=6, inclusive=True)
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   162
    printlazyancestors(s, [11, 13, 7, 9, 8, 3, 6, 4, 1, -1, 0])
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   163
19504
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   164
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   165
# The C gca algorithm requires a real repo. These are textual descriptions of
21024
7731a2281cf0 spelling: fixes from spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 19504
diff changeset
   166
# DAGs that have been known to be problematic.
19504
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   167
dagtests = [
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   168
    '+2*2*2/*3/2',
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   169
    '+3*3/*2*2/*4*4/*4/2*4/2*2',
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   170
]
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   171
def test_gca():
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   172
    u = ui.ui()
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   173
    for i, dag in enumerate(dagtests):
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   174
        repo = hg.repository(u, 'gca%d' % i, create=1)
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   175
        cl = repo.changelog
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   176
        if not util.safehasattr(cl.index, 'ancestors'):
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   177
            # C version not available
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   178
            return
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   179
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   180
        commands.debugbuilddag(u, repo, dag)
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   181
        # Compare the results of the Python and C versions. This does not
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   182
        # include choosing a winner when more than one gca exists -- we make
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   183
        # sure both return exactly the same set of gcas.
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   184
        for a in cl:
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   185
            for b in cl:
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   186
                cgcas = sorted(cl.index.ancestors(a, b))
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   187
                pygcas = sorted(ancestor.ancestors(cl.parentrevs, a, b))
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   188
                if cgcas != pygcas:
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   189
                    print "test_gca: for dag %s, gcas for %d, %d:" % (dag, a, b)
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   190
                    print "  C returned:      %s" % cgcas
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   191
                    print "  Python returned: %s" % pygcas
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   192
23330
37c3731d8a58 test-ancestor: define a main function
Siddharth Agarwal <sid0@fb.com>
parents: 23329
diff changeset
   193
def main():
23331
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   194
    seed = None
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   195
    opts, args = getopt.getopt(sys.argv[1:], 's:', ['seed='])
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   196
    for o, a in opts:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   197
        if o in ('-s', '--seed'):
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   198
            seed = long(a, base=0) # accepts base 10 or 16 strings
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   199
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   200
    if seed is None:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   201
        try:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   202
            seed = long(binascii.hexlify(os.urandom(16)), 16)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   203
        except AttributeError:
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   204
            seed = long(time.time() * 1000)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   205
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   206
    rng = random.Random(seed)
3b1b8f25443e test-ancestor: use random testing for missing ancestors
Siddharth Agarwal <sid0@fb.com>
parents: 23330
diff changeset
   207
    test_missingancestors(seed, rng)
18091
f7f8159caad3 ancestor: add lazy membership testing to lazyancestors
Siddharth Agarwal <sid0@fb.com>
parents: 18079
diff changeset
   208
    test_lazyancestors()
19504
2fa303619b4d ancestor.deepest: ignore ninteresting while building result (issue3984)
Siddharth Agarwal <sid0@fb.com>
parents: 18091
diff changeset
   209
    test_gca()
23330
37c3731d8a58 test-ancestor: define a main function
Siddharth Agarwal <sid0@fb.com>
parents: 23329
diff changeset
   210
37c3731d8a58 test-ancestor: define a main function
Siddharth Agarwal <sid0@fb.com>
parents: 23329
diff changeset
   211
if __name__ == '__main__':
37c3731d8a58 test-ancestor: define a main function
Siddharth Agarwal <sid0@fb.com>
parents: 23329
diff changeset
   212
    main()