Skip to content
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

test: Add self-test cases in the math/power_of_two.cpp file #1640

Merged
merged 5 commits into from
Feb 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update
  • Loading branch information
Panquesito7 committed Feb 10, 2022
commit d777772d928aceb0299611d9f2ceb54bfc48acef
69 changes: 31 additions & 38 deletions math/power_of_two.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
* approach.
*
* @author [Neha Hasija](https://github.com/neha-hasija17)
* @author [Rijul.S](https://github.com/Rijul24)
*/

//Added self implementation Cases Contribution

#include <iostream> /// for std::cout
#include <cassert> ///for assert
#include <iostream> /// for IO operations
#include <cassert> /// for assert


/**
Expand All @@ -33,25 +32,23 @@
*/
namespace math {
/**
* @brief Function to test above algorithm
* @param n description
* @returns int
* @brief This function finds whether a number is power of 2 or not
* @param n value for which we want to check
* prints the result, as "Yes, the number n is a power of 2" or
* "No, the number is not a power of 2" without quotes
* @returns 1 if `n` IS the power of 2
* @returns 0 if n is NOT a power of 2
*/

int power_of_two(int n) {
/**
* This function finds whether a number is power of 2 or not
* @param n value for which we want to check
* returns 1 if n is power of 2
* returns 0 if n is not a power of 2
*/
/// result stores the
/// bitwise and of n and n-1
int result = n & (n - 1);

if(result == 0) return 1 ; //yes it is
else return 0; // no it is not

if (result == 0) {
return 1;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'else' is redundant in this case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! Want to make a PR to fix this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! i can do this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! #1936

return 0;
}
}
} // namespace math

Expand All @@ -60,54 +57,50 @@ int power_of_two(int n) {
* @returns void
*/
static void test() {

std::cout << "First case testing.. \n"; // for n = 32 return 1
std::cout << "First case testing... \n"; // for n = 32 return 1
assert(math::power_of_two(32) == 1);
std::cout << "Passed\n";
std::cout << "\nPassed!\n";

std::cout << "Second case testing.. \n"; // for n = 5 return 0
std::cout << "Second case testing... \n"; // for n = 5 return 0
assert(math::power_of_two(5) == 0);
std::cout << "Passed\n";
std::cout << "\nPassed!\n";

std::cout << "Third case testing.. \n"; // for n = 232 return 0
std::cout << "Third case testing... \n"; // for n = 232 return 0
assert(math::power_of_two(232) == 0);
std::cout << "Passed\n";
std::cout << "\nPassed!\n";

std::cout << "All test cases passed! \n";
std::cout << "\nAll test cases have successfully passed!\n";
}

/**
* @brief take user input
* @brief Take user input in the test cases (optional; currently commented)
* @returns void
*/

void user_input_test() {

int n = 0; // input from user

std::cout << "Enter a number " << std::endl;
std::cin >> n;

/// function call with @param n
int result = math::power_of_two(n);
if(result == 1) std::cout << "Yes, the number " << n << " is a power of 2 \n";
else std::cout << "No, the number " << n << " is not a power of 2\n";

if (result == 1) {
std::cout << "Yes, the number " << n << " is a power of 2\n";
}
else {
std::cout << "No, the number " << n << " is not a power of 2\n";
}
}

Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Main function
* @returns 0 on exit
*/

int main() {

test(); //run self-test implementations
test(); // run self-test implementations

//UN - COMMENT BELOW LINE TO TAKE USER INPUTS
// user_input_test();
// uncomment the line below to take user inputs
//user_input_test();


return 0;
}