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_and.py 3.7 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 LogicAnd(nn.Cell):
  22. def __init__(self):
  23. super(LogicAnd, self).__init__()
  24. self.m = 1
  25. def construct(self, x, y):
  26. and_v = x and y
  27. return and_v
  28. class LogicAndSpec(nn.Cell):
  29. def __init__(self, x, y):
  30. super(LogicAndSpec, self).__init__()
  31. self.x = x
  32. self.y = y
  33. def construct(self, x, y):
  34. and_v = self.x and self.y
  35. return and_v
  36. def test_ms_syntax_operator_logic_int_and_int():
  37. net = LogicAnd()
  38. ret = net(1, 2)
  39. print(ret)
  40. def test_ms_syntax_operator_logic_float_and_float():
  41. net = LogicAnd()
  42. ret = net(1.89, 1.99)
  43. print(ret)
  44. def test_ms_syntax_operator_logic_float_and_int():
  45. net = LogicAnd()
  46. ret = net(1.89, 1)
  47. print(ret)
  48. def test_ms_syntax_operator_logic_tensor_1_int_and_tensor_1_int():
  49. net = LogicAnd()
  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_and_tensor_1_int():
  55. net = LogicAnd()
  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_1_int_and_int():
  61. net = LogicAnd()
  62. x = Tensor(np.ones([1], np.int32))
  63. y = 2
  64. ret = net(x, y)
  65. print(ret)
  66. def test_ms_syntax_operator_logic_tensor_2X2_int_and_tensor_2X2_int():
  67. net = LogicAnd()
  68. x = Tensor(np.ones([2, 2], np.int32))
  69. y = Tensor(np.zeros([2, 2], np.int32))
  70. ret = net(x, y)
  71. print(ret)
  72. def test_ms_syntax_operator_logic_int_and_str():
  73. net = LogicAnd()
  74. ret = net(1, "cba")
  75. print(ret)
  76. def test_ms_syntax_operator_logic_int_and_str_2():
  77. net = LogicAndSpec(1, "cba")
  78. ret = net(1, 2)
  79. print(ret)
  80. def test_ms_syntax_operator_logic_str_and_str():
  81. net = LogicAndSpec("abc", "cba")
  82. ret = net(1, 2)
  83. print(ret)
  84. def test_ms_syntax_operator_logic_list_int_and_list_int():
  85. net = LogicAnd()
  86. ret = net([1, 2, 3], [3, 2, 1])
  87. print(ret)
  88. def test_ms_syntax_operator_logic_list_int_and_int():
  89. net = LogicAnd()
  90. ret = net([1, 2, 3], 1)
  91. print(ret)
  92. def test_ms_syntax_operator_logic_list_int_and_str():
  93. net = LogicAndSpec([1, 2, 3], "aaa")
  94. ret = net(1, 2)
  95. print(ret)
  96. def test_ms_syntax_operator_logic_list_int_and_list_str():
  97. net = LogicAndSpec([1, 2, 3], ["1", "2", "3"])
  98. ret = net(1, 2)
  99. print(ret)
  100. def test_ms_syntax_operator_logic_list_int_and_list_str_var():
  101. left = [1, 2, 3]
  102. right = ["1", "2", "3"]
  103. net = LogicAndSpec(left, right)
  104. ret = net(1, 2)
  105. print(ret)
  106. def test_ms_syntax_operator_logic_list_str_and_tensor_int():
  107. left = ["1", "2", "3"]
  108. right = Tensor(np.ones([2, 2], np.int32))
  109. net = LogicAndSpec(left, right)
  110. ret = net(1, 2)
  111. print(ret)