tests/test-fastannotate-revmap.py
changeset 43076 2372284d9457
parent 41117 0fea133780bf
child 44452 9d2b2df2c2ba
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
    11 from hgext.fastannotate import error, revmap
    11 from hgext.fastannotate import error, revmap
    12 
    12 
    13 if pycompat.ispy3:
    13 if pycompat.ispy3:
    14     xrange = range
    14     xrange = range
    15 
    15 
       
    16 
    16 def genhsh(i):
    17 def genhsh(i):
    17     return pycompat.bytechr(i) + b'\0' * 19
    18     return pycompat.bytechr(i) + b'\0' * 19
       
    19 
    18 
    20 
    19 def gettemppath():
    21 def gettemppath():
    20     fd, path = tempfile.mkstemp()
    22     fd, path = tempfile.mkstemp()
    21     os.close(fd)
    23     os.close(fd)
    22     os.unlink(path)
    24     os.unlink(path)
    23     return path
    25     return path
    24 
    26 
       
    27 
    25 def ensure(condition):
    28 def ensure(condition):
    26     if not condition:
    29     if not condition:
    27         raise RuntimeError('Unexpected')
    30         raise RuntimeError('Unexpected')
       
    31 
    28 
    32 
    29 def testbasicreadwrite():
    33 def testbasicreadwrite():
    30     path = gettemppath()
    34     path = gettemppath()
    31 
    35 
    32     rm = revmap.revmap(path)
    36     rm = revmap.revmap(path)
    34     for i in xrange(5):
    38     for i in xrange(5):
    35         ensure(rm.rev2hsh(i) is None)
    39         ensure(rm.rev2hsh(i) is None)
    36     ensure(rm.hsh2rev(b'\0' * 20) is None)
    40     ensure(rm.hsh2rev(b'\0' * 20) is None)
    37 
    41 
    38     paths = [
    42     paths = [
    39         b'', b'a', None, b'b', b'b', b'c', b'c', None, b'a', b'b', b'a', b'a']
    43         b'',
       
    44         b'a',
       
    45         None,
       
    46         b'b',
       
    47         b'b',
       
    48         b'c',
       
    49         b'c',
       
    50         None,
       
    51         b'a',
       
    52         b'b',
       
    53         b'a',
       
    54         b'a',
       
    55     ]
    40     for i in xrange(1, 5):
    56     for i in xrange(1, 5):
    41         ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i]) == i)
    57         ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i]) == i)
    42 
    58 
    43     ensure(rm.maxrev == 4)
    59     ensure(rm.maxrev == 4)
    44     for i in xrange(1, 5):
    60     for i in xrange(1, 5):
    54         ensure(rm.rev2hsh(i) == genhsh(i))
    70         ensure(rm.rev2hsh(i) == genhsh(i))
    55         ensure(bool(rm.rev2flag(i) & revmap.sidebranchflag) == bool(i & 1))
    71         ensure(bool(rm.rev2flag(i) & revmap.sidebranchflag) == bool(i & 1))
    56 
    72 
    57     # append without calling save() explicitly
    73     # append without calling save() explicitly
    58     for i in xrange(5, 12):
    74     for i in xrange(5, 12):
    59         ensure(rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i],
    75         ensure(
    60                          flush=True) == i)
    76             rm.append(genhsh(i), sidebranch=(i & 1), path=paths[i], flush=True)
       
    77             == i
       
    78         )
    61 
    79 
    62     # re-load and verify
    80     # re-load and verify
    63     rm = revmap.revmap(path)
    81     rm = revmap.revmap(path)
    64     ensure(rm.maxrev == 11)
    82     ensure(rm.maxrev == 11)
    65     for i in xrange(1, 12):
    83     for i in xrange(1, 12):
    83         rm.append(b'\0')
   101         rm.append(b'\0')
    84         ensure(False)
   102         ensure(False)
    85     except Exception:
   103     except Exception:
    86         pass
   104         pass
    87 
   105 
       
   106 
    88 def testcorruptformat():
   107 def testcorruptformat():
    89     path = gettemppath()
   108     path = gettemppath()
    90 
   109 
    91     # incorrect header
   110     # incorrect header
    92     with open(path, 'wb') as f:
   111     with open(path, 'wb') as f:
   125     except error.CorruptedFileError:
   144     except error.CorruptedFileError:
   126         pass
   145         pass
   127 
   146 
   128     os.unlink(path)
   147     os.unlink(path)
   129 
   148 
       
   149 
   130 def testcopyfrom():
   150 def testcopyfrom():
   131     path = gettemppath()
   151     path = gettemppath()
   132     rm = revmap.revmap(path)
   152     rm = revmap.revmap(path)
   133     for i in xrange(1, 10):
   153     for i in xrange(1, 10):
   134         ensure(rm.append(genhsh(i),
   154         ensure(
   135                          sidebranch=(i & 1), path=(b'%d' % (i // 3))) == i)
   155             rm.append(genhsh(i), sidebranch=(i & 1), path=(b'%d' % (i // 3)))
       
   156             == i
       
   157         )
   136     rm.flush()
   158     rm.flush()
   137 
   159 
   138     # copy rm to rm2
   160     # copy rm to rm2
   139     rm2 = revmap.revmap()
   161     rm2 = revmap.revmap()
   140     rm2.copyfrom(rm)
   162     rm2.copyfrom(rm)
   146     ensure(len(set(util.readfile(p) for p in [path, path2])) == 1)
   168     ensure(len(set(util.readfile(p) for p in [path, path2])) == 1)
   147 
   169 
   148     os.unlink(path)
   170     os.unlink(path)
   149     os.unlink(path2)
   171     os.unlink(path2)
   150 
   172 
       
   173 
   151 class fakefctx(object):
   174 class fakefctx(object):
   152     def __init__(self, node, path=None):
   175     def __init__(self, node, path=None):
   153         self._node = node
   176         self._node = node
   154         self._path = path
   177         self._path = path
   155 
   178 
   156     def node(self):
   179     def node(self):
   157         return self._node
   180         return self._node
   158 
   181 
   159     def path(self):
   182     def path(self):
   160         return self._path
   183         return self._path
       
   184 
   161 
   185 
   162 def testcontains():
   186 def testcontains():
   163     path = gettemppath()
   187     path = gettemppath()
   164 
   188 
   165     rm = revmap.revmap(path)
   189     rm = revmap.revmap(path)
   178     for i in xrange(1, 5):
   202     for i in xrange(1, 5):
   179         ensure(rm.append(genhsh(i), path=(b'%d' % (i // 2))) == i)
   203         ensure(rm.append(genhsh(i), path=(b'%d' % (i // 2))) == i)
   180     for i in xrange(1, 5):
   204     for i in xrange(1, 5):
   181         ensure(fakefctx(genhsh(i), path=(b'%d' % (i // 2))) in rm)
   205         ensure(fakefctx(genhsh(i), path=(b'%d' % (i // 2))) in rm)
   182         ensure(fakefctx(genhsh(i), path=b'a') not in rm)
   206         ensure(fakefctx(genhsh(i), path=b'a') not in rm)
       
   207 
   183 
   208 
   184 def testlastnode():
   209 def testlastnode():
   185     path = gettemppath()
   210     path = gettemppath()
   186     ensure(revmap.getlastnode(path) is None)
   211     ensure(revmap.getlastnode(path) is None)
   187     rm = revmap.revmap(path)
   212     rm = revmap.revmap(path)
   191         rm.append(hsh, path=(b'%d' % (i // 2)), flush=True)
   216         rm.append(hsh, path=(b'%d' % (i // 2)), flush=True)
   192         ensure(revmap.getlastnode(path) == hsh)
   217         ensure(revmap.getlastnode(path) == hsh)
   193         rm2 = revmap.revmap(path)
   218         rm2 = revmap.revmap(path)
   194         ensure(rm2.rev2hsh(rm2.maxrev) == hsh)
   219         ensure(rm2.rev2hsh(rm2.maxrev) == hsh)
   195 
   220 
       
   221 
   196 testbasicreadwrite()
   222 testbasicreadwrite()
   197 testcorruptformat()
   223 testcorruptformat()
   198 testcopyfrom()
   224 testcopyfrom()
   199 testcontains()
   225 testcontains()
   200 testlastnode()
   226 testlastnode()