mindspore/tests/ut/python/python_builtin/test_bool.py

188 lines
4.3 KiB
Python
Raw Normal View History

2022-08-26 15:17:46 +08:00
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test graph fallback buildin python function bool"""
import pytest
import numpy as np
from mindspore import ms_function, context, Tensor
context.set_context(mode=context.GRAPH_MODE)
def test_fallback_bool_with_input_tensor():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with tensor input.
Expectation: No exception.
"""
@ms_function
def foo(x):
return bool(x)
with pytest.raises(ValueError) as ex:
foo(Tensor([1, 2, 4]))
assert "The truth value of an array with" in str(ex.value)
def test_fallback_bool_with_input_tensor_3():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with tensor input.
Expectation: No exception.
"""
@ms_function
def foo():
x = Tensor([0])
return bool(x)
assert not foo()
def test_fallback_bool_with_input_tensor_4():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with tensor input.
Expectation: No exception.
"""
@ms_function
def foo():
x = Tensor([1, 2, 3])
return bool(x)
with pytest.raises(ValueError) as ex:
foo()
assert "The truth value of an array with" in str(ex.value)
def test_fallback_bool_with_input_scalar():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with scalar input.
Expectation: No exception.
"""
@ms_function
def foo():
return bool(10.0)
assert foo()
def test_fallback_bool_with_input_list():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with list input.
Expectation: No exception.
"""
@ms_function
def foo():
x = [1, 2, 3]
return bool(x)
assert foo()
def test_fallback_bool_with_input_list_2():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with list input.
Expectation: No exception.
"""
@ms_function
def foo(a):
x = [1, 2, 3, a]
return bool(x)
assert foo(Tensor([1, 2, 3]))
def test_fallback_bool_with_input_string():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with string input.
Expectation: No exception.
"""
@ms_function
def foo():
return bool("test")
assert foo()
def test_fallback_bool_with_input_string_2():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with string input.
Expectation: No exception.
"""
@ms_function
def foo():
return bool("")
assert not foo()
def test_fallback_bool_with_input_numpy():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with numpy input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2, 3, 4])
return bool(x)
with pytest.raises(ValueError) as ex:
foo()
assert "The truth value of an array" in str(ex.value)
def test_fallback_bool_with_input_numpy_2():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with numpy input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1,])
return bool(x)
assert foo()
def test_fallback_bool_with_no_input():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with no input.
Expectation: No exception.
"""
@ms_function
def foo():
return bool()
assert not foo()
def test_fallback_bool_with_type_input():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with type input.
Expectation: No exception.
"""
@ms_function
def foo():
return bool(int)
assert foo()