Browse Source

Add basic functionality tests for the Python bit.

pull/10/merge
Damien 11 years ago
parent
commit
39977a56da
  1. 43
      tests/basics/run-tests
  2. 4
      tests/basics/tests/0prelim.py
  3. 13
      tests/basics/tests/builtin-len1.py
  4. 28
      tests/basics/tests/class1.py
  5. 15
      tests/basics/tests/class2.py
  6. 20
      tests/basics/tests/comprehension1.py
  7. 18
      tests/basics/tests/dict1.py
  8. 10
      tests/basics/tests/dict2.py
  9. 3
      tests/basics/tests/float1.py
  10. 9
      tests/basics/tests/for1.py
  11. 5
      tests/basics/tests/fun1.py
  12. 10
      tests/basics/tests/fun2.py
  13. 6
      tests/basics/tests/fun3.py
  14. 22
      tests/basics/tests/generator1.py
  15. 4
      tests/basics/tests/lambda1.py
  16. 12
      tests/basics/tests/list1.py
  17. 7
      tests/basics/tests/set1.py
  18. 9
      tests/basics/tests/string1.py
  19. 6
      tests/basics/tests/try1.py
  20. 12
      tests/basics/tests/try2.py
  21. 17
      tests/basics/tests/try3.py
  22. 21
      tests/basics/tests/try4.py
  23. 12
      tests/basics/tests/while1.py

43
tests/basics/run-tests

@ -0,0 +1,43 @@
#!/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

4
tests/basics/tests/0prelim.py

@ -0,0 +1,4 @@
# all tests need print to work! make sure it does work
print(1)
print('abc')

13
tests/basics/tests/builtin-len1.py

@ -0,0 +1,13 @@
# 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}))

28
tests/basics/tests/class1.py

@ -0,0 +1,28 @@
# 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()

15
tests/basics/tests/class2.py

@ -0,0 +1,15 @@
# 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)

20
tests/basics/tests/comprehension1.py

@ -0,0 +1,20 @@
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()

18
tests/basics/tests/dict1.py

@ -0,0 +1,18 @@
# 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])

10
tests/basics/tests/dict2.py

@ -0,0 +1,10 @@
# 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'])

3
tests/basics/tests/float1.py

@ -0,0 +1,3 @@
# basic float
x = 1 / 2
print(x)

9
tests/basics/tests/for1.py

@ -0,0 +1,9 @@
# 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()

5
tests/basics/tests/fun1.py

@ -0,0 +1,5 @@
# calling a function
def f():
print(1)
f()

10
tests/basics/tests/fun2.py

@ -0,0 +1,10 @@
# calling a function from a function
def f(x):
print(x + 1)
def g(x):
f(2 * x)
f(4 * x)
g(3)

6
tests/basics/tests/fun3.py

@ -0,0 +1,6 @@
# 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))

22
tests/basics/tests/generator1.py

@ -0,0 +1,22 @@
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__())

4
tests/basics/tests/lambda1.py

@ -0,0 +1,4 @@
# lambda
f = lambda x, y: x + 3 * y
print(f(3, 5))

12
tests/basics/tests/list1.py

@ -0,0 +1,12 @@
# 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)

7
tests/basics/tests/set1.py

@ -0,0 +1,7 @@
# basic sets
s = {1}
print(s)
s = {3, 4, 3, 1}
print(s)

9
tests/basics/tests/string1.py

@ -0,0 +1,9 @@
# basic strings
x = 'abc'
print(x)
x += 'def'
print(x)
print('123' + "456")

6
tests/basics/tests/try1.py

@ -0,0 +1,6 @@
# basic exceptions
x = 1
try:
x.a()
except:
print(x)

12
tests/basics/tests/try2.py

@ -0,0 +1,12 @@
# nested try's
try:
print("try 1")
try:
print("try 2")
foo()
except:
print("except 2")
bar()
except:
print("except 1")

17
tests/basics/tests/try3.py

@ -0,0 +1,17 @@
# nested exceptions
def f():
try:
foo()
except:
print("except 1")
try:
baz()
except:
print("except 2")
bar()
try:
f()
except:
print("f except")

21
tests/basics/tests/try4.py

@ -0,0 +1,21 @@
# triple nested exceptions
def f():
try:
foo()
except:
print("except 1")
try:
bar()
except:
print("except 2")
try:
baz()
except:
print("except 3")
bak()
try:
f()
except:
print("f except")

12
tests/basics/tests/while1.py

@ -0,0 +1,12 @@
# basic while loop
x = 0
while x < 2:
y = 0
while y < 2:
z = 0
while z < 2:
z = z + 1
print(x, y, z)
y = y + 1
x = x + 1
Loading…
Cancel
Save