From 37342632a8bb95fe68ece90d18b8b69263edb83b Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Fri, 4 Oct 2019 18:30:26 +0100 Subject: [PATCH] */t/*.py: Fix the various testing scripts for Python 3 compatibility. * Put parentheses around `print' arguments. (This is most of the churn.) * Cope with `xrange' being renamed to `range' in Python 3. * Cope with `MAP.keys()' not returning a list any more in Python 3. * Cope with integer division not returning an integer any more in Python 3. (Use a shift, because that's not changed.) --- hash/t/unihash-testgen.py | 11 +++++++---- struct/t/da-gtest.py | 2 ++ struct/t/sym-gtest.py | 4 +++- utils/t/bits-testgen.py | 22 ++++++++++++---------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/hash/t/unihash-testgen.py b/hash/t/unihash-testgen.py index 2fe8982..1fde21f 100644 --- a/hash/t/unihash-testgen.py +++ b/hash/t/unihash-testgen.py @@ -3,6 +3,9 @@ ### ### Generate test vectors for universal hashing. +import sys as SYS +if SYS.version_info >= (3,): xrange = range + MOD = 0x104c11db7 def gfmul(x, y): @@ -17,13 +20,13 @@ def gfmul(x, y): def hashtest(k, m): h = k for ch in m: h = gfmul(h ^ ord(ch), k) - print ' 0x%08x "%s" 0x%08x;' % (k, m, h) + print(' 0x%08x "%s" 0x%08x;' % (k, m, h)) -print '''\ +print('''\ ### Test vectors for universal hashing ### [generated] -hash {''' +hash {''') for k, m in [(0x00000000, 'anything you like'), (0x12345678, 'an exaple test string'), @@ -36,4 +39,4 @@ for i in xrange(48): hashtest(k, "If we don't succeed, we run the risk of failure.") k = gfmul(k, m) -print '}' +print('}') diff --git a/struct/t/da-gtest.py b/struct/t/da-gtest.py index 14bc0cc..49c26dc 100644 --- a/struct/t/da-gtest.py +++ b/struct/t/da-gtest.py @@ -6,6 +6,8 @@ import sys as SYS import random as R +if SYS.version_info >= (3,): xrange = range + ###-------------------------------------------------------------------------- ### Command-line parsing. diff --git a/struct/t/sym-gtest.py b/struct/t/sym-gtest.py index ea69034..381ea3a 100644 --- a/struct/t/sym-gtest.py +++ b/struct/t/sym-gtest.py @@ -6,6 +6,8 @@ import sys as SYS import random as R +if SYS.version_info >= (3,): xrange = range + ###-------------------------------------------------------------------------- ### Command-line parsing. @@ -116,7 +118,7 @@ def op_show(): if not MAP: WIN.write('*EMPTY*\n') else: - kk = MAP.keys() + kk = list(MAP.keys()) kk.sort() WIN.write(' '.join(['%s:%d' % (k, MAP[k]) for k in kk]) + '\n') diff --git a/utils/t/bits-testgen.py b/utils/t/bits-testgen.py index 05e4baf..c43f496 100644 --- a/utils/t/bits-testgen.py +++ b/utils/t/bits-testgen.py @@ -7,6 +7,8 @@ import sys as SYS import random as R +if SYS.version_info >= (3,): xrange = range + NVEC = 64 WD = 64 LIMIT = 1 << WD @@ -23,33 +25,33 @@ if seed is None: SEED = R.randrange(0, 1 << 32) else: SEED = int(seed, 0) R.seed(SEED) -print '### Test vectors for 64-bit arithmetic macros' -print '### [generated; seed = 0x%08x]' % SEED +print('### Test vectors for 64-bit arithmetic macros') +print('### [generated; seed = 0x%08x]' % SEED) def rol(x, n): return ((x << n) | (x >> (WD - n))) & MASK def ror(x, n): return ((x >> n) | (x << (WD - n))) & MASK -def put(x): return '%0*x' % (WD/4, x) +def put(x): return '%0*x' % (WD//4, x) for name, func in [('lsl', lambda x, n: x << n), ('lsr', lambda x, n: x >> n), ('rol', rol), ('ror', ror)]: - print '\n%s64 {' % name + print('\n%s64 {' % name) for i in xrange(NVEC): x = R.randrange(LIMIT) sh = R.randrange(0, 70) & 63 - print ' %s %2d %s;' % (put(x), sh, put(func(x, sh) & MASK)) + print(' %s %2d %s;' % (put(x), sh, put(func(x, sh) & MASK))) for i in xrange(4): x = R.randrange(LIMIT) sh = 32 - print ' %s %2d %s;' % (put(x), sh, put(func(x, sh) & MASK)) - print '}' + print(' %s %2d %s;' % (put(x), sh, put(func(x, sh) & MASK))) + print('}') for name, func in [('add', lambda x, y: x + y), ('sub', lambda x, y: x - y)]: - print '\n%s64 {' % name + print('\n%s64 {' % name) for i in xrange(NVEC): x = R.randrange(LIMIT) y = R.randrange(LIMIT) - print ' %s %s %s;' % (put(x), put(y), put(func(x, y) & MASK)) - print '}' + print(' %s %s %s;' % (put(x), put(y), put(func(x, y) & MASK))) + print('}') -- 2.11.0