diff --git a/tests/test_19_closure.py b/tests/test_19_closure.py index 5d8e320c3..b644a8c8f 100644 --- a/tests/test_19_closure.py +++ b/tests/test_19_closure.py @@ -167,33 +167,24 @@ def closure(): return closure -class TestClosure(TestCaseBase): - def test_closure(self): - self.assert_results(foo, 1, paddle.to_tensor(2)) - self.assert_results(foo2, paddle.to_tensor(2)) - self.assert_results(foo3, paddle.to_tensor(2)) - self.assert_results(foo5, paddle.to_tensor(2)) - - def test_global(self): - self.assert_results_with_global_check( - test_global, ["global_z"], paddle.to_tensor(2) - ) - - def test_lambda(self): - with strict_mode_guard(0): - self.assert_results( - lambda_closure, paddle.to_tensor(2), paddle.to_tensor(1) - ) +def non_local_test(t: paddle.Tensor): + a = 1 - def test_numpy(self): - self.assert_results(numpy_sum, paddle.to_tensor(1)) + def func1(): + nonlocal a + t = a + a = 2 + return t - def test_del_deref(self): - self.assert_results(closure_del) + def func2(): + nonlocal a + a = 1 + return a - def test_decorator(self): - self.assert_results(test_builtin_decorator) - self.assert_results(foo6, paddle.to_tensor(2)) + t += func1() # add 2 + t += func2() # add 1 + t += a # add 1 + return t # Side Effect. @@ -214,40 +205,43 @@ def test_slice_in_for_loop(x, iter_num=3): return out -class TestExecutor3(TestCaseBase): +class TestClosure(TestCaseBase): def test_closure(self): - tx = paddle.to_tensor([1.0, 2.0, 3.0]) - # need side effect of list. - # self.assert_results(test_slice_in_for_loop, tx) - + self.assert_results(foo, 1, paddle.to_tensor(2)) + self.assert_results(foo2, paddle.to_tensor(2)) + self.assert_results(foo3, paddle.to_tensor(2)) + self.assert_results(foo5, paddle.to_tensor(2)) -def non_local_test(t: paddle.Tensor): - a = 1 + def test_global(self): + self.assert_results_with_global_check( + test_global, ["global_z"], paddle.to_tensor(2) + ) - def func1(): - nonlocal a - t = a - a = 2 - return t + def test_lambda(self): + with strict_mode_guard(0): + self.assert_results( + lambda_closure, paddle.to_tensor(2), paddle.to_tensor(1) + ) - def func2(): - nonlocal a - a = 1 - return a + def test_numpy(self): + self.assert_results(numpy_sum, paddle.to_tensor(1)) - t += func1() # add 2 - t += func2() # add 1 - t += a # add 1 - return t + def test_del_deref(self): + self.assert_results(closure_del) + def test_decorator(self): + self.assert_results(test_builtin_decorator) + self.assert_results(foo6, paddle.to_tensor(2)) -class TestExecutor4(TestCaseBase): - def test_closure(self): + def test_nolocal(self): tx = paddle.to_tensor([1.0]) self.assert_results(non_local_test, tx) + def test_side_effect(self): + tx = paddle.to_tensor([1.0, 2.0, 3.0]) + # need side effect of list. + self.assert_results_with_side_effects(test_slice_in_for_loop, tx) -class TestCreateClosure(TestCaseBase): def test_create_closure(self): closure = create_closure() self.assert_results(closure)