def test_vectors(self): here = os.path.dirname(__file__) jf = os.path.abspath( os.path.join(here, '../../../test-vectors/appendix_a.json')) if not os.path.exists(jf): logging.warning( 'cannot find test-vectors/appendix_a.json, tried: %r', jf) return if _IS_PY3: testfile = open(jf, 'r') tv = json.load(testfile) else: testfile = open(jf, 'rb') tv = json.load(testfile) anyerr = False for row in tv: rhex = row.get('hex') if 'decoded' in row: decoded = row['decoded'] _check(row, decoded) continue elif 'diagnostic' in row: diag = row['diagnostic'] checkf = _DIAGNOSTIC_TESTS.get(diag) if checkf is not None: _check_foo(row, checkf) continue # variously verbose log of what we're not testing: cbdata = base64.b64decode(row['cbor']) try: pd = pyloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to py load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) pd = '' cd = None if cloads is not None: try: cd = cloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to c load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) cd = '' logging.warning('skipping hex=%s diag=%r py=%s c=%s', rhex, row.get('diagnostic'), pd, cd) testfile.close() assert not anyerr
def _check_foo(row, checkf): cbdata = base64.b64decode(row['cbor']) if cloads is not None: cb = cloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write('expected {0!r} got {1!r} c failed to decode cbor {2}\n'.format(decoded, cb, base64.b16encode(cbdata))) cb = pyloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write('expected {0!r} got {1!r} py failed to decode cbor {2}\n'.format(decoded, cb, base64.b16encode(cbdata)))
def test_loads_usage(self): ''' repeatedly serialize, check that usage doesn't go up ''' if (cdumps is None) or (cloads is None): logger.warn('no C fast CBOR, skipping test_loads_usage') return ## Just a string passes! #ob = 'sntaoheusnatoheusnaotehuasnoetuhaosentuhaoesnth' ## Just an array passes! #ob = [1,2,3,4,5,6,7,8,9,12,12,13] ## Just a dict passes! #ob = {'a':'b', 'c':'d', 'e':'f', 'g':'h'} # dict of dict is doom! #ob = {'a':{'b':'c', 'd':'e', 'f':'g'}, 'x':'p'} ob = { 'aoeu': [1, 2, 3, 4], 'foo': 'bar', 'pants': { 'foo': 0xb44, 'pi': 3.14 }, 'flubber': [{ 'x': 'y', 'z': [None, 2, []] }, 2, 'hello'] } blob = cdumps(ob) start_usage = resource.getrusage(resource.RUSAGE_SELF) usage_history = [start_usage] for o in _range(_TEST_OUTER): for i in _range(_TEST_COUNT): dob = cloads(blob) # and silently drop the result. I hope the garbage collector works! t_usage = resource.getrusage(resource.RUSAGE_SELF) usage_history.append(t_usage) end_usage = usage_history[-1] dmaxrss = end_usage.ru_maxrss - start_usage.ru_maxrss didrss = end_usage.ru_idrss - start_usage.ru_idrss dmaxrsspct = ((end_usage.ru_maxrss != 0) and (dmaxrss / end_usage.ru_maxrss)) or 0 didrsspct = ((end_usage.ru_idrss != 0) and (didrss / end_usage.ru_idrss)) or 0 sys.stderr.write('maxrss: {} - {}, d={} ({:.2f}%)\n'.format( start_usage.ru_maxrss, end_usage.ru_maxrss, dmaxrss, dmaxrsspct * 100.0)) sys.stderr.write('idrss: {} - {}, d={} ({:.2f}%)\n'.format( start_usage.ru_idrss, end_usage.ru_idrss, didrss, didrsspct * 100.0)) assert (dmaxrsspct) < 0.05, [x.ru_maxrss for x in usage_history] assert (didrsspct) < 0.05, [x.ru_idrss for x in usage_history]
def test_vectors(self): here = os.path.dirname(__file__) jf = os.path.abspath(os.path.join(here, '../../../test-vectors/appendix_a.json')) if not os.path.exists(jf): logging.warning('cannot find test-vectors/appendix_a.json, tried: %r', jf) return if _IS_PY3: testfile = open(jf, 'r') tv = json.load(testfile) else: testfile = open(jf, 'rb') tv = json.load(testfile) anyerr = False for row in tv: rhex = row.get('hex') if 'decoded' in row: decoded = row['decoded'] _check(row, decoded) continue elif 'diagnostic' in row: diag = row['diagnostic'] checkf = _DIAGNOSTIC_TESTS.get(diag) if checkf is not None: _check_foo(row, checkf) continue # variously verbose log of what we're not testing: cbdata = base64.b64decode(row['cbor']) try: pd = pyloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to py load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) pd = '' cd = None if cloads is not None: try: cd = cloads(cbdata) except: if rhex and (rhex in _EXPECT_EXCEPTION): pass else: logging.error('failed to c load hex=%s diag=%r', rhex, row.get('diagnostic'), exc_info=True) cd = '' logging.warning('skipping hex=%s diag=%r py=%s c=%s', rhex, row.get('diagnostic'), pd, cd) testfile.close() assert not anyerr
def _check_foo(row, checkf): cbdata = base64.b64decode(row['cbor']) if cloads is not None: cb = cloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write( 'expected {0!r} got {1!r} c failed to decode cbor {2}\n'. format(decoded, cb, base64.b16encode(cbdata))) cb = pyloads(cbdata) if not checkf(cb): anyerr = True sys.stderr.write( 'expected {0!r} got {1!r} py failed to decode cbor {2}\n'.format( decoded, cb, base64.b16encode(cbdata)))
def test_loads_usage(self): ''' repeatedly serialize, check that usage doesn't go up ''' if (cdumps is None) or (cloads is None): logger.warn('no C fast CBOR, skipping test_loads_usage') return ## Just a string passes! #ob = 'sntaoheusnatoheusnaotehuasnoetuhaosentuhaoesnth' ## Just an array passes! #ob = [1,2,3,4,5,6,7,8,9,12,12,13] ## Just a dict passes! #ob = {'a':'b', 'c':'d', 'e':'f', 'g':'h'} # dict of dict is doom! #ob = {'a':{'b':'c', 'd':'e', 'f':'g'}, 'x':'p'} ob = {'aoeu':[1,2,3,4],'foo':'bar','pants':{'foo':0xb44, 'pi':3.14}, 'flubber': [{'x':'y', 'z':[None, 2, []]}, 2, 'hello']} blob = cdumps(ob) start_usage = resource.getrusage(resource.RUSAGE_SELF) usage_history = [start_usage] for o in _range(_TEST_OUTER): for i in _range(_TEST_COUNT): dob = cloads(blob) # and silently drop the result. I hope the garbage collector works! t_usage = resource.getrusage(resource.RUSAGE_SELF) usage_history.append(t_usage) end_usage = usage_history[-1] dmaxrss = end_usage.ru_maxrss - start_usage.ru_maxrss didrss = end_usage.ru_idrss - start_usage.ru_idrss dmaxrsspct = ((end_usage.ru_maxrss != 0) and (dmaxrss / end_usage.ru_maxrss)) or 0 didrsspct = ((end_usage.ru_idrss != 0) and (didrss / end_usage.ru_idrss)) or 0 sys.stderr.write('maxrss: {} - {}, d={} ({:.2f}%)\n'.format(start_usage.ru_maxrss, end_usage.ru_maxrss, dmaxrss, dmaxrsspct * 100.0)) sys.stderr.write('idrss: {} - {}, d={} ({:.2f}%)\n'.format(start_usage.ru_idrss, end_usage.ru_idrss, didrss, didrsspct * 100.0)) assert (dmaxrsspct) < 0.05, [x.ru_maxrss for x in usage_history] assert (didrsspct) < 0.05, [x.ru_idrss for x in usage_history]