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

* Added self implement cases power_of_two.cpp

added assert & a clean code.

* added self implementation cases

* added desciption

* update

Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com>
This commit is contained in:
Rijul.S 2022-02-10 19:30:00 +05:30 committed by GitHub
parent d03a7068d0
commit 27ced495bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,9 +19,12 @@
* approach. * approach.
* *
* @author [Neha Hasija](https://github.com/neha-hasija17) * @author [Neha Hasija](https://github.com/neha-hasija17)
* @author [Rijul.S](https://github.com/Rijul24)
*/ */
#include <iostream> /// for std::cout #include <iostream> /// for IO operations
#include <cassert> /// for assert
/** /**
* @namespace math * @namespace math
@ -29,38 +32,75 @@
*/ */
namespace math { namespace math {
/** /**
* @brief Function to test above algorithm * @brief This function finds whether a number is power of 2 or not
* @param n description * @param n value for which we want to check
* @returns void * 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
*/ */
void power_of_two(int n) { 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
* 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
*/
/// result stores the /// result stores the
/// bitwise and of n and n-1 /// bitwise and of n and n-1
int result = n & (n - 1); int result = n & (n - 1);
if (result == 0) { if (result == 0) {
std::cout << "Yes, the number " << n << " is a power of 2"; return 1;
} else { } else {
std::cout << "No, the number " << n << " is not a power of 2"; return 0;
} }
} }
} // namespace math } // namespace math
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
std::cout << "First case testing... \n"; // for n = 32 return 1
assert(math::power_of_two(32) == 1);
std::cout << "\nPassed!\n";
std::cout << "Second case testing... \n"; // for n = 5 return 0
assert(math::power_of_two(5) == 0);
std::cout << "\nPassed!\n";
std::cout << "Third case testing... \n"; // for n = 232 return 0
assert(math::power_of_two(232) == 0);
std::cout << "\nPassed!\n";
std::cout << "\nAll test cases have successfully passed!\n";
}
/**
* @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";
}
}
/** /**
* @brief Main function * @brief Main function
* @returns 0 on exit * @returns 0 on exit
*/ */
int main() { int main() {
int n = 0; test(); // run self-test implementations
/// n stores the input from the user
std::cout << "enter a number " << std::endl; // uncomment the line below to take user inputs
std::cin >> n; //user_input_test();
/// function call with @param n
math::power_of_two(n);
return 0; return 0;
} }