• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15import pytest
16import mindspore.context as context
17from mindspore import Tensor, ms_function
18from mindspore.common import dtype as mstype
19
20
21@ms_function
22def hof(x):
23    def f(x):
24        return x + 3
25
26    def k(x):
27        return x - 1
28
29    def g(x):
30        if x < 5:
31            return f
32        return k
33
34    ret = g(x)(x)
35    return ret
36
37@pytest.mark.level0
38@pytest.mark.platform_arm_ascend_training
39@pytest.mark.platform_x86_ascend_training
40@pytest.mark.env_onecard
41def test_fun_fun():
42    context.set_context(mode=context.GRAPH_MODE)
43    x = Tensor([10], mstype.int32)
44    ret = hof(x)
45    expect = Tensor([9], mstype.int32)
46    assert ret == expect
47
48
49if __name__ == "__main__":
50    test_fun_fun()
51