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_logic_or.py 3.3 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 syntax for logic expression """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. from mindspore import context
  19. from mindspore.common.tensor import Tensor
  20. context.set_context(mode=context.GRAPH_MODE)
  21. class LogicOr(nn.Cell):
  22. def __init__(self):
  23. super(LogicOr, self).__init__()
  24. self.m = 1
  25. def construct(self, x, y):
  26. or_v = x or y
  27. return or_v
  28. class LogicOrSpec(nn.Cell):
  29. def __init__(self, x, y):
  30. super(LogicOrSpec, self).__init__()
  31. self.x = x
  32. self.y = y
  33. def construct(self, x, y):
  34. or_v = self.x or self.y
  35. return or_v
  36. def test_ms_syntax_operator_logic_int_or_int():
  37. net = LogicOr()
  38. ret = net(1, 2)
  39. print(ret)
  40. def test_ms_syntax_operator_logic_float_or_float():
  41. net = LogicOr()
  42. ret = net(1.89, 1.99)
  43. print(ret)
  44. def test_ms_syntax_operator_logic_float_or_int():
  45. net = LogicOr()
  46. ret = net(1.89, 1)
  47. print(ret)
  48. def test_ms_syntax_operator_logic_tensor_1_int_or_tensor_1_int():
  49. net = LogicOr()
  50. x = Tensor(np.ones([1], np.int32))
  51. y = Tensor(np.zeros([1], np.int32))
  52. ret = net(x, y)
  53. print(ret)
  54. def test_ms_syntax_operator_logic_tensor_1_float_or_tensor_1_int():
  55. net = LogicOr()
  56. x = Tensor(np.ones([1], np.float))
  57. y = Tensor(np.zeros([1], np.int32))
  58. ret = net(x, y)
  59. print(ret)
  60. def test_ms_syntax_operator_logic_tensor_2X2_int_or_tensor_2X2_int():
  61. net = LogicOr()
  62. x = Tensor(np.ones([2, 2], np.int32))
  63. y = Tensor(np.zeros([2, 2], np.int32))
  64. ret = net(x, y)
  65. print(ret)
  66. def test_ms_syntax_operator_logic_int_or_str():
  67. net = LogicOr()
  68. ret = net(1, "cba")
  69. print(ret)
  70. def test_ms_syntax_operator_logic_int_or_str_2():
  71. net = LogicOrSpec(1, "cba")
  72. ret = net(1, 2)
  73. print(ret)
  74. def test_ms_syntax_operator_logic_str_or_str():
  75. net = LogicOrSpec("abc", "cba")
  76. ret = net(1, 2)
  77. print(ret)
  78. def test_ms_syntax_operator_logic_list_int_or_list_int():
  79. net = LogicOr()
  80. ret = net([1, 2, 3], [3, 2, 1])
  81. print(ret)
  82. def test_ms_syntax_operator_logic_list_int_or_int():
  83. net = LogicOr()
  84. ret = net([1, 2, 3], 1)
  85. print(ret)
  86. def test_ms_syntax_operator_logic_list_int_or_str():
  87. net = LogicOrSpec([1, 2, 3], "aaa")
  88. ret = net(1, 2)
  89. print(ret)
  90. def test_ms_syntax_operator_logic_list_int_or_list_str():
  91. net = LogicOrSpec([1, 2, 3], ["1", "2", "3"])
  92. ret = net(1, 2)
  93. print(ret)
  94. def test_ms_syntax_operator_logic_list_int_or_list_str_var():
  95. left = [1, 2, 3]
  96. right = ["1", "2", "3"]
  97. net = LogicOrSpec(left, right)
  98. ret = net(1, 2)
  99. print(ret)