MCPcopy Index your code
hub / github.com/python/cpython / test_one

Method test_one

Lib/test/test_struct.py:211–247  ·  view source on GitHub ↗
(self, x, pack=struct.pack,
                                  unpack=struct.unpack,
                                  unhexlify=binascii.unhexlify)

Source from the content-addressed store, hash-verified

209 self.code)
210
211 def test_one(self, x, pack=struct.pack,
212 unpack=struct.unpack,
213 unhexlify=binascii.unhexlify):
214
215 format = self.format
216 if self.min_value <= x <= self.max_value:
217 expected = x
218 if self.signed and x < 0:
219 expected += 1 << self.bitsize
220 self.assertGreaterEqual(expected, 0)
221 expected = '%x' % expected
222 if len(expected) & 1:
223 expected = "0" + expected
224 expected = expected.encode('ascii')
225 expected = unhexlify(expected)
226 expected = (b"\x00" * (self.bytesize - len(expected)) +
227 expected)
228 if (self.byteorder == '<' or
229 self.byteorder in ('', '@', '=') and not ISBIGENDIAN):
230 expected = string_reverse(expected)
231 self.assertEqual(len(expected), self.bytesize)
232
233 # Pack work?
234 got = pack(format, x)
235 self.assertEqual(got, expected)
236
237 # Unpack work?
238 retrieved = unpack(format, got)[0]
239 self.assertEqual(x, retrieved)
240
241 # Adding any byte should cause a "too big" error.
242 self.assertRaises((struct.error, TypeError), unpack, format,
243 b'\x01' + got)
244 else:
245 # x is out of range -- verify pack realizes that.
246 self.assertRaises((OverflowError, ValueError, struct.error),
247 pack, format, x)
248
249 def run(self):
250 from random import randrange

Callers 1

runMethod · 0.95

Calls 6

string_reverseFunction · 0.85
packFunction · 0.85
assertGreaterEqualMethod · 0.80
encodeMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected