test-revlog-raw: fix "genbits" implementation

The "genbits" implementation is actually incorrect. This patch fixes it. A
good "genbits" implementation should pass the below assertion:

    n = 3 # or other number
    l = list(genbits(n))
    assert 2**(n*2) == len(set((l[i]<<n)+l[i+1] for i in range(len(l)-1)))

An assertion is added to make sure "genbits" won't work unexpectedly.
This commit is contained in:
Jun Wu 2017-04-02 18:12:47 -07:00
parent 7d2129ac1d
commit 09bd75fdff

View File

@ -166,6 +166,7 @@ def genbits(n):
# Gray Code. See https://en.wikipedia.org/wiki/Gray_code
gray = lambda x: x ^ (x >> 1)
reversegray = dict((gray(i), i) for i in range(m))
# Generate (n * 2) bit gray code, yield lower n bits as X, and look for
# the next unused gray code where higher n bits equal to X.
@ -177,7 +178,9 @@ def genbits(n):
x = 0
yield x
for i in range(m * m):
x = reversegray[x]
y = gray(a[x] + x * m) & (m - 1)
assert a[x] < m
a[x] += 1
x = y
yield x