test_struct.py
来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Python 代码 · 共 442 行 · 第 1/2 页
PY
442 行
if x < 0: expected += 1L << self.bitsize assert expected > 0 expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' if len(expected) & 1: expected = "0" + expected expected = unhexlify(expected) expected = "\x00" * (self.bytesize - len(expected)) + expected # Pack work? format = ">" + code got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) # Try little-endian. format = "<" + code expected = string_reverse(expected) # Pack work? got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) else: # x is out of range -- verify pack realizes that. if code in self.BUGGY_RANGE_CHECK: if verbose: print "Skipping buggy range check for code", code else: any_err(pack, ">" + code, x) any_err(pack, "<" + code, x) # Much the same for unsigned. code = self.unsigned_code if self.unsigned_min <= x <= self.unsigned_max: # Try big-endian. format = ">" + code expected = long(x) expected = hex(expected)[2:-1] # chop "0x" and trailing 'L' if len(expected) & 1: expected = "0" + expected expected = unhexlify(expected) expected = "\x00" * (self.bytesize - len(expected)) + expected # Pack work? got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) # Try little-endian. format = "<" + code expected = string_reverse(expected) # Pack work? got = pack(format, x) verify(got == expected, "'%s'-pack of %r gave %r, not %r" % (format, x, got, expected)) # Unpack work? retrieved = unpack(format, got)[0] verify(x == retrieved, "'%s'-unpack of %r gave %r, not %r" % (format, got, retrieved, x)) # Adding any byte should cause a "too big" error. any_err(unpack, format, '\x01' + got) else: # x is out of range -- verify pack realizes that. if code in self.BUGGY_RANGE_CHECK: if verbose: print "Skipping buggy range check for code", code else: any_err(pack, ">" + code, x) any_err(pack, "<" + code, x) def run(self): from random import randrange # Create all interesting powers of 2. values = [] for exp in range(self.bitsize + 3): values.append(1L << exp) # Add some random values. for i in range(self.bitsize): val = 0L for j in range(self.bytesize): val = (val << 8) | randrange(256) values.append(val) # Try all those, and their negations, and +-1 from them. Note # that this tests all power-of-2 boundaries in range, and a few out # of range, plus +-(2**n +- 1). for base in values: for val in -base, base: for incr in -1, 0, 1: x = val + incr try: x = int(x) except OverflowError: pass self.test_one(x) # Some error cases. for direction in "<>": for code in self.formatpair: for badobject in "a string", 3+42j, randrange: any_err(struct.pack, direction + code, badobject)for args in [("bB", 1), ("hH", 2), ("iI", 4), ("lL", 4), ("qQ", 8)]: t = IntTester(*args) t.run()############################################################################ The p ("Pascal string") code.def test_p_code(): for code, input, expected, expectedback in [ ('p','abc', '\x00', ''), ('1p', 'abc', '\x00', ''), ('2p', 'abc', '\x01a', 'a'), ('3p', 'abc', '\x02ab', 'ab'), ('4p', 'abc', '\x03abc', 'abc'), ('5p', 'abc', '\x03abc\x00', 'abc'), ('6p', 'abc', '\x03abc\x00\x00', 'abc'), ('1000p', 'x'*1000, '\xff' + 'x'*999, 'x'*255)]: got = struct.pack(code, input) if got != expected: raise TestFailed("pack(%r, %r) == %r but expected %r" % (code, input, got, expected)) (got,) = struct.unpack(code, got) if got != expectedback: raise TestFailed("unpack(%r, %r) == %r but expected %r" % (code, input, got, expectedback))test_p_code()############################################################################ SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry# from the low-order discarded bits could propagate into the exponent# field, causing the result to be wrong by a factor of 2.def test_705836(): import math for base in range(1, 33): # smaller <- largest representable float less than base. delta = 0.5 while base - delta / 2.0 != base: delta /= 2.0 smaller = base - delta # Packing this rounds away a solid string of trailing 1 bits. packed = struct.pack("<f", smaller) unpacked = struct.unpack("<f", packed)[0] # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and # 16, respectively. verify(base == unpacked) bigpacked = struct.pack(">f", smaller) verify(bigpacked == string_reverse(packed), ">f pack should be byte-reversal of <f pack") unpacked = struct.unpack(">f", bigpacked)[0] verify(base == unpacked) # Largest finite IEEE single. big = (1 << 24) - 1 big = math.ldexp(big, 127 - 23) packed = struct.pack(">f", big) unpacked = struct.unpack(">f", packed)[0] verify(big == unpacked) # The same, but tack on a 1 bit so it rounds up to infinity. big = (1 << 25) - 1 big = math.ldexp(big, 127 - 24) try: packed = struct.pack(">f", big) except OverflowError: pass else: TestFailed("expected OverflowError")test_705836()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?