Skip to content

Commit

Permalink
Add support for float and reorganize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 29, 2020
1 parent b0bafc5 commit a145bda
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
44 changes: 25 additions & 19 deletions dev/VisualStudio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,37 +124,43 @@ main(void) {
printf_run(NULL, "%#2X", 0);
printf_run(NULL, "%#2x", 0);
printf_run(NULL, "%#2o", 0);
printf_run(NULL, "%#2B", 1);
printf_run(NULL, "%#2b", 1);
printf_run(NULL, "%#2B", 0);
printf_run(NULL, "%#2b", 0);
printf_run(NULL, "%#B", 0);
printf_run(NULL, "%#b", 0);
printf_run(NULL, "%#B", 6);
printf_run(NULL, "%#b", 6);

/* Pointers */
printf_run(NULL, "%p", &tests_passed);
printf_run(NULL, "0X%p", &tests_passed);
printf_run(NULL, "0x%p", &tests_passed);


/* Print final output */
printf("\r\n\r\n------------------------\r\n\r\n");
printf("Number of tests passed: %d\r\n", (int)tests_passed);
printf("Number of tests failed: %d\r\n", (int)tests_failed);
printf("Coverage: %f %%\r\n", (float)((tests_passed * 100) / ((float)(tests_passed + tests_failed))));
printf("\r\n\r\n------------------------\r\n\r\n");

/* Those are additional, not supported in classic printf implementation */

/* Binary */
printf_run(NULL, "%llb abc", 123);
printf_run(NULL, "%llb abc", 123);
printf_run(NULL, "%b", 4);
printf_run(NULL, "%#2B", 1);
printf_run(NULL, "%#2b", 1);
printf_run(NULL, "%#2B", 0);
printf_run(NULL, "%#2b", 0);
printf_run(NULL, "%#B", 0);
printf_run(NULL, "%#b", 0);
printf_run(NULL, "%#B", 6);
printf_run(NULL, "%#b", 6);

/* Array test */
uint8_t my_arr[] = { 0x00, 0x01, 0x02, 0x03, 0x04 };
printf_run(NULL, "%5K", my_arr); /* Print fixed length */
printf_run(NULL, "%*K", 3, my_arr); /* Print only first 3 elements of array */
printf_run(NULL, "% *K", 3, my_arr); /* Print only first 3 elements of array with spaces between bytes */

/* Print final output */
printf("----\r\n\r\n");
printf("Number of tests passed: %d\r\n", (int)tests_passed);
printf("Number of tests failed: %d\r\n", (int)tests_failed);
printf("Coverage: %f %%\r\n", (float)((tests_passed) / ((float)(tests_passed + tests_failed)) * 100));
printf("----\r\n\r\n");
uint8_t my_arr[] = { 0x00, 0x01, 0xB5, 0xC6, 0xD7 };
printf_run("0102B5C6D7", "%5K", my_arr);
printf_run("0102B5", "%*K", 3, my_arr);
printf_run("01 02 B5", "% *K", 3, my_arr);
printf_run("0102b5c6d7", "%5k", my_arr);
printf_run("0102b5", "%*k", 3, my_arr);
printf_run("01 02 b5", "% *k", 3, my_arr);

return 0;
}
3 changes: 1 addition & 2 deletions docs/user-manual/format-specifier.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ Column *Supported* gives an overview which specifiers are actually supported by
+-------------+-----------+--------------------------------------------------------------------------+
| ``u`` | Yes | Prints ``unsigned int`` |
+-------------+-----------+--------------------------------------------------------------------------+
| ``f`` ``F`` | Not yet | Prints ``double`` in normal fixed-point notation. |
| ``f`` ``F`` | Yes | Prints ``double`` in normal fixed-point notation. |
| | | ``f`` and ``F`` only differs in how the strings for an infinite number |
| | | or NaN are printed |
| | | (``inf``, ``infinity`` and ``nan`` for ``f``; |
| | | ``INF``, ``INFINITY`` and ``NAN`` for ``F``). |
| | | Currently it will print ``NaN`` when used |
+-------------+-----------+--------------------------------------------------------------------------+
| ``e`` ``E`` | Not yet | Prints ``double`` in standard form ``[-]d.ddd e[+-]ddd``. |
| | | ``e`` uses lower-case and |
Expand Down
8 changes: 8 additions & 0 deletions lwprintf/src/include/lwprintf/lwprintf_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ extern "C" {
#define LWPRINTF_CFG_SUPPORT_TYPE_STRING 1
#endif

/**
* \brief Default `float/double` precision.
* This is a number of digits displayed after decimal point.
*/
#ifndef LWPRINTF_CFG_FLOAT_PRECISION_DEFAULT
#define LWPRINTF_CFG_FLOAT_PRECISION_DEFAULT 6
#endif

/**
* \}
*/
Expand Down
10 changes: 7 additions & 3 deletions lwprintf/src/lwprintf/lwprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,13 @@ prv_double_to_str(lwprintf_int_t* p, double num) {
p->m.flags.is_negative = 1;
num = -num;
}

/* Check precision data */
if (p->m.precision > 9) {
p->m.precision = 9;
p->m.precision = 9; /* Limit to maximum precision */
} else if (!p->m.flags.precision) {
p->m.flags.precision = 1;
p->m.precision = 6;
p->m.precision = LWPRINTF_CFG_FLOAT_PRECISION_DEFAULT; /* Default prevision when not used */
}

/* Get integer and decimal parts, both in integer format */
Expand Down Expand Up @@ -835,6 +837,7 @@ prv_format(lwprintf_int_t* p, va_list arg) {
case 'K': {
unsigned char* ptr = (void *)va_arg(arg, unsigned char *); /* Get input parameter as unsigned char pointer */
int len = p->m.width;
uint8_t is_space = p->m.flags.space == 1;

if (ptr == NULL || len == 0) {
break;
Expand All @@ -844,11 +847,12 @@ prv_format(lwprintf_int_t* p, va_list arg) {
p->m.flags.zero = 1; /* Prepend with zeros if necessary */
p->m.width = 2; /* Each number is 2 chars min/max */
p->m.base = 16; /* Hex format */
p->m.flags.space = 0; /* Delete any flag for space */

/* Output byte by byte w/o hex prefix */
for (int i = 0; i < len; ++i) {
prv_unsigned_int_to_str(p, (unsigned int)*ptr++);
if (p->m.flags.space && i < (len - 1)) {
if (is_space && i < (len - 1)) {
p->out_fn(p, ' '); /* Generate space between numbers */
}
}
Expand Down

0 comments on commit a145bda

Please sign in to comment.