1# Copyright 2021 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# ============================================================================ 15""" test '~' """ 16import numpy as np 17import pytest 18 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore import context 22 23 24class InvertNet(nn.Cell): 25 def __init__(self): 26 super(InvertNet, self).__init__() 27 self.t = Tensor(np.array([True, False, True])) 28 29 def construct(self, x): 30 invert_t = ~self.t 31 invert_x = ~x 32 ret = (invert_t, invert_x) 33 return ret 34 35 36def test_invert_bool_tensor(): 37 net = InvertNet() 38 input_x = Tensor(np.array([False, True, False])) 39 40 context.set_context(mode=context.PYNATIVE_MODE) 41 ret = net(input_x) 42 assert (ret[0].asnumpy() == np.array([False, True, False])).all() 43 assert (ret[1].asnumpy() == np.array([True, False, True])).all() 44 45 context.set_context(mode=context.GRAPH_MODE) 46 net(input_x) 47 48 49def test_invert_int_tensor(): 50 net = InvertNet() 51 input_x = Tensor(np.array([1, 2, 3], np.int32)) 52 53 context.set_context(mode=context.PYNATIVE_MODE) 54 with pytest.raises(TypeError) as err: 55 net(input_x) 56 assert "For 'LogicalNot or '~' operator', the type of 'x' should be Tensor[Bool], " \ 57 "but got Tensor[Int32]" in str(err.value) 58 59 context.set_context(mode=context.GRAPH_MODE) 60 with pytest.raises(TypeError) as err: 61 net(input_x) 62 assert "For 'LogicalNot or '~' operator', the type of 'x' should be Tensor[Bool], " \ 63 "but got Tensor[Int32]" in str(err.value) 64