13
13
* @author [Muhammad Junaid Khalid](https://github.com/mjk22071998)
14
14
*/
15
15
16
- #include < algorithm>
17
- #include < cassert>
18
- #include < iostream>
19
- #include < string>
16
+ #include < algorithm> // / for reverse function
17
+ #include < cassert> // / for tests
18
+ #include < iostream> // / for input and outputs
19
+ #include < string> // / for sting class
20
20
21
21
/* *
22
22
* @namespace
@@ -52,13 +52,9 @@ class BinaryAddition {
52
52
result.push_back ((sum % 2 ) +
53
53
' 0' ); // Append the sum's current bit to result
54
54
}
55
-
56
- // If there's still a carry left, append it
57
55
if (carry) {
58
56
result.push_back (' 1' );
59
57
}
60
-
61
- // The result is built in reverse order, so reverse it before returning
62
58
std::reverse (result.begin (), result.end ());
63
59
return result;
64
60
}
@@ -68,50 +64,28 @@ class BinaryAddition {
68
64
/* *
69
65
* Function to run tests for the addBinary method.
70
66
*/
71
- void runTests () {
67
+ void tests () {
72
68
BinaryAddition binaryAddition;
73
69
74
- // Test case for two binary strings of equal length without any carry over.
75
70
assert (binaryAddition.addBinary (" 1010" , " 1101" ) == " 10111" );
76
-
77
- // Test case for two binary strings of equal length with a carry over.
78
71
assert (binaryAddition.addBinary (" 1111" , " 1111" ) == " 11110" );
79
-
80
- // Test case for two binary strings where one is longer than the other.
81
72
assert (binaryAddition.addBinary (" 101" , " 11" ) == " 1000" );
82
-
83
- // Test case for a binary string of all zeros.
84
73
assert (binaryAddition.addBinary (" 0" , " 0" ) == " 0" );
85
-
86
- // Test case where both binary strings consist of all ones.
87
74
assert (binaryAddition.addBinary (" 1111" , " 1111" ) == " 11110" );
88
-
89
- // Test case where one binary string is zero and the other is non-zero.
90
75
assert (binaryAddition.addBinary (" 0" , " 10101" ) == " 10101" );
91
76
assert (binaryAddition.addBinary (" 10101" , " 0" ) == " 10101" );
92
-
93
- // Test case for large binary numbers with many digits.
94
77
assert (binaryAddition.addBinary (" 101010101010101010101010101010" ,
95
78
" 110110110110110110110110110110" ) ==
96
79
" 1100001100001100001100001100000" );
97
-
98
- // Test case where one binary string is much longer than the other.
99
80
assert (binaryAddition.addBinary (" 1" , " 11111111" ) == " 100000000" );
100
-
101
- // Test case for adding empty strings (edge case).
102
81
assert (binaryAddition.addBinary (" " , " " ) == " " );
103
-
104
- // Test case where both binary strings consist of alternating ones and
105
- // zeros.
106
82
assert (binaryAddition.addBinary (" 10101010" , " 01010101" ) == " 11111111" );
107
-
108
- std::cout << " All tests passed!" << std::endl;
109
83
}
110
84
111
85
/* *
112
86
* Main function to execute the program.
113
87
*/
114
88
int main () {
115
- runTests ();
89
+ tests ();
116
90
return 0 ;
117
91
}
0 commit comments