blob: c84a653f7566615cd2c416d12a0c089583a0853e [file] [log] [blame]
Damien Georged3b32ca2015-05-08 00:19:56 +01001#include <stdio.h>
2
3#include "py/obj.h"
Damien George58f38612016-09-02 15:07:42 +10004#include "py/objstr.h"
Damien Georged3b32ca2015-05-08 00:19:56 +01005#include "py/runtime.h"
6#include "py/repl.h"
Damien Georgea81539d2015-10-01 18:49:37 +01007#include "py/mpz.h"
Damien Georged3b32ca2015-05-08 00:19:56 +01008
9#if defined(MICROPY_UNIX_COVERAGE)
10
Damien George58f38612016-09-02 15:07:42 +100011// str/bytes objects without a valid hash
12STATIC const mp_obj_str_t str_no_hash_obj = {{&mp_type_str}, 0, 10, (const byte*)"0123456789"};
13STATIC const mp_obj_str_t bytes_no_hash_obj = {{&mp_type_bytes}, 0, 10, (const byte*)"0123456789"};
14
Damien Georged3b32ca2015-05-08 00:19:56 +010015// function to run extra tests for things that can't be checked by scripts
Damien Georged792d9e2015-05-08 09:18:38 +010016STATIC mp_obj_t extra_coverage(void) {
Damien Georgea16715a2015-05-28 14:25:07 +000017 // mp_printf (used by ports that don't have a native printf)
18 {
Damien George556c8a92015-10-15 00:23:03 +010019 mp_printf(&mp_plat_print, "# mp_printf\n");
Damien Georgea16715a2015-05-28 14:25:07 +000020 mp_printf(&mp_plat_print, "%d %+d % d\n", -123, 123, 123); // sign
21 mp_printf(&mp_plat_print, "%05d\n", -123); // negative number with zero padding
22 mp_printf(&mp_plat_print, "%ld\n", 123); // long
23 mp_printf(&mp_plat_print, "%X\n", 0x1abcdef); // capital hex
24 mp_printf(&mp_plat_print, "%.2s %.3s\n", "abc", "abc"); // fixed string precision
25 mp_printf(&mp_plat_print, "%.*s\n", -1, "abc"); // negative string precision
26 mp_printf(&mp_plat_print, "%b %b\n", 0, 1); // bools
27 mp_printf(&mp_plat_print, "%s\n", NULL); // null string
Damien George9e677112016-02-01 15:08:42 +000028 mp_printf(&mp_plat_print, "%d\n", 0x80000000); // should print signed
29 mp_printf(&mp_plat_print, "%u\n", 0x80000000); // should print unsigned
30 mp_printf(&mp_plat_print, "%x\n", 0x80000000); // should print unsigned
31 mp_printf(&mp_plat_print, "%X\n", 0x80000000); // should print unsigned
Damien Georgea16715a2015-05-28 14:25:07 +000032 }
33
Damien Georgef6013902015-05-12 23:34:10 +010034 // vstr
35 {
Damien George556c8a92015-10-15 00:23:03 +010036 mp_printf(&mp_plat_print, "# vstr\n");
Damien Georgef6013902015-05-12 23:34:10 +010037 vstr_t *vstr = vstr_new_size(16);
38 vstr_hint_size(vstr, 32);
39 vstr_add_str(vstr, "ts");
40 vstr_ins_byte(vstr, 1, 'e');
41 vstr_ins_char(vstr, 3, 't');
42 vstr_ins_char(vstr, 10, 's');
Damien George556c8a92015-10-15 00:23:03 +010043 mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
Damien Georgef6013902015-05-12 23:34:10 +010044
45 vstr_cut_head_bytes(vstr, 2);
Damien George556c8a92015-10-15 00:23:03 +010046 mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
Damien Georgef6013902015-05-12 23:34:10 +010047
48 vstr_cut_tail_bytes(vstr, 10);
Damien George556c8a92015-10-15 00:23:03 +010049 mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
Damien Georgef6013902015-05-12 23:34:10 +010050
51 vstr_printf(vstr, "t%cst", 'e');
Damien George556c8a92015-10-15 00:23:03 +010052 mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
Damien Georgef6013902015-05-12 23:34:10 +010053
54 vstr_cut_out_bytes(vstr, 3, 10);
Damien George556c8a92015-10-15 00:23:03 +010055 mp_printf(&mp_plat_print, "%.*s\n", (int)vstr->len, vstr->buf);
Damien Georgef6013902015-05-12 23:34:10 +010056
57 VSTR_FIXED(fix, 4);
58 vstr_add_str(&fix, "large");
Damien George556c8a92015-10-15 00:23:03 +010059 mp_printf(&mp_plat_print, "%.*s\n", (int)fix.len, fix.buf);
Damien Georgef6013902015-05-12 23:34:10 +010060 }
61
Damien Georged3b32ca2015-05-08 00:19:56 +010062 // repl autocomplete
63 {
Damien George556c8a92015-10-15 00:23:03 +010064 mp_printf(&mp_plat_print, "# repl\n");
Damien Georgef6013902015-05-12 23:34:10 +010065
Damien Georged3b32ca2015-05-08 00:19:56 +010066 const char *str;
Damien Georged792d9e2015-05-08 09:18:38 +010067 mp_uint_t len = mp_repl_autocomplete("__n", 3, &mp_plat_print, &str);
Damien George556c8a92015-10-15 00:23:03 +010068 mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
Damien Georged3b32ca2015-05-08 00:19:56 +010069
70 mp_store_global(MP_QSTR_sys, mp_import_name(MP_QSTR_sys, mp_const_none, MP_OBJ_NEW_SMALL_INT(0)));
71 mp_repl_autocomplete("sys.", 4, &mp_plat_print, &str);
72 len = mp_repl_autocomplete("sys.impl", 8, &mp_plat_print, &str);
Damien George556c8a92015-10-15 00:23:03 +010073 mp_printf(&mp_plat_print, "%.*s\n", (int)len, str);
Damien Georged3b32ca2015-05-08 00:19:56 +010074 }
Damien Georged792d9e2015-05-08 09:18:38 +010075
Damien Georgef6013902015-05-12 23:34:10 +010076 // attrtuple
77 {
Damien George556c8a92015-10-15 00:23:03 +010078 mp_printf(&mp_plat_print, "# attrtuple\n");
Damien Georgef6013902015-05-12 23:34:10 +010079
80 static const qstr fields[] = {MP_QSTR_start, MP_QSTR_stop, MP_QSTR_step};
81 static const mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(2), MP_OBJ_NEW_SMALL_INT(3)};
82 mp_obj_print_helper(&mp_plat_print, mp_obj_new_attrtuple(fields, 3, items), PRINT_REPR);
Damien George556c8a92015-10-15 00:23:03 +010083 mp_printf(&mp_plat_print, "\n");
Damien Georgef6013902015-05-12 23:34:10 +010084 }
85
Damien George25afc7d2015-09-03 23:06:18 +010086 // str
87 {
Damien George556c8a92015-10-15 00:23:03 +010088 mp_printf(&mp_plat_print, "# str\n");
Damien George25afc7d2015-09-03 23:06:18 +010089
90 // intern string
Damien George556c8a92015-10-15 00:23:03 +010091 mp_printf(&mp_plat_print, "%d\n", MP_OBJ_IS_QSTR(mp_obj_str_intern(mp_obj_new_str("intern me", 9, false))));
Damien George25afc7d2015-09-03 23:06:18 +010092 }
93
Damien Georgea81539d2015-10-01 18:49:37 +010094 // mpz
95 {
Damien George556c8a92015-10-15 00:23:03 +010096 mp_printf(&mp_plat_print, "# mpz\n");
Damien Georgea81539d2015-10-01 18:49:37 +010097
98 mp_uint_t value;
99 mpz_t mpz;
100 mpz_init_zero(&mpz);
101
102 // mpz_as_uint_checked, with success
103 mpz_set_from_int(&mpz, 12345678);
Damien George556c8a92015-10-15 00:23:03 +0100104 mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
105 mp_printf(&mp_plat_print, "%d\n", (int)value);
Damien Georgea81539d2015-10-01 18:49:37 +0100106
107 // mpz_as_uint_checked, with negative arg
108 mpz_set_from_int(&mpz, -1);
Damien George556c8a92015-10-15 00:23:03 +0100109 mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
Damien Georgea81539d2015-10-01 18:49:37 +0100110
111 // mpz_as_uint_checked, with overflowing arg
112 mpz_set_from_int(&mpz, 1);
113 mpz_shl_inpl(&mpz, &mpz, 70);
Damien George556c8a92015-10-15 00:23:03 +0100114 mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
Damien Georgea81539d2015-10-01 18:49:37 +0100115 }
116
Damien George58f38612016-09-02 15:07:42 +1000117 // return a tuple of data for testing on the Python side
118 mp_obj_t items[] = {(mp_obj_t)&str_no_hash_obj, (mp_obj_t)&bytes_no_hash_obj};
119 return mp_obj_new_tuple(MP_ARRAY_SIZE(items), items);
Damien Georged3b32ca2015-05-08 00:19:56 +0100120}
Damien Georged792d9e2015-05-08 09:18:38 +0100121MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage);
Damien Georged3b32ca2015-05-08 00:19:56 +0100122
123#endif