def test_skip_binary(): print("\nTest: skip_binary(binary)") success = "Pass" source = [{ 'in': '1100100', 'out': '1101000' }, { 'in': '0010001000', 'out': '0010010000' }, { 'in': '11111', 'out': '00000' }] my_cc = cc.CC() for src in source: src_in = src['in'] src_out = src['out'] result = my_cc.skip_binary(src_in) if result != src_out: success = "Fail" print(f" input = {src_in}") print(f"expected = {src_out}") print(f" result = {result}") print(result_msg(src_out, result)) return success
def test_has_bit(): print("\nTest: has_bit(binary)") source = [{ 'in': '01001', 'out': True }, { 'in': '1010', 'out': True }, { 'in': '0000000', 'out': False }] success = "Pass" my_cc = cc.CC() for src in source: src_in = src['in'] src_out = src['out'] result = my_cc.has_bit(src_in) if result != src_out: success = "Fail" print(f" input = {src_in}") print(f"expected = {src_out}") print(f" result = {result}") print(result_msg(src_out, result)) return success
def test_combo_size(): print("\nTest: combo_size(binary)") success = "Pass" source = { 'pieces': [{ 'size': 40 }, { 'size': 30 }, { 'size': 60 }], 'containers': [{ 'capacity': 300 }, { 'capacity': 200 }, { 'capacity': 150 }], 'loss': 0 } # sorted: [{'size': 60}, {'size': 30}, {'size': 20}] combos = ["001", "010", "100", "101", "011", "110", "111"] expected = [30, 40, 60, 90, 70, 100, 130] my_cc = cc.CC() my_cc.set_inputs(source['pieces'], source['containers'], source['loss']) print(f"pieces: {json.dumps(my_cc._pieces)}") print(f"loss = {my_cc._loss_per_piece}") for i, combo in enumerate(combos): exp = expected[i] res = my_cc.combo_size(combo) print(f" input = {combo}") print(f"expected = {exp}") print(f" result = {res}") if exp != res: success = "Fail" my_cc.set_inputs(source['pieces'], source['containers'], 0.25) expected = [30.25, 40.25, 60.25, 90.5, 70.5, 100.5, 130.75] print(f"pieces: {json.dumps(my_cc._pieces)}") print(f"loss = {my_cc._loss_per_piece}") for i, combo in enumerate(combos): exp = expected[i] res = my_cc.combo_size(combo) print(f" input = {combo}") print(f"expected = {exp}") print(f" result = {res}") if exp != res: success = "Fail" return success
def test_remove_combos(): print("\nTest: remove_combos(binary)") success = "Pass" source = { 'pieces': [{ 'size': 30 }, { 'size': 60 }, { 'size': 20 }], 'containers': [{ 'capacity': 300 }, { 'capacity': 200 }, { 'capacity': 150 }], 'loss': 0 } my_cc = cc.CC() my_cc.set_inputs(source['pieces'], source['containers'], source['loss']) binary = "010" print(f"binary = {binary}") print(f"before = {json.dumps(my_cc._piece_combos)}") my_cc.remove_combos(binary) expected = { '001': { 'combo_size': 20 }, '100': { 'combo_size': 60 }, '101': { 'combo_size': 80 }, } result = my_cc._piece_combos exp = json.dumps(expected) res = json.dumps(result) print(f"expected = {exp}") print(f" output = {res}") if exp != res: success = "Fail" return success
def test_to_binary(): print("\nTest: to_binary(value, num_bits)") success = "Pass" source = [ { 'p1': 3205, 'p2': 12, 'out': '110010000101' }, { 'p1': 55, 'p2': 6, 'out': '110111' }, { 'p1': 55, 'p2': 12, 'out': '000000110111' }, { 'p1': 3205, 'p2': 5, 'out': '110010000101' }, { 'p1': 1, 'p2': 12, 'out': '000000000001' }, ] my_cc = cc.CC() for src in source: param_1 = src['p1'] param_2 = src['p2'] src_out = src['out'] result = my_cc.to_binary(param_1, param_2) if result != src_out: success = "Fail" print(f" input = {param_1}, {param_2}") print(f"expected = {src_out}") print(f" result = {result}") print(result_msg(src_out, result)) return success
def test_to_integer(): print("\nTest: to_integer(binary)") success = "Pass" source = [{ 'in': '1011', 'out': 11 }, { 'in': '100011', 'out': 35 }, { 'in': '0000000', 'out': 0 }, { 'in': '1001001110001', 'out': 4721 }, { 'in': '00001011', 'out': 11 }, { 'in': '0000100011', 'out': 35 }, { 'in': '00001001001110001', 'out': 4721 }] my_cc = cc.CC() for src in source: src_in = src['in'] src_out = src['out'] result = my_cc.to_integer(src_in) if result != src_out: success = "Fail" print(f" input = {src_in}") print(f"expected = {src_out}") print(f" result = {result}") print(result_msg(src_out, result)) return success
def test_has_common_bit(): print("\nTest: has_common_bit(bin_1, bin_2)") success = "Pass" source = [{ 'p1': '0', 'p2': '0', 'out': False }, { 'p1': '0', 'p2': '1', 'out': False }, { 'p1': '100110', 'p2': '000100', 'out': True }, { 'p1': '0100110011', 'p2': '0010', 'out': True }, { 'p1': '1001001', 'p2': '0110110', 'out': False }] my_cc = cc.CC() for src in source: param_1 = src['p1'] param_2 = src['p2'] src_out = src['out'] result = my_cc.has_common_bit(param_1, param_2) if result != src_out: success = "Fail" print(f" input = {param_1}, {param_2}") print(f"expected = {src_out}") print(f" result = {result}") print(result_msg(src_out, result)) return success
def test_filter_pieces(): print("\nTest: filter_pieces(combo)") success = "Pass" source = { 'pieces': [{ 'size': 30 }, { 'size': 20 }, { 'size': 60 }], 'containers': [{ 'capacity': 300 }, { 'capacity': 200 }, { 'capacity': 150 }], 'loss': 0 } my_cc = cc.CC() my_cc.set_inputs(source['pieces'], source['containers'], source['loss']) # sorted: [{'size': 60}, {'size': 30}, {'size': 20}] combos = ["001", "010", "100", "101", "011", "110", "111"] expected = [[{ 'size': 20 }], [{ 'size': 30 }], [{ 'size': 60 }], [{ 'size': 60 }, { 'size': 20 }], [{ 'size': 30 }, { 'size': 20 }], [{ 'size': 60 }, { 'size': 30 }], [{ 'size': 60 }, { 'size': 30 }, { 'size': 20 }]] print(f"pieces: {json.dumps(my_cc._pieces)}") for i, combo in enumerate(combos): exp = json.dumps(expected[i]) res = json.dumps(my_cc.filter_pieces(combo)) print(f" input = {combo}") print(f"expected = {exp}") print(f" result = {res}") if exp != res: success = "Fail" return success
def test_build_piece_combos(): print("\nTest: build_piece_combos()") success = "Pass" source = { 'pieces': [{ 'size': 30 }, { 'size': 60 }, { 'size': 20 }], 'containers': [{ 'capacity': 300 }, { 'capacity': 200 }, { 'capacity': 150 }], 'loss': 0 } # sorted: [{'size': 60}, {'size': 30}, {'size': 20}] expected = { '001': { 'combo_size': 20 }, '010': { 'combo_size': 30 }, '011': { 'combo_size': 50 }, '100': { 'combo_size': 60 }, '101': { 'combo_size': 80 }, '110': { 'combo_size': 90 }, '111': { 'combo_size': 110 } } my_cc = cc.CC() my_cc.set_inputs(source['pieces'], source['containers'], source['loss']) result = my_cc._piece_combos exp = json.dumps(expected) res = json.dumps(result) for key in source: src = json.dumps(source[key]) print(f" input {key} = {src}") print(f"expected combos = {exp}") print(f" output combos = {res}") if res != exp: success = "Fail" return success
def test_set_inputs(): print("\nTest: set_inputs(pieces, containers, loss)") success = "Pass" source = { 'pieces': [{ 'size': 30 }, { 'size': 60 }, { 'size': 20 }, { 'size': 40 }], 'containers': [{ 'capacity': 300 }, { 'capacity': 200 }, { 'capacity': 150 }], 'loss': 0.25 } expected = { 'pieces': [{ 'size': 60 }, { 'size': 40 }, { 'size': 30 }, { 'size': 20 }], 'containers': [{ 'capacity': 150 }, { 'capacity': 200 }, { 'capacity': 300 }], 'loss': 0.25 } my_cc = cc.CC() my_cc.set_inputs(source['pieces'], source['containers'], source['loss']) result = { 'pieces': my_cc._pieces, 'containers': my_cc._containers, 'loss': my_cc._loss_per_piece } for key in source: src = json.dumps(source[key]) exp = json.dumps(expected[key]) res = json.dumps(result[key]) print(f" input {key} = {src}") print(f"expected {key} = {exp}") print(f" output {key} = {res}") if exp != res: success = "Fail" return success
def test_sort(): print(f"\nTest: sort()") success = "Pass" pieces = [ { 'size': 10 }, { 'size': 10 }, { 'size': 10 }, { 'size': 48 }, { 'size': 48 }, { 'size': 48 }, { 'size': 30 }, { 'size': 30 }, { 'size': 30 }, ] containers = [ { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, { 'capacity': 100 }, ] expected = [{ 'pieces': [{ 'size': 10 }, { 'size': 30 }, { 'size': 30 }, { 'size': 30 }], 'delta': 0 }, { 'pieces': [{ 'size': 48 }, { 'size': 48 }], 'delta': 4 }, { 'pieces': [{ 'size': 10 }, { 'size': 10 }, { 'size': 48 }], 'delta': 32 }] my_cc = cc.CC() print("No loss per piece") my_cc.set_inputs(pieces, containers) result = my_cc.sort() # massage data to compare with expected data = sorted(result['data'], key=lambda i: i['delta']) to_compare = [] for item in data: to_compare.append({ 'pieces': sorted(item['pieces'], key=lambda i: i['size']), 'delta': item['delta'] }) for i, item in enumerate(expected): exp = json.dumps(item) res = json.dumps(to_compare[i]) print(f"expected = {exp}") print(f" output = {res}") if exp != res: success = "Fail" print("With loss per piece") expected = [{ 'pieces': [{ 'size': 10 }, { 'size': 10 }, { 'size': 30 }, { 'size': 48 }], 'delta': 1.0 }, { 'pieces': [{ 'size': 48 }, { 'size': 48 }], 'delta': 3.5 }, { 'pieces': [{ 'size': 10 }, { 'size': 30 }, { 'size': 30 }], 'delta': 29.25 }] my_cc.set_inputs(pieces, containers, 0.25) result = my_cc.sort() # massage data to compare with expected data = sorted(result['data'], key=lambda i: i['delta']) to_compare = [] for item in data: to_compare.append({ 'pieces': sorted(item['pieces'], key=lambda i: i['size']), 'delta': item['delta'] }) for i, item in enumerate(expected): exp = json.dumps(item) res = json.dumps(to_compare[i]) print(f"expected = {exp}") print(f" output = {res}") if exp != res: success = "Fail" return success
def test_best_match(): print("\nTest: best_match()") success = "Pass" source = { 'pieces': [{ 'size': 30 }, { 'size': 60 }, { 'size': 20 }, { 'size': 40 }, { 'size': 50 }], 'containers': [{ 'capacity': 85 }, { 'capacity': 90 }, { 'capacity': 110 }], 'loss': 0.25 } # sorted: [{'size': 60}, {'size': 50}, {'size': 40}, {'size': 30}, {'size': 20}] expected = { 'binary': "10001", 'combo': { 'combo_size': 80.5, }, 'pieces': [{ 'size': 60 }, { 'size': 20 }], 'container': { 'capacity': 85 }, 'delta': 4.5 } expected['remaining_containers'] = [{'capacity': 90}, {'capacity': 110}] my_cc = cc.CC() my_cc.set_inputs(source['pieces'], source['containers'], source['loss']) print(f"pieces: {json.dumps(my_cc._pieces)}") print(f"containers: {json.dumps(my_cc._containers)}") result = my_cc.best_match() result['remaining_containers'] = my_cc._containers for key in expected: exp = json.dumps(expected[key]) res = json.dumps(result[key]) print(f"expected {key} = {exp}") print(f" output {key} = {res}") if exp != res: success = "Fail" return success