Skip to content
Snippets Groups Projects
Commit 39977a56 authored by Damien's avatar Damien
Browse files

Add basic functionality tests for the Python bit.

parent dae7eb72
Branches
Tags
No related merge requests found
Showing
with 256 additions and 0 deletions
#!/bin/bash
RM="/bin/rm -f"
CPYTHON3=python3
MP_PY=../../unix/py
numtests=0
numpassed=0
numfailed=0
namefailed=
for infile in tests/*.py
do
basename=`basename $infile .c`
outfile=${basename}.out
expfile=${basename}.exp
$CPYTHON3 -B $infile > $expfile
$MP_PY $infile > $outfile
diff --brief $expfile $outfile > /dev/null
if [ $? -eq 0 ]
then
echo "pass $infile"
$RM $outfile
$RM $expfile
numpassed=`expr $numpassed + 1`
else
echo "FAIL $infile"
numfailed=`expr $numfailed + 1`
namefailed="$namefailed $basename"
fi
numtests=`expr $numtests + 1`
done
echo "$numtests tests performed"
echo "$numpassed tests passed"
if [ $numfailed -ne 0 ]
then
echo "$numfailed tests failed -$namefailed"
fi
# all tests need print to work! make sure it does work
print(1)
print('abc')
# builtin len
print(len(()))
print(len((1,)))
print(len((1, 2)))
print(len([]))
x = [1, 2, 3]
print(len(x))
f = len
print(f({}))
print(f({1:2, 3:4}))
# basic class
def go():
class C:
def f():
print(1)
def g(self):
print(2)
def set(self, value):
self.value = value
def print(self):
print(self.value)
C.f()
C()
C().g()
o = C()
o.set(3)
o.print()
C.set(o, 4)
C.print(o)
go()
# class with __init__
class C1:
def __init__(self):
self.x = 1
c1 = C1()
print(c1.x)
class C2:
def __init__(self, x):
self.x = x
c2 = C2(4)
print(c2.x)
def f():
# list comprehension
print([a + 1 for a in range(5)])
print([(a, b) for a in range(3) for b in range(2)])
print([a * 2 for a in range(7) if a > 3])
print([a for a in [1, 3, 5]])
print([a for a in [a for a in range(4)]])
# dict comprehension
d = {a : 2 * a for a in range(5)}
print(d[0], d[1], d[2], d[3], d[4])
# set comprehension
print({a for a in range(5)})
f()
# basic dictionary
d = {}
print(d)
d[2] = 123
print(d)
d = {1:2}
d[3] = 3
print(d)
d[1] = 0
print(d)
print(d[1])
x = 1
while x < 1000:
d[x] = x
x += 1
print(d[500])
# using strings as keys in dict
d = {'1': 1, '2': 2}
print(d['1'], d['2'])
d['3'] = 3
print(d['1'], d['2'], d['3'])
d['2'] = 222
print(d['1'], d['2'], d['3'])
# basic float
x = 1 / 2
print(x)
# basic for loop
def f():
for x in range(2):
for y in range(2):
for z in range(2):
print(x, y, z)
f()
# calling a function
def f():
print(1)
f()
# calling a function from a function
def f(x):
print(x + 1)
def g(x):
f(2 * x)
f(4 * x)
g(3)
# function with large number of arguments
def fun(a, b, c, d, e, f, g):
return a + b + c * d + e * f * g
print(fun(1, 2, 3, 4, 5, 6, 7))
def f(x):
print('a')
y = x
print('b')
while y > 0:
print('c')
y -= 1
print('d')
yield y
print('e')
print('f')
return None
for val in f(3):
print(val)
#gen = f(3)
#print(gen)
#print(gen.__next__())
#print(gen.__next__())
#print(gen.__next__())
#print(gen.__next__())
# lambda
f = lambda x, y: x + 3 * y
print(f(3, 5))
# basic list functionality
x = [1, 2, 3 * 4]
print(x)
x[0] = 4
print(x)
x[1] += -4
print(x)
x.append(5)
print(x)
f = x.append
f(4)
print(x)
# basic sets
s = {1}
print(s)
s = {3, 4, 3, 1}
print(s)
# basic strings
x = 'abc'
print(x)
x += 'def'
print(x)
print('123' + "456")
# basic exceptions
x = 1
try:
x.a()
except:
print(x)
# nested try's
try:
print("try 1")
try:
print("try 2")
foo()
except:
print("except 2")
bar()
except:
print("except 1")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment