Skip to content

Commit

Permalink
Merge pull request #49 from baskapteijn/staging/baskapteijn/coverage
Browse files Browse the repository at this point in the history
Staging/baskapteijn/coverage
  • Loading branch information
baskapteijn committed Dec 28, 2018
2 parents d8fd9e8 + bf09e7f commit 0b25f5a
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Any invalid input will cause the help menu to be printed.
```bash
$ ./numconvert
Version 1.0.3
Version 1.0.4
Usage:
numconvert [prefix]<value>[postfix]
Expand Down
3 changes: 3 additions & 0 deletions coverage/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rc=$?; if [[ $rc != 0 ]]; then exit_on_error $rc; fi
./numconvert_cov 1111111111111111111111111111111111111111111111111111111111111111b
./numconvert_cov 111111111111111111111111111111111111111111111111111111111111111b
./numconvert_cov 18446744073709551615
./numconvert_cov 184467440737095516154
./numconvert_cov 0xffffffffffffffff
./numconvert_cov 0xfffffffffffffffff
./numconvert_cov 0x0
Expand All @@ -32,6 +33,8 @@ rc=$?; if [[ $rc != 0 ]]; then exit_on_error $rc; fi
./numconvert_cov 112b
./numconvert_cov Aah
./numconvert_cov 18446744073709551616
./numconvert_cov 17446744073709551616
./numconvert_cov 1744674407370955161
./numconvert_cov 0xg
./numconvert_cov
./numconvert_cov ""
Expand Down
95 changes: 38 additions & 57 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ static bool IsHexadecimalChar(char c);
static void PrintBinary(uint64_t number);
static void PrintDecimal(uint64_t number);
static void PrintHexadecimal(uint64_t number);
static int ParseBinary(const char *string, size_t len, uint64_t *number);
static int ParseDecimal(const char *string, size_t len, uint64_t *number);
static int ParseHexadecimal(const char *string, size_t len, uint64_t *number);
static void ParseBinary(const char *string, size_t len, uint64_t *number);
static void ParseDecimal(const char *string, size_t len, uint64_t *number);
static void ParseHexadecimal(const char *string, size_t len, uint64_t *number);
static bool IsBinary(const char *string, size_t len);
static bool IsDecimal(const char *string, size_t len);
static bool IsHexadecimal(const char *string, size_t len);
static void PrintNumerals(uint64_t number);
static int ParseString(const char *string, size_t len, uint64_t *number, char base);
static void ParseString(const char *string, size_t len, uint64_t *number, char base);
static int CheckString(const char *string, size_t len, char *base);
static void PrintHelp(void);

Expand Down Expand Up @@ -180,10 +180,8 @@ static void PrintHexadecimal(uint64_t number)
* The length of the input string (excluding the string terminator).
* \param[out] number
* The number represented by the numeric string.
* \returns
* Always 0 (successful).
*/
static int ParseBinary(const char *string, size_t len, uint64_t *number)
static void ParseBinary(const char *string, size_t len, uint64_t *number)
{
int64_t i = 0;

Expand All @@ -194,8 +192,6 @@ static int ParseBinary(const char *string, size_t len, uint64_t *number)
*number += (uint64_t)1 << (len - 2 - i);
}
}

return 0;
}

/*!
Expand All @@ -206,10 +202,8 @@ static int ParseBinary(const char *string, size_t len, uint64_t *number)
* The length of the input string (excluding the string terminator).
* \param[out] number
* The number represented by the numeric string.
* \returns
* Always 0 (successful).
*/
static int ParseDecimal(const char *string, size_t len, uint64_t *number)
static void ParseDecimal(const char *string, size_t len, uint64_t *number)
{
uint64_t val = 0;
int64_t i = 0;
Expand All @@ -223,8 +217,6 @@ static int ParseDecimal(const char *string, size_t len, uint64_t *number)
*number += value;
val *= 10;
}

return 0;
}

/*!
Expand All @@ -235,10 +227,8 @@ static int ParseDecimal(const char *string, size_t len, uint64_t *number)
* The length of the input string (excluding the string terminator).
* \param[out] number
* The number represented by the numeric string.
* \returns
* Always 0 (successful).
*/
static int ParseHexadecimal(const char *string, size_t len, uint64_t *number)
static void ParseHexadecimal(const char *string, size_t len, uint64_t *number)
{
int64_t i = 0;
uint64_t value = 0;
Expand Down Expand Up @@ -272,8 +262,6 @@ static int ParseHexadecimal(const char *string, size_t len, uint64_t *number)
}
*number |= value << ((start - i) * 4);
}

return 0;
}

/*!
Expand Down Expand Up @@ -301,8 +289,10 @@ static bool IsBinary(const char *string, size_t len)
}

if (isBinary == true) {
if ((len < 2) ||
(len > BINARY_STRING_LEN_MAX)) {/* 64 characters + b */
/* No need to check for maximum length:
* - (len >= BINARY_STRING_LEN_MAX + 1), or BUFFER_SIZE, is covered by main()
*/
if (len < 2) {
/* This isn't gonna fit in UINT64_MAX. */
isBinary = false;
}
Expand Down Expand Up @@ -333,9 +323,10 @@ static bool IsDecimal(const char *string, size_t len)
}

if (isDecimal == true) {
if (len == 0) {
isDecimal = false;
} else if (len == DECIMAL_STRING_LEN_MAX) {
/* No need to check for minimum length:
* - (len == 0) is covered by CheckString()
*/
if (len == DECIMAL_STRING_LEN_MAX) {
for (i = 0; i < DECIMAL_STRING_LEN_MAX; i++) {
if ((string[i] - 0x30) > (DecimalStringValueMax[i] - 0x30)) {
/* This isn't gonna fit in UINT64_MAX. */
Expand All @@ -352,6 +343,7 @@ static bool IsDecimal(const char *string, size_t len)
} else if (len > DECIMAL_STRING_LEN_MAX) {
isDecimal = false;
}
/* else: will not happen because it's protected by CheckString(). */
}

return isDecimal;
Expand All @@ -368,7 +360,7 @@ static bool IsDecimal(const char *string, size_t len)
*/
static bool IsHexadecimal(const char *string, size_t len)
{
bool isHexaDecimal = false;
bool isHexaDecimal = true;
uint64_t i = 0;
uint64_t start = 0;
uint64_t end = 0;
Expand All @@ -385,19 +377,22 @@ static bool IsHexadecimal(const char *string, size_t len)
end = len;
minLen = 3;
maxLen = HEXADECIMAL_0X_STRING_LEN_MAX;
} else {
isHexaDecimal = false;
}

for (i = start; i < end; i++) {
isHexaDecimal = true;
if (IsHexadecimalChar(string[i]) == false) {
isHexaDecimal = false;
break;
if (isHexaDecimal == true) {
for (i = start; i < end; i++) {
if (IsHexadecimalChar(string[i]) == false) {
isHexaDecimal = false;
break;
}
}
}

if (isHexaDecimal == true) {
if ((len < minLen) ||
(len > maxLen)) {/* 0x + 16 characters */
(len > maxLen)) {
/* This isn't gonna fit in UINT64_MAX. */
isHexaDecimal = false;
}
Expand Down Expand Up @@ -437,29 +432,19 @@ static void PrintNumerals(uint64_t number)
* The number represented by the numeric string in case it checked-out.
* \param base
* The base of the numeric string.
* \returns
* 0 in case of successful completion or any other value in case of an error.
*/
static int ParseString(const char *string, size_t len, uint64_t *number, const char base)
static void ParseString(const char *string, size_t len, uint64_t *number, const char base)
{
int retval = 0;

switch (base) {
case 'b':
retval = ParseBinary(string, len, number);
break;
case 'd':
retval = ParseDecimal(string, len, number);
break;
case 'h':
retval = ParseHexadecimal(string, len, number);
break;
default:
retval = -1;
break;
if (base == 'd') {
ParseDecimal(string, len, number);
} else if (base == 'h') {
ParseHexadecimal(string, len, number);
} else {
/* base == 'b' is the only one left.
* Nothing else will happen because it's protected by CheckString().
*/
ParseBinary(string, len, number);
}

return retval;
}

/*!
Expand Down Expand Up @@ -556,7 +541,7 @@ int main(int argc, char *argv[])

/* Determine string length. */
len = strlen(argv[1]);
if (len >= BUFFER_SIZE) {
if (len >= sizeof(string)) {
PrintHelp();
return -1;
}
Expand All @@ -574,11 +559,7 @@ int main(int argc, char *argv[])
return retval;
}

retval = ParseString(string, len, &number, base);
if (retval != 0) {
PrintHelp();
return retval;
}
ParseString(string, len, &number, base);

PrintNumerals(number);

Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#define VERSION_MAJOR 1u
#define VERSION_MINOR 0u
#define VERSION_PATCH 3u
#define VERSION_PATCH 4u

#define VERSION_STRING_LEN 12 /* mjr.mnr.pat + \0 */

Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/12.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/13.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/26.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/27.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/28.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/29.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/3.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/30.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/31.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/32.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/33.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/34.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/35.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/36.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/37.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/4.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/stdout/7.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version 1.0.3
Version 1.0.4

Usage:
numconvert [prefix]<value>[postfix]
Expand Down

0 comments on commit 0b25f5a

Please sign in to comment.