micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
try:
extra_coverage
except NameError:
print("SKIP")
raise SystemExit
import uerrno
import uio
data = extra_coverage()
# test hashing of str/bytes that have an invalid hash
print(data[0], data[1])
print(hash(data[0]))
print(hash(data[1]))
print(hash(bytes(data[0], "utf8")))
print(hash(str(data[1], "utf8")))
# test streams
stream = data[2] # has set_error and set_buf. Write always returns error
stream.set_error(uerrno.EAGAIN) # non-blocking error
print(stream.read()) # read all encounters non-blocking error
print(stream.read(1)) # read 1 byte encounters non-blocking error
print(stream.readline()) # readline encounters non-blocking error
print(stream.readinto(bytearray(10))) # readinto encounters non-blocking error
print(stream.write(b"1")) # write encounters non-blocking error
print(stream.write1(b"1")) # write1 encounters non-blocking error
stream.set_buf(b"123")
print(stream.read(4)) # read encounters non-blocking error after successful reads
stream.set_buf(b"123")
print(stream.read1(4)) # read1 encounters non-blocking error after successful reads
stream.set_buf(b"123")
print(stream.readline(4)) # readline encounters non-blocking error after successful reads
try:
print(stream.ioctl(0, 0)) # ioctl encounters non-blocking error; raises OSError
except OSError:
print("OSError")
stream.set_error(0)
print(stream.ioctl(0, bytearray(10))) # successful ioctl call
stream2 = data[3] # is textio
print(stream2.read(1)) # read 1 byte encounters non-blocking error with textio stream
# test BufferedWriter with stream errors
stream.set_error(uerrno.EAGAIN)
buf = uio.BufferedWriter(stream, 8)
print(buf.write(bytearray(16)))
# function defined in C++ code
print("cpp", extra_cpp_coverage())
# test user C module
import cexample
print(cexample.add_ints(3, 2))
# test user C module mixed with C++ code
import cppexample
print(cppexample.cppfunc(1, 2))
# test basic import of frozen scripts
import frzstr1
print(frzstr1.__file__)
import frzmpy1
print(frzmpy1.__file__)
# test import of frozen packages with __init__.py
import frzstr_pkg1
print(frzstr_pkg1.__file__, frzstr_pkg1.x)
import frzmpy_pkg1
print(frzmpy_pkg1.__file__, frzmpy_pkg1.x)
# test import of frozen packages without __init__.py
from frzstr_pkg2.mod import Foo
print(Foo.x)
from frzmpy_pkg2.mod import Foo
print(Foo.x)
# test raising exception in frozen script
try:
import frzmpy2
except ZeroDivisionError:
print("ZeroDivisionError")
# test importing various objects
import frzmpy3
# test for MP_QSTR_NULL regression
from frzqstr import returns_NULL
print(returns_NULL())

View File

@@ -0,0 +1,201 @@
# mp_printf
-123 +123 123
-0123
123
123
1ABCDEF
ab abc ' abc' ' True' 'Tru'
false true
(null)
-2147483648
2147483648
80000000
80000000
abc
%
# GC
0
0
# tracked allocation
m_tracked_head = 0
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
m_tracked_head = 0
# vstr
tests
sts
test
tes
RuntimeError:
RuntimeError:
# repl
ame__
mport
builtins micropython _thread _uasyncio
btree cexample cmath cppexample
ffi framebuf gc math
termios uarray ubinascii ucollections
ucryptolib uctypes uerrno uhashlib
uheapq uio ujson umachine
uos urandom ure uselect
usocket ussl ustruct usys
utime utimeq uwebsocket uzlib
ime
utime utimeq
argv atexit byteorder exc_info
exit getsizeof implementation maxsize
modules path platform print_exception
ps1 ps2 stderr stdin
stdout tracebacklimit version version_info
ementation
# attrtuple
(start=1, stop=2, step=3)
# str
1
# bytearray
data
# mpz
1
12345678
0
0
0
0
0
1
12345
6
# runtime utils
TypeError: unsupported type for __abs__: 'str'
TypeError: unsupported types for __divmod__: 'str', 'str'
1
2
OverflowError: overflow converting long int to machine word
OverflowError: overflow converting long int to machine word
ValueError:
Warning: test
# format float
?
+1e+00
+1e+00
# binary
123
456
# VM
2 1
# scheduler
sched(0)=1
sched(1)=1
sched(2)=1
sched(3)=1
sched(4)=0
unlocked
0
1
2
3
KeyboardInterrupt:
KeyboardInterrupt:
10
# ringbuf
99 0
98 1
22
99 0
97 2
aa55
99 0
0 99
-1
1 98
-1
2 97
0
cc99
99 0
0
11bb
0
22ff
-1
-1
# pairheap
create: 0 0 0 0
pop all: 0 1 2 3
create: 7 6 5 4 3 2 1 0
pop all: 0 1 2 3 4 5 6 7
create: 1 - - 1 1 1 1 1 1
pop all: 1 2
create: 1 1 1 1 2 2
pop all: 2 4
create: 1 1 1 1 1
pop all: 1 3 4
create: 3 3 3 1 1 1
pop all: 1 2 4 5
# mp_obj_is_type
1 1
0 0
1 1
1 1
0 0
1 1
# end coverage.c
0123456789 b'0123456789'
7300
7300
7300
7300
None
None
None
None
None
None
b'123'
b'123'
b'123'
OSError
0
None
None
cpp None
5
(3, 'hellocpp')
frzstr1
frzstr1.py
frzmpy1
frzmpy1.py
frzstr_pkg1.__init__
frzstr_pkg1/__init__.py 1
frzmpy_pkg1.__init__
frzmpy_pkg1/__init__.py 1
frzstr_pkg2.mod
1
frzmpy_pkg2.mod
1
ZeroDivisionError
\
X
'\x1b'
b'\x00\xff'
NULL

View File

@@ -0,0 +1,36 @@
try:
import ffi
except ImportError:
print("SKIP")
raise SystemExit
def ffi_open(names):
err = None
for n in names:
try:
mod = ffi.open(n)
return mod
except OSError as e:
err = e
raise err
libc = ffi_open(("libc.so", "libc.so.0", "libc.so.6", "libc.dylib"))
qsort = libc.func("v", "qsort", "piip")
def cmp(pa, pb):
a = ffi.as_bytearray(pa, 1)
b = ffi.as_bytearray(pb, 1)
# print("cmp:", a, b)
return a[0] - b[0]
cmp_c = ffi.callback("i", cmp, "pp")
s = bytearray(b"foobar")
print("org string:", s)
qsort(s, len(s), 1, cmp_c)
print("qsort'ed:", s)

View File

@@ -0,0 +1,2 @@
org string: bytearray(b'foobar')
qsort'ed: bytearray(b'abfoor')

View File

@@ -0,0 +1,49 @@
# test ffi float support
try:
import ffi
except ImportError:
print("SKIP")
raise SystemExit
def ffi_open(names):
err = None
for n in names:
try:
mod = ffi.open(n)
return mod
except OSError as e:
err = e
raise err
libc = ffi_open(("libc.so", "libc.so.0", "libc.so.6", "libc.dylib"))
try:
strtof = libc.func("f", "strtof", "sp")
except OSError:
# Some libc's (e.g. Android's Bionic) define strtof as macro/inline func
# in terms of strtod().
print("SKIP")
raise SystemExit
print("%.6f" % strtof("1.23", None))
strtod = libc.func("d", "strtod", "sp")
print("%.6f" % strtod("1.23", None))
# test passing double and float args
libm = ffi_open(("libm.so", "libm.so.6", "libc.so.0", "libc.so.6", "libc.dylib"))
tgamma = libm.func("d", "tgamma", "d")
for fun_name in ("tgamma",):
fun = globals()[fun_name]
for val in (0.5, 1, 1.0, 1.5, 4, 4.0):
print(fun_name, "%.5f" % fun(val))
# test passing 2x float/double args
powf = libm.func("f", "powf", "ff")
pow = libm.func("d", "pow", "dd")
for fun_name in ("powf", "pow"):
fun = globals()[fun_name]
for args in ((0, 1), (1, 0), (2, 0.5), (3, 4)):
print(fun_name, "%.5f" % fun(*args))

View File

@@ -0,0 +1,16 @@
1.230000
1.230000
tgamma 1.77245
tgamma 1.00000
tgamma 1.00000
tgamma 0.88623
tgamma 6.00000
tgamma 6.00000
powf 0.00000
powf 1.00000
powf 1.41421
powf 81.00000
pow 0.00000
pow 1.00000
pow 1.41421
pow 81.00000

View File

@@ -0,0 +1,32 @@
# test ffi float support
try:
import ffi
except ImportError:
print("SKIP")
raise SystemExit
def ffi_open(names):
err = None
for n in names:
try:
mod = ffi.open(n)
return mod
except OSError as e:
err = e
raise err
libm = ffi_open(("libm.so", "libm.so.6", "libc.so.0", "libc.so.6", "libc.dylib"))
# Some libc's implement tgammaf as header macro with tgamma(), so don't assume
# it'll be in library.
try:
tgammaf = libm.func("f", "tgammaf", "f")
except OSError:
print("SKIP")
raise SystemExit
for fun in (tgammaf,):
for val in (0.5, 1, 1.0, 1.5, 4, 4.0):
print("%.6f" % fun(val))

View File

@@ -0,0 +1,6 @@
1.772454
1.000000
1.000000
0.886227
6.000000
6.000000

View File

@@ -0,0 +1,33 @@
#include <stdint.h>
int8_t f8i(int8_t x) {
return x ^ 1;
}
uint8_t f8u(uint8_t x) {
return x ^ 1;
}
int16_t f16i(int16_t x) {
return x ^ 1;
}
uint16_t f16u(uint16_t x) {
return x ^ 1;
}
int32_t f32i(int32_t x) {
return x ^ 1;
}
uint32_t f32u(uint32_t x) {
return x ^ 1;
}
int64_t f64i(int64_t x) {
return x ^ 1;
}
uint64_t f64u(uint64_t x) {
return x ^ 1;
}

View File

@@ -0,0 +1,51 @@
# test 8/16/32/64 bit signed/unsigned integer arguments and return types for ffi functions
# requires ffi_lib.c to be compiled as: $(CC) -shared -o ffi_lib.so ffi_lib.c
import uos, usys
try:
import ffi
except ImportError:
print("SKIP")
raise SystemExit
ffi_lib_filename = "./" + usys.argv[0].rsplit("/", 1)[0] + "/ffi_lib.so"
try:
uos.stat(ffi_lib_filename)
except OSError:
print("SKIP")
raise SystemExit
ffi_lib = ffi.open(ffi_lib_filename)
f8i = ffi_lib.func("b", "f8i", "b")
f8u = ffi_lib.func("B", "f8u", "B")
f16i = ffi_lib.func("h", "f16i", "h")
f16u = ffi_lib.func("H", "f16u", "H")
f32i = ffi_lib.func("i", "f32i", "i")
f32u = ffi_lib.func("I", "f32u", "I")
f64i = ffi_lib.func("q", "f64i", "q")
f64u = ffi_lib.func("Q", "f64u", "Q")
for func_name in ("f8i", "f8u", "f16i", "f16u", "f32i", "f32u", "f64i", "f64u"):
func = globals()[func_name]
for val in (
0,
0x7F,
0x80,
0xFF,
0x100,
0x7FFF,
0x8000,
0xFFFF,
0x10000,
0x7FFFFFFF,
0x80000000,
0xFFFFFFFF,
0x100000000,
0x7FFF_FFFF_FFFF_FFFF,
0x8000_0000_0000_0000,
0xFFFF_FFFF_FFFF_FFFF,
0x1_0000_0000_0000_0000,
):
print("{}({:x}) = {:x}".format(func_name, val, func(val)))

View File

@@ -0,0 +1,136 @@
f8i(0) = 1
f8i(7f) = 7e
f8i(80) = -7f
f8i(ff) = -2
f8i(100) = 1
f8i(7fff) = -2
f8i(8000) = 1
f8i(ffff) = -2
f8i(10000) = 1
f8i(7fffffff) = -2
f8i(80000000) = 1
f8i(ffffffff) = -2
f8i(100000000) = 1
f8i(7fffffffffffffff) = -2
f8i(8000000000000000) = 1
f8i(ffffffffffffffff) = -2
f8i(10000000000000000) = 1
f8u(0) = 1
f8u(7f) = 7e
f8u(80) = 81
f8u(ff) = fe
f8u(100) = 1
f8u(7fff) = fe
f8u(8000) = 1
f8u(ffff) = fe
f8u(10000) = 1
f8u(7fffffff) = fe
f8u(80000000) = 1
f8u(ffffffff) = fe
f8u(100000000) = 1
f8u(7fffffffffffffff) = fe
f8u(8000000000000000) = 1
f8u(ffffffffffffffff) = fe
f8u(10000000000000000) = 1
f16i(0) = 1
f16i(7f) = 7e
f16i(80) = 81
f16i(ff) = fe
f16i(100) = 101
f16i(7fff) = 7ffe
f16i(8000) = -7fff
f16i(ffff) = -2
f16i(10000) = 1
f16i(7fffffff) = -2
f16i(80000000) = 1
f16i(ffffffff) = -2
f16i(100000000) = 1
f16i(7fffffffffffffff) = -2
f16i(8000000000000000) = 1
f16i(ffffffffffffffff) = -2
f16i(10000000000000000) = 1
f16u(0) = 1
f16u(7f) = 7e
f16u(80) = 81
f16u(ff) = fe
f16u(100) = 101
f16u(7fff) = 7ffe
f16u(8000) = 8001
f16u(ffff) = fffe
f16u(10000) = 1
f16u(7fffffff) = fffe
f16u(80000000) = 1
f16u(ffffffff) = fffe
f16u(100000000) = 1
f16u(7fffffffffffffff) = fffe
f16u(8000000000000000) = 1
f16u(ffffffffffffffff) = fffe
f16u(10000000000000000) = 1
f32i(0) = 1
f32i(7f) = 7e
f32i(80) = 81
f32i(ff) = fe
f32i(100) = 101
f32i(7fff) = 7ffe
f32i(8000) = 8001
f32i(ffff) = fffe
f32i(10000) = 10001
f32i(7fffffff) = 7ffffffe
f32i(80000000) = -7fffffff
f32i(ffffffff) = -2
f32i(100000000) = 1
f32i(7fffffffffffffff) = -2
f32i(8000000000000000) = 1
f32i(ffffffffffffffff) = -2
f32i(10000000000000000) = 1
f32u(0) = 1
f32u(7f) = 7e
f32u(80) = 81
f32u(ff) = fe
f32u(100) = 101
f32u(7fff) = 7ffe
f32u(8000) = 8001
f32u(ffff) = fffe
f32u(10000) = 10001
f32u(7fffffff) = 7ffffffe
f32u(80000000) = 80000001
f32u(ffffffff) = fffffffe
f32u(100000000) = 1
f32u(7fffffffffffffff) = fffffffe
f32u(8000000000000000) = 1
f32u(ffffffffffffffff) = fffffffe
f32u(10000000000000000) = 1
f64i(0) = 1
f64i(7f) = 7e
f64i(80) = 81
f64i(ff) = fe
f64i(100) = 101
f64i(7fff) = 7ffe
f64i(8000) = 8001
f64i(ffff) = fffe
f64i(10000) = 10001
f64i(7fffffff) = 7ffffffe
f64i(80000000) = 80000001
f64i(ffffffff) = fffffffe
f64i(100000000) = 100000001
f64i(7fffffffffffffff) = 7ffffffffffffffe
f64i(8000000000000000) = -7fffffffffffffff
f64i(ffffffffffffffff) = -2
f64i(10000000000000000) = 1
f64u(0) = 1
f64u(7f) = 7e
f64u(80) = 81
f64u(ff) = fe
f64u(100) = 101
f64u(7fff) = 7ffe
f64u(8000) = 8001
f64u(ffff) = fffe
f64u(10000) = 10001
f64u(7fffffff) = 7ffffffe
f64u(80000000) = 80000001
f64u(ffffffff) = fffffffe
f64u(100000000) = 100000001
f64u(7fffffffffffffff) = 7ffffffffffffffe
f64u(8000000000000000) = 8000000000000001
f64u(ffffffffffffffff) = fffffffffffffffe
f64u(10000000000000000) = 1

View File

@@ -0,0 +1,59 @@
try:
import utime as time
except ImportError:
import time
DAYS_PER_MONTH = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
tzseconds = -time.mktime((1970, 1, 1, 14, 0, 0, 0, 0, 0))
def is_leap(year):
return (year % 4) == 0
def test():
seconds = 0
wday = 3 # Jan 1, 1970 was a Thursday
for year in range(1970, 2038):
print("Testing %d" % year)
yday = 1
for month in range(1, 13):
if month == 2 and is_leap(year):
DAYS_PER_MONTH[2] = 29
else:
DAYS_PER_MONTH[2] = 28
for day in range(1, DAYS_PER_MONTH[month] + 1):
secs = time.mktime((year, month, day, 14, 0, 0, 0, 0, 0)) + tzseconds
if secs != seconds:
print(
"mktime failed for %d-%02d-%02d got %d expected %d"
% (year, month, day, secs, seconds)
)
return
tuple = time.localtime(seconds)
secs = time.mktime(tuple)
if secs != seconds:
print(
"localtime failed for %d-%02d-%02d got %d expected %d"
% (year, month, day, secs, seconds)
)
return
seconds += 86400
if yday != tuple[7]:
print(
"locatime for %d-%02d-%02d got yday %d, expecting %d"
% (year, month, day, tuple[7], yday)
)
return
if wday != tuple[6]:
print(
"locatime for %d-%02d-%02d got wday %d, expecting %d"
% (year, month, day, tuple[6], wday)
)
return
yday += 1
wday = (wday + 1) % 7
test()