2020-08-04 22:50:40 +08:00
|
|
|
import pytest
|
2019-10-29 17:35:04 -04:00
|
|
|
|
2021-06-23 17:12:25 +08:00
|
|
|
import taichi as ti
|
2022-02-10 12:37:36 +08:00
|
|
|
from tests import test_utils
|
2021-06-23 17:12:25 +08:00
|
|
|
|
2019-12-16 07:58:06 -05:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2019-10-29 17:35:04 -04:00
|
|
|
def test_try():
|
2020-08-13 11:24:48 +08:00
|
|
|
x = ti.field(ti.f32)
|
2019-10-29 17:35:04 -04:00
|
|
|
|
2020-05-31 16:55:38 -04:00
|
|
|
ti.root.dense(ti.i, 1).place(x)
|
2019-10-29 17:35:04 -04:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
try:
|
|
|
|
|
a = 0
|
|
|
|
|
except:
|
|
|
|
|
a = 1
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2019-10-29 17:38:33 -04:00
|
|
|
|
2019-12-16 07:58:06 -05:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2019-10-29 17:38:33 -04:00
|
|
|
def test_for_else():
|
2020-08-13 11:24:48 +08:00
|
|
|
x = ti.field(ti.f32)
|
2019-10-29 17:38:33 -04:00
|
|
|
|
2020-05-31 16:55:38 -04:00
|
|
|
ti.root.dense(ti.i, 1).place(x)
|
2019-10-29 17:38:33 -04:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
for i in range(10):
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2019-10-29 17:38:33 -04:00
|
|
|
|
2019-12-16 07:58:06 -05:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2019-10-29 17:38:33 -04:00
|
|
|
def test_while_else():
|
2020-08-13 11:24:48 +08:00
|
|
|
x = ti.field(ti.f32)
|
2019-10-29 17:38:33 -04:00
|
|
|
|
2020-05-31 16:55:38 -04:00
|
|
|
ti.root.dense(ti.i, 1).place(x)
|
2019-10-29 17:38:33 -04:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
while True:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2019-10-29 17:38:33 -04:00
|
|
|
|
2019-12-16 07:58:06 -05:00
|
|
|
|
2022-02-14 18:34:43 +08:00
|
|
|
@test_utils.test()
|
|
|
|
|
def test_raise():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo():
|
|
|
|
|
raise Exception()
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match='Unsupported node "Raise"') as e:
|
|
|
|
|
foo()
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2019-10-29 17:49:43 -04:00
|
|
|
def test_loop_var_range():
|
2020-08-13 11:24:48 +08:00
|
|
|
x = ti.field(ti.f32)
|
2019-10-29 17:49:43 -04:00
|
|
|
|
2020-05-31 16:55:38 -04:00
|
|
|
ti.root.dense(ti.i, 1).place(x)
|
2019-10-29 17:49:43 -04:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
i = 0
|
|
|
|
|
for i in range(10):
|
|
|
|
|
pass
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2019-10-29 17:49:43 -04:00
|
|
|
|
2019-12-16 07:58:06 -05:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2019-10-29 17:49:43 -04:00
|
|
|
def test_loop_var_struct():
|
2020-08-13 11:24:48 +08:00
|
|
|
x = ti.field(ti.f32)
|
2019-10-29 17:49:43 -04:00
|
|
|
|
2020-05-31 16:55:38 -04:00
|
|
|
ti.root.dense(ti.i, 1).place(x)
|
2019-10-29 17:49:43 -04:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
i = 0
|
|
|
|
|
for i in x:
|
|
|
|
|
pass
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2019-10-29 17:49:43 -04:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2019-10-29 17:49:43 -04:00
|
|
|
def test_loop_var_struct():
|
2020-08-13 11:24:48 +08:00
|
|
|
x = ti.field(ti.f32)
|
2019-10-29 17:49:43 -04:00
|
|
|
|
2020-05-31 16:55:38 -04:00
|
|
|
ti.root.dense(ti.i, 1).place(x)
|
2019-10-29 17:49:43 -04:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
j = 0
|
|
|
|
|
for i, j in x:
|
|
|
|
|
pass
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2019-10-29 17:49:43 -04:00
|
|
|
|
2019-10-29 17:53:30 -04:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-02-27 22:50:13 -05:00
|
|
|
def test_func_def_in_kernel():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def kernel():
|
2020-03-18 21:30:23 -04:00
|
|
|
@ti.func
|
2020-02-27 22:50:13 -05:00
|
|
|
def func():
|
2020-03-18 21:30:23 -04:00
|
|
|
return 1
|
2020-02-27 22:50:13 -05:00
|
|
|
|
|
|
|
|
print(func())
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
kernel()
|
2020-02-27 22:50:13 -05:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-02-27 22:50:13 -05:00
|
|
|
def test_func_def_in_func():
|
|
|
|
|
@ti.func
|
|
|
|
|
def func():
|
2020-03-18 21:30:23 -04:00
|
|
|
@ti.func
|
2020-02-27 22:50:13 -05:00
|
|
|
def func2():
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
return func2()
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def kernel():
|
|
|
|
|
print(func())
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
kernel()
|
2020-03-20 22:11:54 -04:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=ti.cpu)
|
2020-08-04 22:50:40 +08:00
|
|
|
def test_kernel_bad_argument_annotation():
|
2022-01-28 11:16:32 +08:00
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match="annotation"):
|
2020-08-07 21:02:15 -07:00
|
|
|
|
2020-08-04 22:50:40 +08:00
|
|
|
@ti.kernel
|
|
|
|
|
def kernel(x: "bar"):
|
|
|
|
|
print(x)
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=ti.cpu)
|
2020-08-04 22:50:40 +08:00
|
|
|
def test_func_bad_argument_annotation():
|
2022-01-28 11:16:32 +08:00
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match="annotation"):
|
2020-08-07 21:02:15 -07:00
|
|
|
|
2020-08-04 22:50:40 +08:00
|
|
|
@ti.func
|
|
|
|
|
def func(x: "foo"):
|
|
|
|
|
print(x)
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-03-20 22:11:54 -04:00
|
|
|
def test_nested_static():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
for i in ti.static(ti.static(range(1))):
|
|
|
|
|
pass
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2020-03-22 04:24:31 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-03-20 22:11:54 -04:00
|
|
|
def test_nested_grouped():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
for i in ti.grouped(ti.grouped(range(1))):
|
|
|
|
|
pass
|
2020-03-22 04:24:31 +08:00
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2020-03-22 04:24:31 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-03-20 22:11:54 -04:00
|
|
|
def test_nested_ndrange():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
for i in ti.ndrange(ti.ndrange(1)):
|
|
|
|
|
pass
|
2020-03-22 04:24:31 +08:00
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2020-03-24 20:51:44 -04:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-03-24 20:51:44 -04:00
|
|
|
def test_static_grouped_struct_for():
|
2020-08-13 11:24:48 +08:00
|
|
|
val = ti.field(ti.i32)
|
2020-03-24 20:51:44 -04:00
|
|
|
|
|
|
|
|
ti.root.dense(ti.ij, (1, 1)).place(val)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test():
|
|
|
|
|
for I in ti.static(ti.grouped(val)):
|
|
|
|
|
pass
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
test()
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-07-04 12:03:30 +08:00
|
|
|
def test_is():
|
2020-08-13 11:24:48 +08:00
|
|
|
b = ti.field(ti.i32, shape=())
|
|
|
|
|
c = ti.field(ti.i32, shape=())
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
a = b is c
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-07-04 12:03:30 +08:00
|
|
|
def test_is_not():
|
2020-08-13 11:24:48 +08:00
|
|
|
b = ti.field(ti.i32, shape=())
|
|
|
|
|
c = ti.field(ti.i32, shape=())
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
a = b is not c
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-07-04 12:03:30 +08:00
|
|
|
def test_in():
|
2020-08-13 11:24:48 +08:00
|
|
|
b = ti.field(ti.i32, shape=())
|
|
|
|
|
c = ti.field(ti.i32, shape=())
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
a = b in c
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2020-07-04 12:03:30 +08:00
|
|
|
def test_not_in():
|
2020-08-13 11:24:48 +08:00
|
|
|
b = ti.field(ti.i32, shape=())
|
|
|
|
|
c = ti.field(ti.i32, shape=())
|
2020-07-04 12:03:30 +08:00
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
a = b not in c
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
2020-09-08 20:38:55 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
[Refactor] Split transformer.py into StmtBuilder and ExprBuilder (Stage 1) (#2495)
* ExprBuilder: subscript, call, name, compare, constant
* ExprBuilder: ifexp, unaryop, boolop, binop
* StmtBuilder: augassign, assert
* StmtBuilder: assign, try
* StmtBuilder: while, if, break, continue, expr
* Fix wrong indent and missing attributes
* StmtBuilder: for
* StmtBuilder: functiondef, module, global, import
* StmtBuilder: return
* Fix tests
* Revert export_lang.cpp
* Fix build_BinOp
* Fix List
* Fix List, Tuple, range for
* Fix Assert format runtime error
* Fix Attribute
* Fix ListComp, Ifs, add a test
* Fix nested subscript
* Fix Raise, Starred, and code format
* Fix scope
* Fix deleted parameters
* Try not insert_expr_stmt
* comment, code format
* Support Dict and DictComp
* Deprecate ASTTransformerPreprocess
* code format
* Remove ASTTransformerPreprocess
* code format
* minor fix
* fix typo
* Compatibility for Python 3.7
* Add support of Python set
* Remove support of Python set
* Fix Python 3.9
* Add support of ImportFrom and NamedExpr
* Not support nonlocal
* Change exception to TaichiSyntaxError to make it more user-friendly
* Remove namedexpr test
* [skip ci] Update python/taichi/lang/expr_builder.py
* Apply review
2021-07-29 19:53:56 +08:00
|
|
|
def test_expr_set():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func():
|
|
|
|
|
x = {2, 4, 6}
|
|
|
|
|
|
2021-12-24 19:50:48 +08:00
|
|
|
with pytest.raises(ti.TaichiCompilationError):
|
|
|
|
|
func()
|
[Refactor] Split transformer.py into StmtBuilder and ExprBuilder (Stage 1) (#2495)
* ExprBuilder: subscript, call, name, compare, constant
* ExprBuilder: ifexp, unaryop, boolop, binop
* StmtBuilder: augassign, assert
* StmtBuilder: assign, try
* StmtBuilder: while, if, break, continue, expr
* Fix wrong indent and missing attributes
* StmtBuilder: for
* StmtBuilder: functiondef, module, global, import
* StmtBuilder: return
* Fix tests
* Revert export_lang.cpp
* Fix build_BinOp
* Fix List
* Fix List, Tuple, range for
* Fix Assert format runtime error
* Fix Attribute
* Fix ListComp, Ifs, add a test
* Fix nested subscript
* Fix Raise, Starred, and code format
* Fix scope
* Fix deleted parameters
* Try not insert_expr_stmt
* comment, code format
* Support Dict and DictComp
* Deprecate ASTTransformerPreprocess
* code format
* Remove ASTTransformerPreprocess
* code format
* minor fix
* fix typo
* Compatibility for Python 3.7
* Add support of Python set
* Remove support of Python set
* Fix Python 3.9
* Add support of ImportFrom and NamedExpr
* Not support nonlocal
* Change exception to TaichiSyntaxError to make it more user-friendly
* Remove namedexpr test
* [skip ci] Update python/taichi/lang/expr_builder.py
* Apply review
2021-07-29 19:53:56 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2022-01-21 10:04:57 +08:00
|
|
|
def test_redefining_template_args():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(a: ti.template()):
|
|
|
|
|
a = 5
|
|
|
|
|
|
[Lang] Make scalar kernel arguments immutable (#5990)
Related issue = fixes #5619
<!--
Thank you for your contribution!
If it is your first time contributing to Taichi, please read our
Contributor Guidelines:
https://docs.taichi-lang.org/docs/contributor_guide
- Please always prepend your PR title with tags such as [CUDA], [Lang],
[Doc], [Example]. For a complete list of valid PR tags, please check out
https://github.com/taichi-dev/taichi/blob/master/misc/prtags.json.
- Use upper-case tags (e.g., [Metal]) for PRs that change public APIs.
Otherwise, please use lower-case tags (e.g., [metal]).
- More details:
https://docs.taichi-lang.org/docs/contributor_guide#pr-title-format-and-tags
- Please fill in the issue number that this PR relates to.
- If your PR fixes the issue **completely**, use the `close` or `fixes`
prefix so that GitHub automatically closes the issue when the PR is
merged. For example,
Related issue = close #2345
- If the PR does not belong to any existing issue, free to leave it
blank.
-->
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-09-13 18:16:55 +08:00
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match='Kernel argument "a" is immutable in the kernel'):
|
2022-01-21 10:04:57 +08:00
|
|
|
foo(1)
|
2022-02-09 17:40:36 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test()
|
2022-02-09 17:40:36 +08:00
|
|
|
def test_break_in_outermost_for():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo():
|
|
|
|
|
for i in range(10):
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match="Cannot break in the outermost loop"):
|
|
|
|
|
foo()
|
2022-02-15 08:33:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test()
|
|
|
|
|
def test_funcdef_in_kernel():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo():
|
|
|
|
|
def bar():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match="Function definition is not allowed in 'ti.kernel'"):
|
|
|
|
|
foo()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test()
|
|
|
|
|
def test_funcdef_in_func():
|
|
|
|
|
@ti.func
|
|
|
|
|
def foo():
|
|
|
|
|
def bar():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def baz():
|
|
|
|
|
foo()
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match="Function definition is not allowed in 'ti.func'"):
|
|
|
|
|
baz()
|
2022-08-31 09:30:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test()
|
|
|
|
|
def test_continue_in_static_for_in_non_static_if():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test_static_loop():
|
|
|
|
|
for i in ti.static(range(5)):
|
|
|
|
|
x = 0.1
|
|
|
|
|
if x == 0.0:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match="You are trying to `continue` a static `for` loop"):
|
|
|
|
|
test_static_loop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test()
|
|
|
|
|
def test_break_in_static_for_in_non_static_if():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test_static_loop():
|
|
|
|
|
for i in ti.static(range(5)):
|
|
|
|
|
x = 0.1
|
|
|
|
|
if x == 0.0:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ti.TaichiSyntaxError, match="You are trying to `break` a static `for` loop"):
|
|
|
|
|
test_static_loop()
|