def encode2decode(): encoderTable = Table() decoderTable = Table() for i in range(len(TESTCASE)): stories = [TESTCASE[i] + name for name in os.listdir(TESTCASE[i])] for story in stories: allPass = True with open(story) as f: data = json.loads(f.read()) for seqno in range(len(data["cases"])): if data["cases"][seqno].has_key("header_table_size"): encoderTable.setHeaderTableSize(int(data["cases"][seqno]["header_table_size"])) headers = [[h.keys()[0], h.values()[0]] for h in data["cases"][seqno]["headers"]] wire = encode(headers, 'static' in story or 'linear' in story, 'linear' in story, 'huffman' in story, encoderTable) #init header table or not try: decodedHeaders = decode(wire, decoderTable) if decodedHeaders != data["cases"][seqno]["headers"]: allPass = False print('Missed in %s seqno %d' % (story, seqno)) break except Exception as e: print("Error at %s seqno %d" % (story, seqno)) allPass = False break if allPass: print("Passed the %s" % story)
def encodeTest(): table = Table() for i in range(len(TESTCASE)): cases = [TESTCASE[i] + name for name in os.listdir(TESTCASE[i])] for case in cases: allPass = True with open(case) as f: data = json.loads(f.read()) for seqno in range(len(data["cases"])): if data["cases"][seqno].has_key("header_table_size"): table.setHeaderTableSize(int(data["cases"][seqno]["header_table_size"])) headers = [[h.keys()[0], h.values()[0]]for h in data["cases"][seqno]["headers"]] code = encode(headers, 'static' in case or 'linear' in case, 'linear' in case, 'huffman' in case, table) #init header table or not if code != unhexlify(data["cases"][seqno]["wire"]): allPass = False print('encoder: %s' % code) print('answer: %s' % data["cases"][seqno]["wire"]) print("Missed in %s seqno %d" % (case, seqno)) break if allPass: print('Passed the %s' % case)