Damien
11 years ago
23 changed files with 306 additions and 0 deletions
@ -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 |
@ -0,0 +1,4 @@ |
|||
# all tests need print to work! make sure it does work |
|||
|
|||
print(1) |
|||
print('abc') |
@ -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})) |
@ -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() |
@ -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) |
@ -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() |
@ -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]) |
@ -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']) |
@ -0,0 +1,3 @@ |
|||
# basic float |
|||
x = 1 / 2 |
|||
print(x) |
@ -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() |
@ -0,0 +1,5 @@ |
|||
# calling a function |
|||
|
|||
def f(): |
|||
print(1) |
|||
f() |
@ -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) |
@ -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)) |
@ -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__()) |
@ -0,0 +1,4 @@ |
|||
# lambda |
|||
|
|||
f = lambda x, y: x + 3 * y |
|||
print(f(3, 5)) |
@ -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) |
@ -0,0 +1,7 @@ |
|||
# basic sets |
|||
|
|||
s = {1} |
|||
print(s) |
|||
|
|||
s = {3, 4, 3, 1} |
|||
print(s) |
@ -0,0 +1,9 @@ |
|||
# basic strings |
|||
|
|||
x = 'abc' |
|||
print(x) |
|||
|
|||
x += 'def' |
|||
print(x) |
|||
|
|||
print('123' + "456") |
@ -0,0 +1,6 @@ |
|||
# basic exceptions |
|||
x = 1 |
|||
try: |
|||
x.a() |
|||
except: |
|||
print(x) |
@ -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") |
@ -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") |
@ -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") |
@ -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…
Reference in new issue