forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_add_binary.py
122 lines (97 loc) · 4.38 KB
/
test_add_binary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Unit tests for the add_binary function.
Purpose:
- Validate the correctness of the add_binary function.
- Ensure the function handles edge cases, boundary cases, and invalid inputs.
Test Scenarios:
1. Valid binary strings with various lengths and patterns.
2. Edge cases:
- Inputs with leading zeros.
- Inputs consisting only of zeros.
- Alternating binary patterns.
3. Boundary cases:
- Maximum allowed input length (10,000 characters).
- Large binary strings with mixed patterns.
4. Invalid inputs:
- Empty strings.
- Non-binary characters.
- Inputs exceeding the length constraint.
- Strings with leading or trailing spaces.
Created on 01/01/2025
@author: SiSaR-Pal
"""
import unittest
from solutions.add_binary import add_binary
class TestAddBinary(unittest.TestCase):
"""
Unit tests for the add_binary function.
"""
# Tests for valid inputs
def test_basic_case(self):
"""Test addition of two small binary strings."""
self.assertEqual(add_binary("11", "1"), "100")
def test_different_lengths(self):
"""Test addition of binary strings with different lengths."""
self.assertEqual(add_binary("1010", "1011"), "10101")
def test_all_zeros(self):
"""Test addition of binary strings consisting only of zeros."""
self.assertEqual(add_binary("0", "0"), "0")
def test_leading_zeros(self):
"""Test addition of binary strings with leading zeros."""
self.assertEqual(add_binary("00011", "1"), "100")
def test_large_binary_strings(self):
"""Test addition of very large binary strings."""
bin_num1 = "1" * 10000
bin_num2 = "1"
expected = "1" + "0" * 10000
self.assertEqual(add_binary(bin_num1, bin_num2), expected)
def test_alternating_pattern(self):
"""Test addition of binary strings with alternating patterns."""
self.assertEqual(add_binary("101010", "010101"), "111111")
# Tests for boundary cases
def test_boundary_case_max_length_simple(self):
"""Test addition of maximum-length binary strings with simple patterns."""
bin_num1 = "1" * 10000
bin_num2 = "0" * 10000
expected = "1" * 10000
self.assertEqual(add_binary(bin_num1, bin_num2), expected)
def test_boundary_case_max_length_mixed(self):
"""Test addition of maximum-length binary strings with mixed patterns."""
bin_num1 = "10" * 5000
bin_num2 = "01" * 5000
expected = "1" * 10000
self.assertEqual(add_binary(bin_num1, bin_num2), expected)
# Tests for invalid inputs
def test_empty_first_input(self):
"""Test that a ValueError is raised when the first input is empty."""
with self.assertRaises(ValueError):
add_binary("", "1")
def test_empty_second_input(self):
"""Test that a ValueError is raised when the second input is empty."""
with self.assertRaises(ValueError):
add_binary("1", "")
def test_invalid_characters_in_first_input(self):
"""Test that a ValueError is raised when the first input contains non-binary characters."""
with self.assertRaises(ValueError):
add_binary("102", "1")
def test_invalid_characters_in_second_input(self):
"""Test that a ValueError is raised when the second input contains non-binary characters."""
with self.assertRaises(ValueError):
add_binary("11a", "1")
def test_whitespace_in_first_input(self):
"""Test that a ValueError is raised when the first input has leading or trailing spaces."""
with self.assertRaises(ValueError):
add_binary(" 1010 ", "101")
def test_whitespace_in_second_input(self):
"""Test that a ValueError is raised when the second input has leading or trailing spaces."""
with self.assertRaises(ValueError):
add_binary("1010", " 101 ")
# Additional edge cases
def test_boundary_case_zeros_in_first_input(self):
"""Test addition when the first input contains zeros with leading zeros."""
self.assertEqual(add_binary("0", "00000"), "0")
def test_boundary_case_zeros_in_second_input(self):
"""Test addition when the second input contains zeros with leading zeros."""
self.assertEqual(add_binary("000", "0"), "0")
if __name__ == "__main__":
unittest.main()