2015-05-07 19:19:56 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
2016-09-02 01:07:42 -04:00
|
|
|
#include "py/objstr.h"
|
2015-05-07 19:19:56 -04:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/repl.h"
|
2015-10-01 13:49:37 -04:00
|
|
|
#include "py/mpz.h"
|
2016-12-29 02:07:38 -05:00
|
|
|
#include "py/builtin.h"
|
2017-01-03 22:23:29 -05:00
|
|
|
#include "py/emit.h"
|
2017-01-04 19:12:05 -05:00
|
|
|
#include "py/formatfloat.h"
|
2015-05-07 19:19:56 -04:00
|
|
|
|
|
|
|
#if defined(MICROPY_UNIX_COVERAGE)
|
|
|
|
|
2016-09-02 01:07:42 -04:00
|
|
|
// str/bytes objects without a valid hash
|
|
|
|
STATIC const mp_obj_str_t str_no_hash_obj = {{&mp_type_str}, 0, 10, (const byte*)"0123456789"};
|
|
|
|
STATIC const mp_obj_str_t bytes_no_hash_obj = {{&mp_type_bytes}, 0, 10, (const byte*)"0123456789"};
|
|
|
|
|
2015-05-07 19:19:56 -04:00
|
|
|
// function to run extra tests for things that can't be checked by scripts
|
2015-05-08 04:18:38 -04:00
|
|
|
STATIC mp_obj_t extra_coverage(void) {
|
2015-05-28 10:25:07 -04:00
|
|
|
// mp_printf (used by ports that don't have a native printf)
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# mp_printf\n");
|
2015-05-28 10:25:07 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d %+d % d\n", -123, 123, 123); // sign
|
|
|
|
mp_printf(&mp_plat_print, "%05d\n", -123); // negative number with zero padding
|
|
|
|
mp_printf(&mp_plat_print, "%ld\n", 123); // long
|
|
|
|
mp_printf(&mp_plat_print, "%X\n", 0x1abcdef); // capital hex
|
|
|
|
mp_printf(&mp_plat_print, "%.2s %.3s\n", "abc", "abc"); // fixed string precision
|
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", -1, "abc"); // negative string precision
|
|
|
|
mp_printf(&mp_plat_print, "%b %b\n", 0, 1); // bools
|
|
|
|
mp_printf(&mp_plat_print, "%s\n", NULL); // null string
|
2016-02-01 10:08:42 -05:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", 0x80000000); // should print signed
|
|
|
|
mp_printf(&mp_plat_print, "%u\n", 0x80000000); // should print unsigned
|
|
|
|
mp_printf(&mp_plat_print, "%x\n", 0x80000000); // should print unsigned
|
|
|
|
mp_printf(&mp_plat_print, "%X\n", 0x80000000); // should print unsigned
|
2015-05-28 10:25:07 -04:00
|
|
|
}
|
|
|
|
|
2015-05-12 18:34:10 -04:00
|
|
|
// vstr
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# vstr\n");
|
2016-10-14 01:46:34 -04:00
|
|
|
vstr_t *vstr = vstr_new(16);
|
2015-05-12 18:34:10 -04:00
|
|
|
vstr_hint_size(vstr, 32);
|
|
|
|
vstr_add_str(vstr, "ts");
|
|
|
|
vstr_ins_byte(vstr, 1, 'e');
|
|
|
|
vstr_ins_char(vstr, 3, 't');
|
|
|
|
vstr_ins_char(vstr, 10, 's');
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_cut_head_bytes(vstr, 2);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_cut_tail_bytes(vstr, 10);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_printf(vstr, "t%cst", 'e');
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
vstr_cut_out_bytes(vstr, 3, 10);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
VSTR_FIXED(fix, 4);
|
|
|
|
vstr_add_str(&fix, "large");
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)fix.len, fix.buf);
|
2015-05-12 18:34:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-07 19:19:56 -04:00
|
|
|
// repl autocomplete
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# repl\n");
|
2015-05-12 18:34:10 -04:00
|
|
|
|
2015-05-07 19:19:56 -04:00
|
|
|
const char *str;
|
2015-05-08 04:18:38 -04:00
|
|
|
mp_uint_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
2015-05-07 19:19:56 -04:00
|
|
|
|
|
|
|
mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)));
|
|
|
|
mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str);
|
|
|
|
len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
|
2015-05-07 19:19:56 -04:00
|
|
|
}
|
2015-05-08 04:18:38 -04:00
|
|
|
|
2015-05-12 18:34:10 -04:00
|
|
|
// attrtuple
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# attrtuple\n");
|
2015-05-12 18:34:10 -04:00
|
|
|
|
|
|
|
static const qstr fields[] = {MP_QSTR_start, MP_QSTR_stop, MP_QSTR_step};
|
|
|
|
static const mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(2), MP_OBJ_NEW_SMALL_INT(3)};
|
|
|
|
mp_obj_print_helper(&mp_plat_print, mp_obj_new_attrtuple(fields, 3, items), PRINT_REPR);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "\n");
|
2015-05-12 18:34:10 -04:00
|
|
|
}
|
|
|
|
|
2015-09-03 18:06:18 -04:00
|
|
|
// str
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# str\n");
|
2015-09-03 18:06:18 -04:00
|
|
|
|
|
|
|
// intern string
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", MP_OBJ_IS_QSTR(mp_obj_str_intern(mp_obj_new_str("intern me", 9, false))));
|
2015-09-03 18:06:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-01 13:49:37 -04:00
|
|
|
// mpz
|
|
|
|
{
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "# mpz\n");
|
2015-10-01 13:49:37 -04:00
|
|
|
|
|
|
|
mp_uint_t value;
|
|
|
|
mpz_t mpz;
|
|
|
|
mpz_init_zero(&mpz);
|
|
|
|
|
|
|
|
// mpz_as_uint_checked, with success
|
|
|
|
mpz_set_from_int(&mpz, 12345678);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
|
|
|
|
mp_printf(&mp_plat_print, "%d\n", (int)value);
|
2015-10-01 13:49:37 -04:00
|
|
|
|
|
|
|
// mpz_as_uint_checked, with negative arg
|
|
|
|
mpz_set_from_int(&mpz, -1);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
|
2015-10-01 13:49:37 -04:00
|
|
|
|
|
|
|
// mpz_as_uint_checked, with overflowing arg
|
|
|
|
mpz_set_from_int(&mpz, 1);
|
|
|
|
mpz_shl_inpl(&mpz, &mpz, 70);
|
2015-10-14 19:23:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
|
2015-10-01 13:49:37 -04:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:07:38 -05:00
|
|
|
// runtime utils
|
|
|
|
{
|
|
|
|
mp_printf(&mp_plat_print, "# runtime utils\n");
|
|
|
|
|
|
|
|
// call mp_call_function_1_protected
|
|
|
|
mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), MP_OBJ_NEW_SMALL_INT(1));
|
|
|
|
// call mp_call_function_1_protected with invalid args
|
|
|
|
mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), mp_obj_new_str("abc", 3, false));
|
|
|
|
|
|
|
|
// call mp_call_function_2_protected
|
|
|
|
mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(1));
|
|
|
|
// call mp_call_function_2_protected with invalid args
|
|
|
|
mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), mp_obj_new_str("abc", 3, false), mp_obj_new_str("abc", 3, false));
|
|
|
|
}
|
|
|
|
|
2017-01-03 22:23:29 -05:00
|
|
|
// warning
|
|
|
|
{
|
|
|
|
mp_emitter_warning(MP_PASS_CODE_SIZE, "test");
|
|
|
|
}
|
|
|
|
|
2017-01-04 19:12:05 -05:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2016-09-02 01:07:42 -04:00
|
|
|
// 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);
|
2015-05-07 19:19:56 -04:00
|
|
|
}
|
2015-05-08 04:18:38 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage);
|
2015-05-07 19:19:56 -04:00
|
|
|
|
|
|
|
#endif
|