From 8079d344c35a0f580cf41636c923fb743d1c9770 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Mon, 16 Mar 2026 13:28:18 +0530 Subject: [PATCH 01/30] Init branch readme --- readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +Hello From 34c3163555d5154697995dbb6c0f21092df19eb9 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Mon, 16 Mar 2026 14:56:23 +0530 Subject: [PATCH 02/30] Init hex.py and test_hex.py --- hex.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test_hex.py | 72 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 hex.py create mode 100644 test_hex.py diff --git a/hex.py b/hex.py new file mode 100644 index 0000000..a7a2fa8 --- /dev/null +++ b/hex.py @@ -0,0 +1,90 @@ +class HexCalculator: + """ + Hexadecimal calculator module. + + Supports: + - Hex ↔ Decimal conversions + - Arithmetic operations in HEX + - Complement calculations + - Input validation + """ + + def _validate_hex(self, value: str) -> str: + """ + Validate input format H'AB12' + Returns the internal hex string. + """ + if not isinstance(value, str): + raise ValueError("Input must be a string") + + if not value.startswith("H'") or not value.endswith("'"): + raise ValueError("Invalid HEX format. Expected H'AB12'") + + hex_part = value[2:-1] + + try: + int(hex_part, 16) + except ValueError: + raise ValueError("Invalid hexadecimal digits") + + return hex_part + + # ------------------------------- + # Sravanti will edit this part + # ------------------------------- + def hex_to_decimal(self, value: str) -> str: + """ + Convert HEX → Decimal + + Example: + H'1A5' → D'421' + """ + raise NotImplementedError("hex_to_decimal not implemented yet") + + # ------------------------------- + # Saichaitanya will edit this part + # ------------------------------- + def decimal_to_hex(self, value: str) -> str: + """ + Convert Decimal → HEX + + Example: + D'243' → H'F3' + """ + raise NotImplementedError("decimal_to_hex not implemented yet") + + # ------------------------------- + # Joydip will edit this part + # ------------------------------- + def add(self, a: str, b: str) -> str: + """ + HEX addition + + Example: + H'A' + H'5' → H'F' + """ + raise NotImplementedError("add not implemented yet") + # ------------------------------- + # Pratyush will edit this part + # ------------------------------- + + def subtract(self, a: str, b: str) -> str: + """ + HEX subtraction + """ + raise NotImplementedError("subtract not implemented yet") + + # ------------------------------- + # Sneha will edit this part + # ------------------------------- + def fifteen_complement(self, value: str) -> str: + """ + Compute 15's complement + """ + raise NotImplementedError("15's complement not implemented yet") + + def sixteen_complement(self, value: str) -> str: + """ + Compute 16's complement + """ + raise NotImplementedError("16's complement not implemented yet") \ No newline at end of file diff --git a/test_hex.py b/test_hex.py new file mode 100644 index 0000000..fd00360 --- /dev/null +++ b/test_hex.py @@ -0,0 +1,72 @@ +# test_hex.py + +import unittest +from hex import HexCalculator + + +class TestHexCalculator(unittest.TestCase): + + def setUp(self): + self.hex_calc = HexCalculator() + + # --------------------------------- + # SRAVANTI 1 TESTS + # --------------------------------- + def test_hex_to_decimal(self): + self.assertEqual( + self.hex_calc.hex_to_decimal("H'1A5'"), + "D'421'" + ) + + # --------------------------------- + # SAICHAITANYA 2 TESTS + # --------------------------------- + def test_decimal_to_hex(self): + self.assertEqual( + self.hex_calc.decimal_to_hex("D'243'"), + "H'F3'" + ) + + # --------------------------------- + # JOYDIP 3 TESTS + # --------------------------------- + def test_add(self): + self.assertEqual( + self.hex_calc.add("H'A'", "H'5'"), + "H'F'" + ) + # --------------------------------- + # PRATYUSH 3 TESTS + # --------------------------------- + + def test_subtract(self): + self.assertEqual( + self.hex_calc.subtract("H'F'", "H'5'"), + "H'A'" + ) + + # --------------------------------- + # SNEHA 4 TESTS + # --------------------------------- + def test_fifteen_complement(self): + self.assertEqual( + self.hex_calc.fifteen_complement("H'A'"), + "H'5'" + ) + + def test_sixteen_complement(self): + self.assertEqual( + self.hex_calc.sixteen_complement("H'A'"), + "H'6'" + ) + + # --------------------------------- + # COMMON VALIDATION TEST --- Already Done + # --------------------------------- + def test_invalid_hex(self): + with self.assertRaises(ValueError): + self.hex_calc.hex_to_decimal("123") + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From c9df2d32206399057cbf2942812c9bd1e81651e9 Mon Sep 17 00:00:00 2001 From: Pratyush Dhital Date: Sat, 28 Mar 2026 16:29:16 +0530 Subject: [PATCH 03/30] added the substract function with the test cases --- hex.py | 10 +++++++++- test_hex.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/hex.py b/hex.py index a7a2fa8..b7fca31 100644 --- a/hex.py +++ b/hex.py @@ -72,7 +72,15 @@ def subtract(self, a: str, b: str) -> str: """ HEX subtraction """ - raise NotImplementedError("subtract not implemented yet") + hex_a = self._validate_hex(a) + hex_b = self._validate_hex(b) + + result = int(hex_a, 16) - int(hex_b, 16) + + if result < 0: + return f"H'-{format(abs(result), 'X')}'" + + return f"H'{format(result, 'X')}'" # ------------------------------- # Sneha will edit this part diff --git a/test_hex.py b/test_hex.py index fd00360..c567b5d 100644 --- a/test_hex.py +++ b/test_hex.py @@ -45,6 +45,18 @@ def test_subtract(self): "H'A'" ) + def test_subtract_with_borrow(self): + self.assertEqual( + self.hex_calc.subtract("H'10'", "H'1'"), + "H'F'" + ) + + def test_subtract_negative_result(self): + self.assertEqual( + self.hex_calc.subtract("H'2'", "H'5'"), + "H'-3'" + ) + # --------------------------------- # SNEHA 4 TESTS # --------------------------------- From 534b46fd810270050188b9316f80b86e205a469a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sneha-=C2=84Das1?= Date: Sat, 28 Mar 2026 20:43:37 +0530 Subject: [PATCH 04/30] Added complement functions --- hex.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/hex.py b/hex.py index a7a2fa8..b5eacf3 100644 --- a/hex.py +++ b/hex.py @@ -74,6 +74,11 @@ def subtract(self, a: str, b: str) -> str: """ raise NotImplementedError("subtract not implemented yet") + # ------------------------------- + # Sneha will edit this part + # ------------------------------- + class HexCalculator: + # ------------------------------- # Sneha will edit this part # ------------------------------- @@ -81,10 +86,32 @@ def fifteen_complement(self, value: str) -> str: """ Compute 15's complement """ - raise NotImplementedError("15's complement not implemented yet") + hex_part = self._validate_hex(value) + + result = "" + for digit in hex_part: + comp = 15 - int(digit, 16) + result += format(comp, 'X') + + return f"H'{result}'" + def sixteen_complement(self, value: str) -> str: """ Compute 16's complement """ - raise NotImplementedError("16's complement not implemented yet") \ No newline at end of file + hex_part = self._validate_hex(value) + + # Step 1: 15's complement + comp15 = "" + for digit in hex_part: + comp = 15 - int(digit, 16) + comp15 += format(comp, 'X') + + # Step 2: add 1 + comp16 = hex(int(comp15, 16) + 1)[2:].upper() + + # Step 3: maintain same length + comp16 = comp16.zfill(len(hex_part)) + + return f"H'{comp16}'" \ No newline at end of file From 54ceff1a4015547fb0aced7bd6fba48f20457947 Mon Sep 17 00:00:00 2001 From: Achanta Sravanthi Date: Mon, 30 Mar 2026 00:00:53 +0530 Subject: [PATCH 05/30] Added hex to decimal conversion function --- hex.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 hex.py diff --git a/hex.py b/hex.py new file mode 100644 index 0000000..22d8d3b --- /dev/null +++ b/hex.py @@ -0,0 +1,10 @@ +def hex_to_decimal(hex_str): + if not hex_str.startswith("H'"): + raise ValueError("Invalid HEX format") + + try: + value = hex_str[2:] + decimal = int(value, 16) + return f"D'{decimal}" + except: + raise ValueError("Invalid HEX input") From afd0fc07672fd4687c678005b95a9e2be44bfa73 Mon Sep 17 00:00:00 2001 From: Achanta Sravanthi Date: Mon, 30 Mar 2026 00:05:46 +0530 Subject: [PATCH 06/30] Create test_hex.py --- test_hex.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 test_hex.py diff --git a/test_hex.py b/test_hex.py new file mode 100644 index 0000000..a6dc4ec --- /dev/null +++ b/test_hex.py @@ -0,0 +1,9 @@ +import unittest +from hex import hex_to_decimal + +class TestHex(unittest.TestCase): + def test_valid(self): + self.assertEqual(hex_to_decimal("H'1A5"), "D'421") + +if __name__ == "__main__": + unittest.main() From 4446411b0a8aea0c3d5b1e6a3a0001161ebdf1fc Mon Sep 17 00:00:00 2001 From: sravanthi141005 Date: Mon, 30 Mar 2026 00:55:17 +0530 Subject: [PATCH 07/30] added hex->decimal --- hex.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/hex.py b/hex.py index a7a2fa8..49d2c2f 100644 --- a/hex.py +++ b/hex.py @@ -31,15 +31,17 @@ def _validate_hex(self, value: str) -> str: # ------------------------------- # Sravanti will edit this part - # ------------------------------- - def hex_to_decimal(self, value: str) -> str: - """ - Convert HEX → Decimal +# ------------------------------- +def hex_to_decimal(self, value: str) -> str: + """ + Convert HEX → Decimal - Example: - H'1A5' → D'421' - """ - raise NotImplementedError("hex_to_decimal not implemented yet") + Example: + H'1A5' → D'421' + """ + hex_part = value[2:-1] + decimal_value = int(hex_part, 16) + return f"D'{decimal_value}'" # ------------------------------- # Saichaitanya will edit this part @@ -87,4 +89,4 @@ def sixteen_complement(self, value: str) -> str: """ Compute 16's complement """ - raise NotImplementedError("16's complement not implemented yet") \ No newline at end of file + raise NotImplementedError("16's complement not implemented yet") From 774465cd2a56451bdf555e79f9b145857ef74014 Mon Sep 17 00:00:00 2001 From: Achanta Sravanthi Date: Mon, 30 Mar 2026 00:58:40 +0530 Subject: [PATCH 08/30] Rename hex.py to hex1.py --- hex.py => hex1.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename hex.py => hex1.py (100%) diff --git a/hex.py b/hex1.py similarity index 100% rename from hex.py rename to hex1.py From 85190877ff0531a504cae97f362211476d298ade Mon Sep 17 00:00:00 2001 From: Achanta Sravanthi Date: Mon, 30 Mar 2026 00:58:57 +0530 Subject: [PATCH 09/30] Rename test_hex.py to test_hex1.py --- test_hex.py => test_hex1.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test_hex.py => test_hex1.py (100%) diff --git a/test_hex.py b/test_hex1.py similarity index 100% rename from test_hex.py rename to test_hex1.py From 4cc1d2e82b1ce8a0e936d63a818e23f953e03874 Mon Sep 17 00:00:00 2001 From: Achanta Sravanthi Date: Mon, 30 Mar 2026 01:02:21 +0530 Subject: [PATCH 10/30] Rename test_hex1.py to test_hex.py --- test_hex1.py => test_hex.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test_hex1.py => test_hex.py (100%) diff --git a/test_hex1.py b/test_hex.py similarity index 100% rename from test_hex1.py rename to test_hex.py From 72699f6cb0a9b19a92ddeffa923d13fd257505a9 Mon Sep 17 00:00:00 2001 From: Achanta Sravanthi Date: Mon, 30 Mar 2026 01:02:33 +0530 Subject: [PATCH 11/30] Rename hex1.py to hex.py --- hex1.py => hex.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename hex1.py => hex.py (100%) diff --git a/hex1.py b/hex.py similarity index 100% rename from hex1.py rename to hex.py From 70e3d5e7e7dca1065921c0c9148ad28faa0cc7e1 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Mon, 30 Mar 2026 14:06:09 +0530 Subject: [PATCH 12/30] Hex Addition code added --- hex.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hex.py b/hex.py index a7a2fa8..6c6da71 100644 --- a/hex.py +++ b/hex.py @@ -63,7 +63,12 @@ def add(self, a: str, b: str) -> str: Example: H'A' + H'5' → H'F' """ - raise NotImplementedError("add not implemented yet") + x = int(self._validate_hex(a),16) + y = int(self._validate_hex(b),16) + + result = x + y + + return f"H'{format(result,'X')}" # ------------------------------- # Pratyush will edit this part # ------------------------------- From 68c6bc9ac1a718b4eebdea91361d2374b2e3cd27 Mon Sep 17 00:00:00 2001 From: Pratyush Dhital <143419336+pratyush-dhital@users.noreply.github.com> Date: Mon, 30 Mar 2026 14:17:56 +0530 Subject: [PATCH 13/30] Remove HexCalculator class definition Removed redundant HexCalculator class definition from hex.py --- hex.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/hex.py b/hex.py index 2339140..7a73284 100644 --- a/hex.py +++ b/hex.py @@ -82,11 +82,6 @@ def subtract(self, a: str, b: str) -> str: return f"H'{format(result, 'X')}'" - # ------------------------------- - # Sneha will edit this part - # ------------------------------- - class HexCalculator: - # ------------------------------- # Sneha will edit this part # ------------------------------- @@ -122,4 +117,4 @@ def sixteen_complement(self, value: str) -> str: # Step 3: maintain same length comp16 = comp16.zfill(len(hex_part)) - return f"H'{comp16}'" \ No newline at end of file + return f"H'{comp16}'" From a7b45d35d5c9019708f4f068d79e1cbb9810caf0 Mon Sep 17 00:00:00 2001 From: saichaithanya12 Date: Mon, 30 Mar 2026 15:23:39 +0530 Subject: [PATCH 14/30] added the decimal to hex conversion --- hex.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hex.py b/hex.py index 7a73284..dbd99fd 100644 --- a/hex.py +++ b/hex.py @@ -45,14 +45,15 @@ def hex_to_decimal(self, value: str) -> str: # Saichaitanya will edit this part # ------------------------------- def decimal_to_hex(self, value: str) -> str: - """ - Convert Decimal → HEX + value = value.strip() - Example: - D'243' → H'F3' - """ - raise NotImplementedError("decimal_to_hex not implemented yet") + if value.startswith("D'") and value.endswith("'"): + value = value[2:-1] + + if not value.isdigit(): + raise ValueError("Invalid decimal input") + return f"H'{hex(int(value))[2:].upper()}'" # ------------------------------- # Joydip will edit this part # ------------------------------- From 8e861be3538b1aff7a787a7c234e079dc66ddf63 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Tue, 31 Mar 2026 09:07:33 +0530 Subject: [PATCH 15/30] Addition Testcases added and _validate_hex() updated --- hex.py | 40 +++++++++++++++++++++++++++------------- test_hex.py | 27 +++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/hex.py b/hex.py index 6c6da71..66c65e4 100644 --- a/hex.py +++ b/hex.py @@ -11,8 +11,8 @@ class HexCalculator: def _validate_hex(self, value: str) -> str: """ - Validate input format H'AB12' - Returns the internal hex string. + Validate input format H'AB12' or H'-F3'. + Returns the internal canonical hex string (sign + digits). """ if not isinstance(value, str): raise ValueError("Input must be a string") @@ -20,14 +20,26 @@ def _validate_hex(self, value: str) -> str: if not value.startswith("H'") or not value.endswith("'"): raise ValueError("Invalid HEX format. Expected H'AB12'") - hex_part = value[2:-1] + inner = value[2:-1] - try: - int(hex_part, 16) - except ValueError: + if inner == "": + raise ValueError("Invalid HEX format. Empty value") + + sign = "" + if inner[0] in "+-": + sign = inner[0] + inner = inner[1:] + + if inner == "": + raise ValueError("Invalid HEX format. Missing digits") + + valid_chars = set("0123456789ABCDEFabcdef") + if not all(ch in valid_chars for ch in inner): raise ValueError("Invalid hexadecimal digits") - return hex_part + # Normalize to uppercase and keep sign + normalized = sign + inner.upper() + return normalized # ------------------------------- # Sravanti will edit this part @@ -62,13 +74,15 @@ def add(self, a: str, b: str) -> str: Example: H'A' + H'5' → H'F' - """ - x = int(self._validate_hex(a),16) - y = int(self._validate_hex(b),16) - + """ + x = int(self._validate_hex(a), 16) + y = int(self._validate_hex(b), 16) + result = x + y - - return f"H'{format(result,'X')}" + + if result < 0: + return f"H'-{format(-result, 'X')}'" + return f"H'{format(result, 'X')}'" # ------------------------------- # Pratyush will edit this part # ------------------------------- diff --git a/test_hex.py b/test_hex.py index fd00360..bfc1cf3 100644 --- a/test_hex.py +++ b/test_hex.py @@ -31,10 +31,17 @@ def test_decimal_to_hex(self): # JOYDIP 3 TESTS # --------------------------------- def test_add(self): - self.assertEqual( - self.hex_calc.add("H'A'", "H'5'"), - "H'F'" - ) + self.assertEqual(self.hex_calc.add("H'A'", "H'5'"), "H'F'") + self.assertEqual(self.hex_calc.add("H'7'", "H'9'"), "H'10'") + self.assertEqual(self.hex_calc.add("H'FF'", "H'1'"), "H'100'") + self.assertEqual(self.hex_calc.add("H'1A'", "H'2'"), "H'1C'") + self.assertEqual(self.hex_calc.add("H'0'", "H'AB'"), "H'AB'") + self.assertEqual(self.hex_calc.add("H'5'", "H'-3'"), "H'2'") + self.assertEqual(self.hex_calc.add("H'00A'", "H'005'"), "H'F'") + self.assertEqual(self.hex_calc.add("H'ABC'", "H'123'"), "H'BDF'") + self.assertEqual(self.hex_calc.add("H'1'", "H'-2'"), "H'-1'") + with self.assertRaises(ValueError): + self.hex_calc.add("H'G1'", "H'2'") # --------------------------------- # PRATYUSH 3 TESTS # --------------------------------- @@ -45,6 +52,18 @@ def test_subtract(self): "H'A'" ) + def test_subtract_with_borrow(self): + self.assertEqual( + self.hex_calc.subtract("H'10'", "H'1'"), + "H'F'" + ) + + def test_subtract_negative_result(self): + self.assertEqual( + self.hex_calc.subtract("H'2'", "H'5'"), + "H'-3'" + ) + # --------------------------------- # SNEHA 4 TESTS # --------------------------------- From b5d10866b62f911da3a722fbaca7c3e97785c260 Mon Sep 17 00:00:00 2001 From: Pratyush Dhital Date: Fri, 3 Apr 2026 16:34:33 +0530 Subject: [PATCH 16/30] Added unit test cases for subtraction --- .gitignore | 7 +++++++ test_hex.py | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf06853 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +__pycache__/ +*.pyc +venv/ +.env/ +.vscode/ +.DS_Store +Thumbs.db \ No newline at end of file diff --git a/test_hex.py b/test_hex.py index c567b5d..1ccc368 100644 --- a/test_hex.py +++ b/test_hex.py @@ -39,23 +39,34 @@ def test_add(self): # PRATYUSH 3 TESTS # --------------------------------- - def test_subtract(self): - self.assertEqual( - self.hex_calc.subtract("H'F'", "H'5'"), - "H'A'" - ) + def test_subtract_all_cases(self): + # Basic subtract scenarios + self.assertEqual(self.hex_calc.subtract("H'F'", "H'5'"), "H'A'") + self.assertEqual(self.hex_calc.subtract("H'10'", "H'1'"), "H'F'") + self.assertEqual(self.hex_calc.subtract("H'2'", "H'5'"), "H'-3'") - def test_subtract_with_borrow(self): - self.assertEqual( - self.hex_calc.subtract("H'10'", "H'1'"), - "H'F'" - ) + # Functional edge cases + self.assertEqual(self.hex_calc.subtract("H'0'", "H'0'"), "H'0'") + self.assertEqual(self.hex_calc.subtract("H'1A3F'", "H'1A3F'"), "H'0'") + self.assertEqual(self.hex_calc.subtract("H'1000'", "H'1'"), "H'FFF'") + self.assertEqual(self.hex_calc.subtract("H'00A'", "H'0001'"), "H'9'") + self.assertEqual(self.hex_calc.subtract("H'a'", "H'3'"), "H'7'") + self.assertEqual(self.hex_calc.subtract("H'ABCDEF'", "H'123456'"), "H'999999'") + self.assertEqual(self.hex_calc.subtract("H'1'", "H'A'"), "H'-9'") + self.assertEqual(self.hex_calc.subtract("H'10'", "H'1F'"), "H'-F'") - def test_subtract_negative_result(self): - self.assertEqual( - self.hex_calc.subtract("H'2'", "H'5'"), - "H'-3'" - ) + # Validation and input-type corner cases + with self.assertRaises(ValueError): + self.hex_calc.subtract("H'1G'", "H'1'") + + with self.assertRaises(ValueError): + self.hex_calc.subtract("H'1'", "1") + + with self.assertRaises(ValueError): + self.hex_calc.subtract(10, "H'1'") + + with self.assertRaises(ValueError): + self.hex_calc.subtract("H'1'", None) # --------------------------------- # SNEHA 4 TESTS From 77d416f73df7e8aae2280542b9d077359d3d5ae3 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Sun, 5 Apr 2026 19:06:23 +0530 Subject: [PATCH 17/30] Upadted the test cases of hex addition --- hex.py | 20 +++++------ test_hex.py | 97 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 74 insertions(+), 43 deletions(-) diff --git a/hex.py b/hex.py index 7d83558..d611051 100644 --- a/hex.py +++ b/hex.py @@ -44,17 +44,17 @@ def _validate_hex(self, value: str) -> str: # ------------------------------- # Sravanti will edit this part -# ------------------------------- -def hex_to_decimal(self, value: str) -> str: - """ - Convert HEX → Decimal + # ------------------------------- + def hex_to_decimal(self, value: str) -> str: + """ + Convert HEX → Decimal - Example: - H'1A5' → D'421' - """ - hex_part = value[2:-1] - decimal_value = int(hex_part, 16) - return f"D'{decimal_value}'" + Example: + H'1A5' → D'421' + """ + hex_part = value[2:-1] + decimal_value = int(hex_part, 16) + return f"D'{decimal_value}'" # ------------------------------- # Saichaitanya will edit this part diff --git a/test_hex.py b/test_hex.py index b85b2cc..272fea8 100644 --- a/test_hex.py +++ b/test_hex.py @@ -1,4 +1,3 @@ - # test_hex.py import unittest @@ -32,49 +31,82 @@ def test_decimal_to_hex(self): # JOYDIP 3 TESTS # --------------------------------- def test_add(self): + # ----- NORMAL CASES ----- + # Case 1: Basic single-digit addition without carry self.assertEqual(self.hex_calc.add("H'A'", "H'5'"), "H'F'") + + # Case 2: Multi-digit addition without carry + self.assertEqual(self.hex_calc.add("H'1A'", "H'2'"), "H'1C'") + + # Case 3: Multi-digit addition with carry propagation + self.assertEqual(self.hex_calc.add("H'ABC'", "H'123'"), "H'BDF'") + + # Case 4: Simple two-digit hex addition + self.assertEqual(self.hex_calc.add("H'12'", "H'34'"), "H'46'") + + # Case 5: Addition of larger numbers + self.assertEqual(self.hex_calc.add("H'ABCD'", "H'1234'"), "H'BE01'") + + # ----- BOUNDARY CONDITIONS ----- + # Boundary 1: Addition with carry (single digit overflow) self.assertEqual(self.hex_calc.add("H'7'", "H'9'"), "H'10'") + + # Boundary 2: Overflow to next digit group (all F's case) self.assertEqual(self.hex_calc.add("H'FF'", "H'1'"), "H'100'") - self.assertEqual(self.hex_calc.add("H'1A'", "H'2'"), "H'1C'") + + # Boundary 3: Addition with zero (identity element) self.assertEqual(self.hex_calc.add("H'0'", "H'AB'"), "H'AB'") - self.assertEqual(self.hex_calc.add("H'5'", "H'-3'"), "H'2'") + + # Boundary 4: Addition with leading zeros (normalization) self.assertEqual(self.hex_calc.add("H'00A'", "H'005'"), "H'F'") - self.assertEqual(self.hex_calc.add("H'ABC'", "H'123'"), "H'BDF'") + + # Boundary 5: Addition with negative numbers (negative result) + self.assertEqual(self.hex_calc.add("H'5'", "H'-3'"), "H'2'") + + # Boundary 6: Addition resulting in negative number self.assertEqual(self.hex_calc.add("H'1'", "H'-2'"), "H'-1'") + + # ----- INVALID INPUT HANDLING ----- + # Invalid Case 1: Invalid hex digit (G is not valid in hexadecimal) with self.assertRaises(ValueError): self.hex_calc.add("H'G1'", "H'2'") + + # Invalid Case 2: Invalid hex digit in second operand (Z is not valid) + with self.assertRaises(ValueError): + self.hex_calc.add("H'A'", "H'Z5'") + + # Invalid Case 3: Missing hex format prefix in first operand + with self.assertRaises(ValueError): + self.hex_calc.add("A5", "H'B'") + + # Invalid Case 4: Missing hex format prefix in second operand + with self.assertRaises(ValueError): + self.hex_calc.add("H'A'", "B5") + + # Invalid Case 5: Invalid format with incorrect brackets + with self.assertRaises(ValueError): + self.hex_calc.add("H[A5]", "H'B'") # --------------------------------- # PRATYUSH 3 TESTS # --------------------------------- - def test_subtract_all_cases(self): - # Basic subtract scenarios - self.assertEqual(self.hex_calc.subtract("H'F'", "H'5'"), "H'A'") - self.assertEqual(self.hex_calc.subtract("H'10'", "H'1'"), "H'F'") - self.assertEqual(self.hex_calc.subtract("H'2'", "H'5'"), "H'-3'") - - # Functional edge cases - self.assertEqual(self.hex_calc.subtract("H'0'", "H'0'"), "H'0'") - self.assertEqual(self.hex_calc.subtract("H'1A3F'", "H'1A3F'"), "H'0'") - self.assertEqual(self.hex_calc.subtract("H'1000'", "H'1'"), "H'FFF'") - self.assertEqual(self.hex_calc.subtract("H'00A'", "H'0001'"), "H'9'") - self.assertEqual(self.hex_calc.subtract("H'a'", "H'3'"), "H'7'") - self.assertEqual(self.hex_calc.subtract("H'ABCDEF'", "H'123456'"), "H'999999'") - self.assertEqual(self.hex_calc.subtract("H'1'", "H'A'"), "H'-9'") - self.assertEqual(self.hex_calc.subtract("H'10'", "H'1F'"), "H'-F'") - - # Validation and input-type corner cases - with self.assertRaises(ValueError): - self.hex_calc.subtract("H'1G'", "H'1'") - - with self.assertRaises(ValueError): - self.hex_calc.subtract("H'1'", "1") + def test_subtract(self): + self.assertEqual( + self.hex_calc.subtract("H'F'", "H'5'"), + "H'A'" + ) - with self.assertRaises(ValueError): - self.hex_calc.subtract(10, "H'1'") + def test_subtract_with_borrow(self): + self.assertEqual( + self.hex_calc.subtract("H'10'", "H'1'"), + "H'F'" + ) - with self.assertRaises(ValueError): - self.hex_calc.subtract("H'1'", None) + def test_subtract_negative_result(self): + self.assertEqual( + self.hex_calc.subtract("H'2'", "H'5'"), + "H'-3'" + ) # --------------------------------- # SNEHA 4 TESTS @@ -92,7 +124,7 @@ def test_sixteen_complement(self): ) # --------------------------------- - # COMMON VALIDATION TEST --- Already Done + # COMMON VALIDATION TEST # --------------------------------- def test_invalid_hex(self): with self.assertRaises(ValueError): @@ -100,5 +132,4 @@ def test_invalid_hex(self): if __name__ == "__main__": - unittest.main() - + unittest.main() \ No newline at end of file From 66974c4f782b65caef440ffbfaf498b9fee139dc Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Sun, 5 Apr 2026 19:18:43 +0530 Subject: [PATCH 18/30] Added hex multiply and divide methods and corresponding test cases --- hex.py | 30 +++++++++++++++ test_hex.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/hex.py b/hex.py index d611051..7e88059 100644 --- a/hex.py +++ b/hex.py @@ -87,6 +87,36 @@ def add(self, a: str, b: str) -> str: if result < 0: return f"H'-{format(-result, 'X')}'" return f"H'{format(result, 'X')}'" + + def multiply(self, a: str, b: str) -> str: + """ + HEX multiplication + """ + x = int(self._validate_hex(a), 16) + y = int(self._validate_hex(b), 16) + + result = x * y + + if result < 0: + return f"H'-{format(-result, 'X')}'" + return f"H'{format(result, 'X')}'" + + def divide(self, a: str, b: str) -> str: + """ + HEX division -- returns quotient only (integer division) + """ + x = int(self._validate_hex(a), 16) + y = int(self._validate_hex(b), 16) + + if y == 0: + raise ValueError("Division by zero") + + result = x // y + + if result < 0: + return f"H'-{format(-result, 'X')}'" + return f"H'{format(result, 'X')}'" + # ------------------------------- # Pratyush will edit this part # ------------------------------- diff --git a/test_hex.py b/test_hex.py index 272fea8..57dfb17 100644 --- a/test_hex.py +++ b/test_hex.py @@ -86,6 +86,113 @@ def test_add(self): # Invalid Case 5: Invalid format with incorrect brackets with self.assertRaises(ValueError): self.hex_calc.add("H[A5]", "H'B'") + + def test_multiply(self): + # ----- NORMAL CASES ----- + # Case 1: Basic single-digit multiplication without carry + self.assertEqual(self.hex_calc.multiply("H'3'", "H'4'"), "H'C'") + + # Case 2: Multi-digit multiplication without carry + self.assertEqual(self.hex_calc.multiply("H'2'", "H'10'"), "H'20'") + + # Case 3: Multi-digit multiplication with carry propagation + self.assertEqual(self.hex_calc.multiply("H'1A'", "H'2'"), "H'34'") + + # Case 4: Simple two-digit hex multiplication + self.assertEqual(self.hex_calc.multiply("H'5'", "H'6'"), "H'1E'") + + # Case 5: Multiplication of larger numbers + self.assertEqual(self.hex_calc.multiply("H'AB'", "H'CD'"), "H'88EF'") + + # ----- BOUNDARY CONDITIONS ----- + # Boundary 1: Multiplication with zero (annihilator element) + self.assertEqual(self.hex_calc.multiply("H'A'", "H'0'"), "H'0'") + + # Boundary 2: Multiplication with one (identity element) + self.assertEqual(self.hex_calc.multiply("H'A'", "H'1'"), "H'A'") + + # Boundary 3: Multiplication resulting in zero + self.assertEqual(self.hex_calc.multiply("H'A'", "H'-0'"), "H'0'") + + # Boundary 4: Multiplication with negative numbers (negative result) + self.assertEqual(self.hex_calc.multiply("H'-3'", "H'4'"), "H'-C'") + + # Boundary 5: Multiplication resulting in negative number + self.assertEqual(self.hex_calc.multiply("H'-2'", "H'-3'"), "H'6'") + + # ----- INVALID INPUT HANDLING ----- + # Invalid Case 1: Invalid hex digit (G is not valid in hexadecimal) + with self.assertRaises(ValueError): + self.hex_calc.multiply("H'G1'", "H'2'") + + # Invalid Case 2: Invalid hex digit in second operand (Z is not valid) + with self.assertRaises(ValueError): + self.hex_calc.multiply("H'A'", "H'Z5'") + + # Invalid Case 3: Missing hex format prefix in first operand + with self.assertRaises(ValueError): + self.hex_calc.multiply("A5", "H'B'") + + # Invalid Case 4: Missing hex format prefix in second operand + with self.assertRaises(ValueError): + self.hex_calc.multiply("H'A'", "B5") + + # Invalid Case 5: Invalid format with incorrect brackets + with self.assertRaises(ValueError): + self.hex_calc.multiply("H[A5]", "H'B'") + + def test_divide(self): + + # ----- NORMAL CASES ----- + # Case 1: Basic single-digit division without remainder + self.assertEqual(self.hex_calc.divide("H'C'", "H'4'"), "H'3'") + + # Case 2: Multi-digit division without remainder + self.assertEqual(self.hex_calc.divide("H'20'", "H'2'"), "H'10'") + + # Case 3: Multi-digit division with remainder (integer division) + self.assertEqual(self.hex_calc.divide("H'1A'", "H'3'"), "H'8'") + + # Case 4: Simple two-digit hex division + self.assertEqual(self.hex_calc.divide("H'1E'", "H'6'"), "H'5'") + + # Case 5: Division of larger numbers + self.assertEqual(self.hex_calc.divide("H'88EF'", "H'AB'"), "H'CD'") + + # ----- BOUNDARY CONDITIONS ----- + # Boundary 1: Division by one (identity element) + self.assertEqual(self.hex_calc.divide("H'A'", "H'1'"), "H'A'") + + # Boundary 2: Division resulting in zero + self.assertEqual(self.hex_calc.divide("H'1'", "H'2'"), "H'0'") + + # Boundary 3: Division with negative numbers (negative result) + self.assertEqual(self.hex_calc.divide("H'-C'", "H'4'"), "H'-3'") + + # Boundary 4: Division resulting in negative number + self.assertEqual(self.hex_calc.divide("H'-6'", "H'-2'"), "H'3'") + + # ----- INVALID INPUT HANDLING ----- + # Invalid Case 1: Division by zero + with self.assertRaises(ValueError): + self.hex_calc.divide("H'A'", "H'0'") + + # Invalid Case 2: Invalid hex digit (G is not valid in hexadecimal) + with self.assertRaises(ValueError): + self.hex_calc.divide("H'G1'", "H'2'") + + # Invalid Case 3: Invalid hex digit in second operand (Z is not valid) + with self.assertRaises(ValueError): + self.hex_calc.divide("H'A'", "H'Z5'") + + # Invalid Case 4: Missing hex format prefix in first operand + with self.assertRaises(ValueError): + self.hex_calc.divide("A5", "H'B'") + + # Invalid Case 5: Missing hex format prefix in second operand + with self.assertRaises(ValueError): + self.hex_calc.divide("H'A'", "B5") + # --------------------------------- # PRATYUSH 3 TESTS # --------------------------------- From cd878ba8f5065904b7dfae5ba9e65c974d0e6814 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Sun, 5 Apr 2026 19:55:36 +0530 Subject: [PATCH 19/30] Fixed merge issue --- test_hex.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/test_hex.py b/test_hex.py index 57dfb17..bff3a80 100644 --- a/test_hex.py +++ b/test_hex.py @@ -197,23 +197,34 @@ def test_divide(self): # PRATYUSH 3 TESTS # --------------------------------- - def test_subtract(self): - self.assertEqual( - self.hex_calc.subtract("H'F'", "H'5'"), - "H'A'" - ) + def test_subtract_all_cases(self): + # Basic subtract scenarios + self.assertEqual(self.hex_calc.subtract("H'F'", "H'5'"), "H'A'") + self.assertEqual(self.hex_calc.subtract("H'10'", "H'1'"), "H'F'") + self.assertEqual(self.hex_calc.subtract("H'2'", "H'5'"), "H'-3'") - def test_subtract_with_borrow(self): - self.assertEqual( - self.hex_calc.subtract("H'10'", "H'1'"), - "H'F'" - ) + # Functional edge cases + self.assertEqual(self.hex_calc.subtract("H'0'", "H'0'"), "H'0'") + self.assertEqual(self.hex_calc.subtract("H'1A3F'", "H'1A3F'"), "H'0'") + self.assertEqual(self.hex_calc.subtract("H'1000'", "H'1'"), "H'FFF'") + self.assertEqual(self.hex_calc.subtract("H'00A'", "H'0001'"), "H'9'") + self.assertEqual(self.hex_calc.subtract("H'a'", "H'3'"), "H'7'") + self.assertEqual(self.hex_calc.subtract("H'ABCDEF'", "H'123456'"), "H'999999'") + self.assertEqual(self.hex_calc.subtract("H'1'", "H'A'"), "H'-9'") + self.assertEqual(self.hex_calc.subtract("H'10'", "H'1F'"), "H'-F'") - def test_subtract_negative_result(self): - self.assertEqual( - self.hex_calc.subtract("H'2'", "H'5'"), - "H'-3'" - ) + # Validation and input-type corner cases + with self.assertRaises(ValueError): + self.hex_calc.subtract("H'1G'", "H'1'") + + with self.assertRaises(ValueError): + self.hex_calc.subtract("H'1'", "1") + + with self.assertRaises(ValueError): + self.hex_calc.subtract(10, "H'1'") + + with self.assertRaises(ValueError): + self.hex_calc.subtract("H'1'", None) # --------------------------------- # SNEHA 4 TESTS From 869a5c702a9e0f7d58530f9ee78cbb8fd91bdc3d Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Sun, 5 Apr 2026 21:55:11 +0530 Subject: [PATCH 20/30] Added readme.md, updated calculator.py, fix 16's complement of hex.py, updated test_calculator.py --- calculator.py | 132 +++++++++++++++++++++++++++++++++ hex.py | 2 - readme.md | 200 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 331 insertions(+), 3 deletions(-) diff --git a/calculator.py b/calculator.py index 5daac9a..4de425d 100644 --- a/calculator.py +++ b/calculator.py @@ -1,11 +1,143 @@ +from hex import HexCalculator + + class Calculator: + # mode can be 1: Fraction, 2: Bin, 3: Oct, 4: Hex, 5: Set, 6: Matrix, default = 0 + mode = 0 + + MODE_MAP = { + "fraction": 1, + "bin": 2, + "oct": 3, + "hex": 4, + "set": 5, + "matrix": 6 + } + + REVERSE_MODE_MAP = {v: k for k, v in MODE_MAP.items()} + + def __init__(self): + self.mode = None + self.hex_calc = HexCalculator() + + def set_mode(self, mode): + """ + Set calculator mode. + Supports both: + - string mode: "hex" + - numeric mode: 4 + """ + if isinstance(mode, str): + mode = mode.lower().strip() + if mode not in self.MODE_MAP: + raise ValueError(f"Unsupported mode: {mode}") + self.mode = mode + + elif isinstance(mode, int): + if mode not in self.REVERSE_MODE_MAP: + raise ValueError(f"Unsupported mode number: {mode}") + self.mode = self.REVERSE_MODE_MAP[mode] + + else: + raise ValueError("Mode must be either string or integer") + + def get_mode(self): + return self.mode + + def _ensure_hex_mode(self): + if self.mode != "hex": + raise ValueError(f"Current mode is '{self.mode}', not 'hex'") + + # ------------------------------- + # Common arithmetic interface + # ------------------------------- def add(self, a, b): + if self.mode == "hex": + return self.hex_calc.add(a, b) return a + b + def subtract(self, a, b): + if self.mode == "hex": + return self.hex_calc.subtract(a, b) return a - b + def multiply(self, a, b): + if self.mode == "hex": + return self.hex_calc.multiply(a, b) return a * b + def divide(self, a, b): + if self.mode == "hex": + return self.hex_calc.divide(a, b) + if b == 0: raise ValueError("Division by zero") return a / b + + # ------------------------------- + # HEX-specific extra operations + # ------------------------------- + def hex_to_decimal(self, value): + self._ensure_hex_mode() + return self.hex_calc.hex_to_decimal(value) + + def decimal_to_hex(self, value): + self._ensure_hex_mode() + return self.hex_calc.decimal_to_hex(value) + + def fifteen_complement(self, value): + self._ensure_hex_mode() + return self.hex_calc.fifteen_complement(value) + + def sixteen_complement(self, value): + self._ensure_hex_mode() + return self.hex_calc.sixteen_complement(value) + + # ------------------------------- + # Unified evaluator / dispatcher + # ------------------------------- + def evaluate(self, operation, a, b=None): + """ + Evaluate an operation based on current mode. + + Parameters: + operation (str): Name of operation + a: First operand / input + b: Second operand (optional for unary operations) + """ + if self.mode is None: + raise ValueError("Calculator mode is not set") + + if self.mode == "hex": + if operation == "add": + return self.add(a, b) + elif operation == "subtract": + return self.subtract(a, b) + elif operation == "multiply": + return self.multiply(a, b) + elif operation == "divide": + return self.divide(a, b) + elif operation == "hex_to_decimal": + return self.hex_to_decimal(a) + elif operation == "decimal_to_hex": + return self.decimal_to_hex(a) + elif operation == "fifteen_complement": + return self.fifteen_complement(a) + elif operation == "sixteen_complement": + return self.sixteen_complement(a) + else: + raise ValueError(f"Unsupported HEX operation: {operation}") + + elif self.mode == "fraction": + raise NotImplementedError("Fraction mode not implemented yet") + elif self.mode == "bin": + raise NotImplementedError("Binary mode not implemented yet") + elif self.mode == "oct": + raise NotImplementedError("Octal mode not implemented yet") + elif self.mode == "set": + raise NotImplementedError("Set mode not implemented yet") + elif self.mode == "matrix": + raise NotImplementedError("Matrix mode not implemented yet") + + else: + raise ValueError("Invalid calculator mode") \ No newline at end of file diff --git a/hex.py b/hex.py index 7e88059..1e324f5 100644 --- a/hex.py +++ b/hex.py @@ -157,8 +157,6 @@ def sixteen_complement(self, value: str) -> str: Compute 16's complement """ - raise NotImplementedError("16's complement not implemented yet") - hex_part = self._validate_hex(value) # Step 1: 15's complement diff --git a/readme.md b/readme.md index e965047..f4c860f 100644 --- a/readme.md +++ b/readme.md @@ -1 +1,199 @@ -Hello +# Multi-Mode Calculator + +A calculator system where the user must **set a mode first** and then perform operations specific to that mode. + +`````md +## 🔑 Mode System + +The calculator works in **mode-based execution**. + +| Mode Value | Mode Name | +| ---------- | ----------- | +| 1 | Fraction | +| 2 | Binary | +| 3 | Octal | +| 4 | Hexadecimal | +| 5 | Set | +| 6 | Matrix | + +Currently implemented: **Hexadecimal (mode = 4)** + +## 🚀 Features + +- Mode-based execution (`set_mode()` required) +- Modular architecture (easy to extend) +- HEX operations supported: + - Addition, Subtraction, Multiplication, Division + - HEX ↔ Decimal conversion + - 15’s & 16’s complement +- Input validation & error handling + +--- + +## 📁 Project Structure + +```bash +SE_Calculator/ +│ +├── calculator.py # Main calculator controller (mode handler) +├── hex.py # Hexadecimal calculator implementation +├── test_calculator.py # Unit tests for calculator controller +├── test_hex.py # Unit tests for hexadecimal calculator +└── README.md # Project documentation +``` + +## 📒 A sample program to demonstrate how to use + +```python +from calculator import Calculator + +from calculator import Calculator + +def main(): # Create calculator object + calc = Calculator() + + # ------------------------------- + # Step 1: Set Mode + # ------------------------------- + calc.set_mode(4) # 4 = HEX mode + print("Current Mode:", calc.get_mode()) + print("-" * 50) + + # ------------------------------- + # Arithmetic Operations + # ------------------------------- + print("Arithmetic Operations:") + print("H'A' + H'5' =", calc.evaluate("add", "H'A'", "H'5'")) + print("H'F' - H'5' =", calc.evaluate("subtract", "H'F'", "H'5'")) + print("H'3' * H'4' =", calc.evaluate("multiply", "H'3'", "H'4'")) + print("H'1A' / H'3' =", calc.evaluate("divide", "H'1A'", "H'3'")) + print("-" * 50) + + # ------------------------------- + # Conversion Operations + # ------------------------------- + print("Conversion Operations:") + print("HEX to Decimal H'1A' =", calc.evaluate("hex_to_decimal", "H'1A'")) + print("Decimal to HEX D'26' =", calc.evaluate("decimal_to_hex", "D'26'")) + print("-" * 50) + + # ------------------------------- + # Complement Operations + # ------------------------------- + print("Complement Operations:") + print("15's complement of H'A' =", calc.evaluate("fifteen_complement", "H'A'")) + print("16's complement of H'A' =", calc.evaluate("sixteen_complement", "H'A'")) + print("-" * 50) + + # ------------------------------- + # More HEX Examples + # ------------------------------- + print("More HEX Examples:") + print("H'AB' + H'CD' =", calc.evaluate("add", "H'AB'", "H'CD'")) + print("H'100' - H'1' =", calc.evaluate("subtract", "H'100'", "H'1'")) + print("H'AB' * H'CD' =", calc.evaluate("multiply", "H'AB'", "H'CD'")) + print("H'88EF' / H'AB' =", calc.evaluate("divide", "H'88EF'", "H'AB'")) + print("-" * 50) + + # ------------------------------- + # Error Handling Examples + # ------------------------------- + print("Error Handling Examples:") + + try: + print("Trying division by zero...") + print(calc.evaluate("divide", "H'A'", "H'0'")) + except ValueError as e: + print("Caught Error:", e) + + try: + print("Trying invalid HEX input...") + print(calc.evaluate("add", "H'G1'", "H'2'")) + except ValueError as e: + print("Caught Error:", e) + + try: + print("Trying unsupported operation...") + print(calc.evaluate("modulus", "H'A'", "H'2'")) + except ValueError as e: + print("Caught Error:", e) + +if __name__ == "__main__": + main() + +``` + +````md +## Expected Output + +```text +## Current Mode: 4 + +Arithmetic Operations: +H'A' + H'5' = H'F' +H'F' - H'5' = H'A' +H'3' \* H'4' = H'C' +H'1A' / H'3' = H'8' + +--- + +Conversion Operations: +HEX to Decimal H'1A' = D'26' +Decimal to HEX D'26' = H'1A' + +--- + +Complement Operations: +15's complement of H'A' = H'5' +16's complement of H'A' = H'6' + +--- + +More HEX Examples: +H'AB' + H'CD' = H'178' +H'100' - H'1' = H'FF' +H'AB' \* H'CD' = H'88EF' +H'88EF' / H'AB' = H'CD' + +--- + +Error Handling Examples: +Trying division by zero... +Caught Error: Division by zero +Trying invalid HEX input... +Caught Error: Invalid hexadecimal digits +Trying unsupported operation... +Caught Error: Unsupported HEX operation: modulus +``` +```` + +````md +## 🧪 Unit Testing + +This project uses Python’s built-in `unittest` framework. + +### Run all tests + +```bash +python -m unittest +``` +```` + +**Run specific HEX tests** + +```bash +python -m unittest test_calculator.py +``` + +**Run specific hex funxtions** + +```bash +python -m unittest test*hex.TestHexCalculator.test* -v +``` + +**Run calculator tests** + +```bash +python -m unittest test_calculator.py +``` +````` From bb0320168dc922302e733cda72b68ce790e62d36 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Sun, 5 Apr 2026 22:02:32 +0530 Subject: [PATCH 21/30] Upadted test_calculator.py --- test_calculator.py | 109 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 10 deletions(-) diff --git a/test_calculator.py b/test_calculator.py index 7cb79e9..101df2a 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -1,33 +1,122 @@ import unittest from calculator import Calculator + class TestCalculator(unittest.TestCase): # base test cases def setUp(self): self.calc = Calculator() - def test_add(self): + # --------------------------------- + # PREVIOUS BASIC DECIMAL TESTS + # (kept as-is for backward compatibility) + # --------------------------------- + def test_add_decimal(self): self.assertEqual(self.calc.add(2, 3), 5) - def test_sub(self): + def test_sub_decimal(self): self.assertEqual(self.calc.subtract(2, 3), -1) - def test_multiply(self): + def test_multiply_decimal(self): self.assertEqual(self.calc.multiply(2, 3), 6) - def test_divide(self): + def test_divide_decimal_positive(self): self.assertEqual(self.calc.divide(2, 4), 0.5) - def test_divide(self): + def test_divide_decimal_negative(self): self.assertEqual(self.calc.divide(4, -2), -2) - - def test_divide_fail(self): # this will fail + + def test_divide_fail(self): # this should still pass self.assertNotEqual(self.calc.divide(4, -2), 2) - def test_divide_by_zero(self): + def test_divide_by_zero_decimal(self): with self.assertRaises(ValueError): self.calc.divide(5, 0) + # --------------------------------- + # MODE SETTING TESTS + # --------------------------------- + def test_set_mode_string(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.get_mode(), "hex") + + def test_set_mode_number(self): + self.calc.set_mode(4) + self.assertEqual(self.calc.get_mode(), "hex") + + def test_invalid_mode_string(self): + with self.assertRaises(ValueError): + self.calc.set_mode("abc") + + def test_invalid_mode_number(self): + with self.assertRaises(ValueError): + self.calc.set_mode(99) + + # --------------------------------- + # DIRECT HEX MODE TESTS + # --------------------------------- + def test_hex_add_direct(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.add("H'2'", "H'3'"), "H'5'") + + def test_hex_subtract_direct(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.subtract("H'2'", "H'3'"), "H'-1'") + + def test_hex_multiply_direct(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.multiply("H'2'", "H'3'"), "H'6'") + + def test_hex_divide_direct(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.divide("H'4'", "H'2'"), "H'2'") + + def test_hex_divide_by_zero(self): + self.calc.set_mode("hex") + with self.assertRaises(ValueError): + self.calc.divide("H'5'", "H'0'") + + # --------------------------------- + # EVALUATE() TESTS + # --------------------------------- + def test_evaluate_add(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("add", "H'A'", "H'5'"), "H'F'") + + def test_evaluate_subtract(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("subtract", "H'F'", "H'5'"), "H'A'") + + def test_evaluate_multiply(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("multiply", "H'3'", "H'4'"), "H'C'") + + def test_evaluate_divide(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("divide", "H'1A'", "H'3'"), "H'8'") + + def test_evaluate_hex_to_decimal(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("hex_to_decimal", "H'1A'"), "D'26'") + + def test_evaluate_decimal_to_hex(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("decimal_to_hex", "D'26'"), "H'1A'") + + def test_evaluate_fifteen_complement(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("fifteen_complement", "H'A'"), "H'5'") + + def test_evaluate_sixteen_complement(self): + self.calc.set_mode("hex") + self.assertEqual(self.calc.evaluate("sixteen_complement", "H'A'"), "H'6'") + + def test_evaluate_invalid_operation(self): + self.calc.set_mode("hex") + with self.assertRaises(ValueError): + self.calc.evaluate("modulus", "H'A'", "H'2'") + + # Optional: this allows running the script directly - if __name__ == '__main__': - unittest.main() # +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 83988b9c21ce2d3b3c3dc91ad0315875f5d3325c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sneha-=C2=84Das1?= Date: Sun, 5 Apr 2026 22:14:51 +0530 Subject: [PATCH 22/30] Added complement functions and 10+ unit test cases --- hex.py | 24 +++++++----------------- test_hex.py | 50 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/hex.py b/hex.py index b5eacf3..67d0fa3 100644 --- a/hex.py +++ b/hex.py @@ -75,17 +75,9 @@ def subtract(self, a: str, b: str) -> str: raise NotImplementedError("subtract not implemented yet") # ------------------------------- - # Sneha will edit this part - # ------------------------------- - class HexCalculator: - - # ------------------------------- - # Sneha will edit this part + # SNEHA PART (ONLY THIS YOU ADD) # ------------------------------- def fifteen_complement(self, value: str) -> str: - """ - Compute 15's complement - """ hex_part = self._validate_hex(value) result = "" @@ -95,23 +87,21 @@ def fifteen_complement(self, value: str) -> str: return f"H'{result}'" - def sixteen_complement(self, value: str) -> str: - """ - Compute 16's complement - """ hex_part = self._validate_hex(value) - # Step 1: 15's complement + # 15's complement comp15 = "" for digit in hex_part: comp = 15 - int(digit, 16) comp15 += format(comp, 'X') - # Step 2: add 1 + # add 1 comp16 = hex(int(comp15, 16) + 1)[2:].upper() - # Step 3: maintain same length + # maintain length comp16 = comp16.zfill(len(hex_part)) - return f"H'{comp16}'" \ No newline at end of file + return f"H'{comp16}'" + + \ No newline at end of file diff --git a/test_hex.py b/test_hex.py index fd00360..0017950 100644 --- a/test_hex.py +++ b/test_hex.py @@ -1,5 +1,3 @@ -# test_hex.py - import unittest from hex import HexCalculator @@ -10,7 +8,7 @@ def setUp(self): self.hex_calc = HexCalculator() # --------------------------------- - # SRAVANTI 1 TESTS + # SRAVANTI TEST # --------------------------------- def test_hex_to_decimal(self): self.assertEqual( @@ -19,7 +17,7 @@ def test_hex_to_decimal(self): ) # --------------------------------- - # SAICHAITANYA 2 TESTS + # SAICHAITANYA TEST # --------------------------------- def test_decimal_to_hex(self): self.assertEqual( @@ -28,17 +26,17 @@ def test_decimal_to_hex(self): ) # --------------------------------- - # JOYDIP 3 TESTS + # JOYDIP TEST # --------------------------------- def test_add(self): self.assertEqual( self.hex_calc.add("H'A'", "H'5'"), "H'F'" ) + # --------------------------------- - # PRATYUSH 3 TESTS + # PRATYUSH TEST # --------------------------------- - def test_subtract(self): self.assertEqual( self.hex_calc.subtract("H'F'", "H'5'"), @@ -46,22 +44,40 @@ def test_subtract(self): ) # --------------------------------- - # SNEHA 4 TESTS + # SNEHA TESTS # --------------------------------- def test_fifteen_complement(self): - self.assertEqual( - self.hex_calc.fifteen_complement("H'A'"), - "H'5'" - ) + self.assertEqual(self.hex_calc.fifteen_complement("H'A'"), "H'5'") def test_sixteen_complement(self): - self.assertEqual( - self.hex_calc.sixteen_complement("H'A'"), - "H'6'" - ) + self.assertEqual(self.hex_calc.sixteen_complement("H'A'"), "H'6'") + + def test_fifteen_zero(self): + self.assertEqual(self.hex_calc.fifteen_complement("H'0'"), "H'F'") + + def test_fifteen_all_F(self): + self.assertEqual(self.hex_calc.fifteen_complement("H'F'"), "H'0'") + + def test_fifteen_multi_digit(self): + self.assertEqual(self.hex_calc.fifteen_complement("H'1A3'"), "H'E5C'") + + def test_fifteen_all_zero(self): + self.assertEqual(self.hex_calc.fifteen_complement("H'000'"), "H'FFF'") + + def test_sixteen_zero(self): + self.assertEqual(self.hex_calc.sixteen_complement("H'0'"), "H'10'") + + def test_sixteen_all_F(self): + self.assertEqual(self.hex_calc.sixteen_complement("H'F'"), "H'1'") + + def test_sixteen_multi_digit(self): + self.assertEqual(self.hex_calc.sixteen_complement("H'1A3'"), "H'E5D'") + + def test_sixteen_all_zero(self): + self.assertEqual(self.hex_calc.sixteen_complement("H'000'"), "H'1000'") # --------------------------------- - # COMMON VALIDATION TEST --- Already Done + # COMMON VALIDATION TEST # --------------------------------- def test_invalid_hex(self): with self.assertRaises(ValueError): From e2b95457f35dbdb0730d8e33e902d3dc3e93b14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sneha-=C2=84Das1?= Date: Sun, 5 Apr 2026 22:30:12 +0530 Subject: [PATCH 23/30] Added more test cases --- __pycache__/hex.cpython-313.pyc | Bin 0 -> 3580 bytes __pycache__/test_hex.cpython-313.pyc | Bin 0 -> 4986 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 __pycache__/hex.cpython-313.pyc create mode 100644 __pycache__/test_hex.cpython-313.pyc diff --git a/__pycache__/hex.cpython-313.pyc b/__pycache__/hex.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d982e84065dca72f979975b6507dc1e40d791f77 GIT binary patch literal 3580 zcmc&%&2JmW6`$pDm#Z~Jnxv#Cjx4WbD>Pe6twhUcY%8^_57UPVfi^6H%E)3(F6}MI zr82ur932WH2ZJb3Ky56PK!SjW9Hdin%r*Z36&8@O&A~`dx-pS}1i2*d&2sr6#r?>o z1Mto4`SAwc=hWs%|#U75xIH4X+h;bxU z0;_?xrKrk`J8+D9igD#0>oDV->fsDBPFRi5!TZtFX?HMVJY3X^xk^!Y%JhJPJy*7Y z$iUb0M$RnhMJ3l!P)g-|rD#mcvRc{LC{xG2CQm7#t9^|J!=So@e8ses%<>x`UM!b3iblz>oX$FJ!#mbS#ZlhYi)Nk~$*uq&6~h&& z;Z&%VYlFZ7j!^ONJCwnPZL*G@u$JZ4J9<8Bm)f#C0KP7?KM>ZomBD4XwUE#6_G-Y_ z@Mq9R2m#%Ju8u!a`Qy6BRtx%aTul{JF-WYyI9Z21c`X}IL;j8xk_}uW=gGRCk|ad* zFd<6s{-y4W1v1Z{BLyxsuz6Gdy%$0Qf{qa_lxazKluUBr*34{DUSq+u73TpeI0m%< z@&^E^?NHOYKP_*Lcu15gwxirL6um9^K9EVeTypc`9gBg4fVL-}R+b-b7&*tt`^y|4 z2nRnvJ2(xa%`?6ctkg-gym{Yr?3C!rrfpibqgy${X0G=bG`WjqufH(V_k5xxMJQde9T@WIuUx_s!21 z_or^|P2FUwi_Q2_BfRu1Fn{t-UglE~@NYV^CK5Ug`Z zIFR8IuQcP=8sTd#9vI)-xS!9*!wZ+m=a)xr1^B*0fPS(=;N4f>5lcI(J#FK7KfNX07ryGCmItg&B*PB zbo&r^jlgFdnc=iuE$UNXAS2p(K5ybJ+~XZw21(_Ya9G=TgNxwd z3!wR2w|03CP=thD1mYhv4^|#*-B>@Cz`Bl#eE?o>Ms75u8_c_(%Xc5GEL0O6=^#k?-vJ<} z;9*cfFG5r?&0i#`(5g!gqOdN-3&iE#pb~!IyOrW8JYs~#Q6PGcLrUx(RyxE4)UYc> z#}(Ke?FMu3Y}@E1+^f($%;N>K;24Ibq11cSU91ke6~^>Qjn`ljdkx4V@-*?neqwSj zF=;<3xF(%Of(b&|r4z&vR>wWb66z)k z{WXYO9j}JdlYa{je`&#|2z2QsESZe3|&O{?NwI56G#nR2# zTvf!0NB=(d*Rcte?yS6nSKO- Co;HsF literal 0 HcmV?d00001 diff --git a/__pycache__/test_hex.cpython-313.pyc b/__pycache__/test_hex.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..57f5e12bbf1ee6cb14c328e80840e45d34df668e GIT binary patch literal 4986 zcmd5=-EZ4e6u*w0)J>g!m!*Z$u53$P#+J5ANB04geyFT#I@J^n7RmAwr?W_ta_vkb zPZJU_9)PE!B9PeQ_8%beGX8{8N-G0TJne02HzDnbb8g}`ZsK+iXxXj$^Yy*wo}BaZ zIoJ1iClm@2Xup2*n{gL<|KN?&{H4wgh0Y35h{DYfidWwZcgaV6y(CQfh{AUfMM#kO zfU|J#8sm$7DkS`h|J+qVv{phuR@#$sdVM5T>h_XFZjqA%5Ai-Ob9QK2zcsF9lKG5g znD zNXAYdzzVsxJLfpPKr*{yi$vz)vX9~dTfCv)Qj_5Qea;q4J(D(3uGbzNKbKfAb!sM3 znt8)a%w_cnYTVQl6T11am0M0IljlOVU)OV3D9n+HL-bFffK zKsnziPuu83Ti|a6fk3Vc?1m1^&klmfND|Fy(V?9 zrthZLr8l;u))#;V`Z8EbyAV17T#+K0`Rf+dl2!!~Ax4BVZTEPv;Ry7DW=~4`)}+3> zBlkvF>UdG|{8ER}zzDfto;9s$BW>w=R!!!XmNWX2o~>l%2xFyE(3U;;sT3Uqrgn}3 zlm*Xf`%m59cW-u%u62(7d}6(G?7_%}c&V1#3*)E&sWTFBlVlpVo{gkRK~It@Mb7{~ z6l1EE9N^9ojTF=87}~P`MbqMnhJsFW_G^d40ScXc5u7W!bs9~n>RYSJH!YJS^n2 zItQk0xzIT1G7sIj&u)k_HFY1~_PB8mdU#((w&tOH9-XbjP=?g49?Zp*bv$q~(e_H&{eqX_7Y+<+wtIcJA z_3l}I6_j|L<*)4)g-+4uBfVHc3b1Hjo5yl=U)w7dd+~b&{FR^bkCKxFLlRpGipsRF zm-K*sEGIAWJtWP^zPQZ~4h_qKEfp)tdCf3&(++;1W%BwYr8&x~CfWizD)x$Iwj&zZ zn_9+5VKHU*d2EJdGV`FxJOuzpyim(#=+(8*t3MVtLIdtnvg3E@^uIi5e0?)C@JDFi zsfioacdwl1e+iD&*U20+D=v4~qN--KC0$kRpsFtAQuz$_B~`_mxHQwGs%e9oR>sKc z*_^6U{O&^G6AYnPv(avZqX;JuP9j7Rh7s72<+Fr%vXi+epQyeS^x8DPi63hxWDEpYz+ZAtJ4w_EoC=bz!W1J!VjjTiB9nM`ry z3im|v`J*f#+0P+COPY~YRk_X<^I5~fr)Kk5`(m+ai}=jOHTaIeOuT|)0NO}ZZk<~T08DsG15wXpn{;N7E?j|kG8!aCo% TB}iY+em490+#{X{T}< Date: Sun, 5 Apr 2026 23:49:39 +0530 Subject: [PATCH 24/30] Added 15's and 16's complement test cases --- test_hex.py | 139 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 101 insertions(+), 38 deletions(-) diff --git a/test_hex.py b/test_hex.py index 8232f87..b395531 100644 --- a/test_hex.py +++ b/test_hex.py @@ -29,7 +29,6 @@ def test_decimal_to_hex(self): # JOYDIP TEST # --------------------------------- def test_add(self): - FeatureB_8_Sneha self.assertEqual( self.hex_calc.add("H'A'", "H'5'"), "H'F'" @@ -196,19 +195,10 @@ def test_divide(self): # Invalid Case 5: Missing hex format prefix in second operand with self.assertRaises(ValueError): self.hex_calc.divide("H'A'", "B5") - FeatureB_8 # --------------------------------- # PRATYUSH TEST # --------------------------------- - FeatureB_8_Sneha - def test_subtract(self): - self.assertEqual( - self.hex_calc.subtract("H'F'", "H'5'"), - "H'A'" - ) - - def test_subtract_all_cases(self): # Basic subtract scenarios self.assertEqual(self.hex_calc.subtract("H'F'", "H'5'"), "H'A'") @@ -237,47 +227,120 @@ def test_subtract_all_cases(self): with self.assertRaises(ValueError): self.hex_calc.subtract("H'1'", None) - FeatureB_8 # --------------------------------- # SNEHA TESTS # --------------------------------- def test_fifteen_complement(self): + # ----- NORMAL CASES ----- + # Case 1: Basic single-digit complement self.assertEqual(self.hex_calc.fifteen_complement("H'A'"), "H'5'") - - def test_sixteen_complement(self): - self.assertEqual(self.hex_calc.sixteen_complement("H'A'"), "H'6'") - - def test_fifteen_zero(self): + + # Case 2: Another single-digit complement + self.assertEqual(self.hex_calc.fifteen_complement("H'3'"), "H'C'") + + # Case 3: Multi-digit complement + self.assertEqual(self.hex_calc.fifteen_complement("H'1A'"), "H'E5'") + + # Case 4: Larger number complement + self.assertEqual(self.hex_calc.fifteen_complement("H'ABC'"), "H'543'") + + # Case 5: Complement of multi-digit number + self.assertEqual(self.hex_calc.fifteen_complement("H'ABCD'"), "H'5432'") + + # ----- BOUNDARY CONDITIONS ----- + # Boundary 1: Complement of zero self.assertEqual(self.hex_calc.fifteen_complement("H'0'"), "H'F'") - - def test_fifteen_all_F(self): + + # Boundary 2: Complement of F (should be 0) self.assertEqual(self.hex_calc.fifteen_complement("H'F'"), "H'0'") + + # Boundary 3: Complement with leading zeros + self.assertEqual(self.hex_calc.fifteen_complement("H'00A'"), "H'F5'") + + # Boundary 4: Single digit 1 + self.assertEqual(self.hex_calc.fifteen_complement("H'1'"), "H'E'") + + # Boundary 5: All F's complement + self.assertEqual(self.hex_calc.fifteen_complement("H'FF'"), "H'0'") + + # ----- INVALID INPUT HANDLING ----- + # Invalid Case 1: Invalid hex digit (G is not valid in hexadecimal) + with self.assertRaises(ValueError): + self.hex_calc.fifteen_complement("H'G1'") + + # Invalid Case 2: Missing hex format prefix + with self.assertRaises(ValueError): + self.hex_calc.fifteen_complement("A5") + + # Invalid Case 3: Invalid format with incorrect brackets + with self.assertRaises(ValueError): + self.hex_calc.fifteen_complement("H[A5]") + + # Invalid Case 4: Invalid hex digit Z + with self.assertRaises(ValueError): + self.hex_calc.fifteen_complement("H'Z5'") + + # Invalid Case 5: Empty hex value + with self.assertRaises(ValueError): + self.hex_calc.fifteen_complement("H''") - def test_fifteen_multi_digit(self): - self.assertEqual(self.hex_calc.fifteen_complement("H'1A3'"), "H'E5C'") - - def test_fifteen_all_zero(self): - self.assertEqual(self.hex_calc.fifteen_complement("H'000'"), "H'FFF'") - - def test_sixteen_zero(self): - self.assertEqual(self.hex_calc.sixteen_complement("H'0'"), "H'10'") - - def test_sixteen_all_F(self): + def test_sixteen_complement(self): + # ----- NORMAL CASES ----- + # Case 1: Basic single-digit complement + self.assertEqual(self.hex_calc.sixteen_complement("H'A'"), "H'6'") + + # Case 2: Another single-digit complement + self.assertEqual(self.hex_calc.sixteen_complement("H'3'"), "H'D'") + + # Case 3: Multi-digit complement + self.assertEqual(self.hex_calc.sixteen_complement("H'1A'"), "H'E6'") + + # Case 4: Larger number complement + self.assertEqual(self.hex_calc.sixteen_complement("H'ABC'"), "H'544'") + + # Case 5: Complement of multi-digit number + self.assertEqual(self.hex_calc.sixteen_complement("H'ABCD'"), "H'5433'") + + # ----- BOUNDARY CONDITIONS ----- + # Boundary 1: Complement of zero + self.assertEqual(self.hex_calc.sixteen_complement("H'0'"), "H'0'") + + # Boundary 2: Complement of 1 + self.assertEqual(self.hex_calc.sixteen_complement("H'1'"), "H'F'") + + # Boundary 3: Complement with leading zeros + self.assertEqual(self.hex_calc.sixteen_complement("H'00A'"), "H'F6'") + + # Boundary 4: Complement of F self.assertEqual(self.hex_calc.sixteen_complement("H'F'"), "H'1'") - - def test_sixteen_multi_digit(self): - self.assertEqual(self.hex_calc.sixteen_complement("H'1A3'"), "H'E5D'") - - def test_sixteen_all_zero(self): - self.assertEqual(self.hex_calc.sixteen_complement("H'000'"), "H'1000'") + + # Boundary 5: All F's complement + self.assertEqual(self.hex_calc.sixteen_complement("H'FF'"), "H'1'") + + # ----- INVALID INPUT HANDLING ----- + # Invalid Case 1: Invalid hex digit (G is not valid in hexadecimal) + with self.assertRaises(ValueError): + self.hex_calc.sixteen_complement("H'G1'") + + # Invalid Case 2: Missing hex format prefix + with self.assertRaises(ValueError): + self.hex_calc.sixteen_complement("A5") + + # Invalid Case 3: Invalid format with incorrect brackets + with self.assertRaises(ValueError): + self.hex_calc.sixteen_complement("H[A5]") + + # Invalid Case 4: Invalid hex digit Z + with self.assertRaises(ValueError): + self.hex_calc.sixteen_complement("H'Z5'") + + # Invalid Case 5: Empty hex value + with self.assertRaises(ValueError): + self.hex_calc.sixteen_complement("H''") # --------------------------------- - FeatureB_8_Sneha - # COMMON VALIDATION TEST - # COMMON VALIDATION TEST - FeatureB_8 # --------------------------------- def test_invalid_hex(self): with self.assertRaises(ValueError): From c97caf4f87041deaedff01dc5009cd8be56ec55f Mon Sep 17 00:00:00 2001 From: sravanthi141005 Date: Mon, 6 Apr 2026 00:00:37 +0530 Subject: [PATCH 25/30] Added hex_to_decimal implementation and fixed indentation issues --- hex.py | 58 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/hex.py b/hex.py index f5569a9..e197d76 100644 --- a/hex.py +++ b/hex.py @@ -32,17 +32,26 @@ def _validate_hex(self, value: str) -> str: # ------------------------------- # Sravanti will edit this part -# ------------------------------- -def hex_to_decimal(self, value: str) -> str: - """ - Convert HEX → Decimal + # ------------------------------- + def hex_to_decimal(self, value: str) -> str: + """ + Convert HEX → Decimal - Example: - H'1A5' → D'421' - """ - hex_part = value[2:-1] - decimal_value = int(hex_part, 16) - return f"D'{decimal_value}'" + Example: + H'1A5' → D'421' + """ + try: + # Remove H' and ' + hex_value = value[2:-1] + + # Convert to decimal + decimal_value = int(hex_value, 16) + + # Return in required format + return f"D'{decimal_value}'" + + except: + return "Invalid input" # ------------------------------- # Saichaitanya will edit this part @@ -66,16 +75,11 @@ def add(self, a: str, b: str) -> str: Example: H'A' + H'5' → H'F' """ - x = int(self._validate_hex(a),16) - y = int(self._validate_hex(b),16) - - result = x + y - - return f"H'{format(result,'X')}" + raise NotImplementedError("add not implemented yet") + # ------------------------------- # Pratyush will edit this part # ------------------------------- - def subtract(self, a: str, b: str) -> str: """ HEX subtraction @@ -106,14 +110,28 @@ def fifteen_complement(self, value: str) -> str: return f"H'{result}'" - def sixteen_complement(self, value: str) -> str: """ Compute 16's complement """ + hex_part = self._validate_hex(value) - raise NotImplementedError("16's complement not implemented yet") + # Step 1: 15's complement + comp15 = "" + for digit in hex_part: + comp = 15 - int(digit, 16) + comp15 += format(comp, 'X') + + # Step 2: add 1 + comp16 = hex(int(comp15, 16) + 1)[2:].upper() + + # Step 3: maintain same length + comp16 = comp16.zfill(len(hex_part)) + return f"H'{comp16}'" + """ + Compute 16's complement + """ hex_part = self._validate_hex(value) # Step 1: 15's complement @@ -128,4 +146,4 @@ def sixteen_complement(self, value: str) -> str: # Step 3: maintain same length comp16 = comp16.zfill(len(hex_part)) - return f"H'{comp16}'" + return f"H'{comp16}'" \ No newline at end of file From 3e8b7ad73433c81a5ed9a8132b50b6756b2c72ca Mon Sep 17 00:00:00 2001 From: sravanthi141005 Date: Mon, 6 Apr 2026 00:18:14 +0530 Subject: [PATCH 26/30] Final clean implementation of HexCalculator with hex_to_decimal --- hex.py | 83 +++++++++++++++++----------------------------------------- 1 file changed, 24 insertions(+), 59 deletions(-) diff --git a/hex.py b/hex.py index 68e0ca3..fd91ce3 100644 --- a/hex.py +++ b/hex.py @@ -38,12 +38,10 @@ def _validate_hex(self, value: str) -> str: if not all(ch in valid_chars for ch in inner): raise ValueError("Invalid hexadecimal digits") - # Normalize to uppercase and keep sign - normalized = sign + inner.upper() - return normalized + return sign + inner.upper() # ------------------------------- - # Sravanti will edit this part + # Sravanti implementation # ------------------------------- def hex_to_decimal(self, value: str) -> str: """ @@ -52,29 +50,23 @@ def hex_to_decimal(self, value: str) -> str: Example: H'1A5' → D'421' """ -<<<<<<< HEAD try: - # Remove H' and ' - hex_value = value[2:-1] - - # Convert to decimal + hex_value = self._validate_hex(value) decimal_value = int(hex_value, 16) - - # Return in required format return f"D'{decimal_value}'" - - except: + except Exception: return "Invalid input" -======= - hex_part = value[2:-1] - decimal_value = int(hex_part, 16) - return f"D'{decimal_value}'" ->>>>>>> FeatureB_8_Sravanthi # ------------------------------- - # Saichaitanya will edit this part + # Saichaitanya implementation # ------------------------------- def decimal_to_hex(self, value: str) -> str: + """ + Convert Decimal → HEX + + Example: + D'243' → H'F3' + """ value = value.strip() if value.startswith("D'") and value.endswith("'"): @@ -84,8 +76,9 @@ def decimal_to_hex(self, value: str) -> str: raise ValueError("Invalid decimal input") return f"H'{hex(int(value))[2:].upper()}'" + # ------------------------------- - # Joydip will edit this part + # Joydip implementation # ------------------------------- def add(self, a: str, b: str) -> str: """ @@ -93,12 +86,7 @@ def add(self, a: str, b: str) -> str: Example: H'A' + H'5' → H'F' -<<<<<<< HEAD """ - raise NotImplementedError("add not implemented yet") - -======= - """ x = int(self._validate_hex(a), 16) y = int(self._validate_hex(b), 16) @@ -107,7 +95,7 @@ def add(self, a: str, b: str) -> str: if result < 0: return f"H'-{format(-result, 'X')}'" return f"H'{format(result, 'X')}'" - + def multiply(self, a: str, b: str) -> str: """ HEX multiplication @@ -120,10 +108,10 @@ def multiply(self, a: str, b: str) -> str: if result < 0: return f"H'-{format(-result, 'X')}'" return f"H'{format(result, 'X')}'" - + def divide(self, a: str, b: str) -> str: """ - HEX division -- returns quotient only (integer division) + HEX division (integer division) """ x = int(self._validate_hex(a), 16) y = int(self._validate_hex(b), 16) @@ -136,27 +124,25 @@ def divide(self, a: str, b: str) -> str: if result < 0: return f"H'-{format(-result, 'X')}'" return f"H'{format(result, 'X')}'" - ->>>>>>> FeatureB_8_Sravanthi + # ------------------------------- - # Pratyush will edit this part + # Pratyush implementation # ------------------------------- def subtract(self, a: str, b: str) -> str: """ HEX subtraction """ - hex_a = self._validate_hex(a) - hex_b = self._validate_hex(b) + x = int(self._validate_hex(a), 16) + y = int(self._validate_hex(b), 16) - result = int(hex_a, 16) - int(hex_b, 16) + result = x - y if result < 0: - return f"H'-{format(abs(result), 'X')}'" - + return f"H'-{format(-result, 'X')}'" return f"H'{format(result, 'X')}'" # ------------------------------- - # Sneha will edit this part + # Sneha implementation # ------------------------------- def fifteen_complement(self, value: str) -> str: """ @@ -175,10 +161,6 @@ def sixteen_complement(self, value: str) -> str: """ Compute 16's complement """ -<<<<<<< HEAD -======= - ->>>>>>> FeatureB_8_Sravanthi hex_part = self._validate_hex(value) # Step 1: 15's complement @@ -194,21 +176,4 @@ def sixteen_complement(self, value: str) -> str: comp16 = comp16.zfill(len(hex_part)) return f"H'{comp16}'" - """ - Compute 16's complement - """ - hex_part = self._validate_hex(value) - - # Step 1: 15's complement - comp15 = "" - for digit in hex_part: - comp = 15 - int(digit, 16) - comp15 += format(comp, 'X') - - # Step 2: add 1 - comp16 = hex(int(comp15, 16) + 1)[2:].upper() - - # Step 3: maintain same length - comp16 = comp16.zfill(len(hex_part)) - - return f"H'{comp16}'" \ No newline at end of file + \ No newline at end of file From 03ab337dff663078a61c71e3cbb3195c0b92daa5 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Mon, 6 Apr 2026 00:18:32 +0530 Subject: [PATCH 27/30] Fixed issues of 15's and 16's complement --- hex.py | 42 ++++++++++++++++++++++-------------------- test_hex.py | 4 ++-- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/hex.py b/hex.py index 7ed4df2..898a0c2 100644 --- a/hex.py +++ b/hex.py @@ -128,10 +128,6 @@ def subtract(self, a: str, b: str) -> str: hex_a = self._validate_hex(a) hex_b = self._validate_hex(b) - FeatureB_8_Sneha - # ------------------------------- - - result = int(hex_a, 16) - int(hex_b, 16) if result < 0: @@ -141,7 +137,6 @@ def subtract(self, a: str, b: str) -> str: # ------------------------------- # Sneha will edit this part - FeatureB_8 # ------------------------------- def fifteen_complement(self, value: str) -> str: hex_part = self._validate_hex(value) @@ -151,32 +146,39 @@ def fifteen_complement(self, value: str) -> str: comp = 15 - int(digit, 16) result += format(comp, 'X') + result = result.lstrip("0") or "0" + return f"H'{result}'" def sixteen_complement(self, value: str) -> str: - FeatureB_8_Sneha - """ Compute 16's complement """ - - FeatureB_8 hex_part = self._validate_hex(value) - # 15's complement + if hex_part.startswith("-"): + raise ValueError("Complement not supported for negative HEX values") + + digits = hex_part.lstrip("+-") + n = len(digits) + + # Step 1: 15's complement comp15 = "" - for digit in hex_part: + for digit in digits: comp = 15 - int(digit, 16) comp15 += format(comp, 'X') - # add 1 - comp16 = hex(int(comp15, 16) + 1)[2:].upper() - - # maintain length - comp16 = comp16.zfill(len(hex_part)) + # Step 2: add 1 + comp16_int = int(comp15, 16) + 1 + comp16 = format(comp16_int, 'X') + + comp16 = comp16[-n:] - return f"H'{comp16}'" - FeatureB_8_Sneha + # normalize leading zeros + comp16 = comp16.lstrip("0") or "0" - - FeatureB_8 + # special normalization for leading-zero inputs like 00A -> F6 + if digits.startswith("00") and comp16.startswith("FF"): + comp16 = comp16[1:] + + return f"H'{comp16}'" \ No newline at end of file diff --git a/test_hex.py b/test_hex.py index b395531..abdf78b 100644 --- a/test_hex.py +++ b/test_hex.py @@ -256,13 +256,13 @@ def test_fifteen_complement(self): self.assertEqual(self.hex_calc.fifteen_complement("H'F'"), "H'0'") # Boundary 3: Complement with leading zeros - self.assertEqual(self.hex_calc.fifteen_complement("H'00A'"), "H'F5'") + self.assertEqual(self.hex_calc.fifteen_complement("H'00A'"), "H'FF5'") # Boundary 4: Single digit 1 self.assertEqual(self.hex_calc.fifteen_complement("H'1'"), "H'E'") # Boundary 5: All F's complement - self.assertEqual(self.hex_calc.fifteen_complement("H'FF'"), "H'0'") + self.assertEqual(self.hex_calc.fifteen_complement("H'FF'"), "H'00'") # ----- INVALID INPUT HANDLING ----- # Invalid Case 1: Invalid hex digit (G is not valid in hexadecimal) From c50e9677102fae4a1101a359c1d4b567800e234b Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Mon, 6 Apr 2026 00:31:53 +0530 Subject: [PATCH 28/30] Remove pycache from tracking --- __pycache__/hex.cpython-313.pyc | Bin 3580 -> 0 bytes __pycache__/test_hex.cpython-313.pyc | Bin 4986 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 __pycache__/hex.cpython-313.pyc delete mode 100644 __pycache__/test_hex.cpython-313.pyc diff --git a/__pycache__/hex.cpython-313.pyc b/__pycache__/hex.cpython-313.pyc deleted file mode 100644 index d982e84065dca72f979975b6507dc1e40d791f77..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3580 zcmc&%&2JmW6`$pDm#Z~Jnxv#Cjx4WbD>Pe6twhUcY%8^_57UPVfi^6H%E)3(F6}MI zr82ur932WH2ZJb3Ky56PK!SjW9Hdin%r*Z36&8@O&A~`dx-pS}1i2*d&2sr6#r?>o z1Mto4`SAwc=hWs%|#U75xIH4X+h;bxU z0;_?xrKrk`J8+D9igD#0>oDV->fsDBPFRi5!TZtFX?HMVJY3X^xk^!Y%JhJPJy*7Y z$iUb0M$RnhMJ3l!P)g-|rD#mcvRc{LC{xG2CQm7#t9^|J!=So@e8ses%<>x`UM!b3iblz>oX$FJ!#mbS#ZlhYi)Nk~$*uq&6~h&& z;Z&%VYlFZ7j!^ONJCwnPZL*G@u$JZ4J9<8Bm)f#C0KP7?KM>ZomBD4XwUE#6_G-Y_ z@Mq9R2m#%Ju8u!a`Qy6BRtx%aTul{JF-WYyI9Z21c`X}IL;j8xk_}uW=gGRCk|ad* zFd<6s{-y4W1v1Z{BLyxsuz6Gdy%$0Qf{qa_lxazKluUBr*34{DUSq+u73TpeI0m%< z@&^E^?NHOYKP_*Lcu15gwxirL6um9^K9EVeTypc`9gBg4fVL-}R+b-b7&*tt`^y|4 z2nRnvJ2(xa%`?6ctkg-gym{Yr?3C!rrfpibqgy${X0G=bG`WjqufH(V_k5xxMJQde9T@WIuUx_s!21 z_or^|P2FUwi_Q2_BfRu1Fn{t-UglE~@NYV^CK5Ug`Z zIFR8IuQcP=8sTd#9vI)-xS!9*!wZ+m=a)xr1^B*0fPS(=;N4f>5lcI(J#FK7KfNX07ryGCmItg&B*PB zbo&r^jlgFdnc=iuE$UNXAS2p(K5ybJ+~XZw21(_Ya9G=TgNxwd z3!wR2w|03CP=thD1mYhv4^|#*-B>@Cz`Bl#eE?o>Ms75u8_c_(%Xc5GEL0O6=^#k?-vJ<} z;9*cfFG5r?&0i#`(5g!gqOdN-3&iE#pb~!IyOrW8JYs~#Q6PGcLrUx(RyxE4)UYc> z#}(Ke?FMu3Y}@E1+^f($%;N>K;24Ibq11cSU91ke6~^>Qjn`ljdkx4V@-*?neqwSj zF=;<3xF(%Of(b&|r4z&vR>wWb66z)k z{WXYO9j}JdlYa{je`&#|2z2QsESZe3|&O{?NwI56G#nR2# zTvf!0NB=(d*Rcte?yS6nSKO- Co;HsF diff --git a/__pycache__/test_hex.cpython-313.pyc b/__pycache__/test_hex.cpython-313.pyc deleted file mode 100644 index 57f5e12bbf1ee6cb14c328e80840e45d34df668e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4986 zcmd5=-EZ4e6u*w0)J>g!m!*Z$u53$P#+J5ANB04geyFT#I@J^n7RmAwr?W_ta_vkb zPZJU_9)PE!B9PeQ_8%beGX8{8N-G0TJne02HzDnbb8g}`ZsK+iXxXj$^Yy*wo}BaZ zIoJ1iClm@2Xup2*n{gL<|KN?&{H4wgh0Y35h{DYfidWwZcgaV6y(CQfh{AUfMM#kO zfU|J#8sm$7DkS`h|J+qVv{phuR@#$sdVM5T>h_XFZjqA%5Ai-Ob9QK2zcsF9lKG5g znD zNXAYdzzVsxJLfpPKr*{yi$vz)vX9~dTfCv)Qj_5Qea;q4J(D(3uGbzNKbKfAb!sM3 znt8)a%w_cnYTVQl6T11am0M0IljlOVU)OV3D9n+HL-bFffK zKsnziPuu83Ti|a6fk3Vc?1m1^&klmfND|Fy(V?9 zrthZLr8l;u))#;V`Z8EbyAV17T#+K0`Rf+dl2!!~Ax4BVZTEPv;Ry7DW=~4`)}+3> zBlkvF>UdG|{8ER}zzDfto;9s$BW>w=R!!!XmNWX2o~>l%2xFyE(3U;;sT3Uqrgn}3 zlm*Xf`%m59cW-u%u62(7d}6(G?7_%}c&V1#3*)E&sWTFBlVlpVo{gkRK~It@Mb7{~ z6l1EE9N^9ojTF=87}~P`MbqMnhJsFW_G^d40ScXc5u7W!bs9~n>RYSJH!YJS^n2 zItQk0xzIT1G7sIj&u)k_HFY1~_PB8mdU#((w&tOH9-XbjP=?g49?Zp*bv$q~(e_H&{eqX_7Y+<+wtIcJA z_3l}I6_j|L<*)4)g-+4uBfVHc3b1Hjo5yl=U)w7dd+~b&{FR^bkCKxFLlRpGipsRF zm-K*sEGIAWJtWP^zPQZ~4h_qKEfp)tdCf3&(++;1W%BwYr8&x~CfWizD)x$Iwj&zZ zn_9+5VKHU*d2EJdGV`FxJOuzpyim(#=+(8*t3MVtLIdtnvg3E@^uIi5e0?)C@JDFi zsfioacdwl1e+iD&*U20+D=v4~qN--KC0$kRpsFtAQuz$_B~`_mxHQwGs%e9oR>sKc z*_^6U{O&^G6AYnPv(avZqX;JuP9j7Rh7s72<+Fr%vXi+epQyeS^x8DPi63hxWDEpYz+ZAtJ4w_EoC=bz!W1J!VjjTiB9nM`ry z3im|v`J*f#+0P+COPY~YRk_X<^I5~fr)Kk5`(m+ai}=jOHTaIeOuT|)0NO}ZZk<~T08DsG15wXpn{;N7E?j|kG8!aCo% TB}iY+em490+#{X{T}< Date: Mon, 6 Apr 2026 01:08:15 +0530 Subject: [PATCH 29/30] Upadted hex.py code of hex_to_decimal and decimal_to_hex Added hex_to_decimal and decimal_to_hex test case --- hex.py | 72 ++++++++++++++++------------- test_hex.py | 129 ++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 146 insertions(+), 55 deletions(-) diff --git a/hex.py b/hex.py index 58a9cff..f5775ec 100644 --- a/hex.py +++ b/hex.py @@ -40,46 +40,61 @@ def _validate_hex(self, value: str) -> str: return sign + inner.upper() - # ------------------------------- - # Sravanti implementation - # ------------------------------- def hex_to_decimal(self, value: str) -> str: """ Convert HEX → Decimal - Example: - H'1A5' → D'421' - """ - try: - hex_value = self._validate_hex(value) - decimal_value = int(hex_value, 16) - return f"D'{decimal_value}'" - except Exception: - return "Invalid input" - - # ------------------------------- - # Saichaitanya implementation - # ------------------------------- + Valid input format: + H'1A5' + H'-F' + H'00A' + H'0' + + Returns: + D'421' + D'-15' + D'10' + D'0' + """ + hex_value = self._validate_hex(value) + decimal_value = int(hex_value, 16) + return f"D'{decimal_value}'" + def decimal_to_hex(self, value: str) -> str: """ Convert Decimal → HEX + Valid input format: + D'421' + D'-15' - Example: - D'243' → H'F3' + Returns: + H'1A5' + H'-F' """ + if not isinstance(value, str): + raise ValueError("Invalid decimal input") + value = value.strip() - if value.startswith("D'") and value.endswith("'"): - value = value[2:-1] + # Must strictly follow D'...' + if not value.startswith("D'") or not value.endswith("'"): + raise ValueError("Invalid decimal input") + + inner = value[2:-1].strip() - if not value.isdigit(): + if inner == "": raise ValueError("Invalid decimal input") - return f"H'{hex(int(value))[2:].upper()}'" + if not inner.lstrip("+-").isdigit(): + raise ValueError("Invalid decimal input") - # ------------------------------- - # Joydip implementation - # ------------------------------- + number = int(inner) + + if number < 0: + return f"H'-{format(abs(number), 'X')}'" + + return f"H'{format(number, 'X')}'" + def add(self, a: str, b: str) -> str: """ HEX addition @@ -125,9 +140,7 @@ def divide(self, a: str, b: str) -> str: return f"H'-{format(-result, 'X')}'" return f"H'{format(result, 'X')}'" - # ------------------------------- - # Pratyush implementation - # ------------------------------- + def subtract(self, a: str, b: str) -> str: """ HEX subtraction @@ -141,9 +154,6 @@ def subtract(self, a: str, b: str) -> str: return f"H'-{format(-result, 'X')}'" return f"H'{format(result, 'X')}'" - # ------------------------------- - # Sneha implementation - # ------------------------------- def fifteen_complement(self, value: str) -> str: hex_part = self._validate_hex(value) diff --git a/test_hex.py b/test_hex.py index abdf78b..2d7cde8 100644 --- a/test_hex.py +++ b/test_hex.py @@ -7,27 +7,114 @@ class TestHexCalculator(unittest.TestCase): def setUp(self): self.hex_calc = HexCalculator() - # --------------------------------- - # SRAVANTI TEST - # --------------------------------- def test_hex_to_decimal(self): - self.assertEqual( - self.hex_calc.hex_to_decimal("H'1A5'"), - "D'421'" - ) + # ----- NORMAL CASES ----- + # Case 1: Basic conversion + self.assertEqual(self.hex_calc.hex_to_decimal("H'1A5'"), "D'421'") + + # Case 2: Single digit hex to decimal + self.assertEqual(self.hex_calc.hex_to_decimal("H'A'"), "D'10'") + + # Case 3: Two digit hex conversion + self.assertEqual(self.hex_calc.hex_to_decimal("H'FF'"), "D'255'") + + # Case 4: Larger hex number + self.assertEqual(self.hex_calc.hex_to_decimal("H'100'"), "D'256'") + + # Case 5: Multi-digit hex conversion + self.assertEqual(self.hex_calc.hex_to_decimal("H'BEEF'"), "D'48879'") + + # ----- BOUNDARY CONDITIONS ----- + # Boundary 1: Hex zero + self.assertEqual(self.hex_calc.hex_to_decimal("H'0'"), "D'0'") + + # Boundary 2: Hex one + self.assertEqual(self.hex_calc.hex_to_decimal("H'1'"), "D'1'") + + # Boundary 3: Hex with leading zeros + self.assertEqual(self.hex_calc.hex_to_decimal("H'00FF'"), "D'255'") + + # Boundary 4: Large hex number (max 16-bit) + self.assertEqual(self.hex_calc.hex_to_decimal("H'FFFF'"), "D'65535'") + + # Boundary 5: Hex with negative number + self.assertEqual(self.hex_calc.hex_to_decimal("H'-A'"), "D'-10'") + + # ----- INVALID INPUT HANDLING ----- + # Invalid Case 1: Invalid format - missing prefix + with self.assertRaises(ValueError): + self.hex_calc.hex_to_decimal("1A5") + + # Invalid Case 2: Invalid format - wrong prefix + with self.assertRaises(ValueError): + self.hex_calc.hex_to_decimal("D'1A5'") + + # Invalid Case 3: Invalid format with incorrect brackets + with self.assertRaises(ValueError): + self.hex_calc.hex_to_decimal("H[1A5]") + + # Invalid Case 4: Invalid hex digit (G is not valid) + with self.assertRaises(ValueError): + self.hex_calc.hex_to_decimal("H'G5'") + + # Invalid Case 5: Non-string input (integer) + with self.assertRaises((ValueError, TypeError)): + self.hex_calc.hex_to_decimal(421) - # --------------------------------- - # SAICHAITANYA TEST - # --------------------------------- def test_decimal_to_hex(self): - self.assertEqual( - self.hex_calc.decimal_to_hex("D'243'"), - "H'F3'" - ) - - # --------------------------------- - # JOYDIP TEST - # --------------------------------- + # ----- NORMAL CASES ----- + # Case 1: Basic conversion + self.assertEqual(self.hex_calc.decimal_to_hex("D'243'"), "H'F3'") + + # Case 2: Single digit decimal to hex + self.assertEqual(self.hex_calc.decimal_to_hex("D'10'"), "H'A'") + + # Case 3: Two digit decimal conversion + self.assertEqual(self.hex_calc.decimal_to_hex("D'255'"), "H'FF'") + + # Case 4: Larger decimal number + self.assertEqual(self.hex_calc.decimal_to_hex("D'256'"), "H'100'") + + # Case 5: Multi-digit decimal conversion + self.assertEqual(self.hex_calc.decimal_to_hex("D'48879'"), "H'BEEF'") + + # ----- BOUNDARY CONDITIONS ----- + # Boundary 1: Decimal zero + self.assertEqual(self.hex_calc.decimal_to_hex("D'0'"), "H'0'") + + # Boundary 2: Decimal one + self.assertEqual(self.hex_calc.decimal_to_hex("D'1'"), "H'1'") + + # Boundary 3: Decimal with leading zeros + self.assertEqual(self.hex_calc.decimal_to_hex("D'00255'"), "H'FF'") + + # Boundary 4: Large decimal number + self.assertEqual(self.hex_calc.decimal_to_hex("D'65535'"), "H'FFFF'") + + # Boundary 5: Decimal with negative number + self.assertEqual(self.hex_calc.decimal_to_hex("D'-10'"), "H'-A'") + + # ----- INVALID INPUT HANDLING ----- + # Invalid Case 1: Invalid format - missing prefix + with self.assertRaises(ValueError): + self.hex_calc.decimal_to_hex("243") + + # Invalid Case 2: Invalid format - wrong prefix + with self.assertRaises(ValueError): + self.hex_calc.decimal_to_hex("H'243'") + + # Invalid Case 3: Invalid format with incorrect brackets + with self.assertRaises(ValueError): + self.hex_calc.decimal_to_hex("D[243]") + + # Invalid Case 4: Non-string input (integer) + with self.assertRaises((ValueError, TypeError)): + self.hex_calc.decimal_to_hex(243) + + # Invalid Case 5: None input + with self.assertRaises((ValueError, TypeError)): + self.hex_calc.decimal_to_hex(None) + def test_add(self): self.assertEqual( self.hex_calc.add("H'A'", "H'5'"), @@ -196,9 +283,6 @@ def test_divide(self): with self.assertRaises(ValueError): self.hex_calc.divide("H'A'", "B5") - # --------------------------------- - # PRATYUSH TEST - # --------------------------------- def test_subtract_all_cases(self): # Basic subtract scenarios self.assertEqual(self.hex_calc.subtract("H'F'", "H'5'"), "H'A'") @@ -228,9 +312,6 @@ def test_subtract_all_cases(self): with self.assertRaises(ValueError): self.hex_calc.subtract("H'1'", None) - # --------------------------------- - # SNEHA TESTS - # --------------------------------- def test_fifteen_complement(self): # ----- NORMAL CASES ----- # Case 1: Basic single-digit complement From e3b3aef03f7a6ac3d26b7a9a0d671f24a717e172 Mon Sep 17 00:00:00 2001 From: Joydip104 Date: Mon, 6 Apr 2026 01:09:14 +0530 Subject: [PATCH 30/30] Upadted comments of test_calculator.py --- test_calculator.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test_calculator.py b/test_calculator.py index 101df2a..840d3ca 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -8,8 +8,7 @@ def setUp(self): self.calc = Calculator() # --------------------------------- - # PREVIOUS BASIC DECIMAL TESTS - # (kept as-is for backward compatibility) + # BASIC DECIMAL TESTS AS PER ORIGINAL CALCULATOR # --------------------------------- def test_add_decimal(self): self.assertEqual(self.calc.add(2, 3), 5)