Skip to content

Commit 58f3861

Browse files
committed
tests/unix/extra_coverage: Add test for str/bytes with invalid hash.
1 parent 5f3bda4 commit 58f3861

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

tests/unix/extra_coverage.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@
55
import sys
66
sys.exit()
77

8-
extra_coverage()
8+
data = extra_coverage()
9+
10+
# test hashing of str/bytes that have an invalid hash
11+
print(data)
12+
print(hash(data[0]))
13+
print(hash(data[1]))
14+
print(hash(bytes(data[0], 'utf8')))
15+
print(hash(str(data[1], 'utf8')))

tests/unix/extra_coverage.py.exp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ ementation
3535
12345678
3636
0
3737
0
38+
('0123456789', b'0123456789')
39+
7300
40+
7300
41+
7300
42+
7300

unix/coverage.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#include <stdio.h>
22

33
#include "py/obj.h"
4+
#include "py/objstr.h"
45
#include "py/runtime.h"
56
#include "py/repl.h"
67
#include "py/mpz.h"
78

89
#if defined(MICROPY_UNIX_COVERAGE)
910

11+
// str/bytes objects without a valid hash
12+
STATIC const mp_obj_str_t str_no_hash_obj = {{&mp_type_str}, 0, 10, (const byte*)"0123456789"};
13+
STATIC const mp_obj_str_t bytes_no_hash_obj = {{&mp_type_bytes}, 0, 10, (const byte*)"0123456789"};
14+
1015
// function to run extra tests for things that can't be checked by scripts
1116
STATIC mp_obj_t extra_coverage(void) {
1217
// mp_printf (used by ports that don't have a native printf)
@@ -109,7 +114,9 @@ STATIC mp_obj_t extra_coverage(void) {
109114
mp_printf(&mp_plat_print, "%d\n", mpz_as_uint_checked(&mpz, &value));
110115
}
111116

112-
return mp_const_none;
117+
// 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);
113120
}
114121
MP_DEFINE_CONST_FUN_OBJ_0(extra_coverage_obj, extra_coverage);
115122

0 commit comments

Comments
 (0)