Jelajahi Sumber

format builtins test and example

Lyon 2 tahun lalu
induk
melakukan
4e731b50fe
70 mengubah file dengan 460 tambahan dan 27 penghapusan
  1. 0 0
      examples/builtins/XOR.py
  2. 0 0
      examples/builtins/bigfile.py
  3. 0 0
      examples/builtins/bytes.py
  4. 0 0
      examples/builtins/class_hint.py
  5. 0 0
      examples/builtins/class_script.py
  6. 0 0
      examples/builtins/ctypes.py
  7. 0 0
      examples/builtins/dict.py
  8. 0 0
      examples/builtins/encode_decode.py
  9. 0 0
      examples/builtins/eval.py
  10. 0 0
      examples/builtins/file.py
  11. 0 0
      examples/builtins/file2.py
  12. 0 0
      examples/builtins/file3.py
  13. 0 0
      examples/builtins/file_nofound.py
  14. 0 0
      examples/builtins/fn_default1.py
  15. 0 0
      examples/builtins/fn_default_tuple.py
  16. 0 0
      examples/builtins/fn_fn.py
  17. 0 0
      examples/builtins/fn_pos_vars.py
  18. 0 0
      examples/builtins/for_return.py
  19. 0 0
      examples/builtins/function.py
  20. 0 0
      examples/builtins/getitem.py
  21. 0 0
      examples/builtins/int_extern.py
  22. 0 0
      examples/builtins/is_not.py
  23. 0 0
      examples/builtins/isinstance.py
  24. 0 0
      examples/builtins/list_tuple_equ.py
  25. 0 0
      examples/builtins/max_min.py
  26. 0 0
      examples/builtins/opt_self.py
  27. 0 0
      examples/builtins/print.py
  28. 0 0
      examples/builtins/seek.py
  29. 0 0
      examples/builtins/slice.py
  30. 0 0
      examples/builtins/split.py
  31. 0 0
      examples/builtins/str_join.py
  32. 0 0
      examples/builtins/tuple.py
  33. 0 0
      examples/builtins/type.py
  34. 0 0
      examples/builtins/utf8.py
  35. 11 0
      port/linux/std_push.sh
  36. 0 27
      test/python/builtin/type.py
  37. 21 0
      test/python/builtins/XOR.py
  38. 3 0
      test/python/builtins/bigfile.py
  39. 7 0
      test/python/builtins/bytes.py
  40. 8 0
      test/python/builtins/class_hint.py
  41. 9 0
      test/python/builtins/class_script.py
  42. 9 0
      test/python/builtins/ctypes.py
  43. 18 0
      test/python/builtins/dict.py
  44. 9 0
      test/python/builtins/encode_decode.py
  45. 25 0
      test/python/builtins/eval.py
  46. 11 0
      test/python/builtins/file.py
  47. 9 0
      test/python/builtins/file2.py
  48. 8 0
      test/python/builtins/file3.py
  49. 4 0
      test/python/builtins/file_nofound.py
  50. 45 0
      test/python/builtins/fn_default1.py
  51. 13 0
      test/python/builtins/fn_default_tuple.py
  52. 13 0
      test/python/builtins/fn_fn.py
  53. 7 0
      test/python/builtins/fn_pos_vars.py
  54. 16 0
      test/python/builtins/for_return.py
  55. 4 0
      test/python/builtins/function.py
  56. 10 0
      test/python/builtins/getitem.py
  57. 7 0
      test/python/builtins/int_extern.py
  58. 10 0
      test/python/builtins/is_not.py
  59. 36 0
      test/python/builtins/isinstance.py
  60. 17 0
      test/python/builtins/list_tuple_equ.py
  61. 13 0
      test/python/builtins/max_min.py
  62. 31 0
      test/python/builtins/opt_self.py
  63. 4 0
      test/python/builtins/print.py
  64. 4 0
      test/python/builtins/seek.py
  65. 20 0
      test/python/builtins/slice.py
  66. 9 0
      test/python/builtins/split.py
  67. 8 0
      test/python/builtins/str_join.py
  68. 20 0
      test/python/builtins/tuple.py
  69. 3 0
      test/python/builtins/type.py
  70. 18 0
      test/python/builtins/utf8.py

+ 0 - 0
test/python/builtin/XOR.py → examples/builtins/XOR.py


+ 0 - 0
examples/BuiltIn/bigfile.py → examples/builtins/bigfile.py


+ 0 - 0
examples/BuiltIn/bytes.py → examples/builtins/bytes.py


+ 0 - 0
test/python/builtin/class_hint.py → examples/builtins/class_hint.py


+ 0 - 0
test/python/builtin/class_script.py → examples/builtins/class_script.py


+ 0 - 0
examples/BuiltIn/ctypes.py → examples/builtins/ctypes.py


+ 0 - 0
examples/BuiltIn/dict.py → examples/builtins/dict.py


+ 0 - 0
examples/BuiltIn/encode_decode.py → examples/builtins/encode_decode.py


+ 0 - 0
test/python/builtin/eval.py → examples/builtins/eval.py


+ 0 - 0
examples/BuiltIn/file.py → examples/builtins/file.py


+ 0 - 0
examples/BuiltIn/file2.py → examples/builtins/file2.py


+ 0 - 0
examples/BuiltIn/file3.py → examples/builtins/file3.py


+ 0 - 0
examples/BuiltIn/file_nofound.py → examples/builtins/file_nofound.py


+ 0 - 0
test/python/builtin/fn_default1.py → examples/builtins/fn_default1.py


+ 0 - 0
test/python/builtin/fn_default_tuple.py → examples/builtins/fn_default_tuple.py


+ 0 - 0
test/python/builtin/fn_fn.py → examples/builtins/fn_fn.py


+ 0 - 0
test/python/builtin/fn_pos_vars.py → examples/builtins/fn_pos_vars.py


+ 0 - 0
test/python/builtin/for_return.py → examples/builtins/for_return.py


+ 0 - 0
examples/BuiltIn/function.py → examples/builtins/function.py


+ 0 - 0
test/python/builtin/getitem.py → examples/builtins/getitem.py


+ 0 - 0
test/python/builtin/int_extern.py → examples/builtins/int_extern.py


+ 0 - 0
test/python/builtin/is_not.py → examples/builtins/is_not.py


+ 0 - 0
test/python/builtin/isinstance.py → examples/builtins/isinstance.py


+ 0 - 0
test/python/builtin/list_tuple_equ.py → examples/builtins/list_tuple_equ.py


+ 0 - 0
test/python/builtin/max_min.py → examples/builtins/max_min.py


+ 0 - 0
test/python/builtin/opt_self.py → examples/builtins/opt_self.py


+ 0 - 0
examples/BuiltIn/print.py → examples/builtins/print.py


+ 0 - 0
examples/BuiltIn/seek.py → examples/builtins/seek.py


+ 0 - 0
test/python/builtin/slice.py → examples/builtins/slice.py


+ 0 - 0
test/python/builtin/split.py → examples/builtins/split.py


+ 0 - 0
test/python/builtin/str_join.py → examples/builtins/str_join.py


+ 0 - 0
test/python/builtin/tuple.py → examples/builtins/tuple.py


+ 0 - 0
examples/BuiltIn/type.py → examples/builtins/type.py


+ 0 - 0
examples/BuiltIn/utf8.py → examples/builtins/utf8.py


+ 11 - 0
port/linux/std_push.sh

@@ -5,3 +5,14 @@ cp package/pikascript/$pkg.pyi ../../package/PikaStdLib/
 git add package/pikascript/pikascript-lib/PikaStdLib
 git add package/pikascript/$pkg.pyi
 git add ../../package/PikaStdLib
+
+# check if test/python/$pkg folder is exit
+if [ -d "test/python/$pkg" ] ; then
+echo "$FLAG_INFO python test files added:"
+rm ../../examples/$pkg -rf
+mkdir ../../examples/$pkg
+cp test/python/$pkg/*.py ../../examples/$pkg -r
+git add ../../test/python/$pkg
+git add ../../examples/$pkg
+ls ../../examples/$pkg
+fi

+ 0 - 27
test/python/builtin/type.py

@@ -1,27 +0,0 @@
-
-class Test1:
-    def test(self):
-        return 'test 1'
-
-
-class Test2:
-    def test(self):
-        return 'test 2'
-
-t1 = Test1()
-tt1 = type(t1)
-ttt1 = tt1()
-
-t2 = Test2()
-tt2 = type(t2)
-ttt2 = tt2()
-
-assert ttt1.test() == 'test 1'
-assert ttt2.test() == 'test 2'
-assert type(ttt1) == Test1
-assert type(ttt2) == Test2
-assert type(ttt1) != type(ttt2)
-assert type(ttt1) != Test2
-assert type(ttt2) != Test1
-
-print('PASS')

+ 21 - 0
test/python/builtins/XOR.py

@@ -0,0 +1,21 @@
+# '6' in binary is '110', '3' in binary is '011'.
+# Bitwise XOR operation: '110' ^ '011' = '101' = '5' in decimal
+assert 6 ^ 3 == 5
+
+# '10' in binary is '1010', '6' in binary is '0110'.
+# Bitwise XOR operation: '1010' ^ '0110' = '1100' = '12' in decimal
+assert 10 ^ 6 == 12
+
+# Start with '6' ('110' in binary)
+value = 6
+
+# Bitwise XOR and assign with '3' ('011' in binary), '110' ^ '011' = '101'
+value ^= 3
+assert value == 5
+
+# Bitwise XOR and assign with '10' ('1010' in binary), '101' ^ '1010' = '1111'
+value ^= 10
+assert value == 15
+
+
+print('PASS')

+ 3 - 0
test/python/builtins/bigfile.py

@@ -0,0 +1,3 @@
+f = open('test/assets/pic1.jpg', 'rb')
+b = f.read(-1)
+f.close()

+ 7 - 0
test/python/builtins/bytes.py

@@ -0,0 +1,7 @@
+def test():
+    a = bytes(10)
+    a[1] = 1
+    return a
+
+print(test())
+res = test()

+ 8 - 0
test/python/builtins/class_hint.py

@@ -0,0 +1,8 @@
+
+class Test:
+    a:int
+    def __init__(self, a):
+        self.a = a
+
+t = Test(1)
+print(t.a)

+ 9 - 0
test/python/builtins/class_script.py

@@ -0,0 +1,9 @@
+class Obj1:
+    def test(self):
+        print("Obj1.test")
+
+class Test:
+    a = Obj1()
+    a.test()
+
+t = Test()

+ 9 - 0
test/python/builtins/ctypes.py

@@ -0,0 +1,9 @@
+import ctypes
+read_data = ctypes.c_buffer(b'', 16)
+print("----read size----")
+datalen = len(read_data.raw)
+print(datalen)
+print(read_data.raw)
+print("----read data----")
+for i in range(0,datalen):
+    print(read_data.raw[i])

+ 18 - 0
test/python/builtins/dict.py

@@ -0,0 +1,18 @@
+d = {
+    'a': 1,
+    'b': 2,
+    'c': 'test'
+}
+print(d)
+for item in d:
+    print(item)
+keys = d.keys()
+for k in keys:
+    print(k)
+print(keys)
+
+dd = {
+    'list': [1, 2, 3],
+    'len': 3
+}
+print(dd)

+ 9 - 0
test/python/builtins/encode_decode.py

@@ -0,0 +1,9 @@
+from PikaStdData import String as string
+from PikaStdData import ByteArray as bytearray
+
+b = bytearray(b'test')
+b_s = b.decode()
+print(b_s)
+s = string('test')
+s_b = s.encode()
+print(s_b)

+ 25 - 0
test/python/builtins/eval.py

@@ -0,0 +1,25 @@
+
+
+def bar():
+    # local scope
+    local = 5
+    assert eval('local') == 5
+
+
+bar()
+
+assert eval('1+1') == 2
+
+
+def foo():
+    return 3
+
+
+# global scope
+assert eval('foo()') == 3
+
+g_val = 4
+assert eval('g_val') == 4
+
+
+print('PASS')

+ 11 - 0
test/python/builtins/file.py

@@ -0,0 +1,11 @@
+f = open('test/python/main.py', 'r')
+s = f.read(10)
+print(s)
+f.close()
+f = open('test/assets/test.jpg', 'rb')
+b = f.read(-1)
+print(b)
+f.close()
+f = open('test/out/write.txt', 'w')
+f.write('Hello World!\n')
+f.close()

+ 9 - 0
test/python/builtins/file2.py

@@ -0,0 +1,9 @@
+f = open('test/python/main.py', 'r')
+
+s = f.readline()
+print(s)
+
+lines = f.readlines()
+print(lines)
+
+f.close()

+ 8 - 0
test/python/builtins/file3.py

@@ -0,0 +1,8 @@
+f = open('test/out/file3.txt', 'w')
+seq = [
+    'This is the first line.\n',
+    'This is the second line.\n',
+    'This is the third line.\n'
+]
+f.writelines(seq)
+f.close()

+ 4 - 0
test/python/builtins/file_nofound.py

@@ -0,0 +1,4 @@
+try:
+    f = open("_no_file.txt", "r")
+except:
+    print("File not found")

+ 45 - 0
test/python/builtins/fn_default1.py

@@ -0,0 +1,45 @@
+
+class Test:
+    def test_default(self,
+                     dev: str,
+                     baudrate=115200,
+                     bytesize=8,
+                     parity='N',
+                     stopbits=1,
+                     xonxoff=0
+                     ):
+        return dev, baudrate, bytesize, parity, stopbits, xonxoff
+
+
+def test_default(
+    dev: str,
+    baudrate=115200,
+    bytesize=8,
+    parity='N',
+    stopbits=1,
+    xonxoff=0
+):
+    return dev, baudrate, bytesize, parity, stopbits, xonxoff
+
+
+dev, baudrate, bytesize, parity, stopbits, xonxoff = test_default(
+    'uart3', 9600)
+
+assert dev == 'uart3'
+assert baudrate == 9600
+assert bytesize == 8
+assert parity == 'N'
+assert stopbits == 1
+assert xonxoff == 0
+
+test = Test()
+dev, baudrate, bytesize, parity, stopbits, xonxoff = test.test_default(
+    'uart3', 9600)
+
+assert dev == 'uart3'
+assert baudrate == 9600
+assert bytesize == 8
+assert parity == 'N'
+assert stopbits == 1
+assert xonxoff == 0
+print('PASS')

+ 13 - 0
test/python/builtins/fn_default_tuple.py

@@ -0,0 +1,13 @@
+
+def test_default_tuple(t=(1, 2), l=[3, 4], d={'a': 5, 'b': 6}):
+    return t, l, d
+
+
+tt, ll, dd = test_default_tuple()
+assert tt[0] == 1
+assert tt[1] == 2
+assert ll[0] == 3
+assert ll[1] == 4
+assert dd['a'] == 5
+assert dd['b'] == 6
+print('PASS')

+ 13 - 0
test/python/builtins/fn_fn.py

@@ -0,0 +1,13 @@
+
+
+def test1():
+    return "test1"
+
+def test():
+    return test1
+
+l = [test1]
+
+assert l[0]() == "test1"
+assert test()() == "test1"
+print("PASS")

+ 7 - 0
test/python/builtins/fn_pos_vars.py

@@ -0,0 +1,7 @@
+
+def log(level: str, title: str, *var):
+    print('---------  ', title, '  ---------')
+    for i in var:
+        print('[%r]' % level, i)
+
+log("test")

+ 16 - 0
test/python/builtins/for_return.py

@@ -0,0 +1,16 @@
+
+
+def test1():
+    for i in range(10):
+        if i == 3:
+            return
+
+def test2():
+    for i in range(10):
+        if i == 3:
+            break
+
+test1()
+test2()
+
+gcdump()

+ 4 - 0
test/python/builtins/function.py

@@ -0,0 +1,4 @@
+def test(a, b, e=10, *vars, **keys):
+    print(keys['c'], keys['d'], a, b, vars[0], e)
+test(3, 4, 5, c=1, d= 2)
+test(3, 4, 5, c=1, d= 2, e = 12)

+ 10 - 0
test/python/builtins/getitem.py

@@ -0,0 +1,10 @@
+
+class Test:
+    def __getitem__(self, key):
+        return key
+
+
+t = Test()
+assert t[1] == 1
+assert t['a'] == 'a'
+print("PASS")

+ 7 - 0
test/python/builtins/int_extern.py

@@ -0,0 +1,7 @@
+# 测试用例
+assert int('1010', 2) == 10
+assert int('FF', 16) == 255
+assert int('77', 8) == 63
+assert int('10', 10) == 10
+assert int('10101', 2) == 21
+print('PASS')

+ 10 - 0
test/python/builtins/is_not.py

@@ -0,0 +1,10 @@
+
+assert (1 is not 1) == False
+assert (1 is not 2) == True
+assert (1 is not None) == True
+assert (None is not None) == False
+assert (None is not 1) == True
+assert (1 is not 1.0) == True
+assert (1.0 is not 1) == True
+assert (1.0 is not 1.0) == False
+print("PASS")

+ 36 - 0
test/python/builtins/isinstance.py

@@ -0,0 +1,36 @@
+# Testing with builtin types
+assert isinstance(10, int) == True
+assert isinstance("Hello, world!", str) == True
+assert isinstance([1, 2, 3, 4, 5], list) == True
+assert isinstance({"key": "value"}, dict) == True
+assert isinstance(3.14, float) == True
+assert isinstance(object(), object) == True
+
+class MyClass:
+    pass
+
+class BaseClass(object):
+    def __init__(self):
+        self.a = 1
+
+class DerivedClass(BaseClass):
+    def __init__(self):
+        super().__init__()
+        self.b = 2
+
+base_instance = BaseClass()
+derived_instance = DerivedClass()
+
+# Instances of DerivedClass should also be instances of BaseClass
+assert isinstance(MyClass(), object) == True
+assert isinstance(base_instance, object) == True
+assert isinstance(base_instance, BaseClass) == True
+assert isinstance(derived_instance, BaseClass) == True
+
+# However, instances of BaseClass are not instances of DerivedClass
+assert isinstance(base_instance, DerivedClass) == False
+
+# And instances of DerivedClass are, of course, instances of DerivedClass
+assert isinstance(derived_instance, DerivedClass) == True
+
+print('PASS')

+ 17 - 0
test/python/builtins/list_tuple_equ.py

@@ -0,0 +1,17 @@
+""" TODO: need fix
+# Test list equivalence
+assert [1, 2, 3] == [1, 2, 3]
+assert [1, 2, 3] != [1, 2, 4]
+assert [] == []
+assert [1, 2, 3] != []
+assert [1, 2, 3] != [1, 2, 3, 4]
+
+# Test tuple equivalence
+assert (1, 2, 3) == (1, 2, 3)
+assert (1, 2, 3) != (1, 2, 4)
+assert () == ()
+assert (1, 2, 3) != ()
+assert (1, 2, 3) != (1, 2, 3, 4)
+
+"""
+print("PASS")

+ 13 - 0
test/python/builtins/max_min.py

@@ -0,0 +1,13 @@
+# 测试 max() 函数
+assert max(1, 2, 3) == 3, "max() 函数错误: max(1, 2, 3) 应返回 3"
+assert max(-1, -2, -3) == -1, "max() 函数错误: max(-1, -2, -3) 应返回 -1"
+assert max(5, 5) == 5, "max() 函数错误: max(5, 5) 应返回 5"
+assert max([1, 2, 3, 4]) == 4, "max() 函数错误: max([1, 2, 3, 4]) 应返回 4"
+
+# 测试 min() 函数
+assert min(1, 2, 3) == 1, "min() 函数错误: min(1, 2, 3) 应返回 1"
+assert min(-1, -2, -3) == -3, "min() 函数错误: min(-1, -2, -3) 应返回 -3"
+assert min(5, 5) == 5, "min() 函数错误: min(5, 5) 应返回 5"
+assert min([1, 2, 3, 4]) == 1, "min() 函数错误: min([1, 2, 3, 4]) 应返回 1"
+
+print('PASS')

+ 31 - 0
test/python/builtins/opt_self.py

@@ -0,0 +1,31 @@
+a = 1
+
+a += -1  # Should result in 0
+assert a == 0
+
+a -= -1  # Subtracting -1 is same as adding 1, should result in 1
+assert a == 1
+
+a *= -1  # Should result in -1
+assert a == -1
+
+# Be careful with dividing by -1 if a is 0, it will raise a ZeroDivisionError
+a /= -1  # Should result in 1
+assert a == 1
+
+a **= -1  # Should result in 1.0 (since a is 1)
+assert a == 1.0
+
+# Be careful with floordiv by -1 if a is 0, it will raise a ZeroDivisionError
+a //= -1  # Should result in -1.0
+assert a == -1.0
+
+# Here onwards, comparison operators are used, they will return a boolean
+assert (a >= -1) == True
+assert (a <= -1) == True
+assert (a != -1) == False
+
+# Be careful with modulo by -1 if a is 0, it will raise a ZeroDivisionError
+a %= -1  # Should result in 0
+assert a == 0
+print("PASS")

+ 4 - 0
test/python/builtins/print.py

@@ -0,0 +1,4 @@
+print('test')
+print('my name is', 'old wang', 'my age is', 43)
+print('format: %s,%04d,%.2f' % ('test', 123, 15.5))
+

+ 4 - 0
test/python/builtins/seek.py

@@ -0,0 +1,4 @@
+f = open('test/assets/test.jpg', 'rb')
+len = f.seek(0, 2)
+print(len)
+f.close()

+ 20 - 0
test/python/builtins/slice.py

@@ -0,0 +1,20 @@
+my_string = "Hello, World!"
+out_of_range = False
+
+# 测试索引超出字符串长度的情况
+try:
+    my_string[len(my_string)] == ""
+except:
+    print("1.IndexError: 索引超出字符串长度")
+    out_of_range = True
+
+assert out_of_range
+
+# 测试切片超出字符串长度的情况
+b = my_string[0:100]
+print(b)
+
+# 测试使用负数索引访问字符串的情况
+assert my_string[-1] == "!"
+assert my_string[-5:-1] == "orld"
+print("PASS")

+ 9 - 0
test/python/builtins/split.py

@@ -0,0 +1,9 @@
+a, b = 'test asd'.split()
+assert a == 'test'
+assert b == 'asd'
+
+a, b = 'test asd'.split(' ')
+assert a == 'test'
+assert b == 'asd'
+
+print('PASS')

+ 8 - 0
test/python/builtins/str_join.py

@@ -0,0 +1,8 @@
+# 测试用例
+assert ''.join([]) == ''
+assert ','.join(['a']) == 'a'
+assert ','.join(['a', 'b']) == 'a,b'
+assert ''.join(['a', 'b', 'c']) == 'abc'
+assert '-'.join(['a', 'b', 'c']) == 'a-b-c'
+assert ' '.join(['hello', 'world']) == 'hello world'
+print("PASS")

+ 20 - 0
test/python/builtins/tuple.py

@@ -0,0 +1,20 @@
+t = tuple()
+assert type(t) == tuple
+
+l = [1, 2, 3, 4, 5]
+t = tuple(l)
+assert type(t) == tuple
+assert t[0] == 1
+assert t[1] == 2
+assert t[2] == 3
+assert t[3] == 4
+assert t[4] == 5
+
+t = tuple('test')
+assert type(t) == tuple
+assert t[0] == 't'
+assert t[1] == 'e'
+assert t[2] == 's'
+assert t[3] == 't'
+
+print('PASS')

+ 3 - 0
test/python/builtins/type.py

@@ -0,0 +1,3 @@
+mylist = [1, 2, 3]
+if str(type(mylist)) == "<class 'list'>":
+    print('OK')

+ 18 - 0
test/python/builtins/utf8.py

@@ -0,0 +1,18 @@
+import PikaStdLib
+import PikaStdData as std
+import re
+
+s2 = std.String('你好,Hello, Bonjour.')
+
+i = 0
+for c in s2:
+    print(i, c)
+    i += 1
+print(s2[2])
+s2[2] = ','
+print(s2)
+s2[2] = ','
+print(s2)
+print(s2.replace('你好,', 'Hi, '))
+
+