Commit 9dd36404 authored by Damien George's avatar Damien George
Browse files

tests: Add missing tests for builtins, and many other things.

parent 7e758b1c
# test builtin chr (whether or not we support unicode)
print(chr(65))
try:
chr(0x110000)
except ValueError:
print("ValueError")
......@@ -7,10 +7,9 @@ def have_compile():
except NameError:
return False
# global variable for compiled code to access
x = 1
def test():
global x
c = compile("print(x)", "file", "exec")
try:
......@@ -18,11 +17,31 @@ def test():
except NameError:
print("NameError")
# global variable for compiled code to access
x = 1
exec(c)
exec(c, {"x":2})
exec(c, {}, {"x":3})
# single/eval mode
exec(compile('print(1 + 1)', 'file', 'single'))
print(eval(compile('1 + 1', 'file', 'eval')))
# bad mode
try:
compile('1', 'file', '')
except ValueError:
print("ValueError")
# exception within compiled code
try:
exec(compile('noexist', 'file', 'exec'))
except NameError:
print("NameError")
print(x) # check 'x' still exists as a global
if have_compile():
test()
else:
......
# test builtin dir
# dir of locals
print('__name__' in dir())
# dir of module
import sys
print('platform' in dir(sys))
# test builtin divmod
print(divmod(0, 2))
print(divmod(3, 4))
print(divmod(20, 3))
try:
divmod(1, 0)
except ZeroDivisionError:
print("ZeroDivisionError")
try:
divmod('a', 'b')
except TypeError:
print("TypeError")
......@@ -23,3 +23,9 @@ print(max(lst, key=lambda x:x))
print(max(lst, key=lambda x:-x))
print(max(1, 2, 3, 4, key=lambda x:-x))
print(max(4, 3, 2, 1, key=lambda x:-x))
# need at least 1 item in the iterable
try:
min([])
except ValueError:
print("ValueError")
# test builtin ord (whether or not we support unicode)
print(ord('a'))
try:
ord('')
except TypeError:
print("TypeError")
# test builtin pow() with integral values
# 2 arg version
print(pow(0, 1))
print(pow(1, 0))
print(pow(-2, 3))
print(pow(3, 8))
# 3 arg version
print(pow(3, 4, 7))
# test builtin property
# create a property object explicitly
property()
property(1, 2, 3)
# use its accessor methods
p = property()
p.getter(1)
p.setter(2)
p.deleter(3)
# basic use as a decorator
class A:
def __init__(self, x):
self._x = x
......@@ -15,6 +28,7 @@ try:
except AttributeError:
print("AttributeError")
# explicit use within a class
class B:
def __init__(self, x):
self._x = x
......@@ -27,13 +41,18 @@ class B:
print("x set")
self._x = value
x = property(xget, xset)
def xdel(self):
print("x del")
x = property(xget, xset, xdel)
b = B(3)
print(b.x)
b.x = 4
print(b.x)
del b.x
# full use as a decorator
class C:
def __init__(self, x):
self._x = x
......@@ -48,7 +67,12 @@ class C:
print("x set")
self._x = value
@x.deleter
def x(self):
print("x del")
c = C(5)
print(c.x)
c.x = 6
print(c.x)
del c.x
# test builtin slice
# print slice
class A:
def __getitem__(self, idx):
print(idx)
A()[1:2:3]
# test builtin sorted
print(sorted(set(range(100))))
print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2)))
# need to use keyword argument
try:
sorted([], None)
except TypeError:
print("TypeError")
class Descriptor:
def __get__(self, obj, cls):
print('get')
print(type(obj) is Main)
print(cls is Main)
return 'result'
def __set__(self, obj, val):
print('set')
print(type(obj) is Main)
print(val)
def __delete__(self, obj):
print('delete')
print(type(obj) is Main)
class Main:
Forward = Descriptor()
......@@ -18,4 +24,5 @@ if 'Descriptor' in repr(r.__class__):
else:
print(r)
m.Forward = 'a'
del m.Forward
# test errors from bad function calls
def test_exc(code, exc):
try:
exec(code)
print("no exception")
except exc:
print("right exception")
except:
print("wrong exception")
# function doesn't take keyword args
test_exc("[].append(x=1)", TypeError)
# function with variable number of positional args given too few
test_exc("round()", TypeError)
# function with variable number of positional args given too many
test_exc("round(1, 2, 3)", TypeError)
# function with fixed number of positional args given wrong number
test_exc("[].append(1, 2)", TypeError)
# function with keyword args given extra positional args
test_exc("[].sort(1)", TypeError)
# function with keyword args given extra keyword args
test_exc("[].sort(noexist=1)", TypeError)
# function with keyword args not given a specific keyword arg
test_exc("enumerate()", TypeError)
......@@ -6,3 +6,18 @@ for rhs in range(2, 11):
print(lhs, '*', rhs, '=', res)
lhs = res
# below tests pos/neg combinations that overflow small int
# 31-bit overflow
i = 1 << 20
print(i * i)
print(i * -i)
print(-i * i)
print(-i * -i)
# 63-bit overflow
i = 1 << 40
print(i * i)
print(i * -i)
print(-i * i)
print(-i * -i)
# test behaviour of module objects
# this module should always exist
import __main__
# print module
print(repr(__main__).startswith("<module '__main__'"))
# store new attribute
__main__.x = 1
# delete attribute
del __main__.x
# uPy behaviour only: builtin modules are read-only
import sys
try:
sys.x = 1
except AttributeError:
print("AttributeError")
# test builtin object()
# creation
object()
# printing
print(repr(object())[:7])
......@@ -11,6 +11,7 @@ def test_exc(code, exc):
# unsupported unary operators
test_exc("~None", TypeError)
test_exc("~''", TypeError)
test_exc("~[]", TypeError)
test_exc("~bytearray()", TypeError)
......@@ -28,6 +29,8 @@ test_exc("(1 << 70) in 1", TypeError)
# unsupported subscription
test_exc("1[0]", TypeError)
test_exc("1[0] = 1", TypeError)
test_exc("''['']", TypeError)
test_exc("'a'[0] = 1", TypeError)
test_exc("del 1[0]", TypeError)
# not callable
......
......@@ -26,9 +26,11 @@ except IndexError:
# iter
print(list('str'))
# comparison
print('123' + '789' == '123789')
print('a' + 'b' != 'a' + 'b ')
print('1' + '2' > '2')
print('1' + '2' < '2')
# Not implemented so far
# print('1' + '2' > '2')
# print('1' + '2' < '2')
# printing quote char in string
print(repr('\'\"'))
# test sys module
import sys
print(sys.__name__)
print(type(sys.path))
print(type(sys.argv))
print(sys.version[:3])
print(sys.version_info[0], sys.version_info[1])
print(sys.byteorder in ('little', 'big'))
print(sys.maxsize > 100)
try:
sys.exit()
except SystemExit as e:
print("SystemExit", e.args)
try:
sys.exit(42)
except SystemExit as e:
print("SystemExit", e.args)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment