You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_invert.py 2.1 kB

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