From 12f0ec3c45fc2789be69a8a579b0eee92e356412 Mon Sep 17 00:00:00 2001 From: Neeraj C Date: Tue, 16 Jun 2020 08:03:14 +0530 Subject: [PATCH] test: add two test functions --- math/sum_of_digits.cpp | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/math/sum_of_digits.cpp b/math/sum_of_digits.cpp index cf8c28360..460bff691 100644 --- a/math/sum_of_digits.cpp +++ b/math/sum_of_digits.cpp @@ -11,8 +11,17 @@ * Function to find the sum of the digits of an integer. * @param num The integer. * @return Sum of the digits of the integer. + * + * \detail + * First the algorithm check whether the num is negative or positive, + * if it is negative, then we neglect the negative sign. + * Next, the algorithm extract the last digit of num by dividing by 10 + * and extracting the remainder and this is added to the sum. + * The number is then divided by 10 to remove the last digit. + * This loop continues until num becomes 0. */ int sum_of_digits(int num) { + // If num is negative then negative sign is neglected. if (num < 0) { num = -1 * num; } @@ -25,17 +34,38 @@ int sum_of_digits(int num) { } /** - * Test function. + * Function for testing the sum_of_digits() function with a + * first test case of 119765 and assert statement. */ -void test() { +void test1() { int test_case_1 = sum_of_digits(119765); - int test_case_2 = sum_of_digits(-12256); assert(test_case_1 == 29); - assert(test_case_2 == 16); } +/** + * Function for testing the sum_of_digits() function with a + * second test case of -12256 and assert statement. + */ +void test2() { + int test_case_2 = sum_of_digits(-12256); + assert(test_case_2 == 16); + } +/** + * Function for testing the sum_of_digits() with + * all the test cases. + */ +void test() { + //First test. + test1(); + //Second test. + test2(); + } + +/** + * Main Function +*/ int main() { test(); - std::cout << "Success."; + std::cout << "Success." << std::endl; return 0; }