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 MemberNotIn(nn.Cell): 27 def __init__(self): 28 super(MemberNotIn, self).__init__() 29 self.m = 1 30 31 def construct(self, x, y): 32 not_in_v = x not in y 33 return not_in_v 34 35 36class MemberNotInSpec(nn.Cell): 37 def __init__(self, x, y): 38 super(MemberNotInSpec, self).__init__() 39 self.x = x 40 self.y = y 41 42 def construct(self, x, y): 43 not_in_v = self.x not in self.y 44 return not_in_v 45 46 47def test_ms_syntax_operator_int_not_in_int(): 48 net = MemberNotIn() 49 ret = net(1, 2) 50 print(ret) 51 52 53def test_ms_syntax_operator_int_not_in_list_int(): 54 net = MemberNotIn() 55 ret = net(1, [1, 2]) 56 print(ret) 57 58 59def test_ms_syntax_operator_int_not_in_list_str(): 60 net = MemberNotInSpec(1, ["1", "2"]) 61 ret = net(1, 2) 62 print(ret) 63 64 65def test_ms_syntax_operator_int_not_in_list_combine(): 66 net = MemberNotInSpec(1, ["1", 2]) 67 ret = net(1, 2) 68 print(ret) 69 70 71def test_ms_syntax_operator_int_not_in_tuple_int(): 72 net = MemberNotIn() 73 ret = net(1, (1, 2)) 74 print(ret) 75 76 77def test_ms_syntax_operator_int_not_in_tuple_str(): 78 net = MemberNotInSpec(1, ("1", 2)) 79 ret = net(1, 2) 80 print(ret) 81 82 83def test_ms_syntax_operator_int_not_in_dict_int(): 84 dict_y = {1: 2, 2: 3} 85 net = MemberNotInSpec(1, dict_y) 86 ret = net(1, 2) 87 print(ret) 88 89 90def test_ms_syntax_operator_int_not_in_dict_str(): 91 dict_y = {"1": 2, "2": 3} 92 net = MemberNotInSpec(1, dict_y) 93 ret = net(1, 2) 94 print(ret) 95 96 97def test_ms_syntax_operator_str_not_in_dict_str(): 98 dict_y = {"1": 2, "2": 3} 99 net = MemberNotInSpec("1", dict_y) 100 ret = net(1, 2) 101 print(ret) 102 103 104def test_ms_syntax_operator_str_not_in_dict_combine(): 105 dict_y = {"1": 2, 2: 3} 106 net = MemberNotInSpec("1", dict_y) 107 ret = net(1, 2) 108 print(ret) 109 110 111def test_ms_syntax_operator_tensor_not_in_list_tensor(): 112 net = MemberNotIn() 113 x = Tensor(np.ones([2, 2], np.int32)) 114 y = Tensor(np.zeros([2, 2], np.int32)) 115 ret = net(x, [x, y]) 116 print(ret) 117 118 119def test_ms_syntax_operator_tensor_not_in_list_combine(): 120 x = Tensor(np.ones([2, 2], np.int32)) 121 y = Tensor(np.zeros([2, 2], np.int32)) 122 net = MemberNotInSpec(x, [y, "a"]) 123 ret = net(1, 2) 124 print(ret) 125 126 127def test_ms_syntax_operator_tensor_not_in_tuple_tensor(): 128 net = MemberNotIn() 129 x = Tensor(np.ones([2, 2], np.int32)) 130 y = Tensor(np.zeros([2, 2], np.int32)) 131 ret = net(x, (x, y)) 132 print(ret) 133 134 135def test_ms_syntax_operator_tensor_not_in_tuple_combine(): 136 x = Tensor(np.ones([2, 2], np.int32)) 137 net = MemberNotInSpec(x, (x, "a")) 138 ret = net(1, 2) 139 print(ret) 140