added test cases and updated the documentation Fixes: #633

This commit is contained in:
Rachit Bhalla 2020-10-18 23:48:36 +05:30
parent 6b1c8e65be
commit 5e11041d41

View File

@ -1,8 +1,21 @@
/**
* @brief Octal to hexadecimal conversion by scanning user input
* @details
* The octalToHexadecimal function take the octal number as long
* return a string hexadecimal value after conversion
* @author [Rachit Bhalla](https://github.com/rachitbhalla)
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Converts octal number to decimal number
/**
* @brief Convert octal number to decimal number
* @param octalValue is the octal number that needs to be converted
* @returns A decimal number after conversion
*/
long octalToDecimal(long octalValue){
long decimalValue = 0;
int i = 0;
@ -14,15 +27,40 @@ long octalToDecimal(long octalValue){
}
return decimalValue;
}
/**
* @brief Convert octal number to hexadecimal number
* @param octalValue is the octal number that needs to be converted
* @returns A hexadecimal value as a string after conversion
*/
char *octalToHexadecimal(long octalValue){
char *hexadecimalValue = malloc(256 * sizeof(char));
sprintf(hexadecimalValue, "%lX", octalToDecimal(octalValue));
return hexadecimalValue;
}
/**
* @brief Test function
* @returns void
*/
static void test() {
/* test that hexadecimal value of octal number 213 is 8B */
assert(strcmp(octalToHexadecimal(213), "8B") == 0);
/* test that hexadecimal value of octal number 174 is 7C */
assert(strcmp(octalToHexadecimal(174), "7C") == 0);
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main()
{
int octalValue;
//execute the tests
test();
int octalValue;
printf("Enter an octal number: ");
scanf("%d", &octalValue);