diff --git a/src/binary_converter.py b/src/binary_converter.py index 57139210..8bd6fbfc 100644 --- a/src/binary_converter.py +++ b/src/binary_converter.py @@ -5,3 +5,53 @@ # Insert first number: 8 # The binary number is: 1000 # + + +def division(a: int) -> str: + b = "" + while a > 0: + if a % 2 == 0: + b = "0" + b + else: + b = "1" + b + a = a // 2 + return f"{b}" + + +def carry(a: str) -> str: + result_list = list(a) # conversione dell'array in lista + + for i in range(len(result_list) - 1, -1, -1): # gestione del riporto di 1 + if result_list[i] == "0": + result_list[i] = "1" + break + result_list[i] = "0" + + a = "".join(result_list) + return a + + +def invert(res: str) -> str: + res = res.zfill(16) + res = res.replace("0", "&").replace("1", "0").replace("&", "1") + return res + + +if __name__ == "__main__": + result = "" + input = int(input("Inserisci il numero da convertire: ")) + + if input == 0: + print("0") + + elif input < 0: + input = -input + result = division(input) + result = invert(result) + + print(f"Stringa modificata: {carry(result)}") + print("Il seguente numero è calcolato con la tecnica del complemento a due.") + print("Il risultato è rappresentato in 16 bit.") + + elif input >= 0: + print(division(input)) diff --git a/tests/test_binary_conv.py b/tests/test_binary_conv.py new file mode 100644 index 00000000..99123878 --- /dev/null +++ b/tests/test_binary_conv.py @@ -0,0 +1,49 @@ +from typing import List +import pytest +from src import binary_converter + +parametri: List[dict] = [ + {"a": 5, "exp_ris": f"{bin(5)[2:]}", "bin_post_carry": list("110")}, + {"a": 1, "exp_ris": f"{bin(1)[2:]}", "bin_post_carry": list("0")}, + {"a": 8, "exp_ris": f"{bin(8)[2:]}", "bin_post_carry": list("1001")}, + {"a": 111, "exp_ris": f"{bin(111)[2:]}", "bin_post_carry": list("1110000")}, + {"a": 2.5, "exp_ris": f"{bin(round(2.5))[2:]}", "bin_post_carry": list("11")}, + { + "a": 4.0000000, + "exp_ris": f"{bin(int(4.0000000))[2:]}", + "bin_post_carry": list("101"), + }, + {"a": -1, "exp_ris": f"{bin(-1)[3:]}", "bin_post_carry": list("0")}, + { + "a": 101.0110, + "exp_ris": f"{bin(int(101.0110))[2:]}", + "bin_post_carry": list("1100110"), + }, +] + +param_invert: List[dict] = [ + {"input": "1", "exp_out": "1111111111111110"}, + {"input": "", "exp_out": "1111111111111111"}, + {"input": "10101010", "exp_out": "1111111101010101"}, + {"input": "1010101010101010", "exp_out": "0101010101010101"}, + {"input": "1111111111111111", "exp_out": "0000000000000000"}, + {"input": "10101010101010101", "exp_out": "01010101010101010"}, +] + + +@pytest.mark.parametrize("test", parametri) +def test_division(test: dict) -> int: + act_ris = binary_converter.division(int(abs(test["a"]))) + assert test["exp_ris"] == act_ris + + +@pytest.mark.parametrize("test", parametri) +def test_carry(test: dict) -> str: + act_ris = binary_converter.carry(test["exp_ris"]) + assert "".join(test["bin_post_carry"]) == act_ris + + +@pytest.mark.parametrize("test", param_invert) +def test_invert(test: dict) -> str: + act_ris = binary_converter.invert(test["input"]) + assert test["exp_out"] == act_ris diff --git a/tests/test_sum.py b/tests/test_sum.py deleted file mode 100644 index 4874e5c8..00000000 --- a/tests/test_sum.py +++ /dev/null @@ -1,7 +0,0 @@ -from src.sum import sum - - -def test_sum() -> None: - assert sum(3, 4) == 7 - assert sum(0, 0) == 0 - assert sum(1, 2) == 3