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 17import numpy as np 18 19import mindspore.nn as nn 20from mindspore import context 21from mindspore.common.tensor import Tensor 22 23context.set_context(mode=context.GRAPH_MODE) 24 25 26class MemberIn(nn.Cell): 27 def __init__(self): 28 super(MemberIn, self).__init__() 29 self.m = 1 30 31 def construct(self, x, y): 32 in_v = x in y 33 return in_v 34 35 36class MemberInSpec(nn.Cell): 37 def __init__(self, x, y): 38 super(MemberInSpec, self).__init__() 39 self.x = x 40 self.y = y 41 42 43 def construct(self, x, y): 44 in_v = self.x in self.y 45 return in_v 46 47 48def test_ms_syntax_operator_int_in_int(): 49 net = MemberIn() 50 ret = net(1, 2) 51 print(ret) 52 53 54def test_ms_syntax_operator_int_in_list_int(): 55 net = MemberIn() 56 ret = net(1, [1, 2]) 57 print(ret) 58 59 60def test_ms_syntax_operator_int_in_list_str(): 61 net = MemberInSpec(1, ["1", "2"]) 62 ret = net(1, 2) 63 print(ret) 64 65 66def test_ms_syntax_operator_int_in_list_combine(): 67 net = MemberInSpec(1, ["1", 2]) 68 ret = net(1, 2) 69 print(ret) 70 71 72def test_ms_syntax_operator_int_in_tuple_int(): 73 net = MemberIn() 74 ret = net(1, (1, 2)) 75 print(ret) 76 77 78def test_ms_syntax_operator_int_in_tuple_str(): 79 net = MemberInSpec(1, ("1", 2)) 80 ret = net(1, 2) 81 print(ret) 82 83 84def test_ms_syntax_operator_int_in_dict_str(): 85 dict_y = {"1": 2, "2": 3} 86 net = MemberInSpec(1, dict_y) 87 ret = net(1, 2) 88 print(ret) 89 90 91def test_ms_syntax_operator_str_in_dict_str(): 92 dict_y = {"1": 2, "2": 3} 93 net = MemberInSpec("1", dict_y) 94 ret = net(1, 2) 95 print(ret) 96 97 98def test_ms_syntax_operator_str_in_dict_combine(): 99 dict_y = {"1": 2, 2: 3} 100 net = MemberInSpec("1", dict_y) 101 ret = net(1, 2) 102 print(ret) 103 104 105def test_ms_syntax_operator_int_in_dict_combine(): 106 dict_y = {"1": 2, 2: 3} 107 net = MemberInSpec(1, dict_y) 108 ret = net(1, 2) 109 print(ret) 110 111 112def test_ms_syntax_operator_tensor_in_list_tensor(): 113 net = MemberIn() 114 x = Tensor(np.ones([2, 2], np.int32)) 115 y = Tensor(np.zeros([2, 2], np.int32)) 116 ret = net(x, [x, y]) 117 print(ret) 118 119 120def test_ms_syntax_operator_tensor_in_list_combine(): 121 x = Tensor(np.ones([2, 2], np.int32)) 122 y = Tensor(np.zeros([2, 2], np.int32)) 123 net = MemberInSpec(x, [y, "a"]) 124 ret = net(1, 2) 125 print(ret) 126 127 128def test_ms_syntax_operator_tensor_in_tuple_tensor(): 129 net = MemberIn() 130 x = Tensor(np.ones([2, 2], np.int32)) 131 y = Tensor(np.zeros([2, 2], np.int32)) 132 ret = net(x, (x, y)) 133 print(ret) 134 135 136def test_ms_syntax_operator_tensor_in_tuple_combine(): 137 x = Tensor(np.ones([2, 2], np.int32)) 138 net = MemberInSpec(x, (x, "a")) 139 ret = net(1, 2) 140 print(ret) 141