• 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# ============================================================================
15from mindspore import Tensor, jit
16from mindspore.common import dtype as mstype
17import pytest
18
19
20@jit
21def t1_while(x, y):
22    y = y + 4
23    while x < y:
24        x = x + 1
25    x = x + 3
26    return x
27
28
29@jit
30def const_branch(y):
31    if y >= 0:
32        while y > 1:
33            y -= 1
34        return y
35    return 2
36
37
38def test_const_branch():
39    """
40    Feature: control flow .
41    Description: Set one branch abstract with the other branch type
42    when all the branches can not be inferred.
43    Expectation: No error raised.
44    """
45    y = Tensor(5)
46    with pytest.raises(TypeError) as exc:
47        with const_branch(y):
48            pass
49
50    assert "join" in str(exc.value)
51
52
53def test_net():
54    c1 = Tensor([2], mstype.int32)
55    c2 = Tensor([14], mstype.int32)
56    expect = Tensor([21], mstype.int32)
57    ret = t1_while(c1, c2)
58    assert ret == expect
59
60
61if __name__ == "__main__":
62    test_net()
63