-
Notifications
You must be signed in to change notification settings - Fork 31
Feature_A_6 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Group_A
Are you sure you want to change the base?
Feature_A_6 #12
Changes from all commits
51d8fd8
13e6362
da50bbb
8527b63
bd1f65b
b1ef391
7e9cb82
777c85c
a8615e1
19f793b
09edc93
d7cb7e3
44d9aa2
cf66032
1cbdbe7
d345fcc
4b59734
3933476
e91eefe
8ff6efb
499ce2e
7739903
e6bc45e
0550227
6ecad9e
73f3e21
0c8c641
7a2dcc2
c89fc19
4936221
ff7f6fc
2a65dfb
76dac91
594be35
fc35335
a300249
0a848aa
68ad377
283e97b
b71c849
1ee3594
4577c53
81e499e
b7b62ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| name: Run Tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: ["**"] | ||
| pull_request: | ||
| branches: ["**"] | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.11" | ||
|
|
||
| - name: Install dependencies | ||
| run: pip install pytest pytest-cov | ||
|
|
||
| - name: Run base calculator tests | ||
| run: pytest test_calculator.py -v | ||
|
|
||
| - name: Run binary module tests | ||
| run: pytest tests/testbinary.py -v | ||
|
|
||
| - name: Coverage report | ||
| run: pytest tests/testbinary.py --cov=modules --cov-report=term-missing |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Virtual environment | ||
| .venv/ | ||
| env/ | ||
| venv/ | ||
|
|
||
| # Python cache | ||
| __pycache__/ | ||
| *.pyc | ||
| *.pyo | ||
| *.pyd | ||
|
|
||
| # Pytest cache | ||
| .pytest_cache/ | ||
| .coverage | ||
| htmlcov/ | ||
|
|
||
| # IDE | ||
| .vscode/ | ||
| .idea/ | ||
|
|
||
| # OS | ||
| .DS_Store | ||
| Thumbs.db |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # SE Calculator — Feature 5.6: Binary Number System | ||
|
|
||
| Group 6 | Branch: FeatureA_6 | Process: Classical Waterfall | ||
|
|
||
| ## Features | ||
| - Binary ↔ Decimal conversion | ||
| - Binary arithmetic (add, sub, mul, div) | ||
| - 1's and 2's complement | ||
| - Input validation with custom exceptions | ||
|
|
||
| ## Running Tests | ||
| pytest tests/testbinary.py -v | ||
|
|
||
| ## Input Format | ||
| - Binary: B'1010 | ||
| - Decimal: D'10 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from modules.binary import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| binary_to_decimal, decimal_to_binary, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| binary_add, binary_subtract, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| binary_multiply, binary_divide, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ones_complement, twos_complement, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Calculator: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def add(self, a, b): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return a + b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def subtract(self, a, b): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return a - b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def multiply(self, a, b): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return a * b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def add(self, a, b): return a + b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def subtract(self, a, b): return a - b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def multiply(self, a, b): return a * b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def divide(self, a, b): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if b == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("Division by zero") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return a / b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def evaluate(self, expression: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Route string expressions to the correct module. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Examples: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "bin(10)" -> "B'1010" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "dec(B'1010)" -> "D'10" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "1s(B'1010)" -> "B'0101" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "2s(B'0111)" -> "B'1001" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "B'011 + B'010" -> "B'101" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expr = expression.strip() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if re.match(r"^bin\(", expr, re.I): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val = re.search(r"\((.+)\)", expr).group(1).strip() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return decimal_to_binary(f"D'{val}" if not val.upper().startswith("D'") else val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if re.match(r"^dec\(", expr, re.I): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val = re.search(r"\((.+)\)", expr).group(1).strip() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return binary_to_decimal(val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if re.match(r"^1s\(", expr, re.I): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val = re.search(r"\((.+)\)", expr).group(1).strip() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ones_complement(val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if re.match(r"^2s\(", expr, re.I): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val = re.search(r"\((.+)\)", expr).group(1).strip() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+44
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val = re.search(r"\((.+)\)", expr).group(1).strip() | |
| return decimal_to_binary(f"D'{val}" if not val.upper().startswith("D'") else val) | |
| if re.match(r"^dec\(", expr, re.I): | |
| val = re.search(r"\((.+)\)", expr).group(1).strip() | |
| return binary_to_decimal(val) | |
| if re.match(r"^1s\(", expr, re.I): | |
| val = re.search(r"\((.+)\)", expr).group(1).strip() | |
| return ones_complement(val) | |
| if re.match(r"^2s\(", expr, re.I): | |
| val = re.search(r"\((.+)\)", expr).group(1).strip() | |
| m = re.search(r"\((.+)\)", expr) | |
| if not m: | |
| raise ValueError(f"Malformed expression: {expression}") | |
| val = m.group(1).strip() | |
| return decimal_to_binary(f"D'{val}" if not val.upper().startswith("D'") else val) | |
| if re.match(r"^dec\(", expr, re.I): | |
| m = re.search(r"\((.+)\)", expr) | |
| if not m: | |
| raise ValueError(f"Malformed expression: {expression}") | |
| val = m.group(1).strip() | |
| return binary_to_decimal(val) | |
| if re.match(r"^1s\(", expr, re.I): | |
| m = re.search(r"\((.+)\)", expr) | |
| if not m: | |
| raise ValueError(f"Malformed expression: {expression}") | |
| val = m.group(1).strip() | |
| return ones_complement(val) | |
| if re.match(r"^2s\(", expr, re.I): | |
| m = re.search(r"\((.+)\)", expr) | |
| if not m: | |
| raise ValueError(f"Malformed expression: {expression}") | |
| val = m.group(1).strip() |
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The binary arithmetic regex matches only a prefix of the expression (no ^...$/fullmatch), so trailing junk like B'1 + B'1 foo would be accepted and partially evaluated. Anchor the pattern to the full string (or use re.fullmatch) so invalid expressions are rejected consistently.
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
evaluate() is new public behavior but there are no tests exercising its supported expression forms (bin/dec/1s/2s/arithmetic) or its error handling for invalid expressions. Add unit tests (likely alongside test_calculator.py) to lock in the routing/parsing behavior and prevent regressions.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from .binary import ( | ||
| binary_to_decimal, | ||
| decimal_to_binary, | ||
| binary_add, | ||
| binary_subtract, | ||
| binary_multiply, | ||
| binary_divide, | ||
| ones_complement, | ||
| twos_complement, | ||
| ) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||||||||||||||||||||||||||||||||||
| # -- Pritam 038 ------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| from .exceptions import InvalidBinaryInputError, NegativeBinaryResultError | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def _strip(b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """Strip B' prefix and whitespace.""" | ||||||||||||||||||||||||||||||||||||
| b = b.strip().upper() | ||||||||||||||||||||||||||||||||||||
| if b.startswith("B'"): | ||||||||||||||||||||||||||||||||||||
| b = b[2:] | ||||||||||||||||||||||||||||||||||||
| return b | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def _validate(b: str) -> None: | ||||||||||||||||||||||||||||||||||||
| if not b or not all(c in '01' for c in b): | ||||||||||||||||||||||||||||||||||||
| raise InvalidBinaryInputError(b) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def binary_to_decimal(b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Convert binary string to decimal. | ||||||||||||||||||||||||||||||||||||
| e.g. binary_to_decimal("B'1010") -> "D'10" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| raw = _strip(b) | ||||||||||||||||||||||||||||||||||||
| _validate(raw) | ||||||||||||||||||||||||||||||||||||
| return f"D'{int(raw, 2)}" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def decimal_to_binary(d: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Convert decimal string to binary. | ||||||||||||||||||||||||||||||||||||
| e.g. decimal_to_binary("D'10") -> "B'1010" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| d = d.strip().upper() | ||||||||||||||||||||||||||||||||||||
| if d.startswith("D'"): | ||||||||||||||||||||||||||||||||||||
| d = d[2:] | ||||||||||||||||||||||||||||||||||||
| n = int(d) | ||||||||||||||||||||||||||||||||||||
| if n < 0: | ||||||||||||||||||||||||||||||||||||
| raise ValueError("Negative decimal not supported in basic mode") | ||||||||||||||||||||||||||||||||||||
| return f"B'{bin(n)[2:]}" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # ── Saiyam 037: Arithmetic ────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def binary_add(a: str, b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Add two binary numbers. | ||||||||||||||||||||||||||||||||||||
| e.g. binary_add("B'011", "B'010") -> "B'101" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| da = int(binary_to_decimal(a).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| db = int(binary_to_decimal(b).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| return decimal_to_binary(f"D'{da + db}") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def binary_subtract(a: str, b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Subtract two binary numbers. | ||||||||||||||||||||||||||||||||||||
| e.g. binary_subtract("B'101", "B'010") -> "B'011" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| da = int(binary_to_decimal(a).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| db = int(binary_to_decimal(b).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| if da < db: | ||||||||||||||||||||||||||||||||||||
| raise NegativeBinaryResultError() | ||||||||||||||||||||||||||||||||||||
| bit_len = max(len(_strip(a)), len(_strip(b))) | ||||||||||||||||||||||||||||||||||||
| result = bin(da - db)[2:].zfill(bit_len) | ||||||||||||||||||||||||||||||||||||
| return f"B'{result}" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def binary_multiply(a: str, b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Multiply two binary numbers. | ||||||||||||||||||||||||||||||||||||
| e.g. binary_multiply("B'011", "B'010") -> "B'110" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| da = int(binary_to_decimal(a).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| db = int(binary_to_decimal(b).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| return decimal_to_binary(f"D'{da * db}") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def binary_divide(a: str, b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Integer divide two binary numbers. | ||||||||||||||||||||||||||||||||||||
| e.g. binary_divide("B'110", "B'010") -> "B'11" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| da = int(binary_to_decimal(a).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| db = int(binary_to_decimal(b).split("'")[1]) | ||||||||||||||||||||||||||||||||||||
| if db == 0: | ||||||||||||||||||||||||||||||||||||
| raise ValueError("Binary division by zero") | ||||||||||||||||||||||||||||||||||||
| return decimal_to_binary(f"D'{da // db}") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| #-----------suhani 034: complement------------------------------- | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def ones_complement(b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Flip all bits. | ||||||||||||||||||||||||||||||||||||
| e.g. ones_complement("B'1010") -> "B'0101" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| raw = _strip(b) | ||||||||||||||||||||||||||||||||||||
| _validate(raw) | ||||||||||||||||||||||||||||||||||||
| return "B'" + ''.join('1' if bit == '0' else '0' for bit in raw) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def twos_complement(b: str) -> str: | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Add 1 to the one's complement. | ||||||||||||||||||||||||||||||||||||
| e.g. twos_complement("B'0111") -> "B'1001" | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| raw = _strip(b) | ||||||||||||||||||||||||||||||||||||
| _validate(raw) | ||||||||||||||||||||||||||||||||||||
| ones = ones_complement(f"B'{raw}")[2:] | ||||||||||||||||||||||||||||||||||||
| val = int(ones, 2) + 1 # no modulo | ||||||||||||||||||||||||||||||||||||
| result = bin(val)[2:].zfill(len(ones)) # zfill won't truncate carry | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+108
to
+115
|
||||||||||||||||||||||||||||||||||||
| Add 1 to the one's complement. | |
| e.g. twos_complement("B'0111") -> "B'1001" | |
| """ | |
| raw = _strip(b) | |
| _validate(raw) | |
| ones = ones_complement(f"B'{raw}")[2:] | |
| val = int(ones, 2) + 1 # no modulo | |
| result = bin(val)[2:].zfill(len(ones)) # zfill won't truncate carry | |
| Add 1 to the one's complement (modulo 2^n, preserving bit-width). | |
| e.g. twos_complement("B'0111") -> "B'1001" | |
| """ | |
| raw = _strip(b) | |
| _validate(raw) | |
| bit_len = len(raw) | |
| ones = ones_complement(b)[2:] | |
| val = (int(ones, 2) + 1) & ((1 << bit_len) - 1) | |
| result = format(val, f"0{bit_len}b") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
|
|
||
| # ---- Harsh 035: Exceptions ---------- | ||
| class InvalidBinaryInputError(ValueError): | ||
| def __init__(self, value: str): | ||
| super().__init__(f"'{value}' is not a valid binary string") | ||
|
|
||
| class NegativeBinaryResultError(ValueError): | ||
| def __init__(self): | ||
| super().__init__("Binary subtraction result cannot be negative") |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1 @@ | ||||
| from .testbinary import * | ||||
|
||||
| from .testbinary import * |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,85 @@ | ||||||
|
|
||||||
| # ── deepjit pal (036): Exceptions───────────────────────────────────────────────────── | ||||||
|
|
||||||
|
|
||||||
| import unittest | ||||||
| from modules.binary import ( | ||||||
| binary_to_decimal, decimal_to_binary, | ||||||
| binary_add, binary_subtract, | ||||||
| binary_multiply, binary_divide, | ||||||
| ones_complement, twos_complement, | ||||||
| ) | ||||||
| from modules.exceptions import InvalidBinaryInputError, NegativeBinaryResultError | ||||||
|
|
||||||
|
|
||||||
| class TestBinaryConversions(unittest.TestCase): | ||||||
| def test_bin_to_dec_basic(self): | ||||||
| self.assertEqual(binary_to_decimal("B'1010"), "D'10") | ||||||
|
|
||||||
| def test_bin_to_dec_zero(self): | ||||||
| self.assertEqual(binary_to_decimal("B'0"), "D'0") | ||||||
|
|
||||||
| def test_bin_to_dec_one(self): | ||||||
| self.assertEqual(binary_to_decimal("B'1"), "D'1") | ||||||
|
|
||||||
| def test_dec_to_bin_basic(self): | ||||||
| self.assertEqual(decimal_to_binary("D'10"), "B'1010") | ||||||
|
|
||||||
| def test_dec_to_bin_zero(self): | ||||||
| self.assertEqual(decimal_to_binary("D'0"), "B'0") | ||||||
|
|
||||||
|
|
||||||
| class TestBinaryArithmetic(unittest.TestCase): | ||||||
| def test_add(self): | ||||||
| self.assertEqual(binary_add("B'011", "B'010"), "B'101") | ||||||
|
|
||||||
| def test_subtract(self): | ||||||
| self.assertEqual(binary_subtract("B'101", "B'010"), "B'011") | ||||||
|
|
||||||
| def test_subtract_negative_raises(self): | ||||||
| with self.assertRaises(NegativeBinaryResultError): | ||||||
| binary_subtract("B'001", "B'010") | ||||||
|
|
||||||
| def test_multiply(self): | ||||||
| self.assertEqual(binary_multiply("B'011", "B'010"), "B'110") | ||||||
|
|
||||||
| def test_divide(self): | ||||||
| self.assertEqual(binary_divide("B'110", "B'010"), "B'11") | ||||||
|
|
||||||
| def test_divide_by_zero(self): | ||||||
| with self.assertRaises(ValueError): | ||||||
| binary_divide("B'110", "B'0") | ||||||
|
|
||||||
|
|
||||||
| class TestComplements(unittest.TestCase): | ||||||
| def test_ones_complement(self): | ||||||
| self.assertEqual(ones_complement("B'1010"), "B'0101") | ||||||
|
|
||||||
| def test_ones_all_zeros(self): | ||||||
| self.assertEqual(ones_complement("B'0000"), "B'1111") | ||||||
|
|
||||||
| def test_twos_complement(self): | ||||||
| self.assertEqual(twos_complement("B'0111"), "B'1001") | ||||||
|
|
||||||
|
|
||||||
| def test_twos_complement_zero(self): | ||||||
| self.assertEqual(twos_complement("B'0000"), "B'10000") | ||||||
|
||||||
| self.assertEqual(twos_complement("B'0000"), "B'10000") | |
| self.assertEqual(twos_complement("B'0000"), "B'0000") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These one-line method definitions reduce readability and make future edits (e.g., adding type hints/docstrings/logging) harder. Consider expanding them to the standard multi-line form to keep the class consistent with typical Python formatting.