summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--py/builtin.c5
-rw-r--r--py/obj.h1
-rw-r--r--py/objdict.c25
-rw-r--r--py/runtime.c2
-rw-r--r--py/showbc.c10
-rwxr-xr-xtests/basics/run-tests43
-rw-r--r--tests/basics/tests/0prelim.py4
-rw-r--r--tests/basics/tests/builtin-len1.py13
-rw-r--r--tests/basics/tests/class1.py28
-rw-r--r--tests/basics/tests/class2.py15
-rw-r--r--tests/basics/tests/comprehension1.py20
-rw-r--r--tests/basics/tests/dict1.py18
-rw-r--r--tests/basics/tests/dict2.py10
-rw-r--r--tests/basics/tests/float1.py3
-rw-r--r--tests/basics/tests/for1.py9
-rw-r--r--tests/basics/tests/fun1.py5
-rw-r--r--tests/basics/tests/fun2.py10
-rw-r--r--tests/basics/tests/fun3.py6
-rw-r--r--tests/basics/tests/generator1.py22
-rw-r--r--tests/basics/tests/lambda1.py4
-rw-r--r--tests/basics/tests/list1.py12
-rw-r--r--tests/basics/tests/set1.py7
-rw-r--r--tests/basics/tests/string1.py9
-rw-r--r--tests/basics/tests/try1.py6
-rw-r--r--tests/basics/tests/try2.py12
-rw-r--r--tests/basics/tests/try3.py17
-rw-r--r--tests/basics/tests/try4.py21
-rw-r--r--tests/basics/tests/while1.py12
28 files changed, 330 insertions, 19 deletions
diff --git a/py/builtin.c b/py/builtin.c
index f6b57109c..f39ab03d4 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -209,11 +209,8 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
mp_obj_t *seq_items;
mp_obj_list_get(o_in, &seq_len, &seq_items);
len = seq_len;
- /* TODO
} else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
- mp_obj_base_t *o = o_in;
- len = o->u_map.used;
- */
+ len = mp_obj_dict_len(o_in);
} else {
nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
}
diff --git a/py/obj.h b/py/obj.h
index 8d857b195..1a7b91aaa 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -206,6 +206,7 @@ void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
// dict
extern const mp_obj_type_t dict_type;
+uint mp_obj_dict_len(mp_obj_t self_in);
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
// set
diff --git a/py/objdict.c b/py/objdict.c
index 02753e381..50ce27904 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -53,13 +53,6 @@ mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
}
-mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
- assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
- mp_obj_dict_t *self = self_in;
- mp_map_lookup_helper(&self->map, key, true)->value = value;
- return self_in;
-}
-
const mp_obj_type_t dict_type = {
{ &mp_const_type },
"dict",
@@ -78,3 +71,21 @@ mp_obj_t mp_obj_new_dict(int n_args) {
mp_map_init(&o->map, MP_MAP_OBJ, n_args);
return o;
}
+
+uint mp_obj_dict_len(mp_obj_t self_in) {
+ mp_obj_dict_t *self = self_in;
+ uint len = 0;
+ for (int i = 0; i < self->map.alloc; i++) {
+ if (self->map.table[i].key != NULL) {
+ len += 1;
+ }
+ }
+ return len;
+}
+
+mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
+ assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
+ mp_obj_dict_t *self = self_in;
+ mp_map_lookup_helper(&self->map, key, true)->value = value;
+ return self_in;
+}
diff --git a/py/runtime.c b/py/runtime.c
index 7258b36ca..1598cc291 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -737,7 +737,7 @@ mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
}
mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
- mp_set_lookup(set, item, true);
+ mp_obj_set_store(set, item);
return set;
}
diff --git a/py/showbc.c b/py/showbc.c
index 4bdbee89f..600d49796 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -279,21 +279,17 @@ void mp_show_byte_code(const byte *ip, int len) {
rt_store_map(sp[unum + 1], sp[0], sp[1]);
sp += 2;
break;
+ */
case MP_BC_BUILD_SET:
DECODE_UINT;
- obj1 = rt_build_set(unum, sp);
- sp += unum - 1;
- *sp = obj1;
+ printf("BUILD_SET %lu", unum);
break;
case MP_BC_SET_ADD:
DECODE_UINT;
- // I think it's guaranteed by the compiler that sp[unum] is a set
- rt_store_set(sp[unum], sp[0]);
- sp++;
+ printf("SET_ADD %lu", unum);
break;
- */
case MP_BC_UNPACK_SEQUENCE:
DECODE_UINT;
diff --git a/tests/basics/run-tests b/tests/basics/run-tests
new file mode 100755
index 000000000..1d3d3b9de
--- /dev/null
+++ b/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=numpassed + 1))
+ else
+ echo "FAIL $infile"
+ ((numfailed=numfailed + 1))
+ namefailed="$namefailed $basename"
+ fi
+
+ ((numtests=numtests + 1))
+done
+
+echo "$numtests tests performed"
+echo "$numpassed tests passed"
+if [[ $numfailed != 0 ]]
+then
+ echo "$numfailed tests failed -$namefailed"
+fi
diff --git a/tests/basics/tests/0prelim.py b/tests/basics/tests/0prelim.py
new file mode 100644
index 000000000..2da65dc27
--- /dev/null
+++ b/tests/basics/tests/0prelim.py
@@ -0,0 +1,4 @@
+# all tests need print to work! make sure it does work
+
+print(1)
+print('abc')
diff --git a/tests/basics/tests/builtin-len1.py b/tests/basics/tests/builtin-len1.py
new file mode 100644
index 000000000..6a082394c
--- /dev/null
+++ b/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}))
diff --git a/tests/basics/tests/class1.py b/tests/basics/tests/class1.py
new file mode 100644
index 000000000..bf5f08e64
--- /dev/null
+++ b/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()
diff --git a/tests/basics/tests/class2.py b/tests/basics/tests/class2.py
new file mode 100644
index 000000000..0b3b21867
--- /dev/null
+++ b/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)
diff --git a/tests/basics/tests/comprehension1.py b/tests/basics/tests/comprehension1.py
new file mode 100644
index 000000000..7f541ee53
--- /dev/null
+++ b/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()
diff --git a/tests/basics/tests/dict1.py b/tests/basics/tests/dict1.py
new file mode 100644
index 000000000..1356cd428
--- /dev/null
+++ b/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])
diff --git a/tests/basics/tests/dict2.py b/tests/basics/tests/dict2.py
new file mode 100644
index 000000000..1d5aae576
--- /dev/null
+++ b/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'])
diff --git a/tests/basics/tests/float1.py b/tests/basics/tests/float1.py
new file mode 100644
index 000000000..200d95585
--- /dev/null
+++ b/tests/basics/tests/float1.py
@@ -0,0 +1,3 @@
+# basic float
+x = 1 / 2
+print(x)
diff --git a/tests/basics/tests/for1.py b/tests/basics/tests/for1.py
new file mode 100644
index 000000000..5a2635638
--- /dev/null
+++ b/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()
diff --git a/tests/basics/tests/fun1.py b/tests/basics/tests/fun1.py
new file mode 100644
index 000000000..e12bdbe03
--- /dev/null
+++ b/tests/basics/tests/fun1.py
@@ -0,0 +1,5 @@
+# calling a function
+
+def f():
+ print(1)
+f()
diff --git a/tests/basics/tests/fun2.py b/tests/basics/tests/fun2.py
new file mode 100644
index 000000000..a3c3e7bab
--- /dev/null
+++ b/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)
diff --git a/tests/basics/tests/fun3.py b/tests/basics/tests/fun3.py
new file mode 100644
index 000000000..f1458df82
--- /dev/null
+++ b/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))
diff --git a/tests/basics/tests/generator1.py b/tests/basics/tests/generator1.py
new file mode 100644
index 000000000..d4028b0ce
--- /dev/null
+++ b/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__())
diff --git a/tests/basics/tests/lambda1.py b/tests/basics/tests/lambda1.py
new file mode 100644
index 000000000..06111d6a9
--- /dev/null
+++ b/tests/basics/tests/lambda1.py
@@ -0,0 +1,4 @@
+# lambda
+
+f = lambda x, y: x + 3 * y
+print(f(3, 5))
diff --git a/tests/basics/tests/list1.py b/tests/basics/tests/list1.py
new file mode 100644
index 000000000..8caa9e17c
--- /dev/null
+++ b/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)
diff --git a/tests/basics/tests/set1.py b/tests/basics/tests/set1.py
new file mode 100644
index 000000000..c54fbd837
--- /dev/null
+++ b/tests/basics/tests/set1.py
@@ -0,0 +1,7 @@
+# basic sets
+
+s = {1}
+print(s)
+
+s = {3, 4, 3, 1}
+print(s)
diff --git a/tests/basics/tests/string1.py b/tests/basics/tests/string1.py
new file mode 100644
index 000000000..28aeaddbc
--- /dev/null
+++ b/tests/basics/tests/string1.py
@@ -0,0 +1,9 @@
+# basic strings
+
+x = 'abc'
+print(x)
+
+x += 'def'
+print(x)
+
+print('123' + "456")
diff --git a/tests/basics/tests/try1.py b/tests/basics/tests/try1.py
new file mode 100644
index 000000000..b3b85372d
--- /dev/null
+++ b/tests/basics/tests/try1.py
@@ -0,0 +1,6 @@
+# basic exceptions
+x = 1
+try:
+ x.a()
+except:
+ print(x)
diff --git a/tests/basics/tests/try2.py b/tests/basics/tests/try2.py
new file mode 100644
index 000000000..1cca9e039
--- /dev/null
+++ b/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")
diff --git a/tests/basics/tests/try3.py b/tests/basics/tests/try3.py
new file mode 100644
index 000000000..31bacd3b3
--- /dev/null
+++ b/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")
diff --git a/tests/basics/tests/try4.py b/tests/basics/tests/try4.py
new file mode 100644
index 000000000..4d324c187
--- /dev/null
+++ b/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")
diff --git a/tests/basics/tests/while1.py b/tests/basics/tests/while1.py
new file mode 100644
index 000000000..a9bb5d279
--- /dev/null
+++ b/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