summaryrefslogtreecommitdiffstatshomepage
path: root/unix
diff options
context:
space:
mode:
authorRami Ali <flowergrass@users.noreply.github.com>2017-01-05 11:12:05 +1100
committerDamien George <damien.p.george@gmail.com>2017-01-05 12:31:05 +1100
commitd7e168428bc9fd7524dd83b973c283b3488bfd3b (patch)
treec5f338c1d4a3b831f63c84372b704f99f3b30fbc /unix
parent64dc925c4afad63bc5c8a6017d969abbab1d2ac5 (diff)
downloadmicropython-d7e168428bc9fd7524dd83b973c283b3488bfd3b.tar.gz
micropython-d7e168428bc9fd7524dd83b973c283b3488bfd3b.zip
tests/unix: Improve formatfloat.c test coverage using C.
Diffstat (limited to 'unix')
-rw-r--r--unix/coverage.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/unix/coverage.c b/unix/coverage.c
index 033f09ed30..7140be41b9 100644
--- a/unix/coverage.c
+++ b/unix/coverage.c
@@ -7,6 +7,7 @@
#include "py/mpz.h"
#include "py/builtin.h"
#include "py/emit.h"
+#include "py/formatfloat.h"
#if defined(MICROPY_UNIX_COVERAGE)
@@ -136,6 +137,26 @@ STATIC mp_obj_t extra_coverage(void) {
mp_emitter_warning(MP_PASS_CODE_SIZE, "test");
}
+ // format float
+ {
+ mp_printf(&mp_plat_print, "# format float\n");
+
+ // format with inadequate buffer size
+ char buf[5];
+ mp_format_float(1, buf, sizeof(buf), 'g', 0, '+');
+ mp_printf(&mp_plat_print, "%s\n", buf);
+
+ // format with just enough buffer so that precision must be
+ // set from 0 to 1 twice
+ char buf2[8];
+ mp_format_float(1, buf2, sizeof(buf2), 'g', 0, '+');
+ mp_printf(&mp_plat_print, "%s\n", buf2);
+
+ // format where precision is trimmed to avoid buffer overflow
+ mp_format_float(1, buf2, sizeof(buf2), 'e', 0, '+');
+ mp_printf(&mp_plat_print, "%s\n", buf2);
+ }
+
// return a tuple of data for testing on the Python side
mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj};
return mp_obj_new_tuple(MP_ARRAY_SIZE(items), items);