Skip to content

Commit d7e1684

Browse files
flowergrassdpgeorge
authored andcommitted
tests/unix: Improve formatfloat.c test coverage using C.
1 parent 64dc925 commit d7e1684

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

tests/unix/extra_coverage.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ ementation
3939
TypeError: can't convert str to int
4040
TypeError: unsupported types for : 'str', 'str'
4141
Warning: test
42+
# format float
43+
?
44+
+1e+00
45+
+1e+00
4246
('0123456789', b'0123456789')
4347
7300
4448
7300

unix/coverage.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "py/mpz.h"
88
#include "py/builtin.h"
99
#include "py/emit.h"
10+
#include "py/formatfloat.h"
1011

1112
#if defined(MICROPY_UNIX_COVERAGE)
1213

@@ -136,6 +137,26 @@ STATIC mp_obj_t extra_coverage(void) {
136137
mp_emitter_warning(MP_PASS_CODE_SIZE, "test");
137138
}
138139

140+
// format float
141+
{
142+
mp_printf(&mp_plat_print, "# format float\n");
143+
144+
// format with inadequate buffer size
145+
char buf[5];
146+
mp_format_float(1, buf, sizeof(buf), 'g', 0, '+');
147+
mp_printf(&mp_plat_print, "%s\n", buf);
148+
149+
// format with just enough buffer so that precision must be
150+
// set from 0 to 1 twice
151+
char buf2[8];
152+
mp_format_float(1, buf2, sizeof(buf2), 'g', 0, '+');
153+
mp_printf(&mp_plat_print, "%s\n", buf2);
154+
155+
// format where precision is trimmed to avoid buffer overflow
156+
mp_format_float(1, buf2, sizeof(buf2), 'e', 0, '+');
157+
mp_printf(&mp_plat_print, "%s\n", buf2);
158+
}
159+
139160
// return a tuple of data for testing on the Python side
140161
mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj};
141162
return mp_obj_new_tuple(MP_ARRAY_SIZE(items), items);

0 commit comments

Comments
 (0)