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_member_in.py 3.4 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 MemberIn(nn.Cell):
  22. def __init__(self):
  23. super(MemberIn, self).__init__()
  24. self.m = 1
  25. def construct(self, x, y):
  26. in_v = x in y
  27. return in_v
  28. class MemberInSpec(nn.Cell):
  29. def __init__(self, x, y):
  30. super(MemberInSpec, self).__init__()
  31. self.x = x
  32. self.y = y
  33. def construct(self, x, y):
  34. in_v = self.x in self.y
  35. return in_v
  36. def test_ms_syntax_operator_int_in_int():
  37. net = MemberIn()
  38. ret = net(1, 2)
  39. print(ret)
  40. def test_ms_syntax_operator_int_in_list_int():
  41. net = MemberIn()
  42. ret = net(1, [1, 2])
  43. print(ret)
  44. def test_ms_syntax_operator_int_in_list_str():
  45. net = MemberInSpec(1, ["1", "2"])
  46. ret = net(1, 2)
  47. print(ret)
  48. def test_ms_syntax_operator_int_in_list_combine():
  49. net = MemberInSpec(1, ["1", 2])
  50. ret = net(1, 2)
  51. print(ret)
  52. def test_ms_syntax_operator_int_in_tuple_int():
  53. net = MemberIn()
  54. ret = net(1, (1, 2))
  55. print(ret)
  56. def test_ms_syntax_operator_int_in_tuple_str():
  57. net = MemberInSpec(1, ("1", 2))
  58. ret = net(1, 2)
  59. print(ret)
  60. def test_ms_syntax_operator_int_in_dict_str():
  61. dict_y = {"1": 2, "2": 3}
  62. net = MemberInSpec(1, dict_y)
  63. ret = net(1, 2)
  64. print(ret)
  65. def test_ms_syntax_operator_str_in_dict_str():
  66. dict_y = {"1": 2, "2": 3}
  67. net = MemberInSpec("1", dict_y)
  68. ret = net(1, 2)
  69. print(ret)
  70. def test_ms_syntax_operator_str_in_dict_combine():
  71. dict_y = {"1": 2, 2: 3}
  72. net = MemberInSpec("1", dict_y)
  73. ret = net(1, 2)
  74. print(ret)
  75. def test_ms_syntax_operator_int_in_dict_combine():
  76. dict_y = {"1": 2, 2: 3}
  77. net = MemberInSpec(1, dict_y)
  78. ret = net(1, 2)
  79. print(ret)
  80. def test_ms_syntax_operator_tensor_in_list_tensor():
  81. net = MemberIn()
  82. x = Tensor(np.ones([2, 2], np.int32))
  83. y = Tensor(np.zeros([2, 2], np.int32))
  84. ret = net(x, [x, y])
  85. print(ret)
  86. def test_ms_syntax_operator_tensor_in_list_combine():
  87. x = Tensor(np.ones([2, 2], np.int32))
  88. y = Tensor(np.zeros([2, 2], np.int32))
  89. net = MemberInSpec(x, [y, "a"])
  90. ret = net(1, 2)
  91. print(ret)
  92. def test_ms_syntax_operator_tensor_in_tuple_tensor():
  93. net = MemberIn()
  94. x = Tensor(np.ones([2, 2], np.int32))
  95. y = Tensor(np.zeros([2, 2], np.int32))
  96. ret = net(x, (x, y))
  97. print(ret)
  98. def test_ms_syntax_operator_tensor_in_tuple_combine():
  99. x = Tensor(np.ones([2, 2], np.int32))
  100. net = MemberInSpec(x, (x, "a"))
  101. ret = net(1, 2)
  102. print(ret)