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 LogicAnd(nn.Cell): 27 def __init__(self): 28 super(LogicAnd, self).__init__() 29 self.m = 1 30 31 def construct(self, x, y): 32 and_v = x and y 33 return and_v 34 35 36class LogicAndSpec(nn.Cell): 37 def __init__(self, x, y): 38 super(LogicAndSpec, self).__init__() 39 self.x = x 40 self.y = y 41 42 def construct(self, x, y): 43 and_v = self.x and self.y 44 return and_v 45 46 47def test_ms_syntax_operator_logic_int_and_int(): 48 net = LogicAnd() 49 ret = net(1, 2) 50 print(ret) 51 52 53def test_ms_syntax_operator_logic_float_and_float(): 54 net = LogicAnd() 55 ret = net(1.89, 1.99) 56 print(ret) 57 58 59def test_ms_syntax_operator_logic_float_and_int(): 60 net = LogicAnd() 61 ret = net(1.89, 1) 62 print(ret) 63 64 65def test_ms_syntax_operator_logic_tensor_1_int_and_tensor_1_int(): 66 net = LogicAnd() 67 x = Tensor(np.ones([1], np.int32)) 68 y = Tensor(np.zeros([1], np.int32)) 69 ret = net(x, y) 70 print(ret) 71 72 73def test_ms_syntax_operator_logic_tensor_1_float_and_tensor_1_int(): 74 net = LogicAnd() 75 x = Tensor(np.ones([1], np.float)) 76 y = Tensor(np.zeros([1], np.int32)) 77 ret = net(x, y) 78 print(ret) 79 80 81def test_ms_syntax_operator_logic_tensor_1_int_and_int(): 82 net = LogicAnd() 83 x = Tensor(np.ones([1], np.int32)) 84 y = 2 85 ret = net(x, y) 86 print(ret) 87 88 89def test_ms_syntax_operator_logic_tensor_2X2_int_and_tensor_2X2_int(): 90 net = LogicAnd() 91 x = Tensor(np.ones([2, 2], np.int32)) 92 y = Tensor(np.zeros([2, 2], np.int32)) 93 ret = net(x, y) 94 print(ret) 95 96 97def test_ms_syntax_operator_logic_int_and_str(): 98 net = LogicAnd() 99 ret = net(1, "cba") 100 print(ret) 101 102 103def test_ms_syntax_operator_logic_int_and_str_2(): 104 net = LogicAndSpec(1, "cba") 105 ret = net(1, 2) 106 print(ret) 107 108 109def test_ms_syntax_operator_logic_str_and_str(): 110 net = LogicAndSpec("abc", "cba") 111 ret = net(1, 2) 112 print(ret) 113 114 115def test_ms_syntax_operator_logic_list_int_and_list_int(): 116 net = LogicAnd() 117 ret = net([1, 2, 3], [3, 2, 1]) 118 print(ret) 119 120 121def test_ms_syntax_operator_logic_list_int_and_int(): 122 net = LogicAnd() 123 ret = net([1, 2, 3], 1) 124 print(ret) 125 126 127def test_ms_syntax_operator_logic_list_int_and_str(): 128 net = LogicAndSpec([1, 2, 3], "aaa") 129 ret = net(1, 2) 130 print(ret) 131 132 133def test_ms_syntax_operator_logic_list_int_and_list_str(): 134 net = LogicAndSpec([1, 2, 3], ["1", "2", "3"]) 135 ret = net(1, 2) 136 print(ret) 137 138 139def test_ms_syntax_operator_logic_list_int_and_list_str_var(): 140 left = [1, 2, 3] 141 right = ["1", "2", "3"] 142 net = LogicAndSpec(left, right) 143 ret = net(1, 2) 144 print(ret) 145 146 147def test_ms_syntax_operator_logic_list_str_and_tensor_int(): 148 left = ["1", "2", "3"] 149 right = Tensor(np.ones([2, 2], np.int32)) 150 net = LogicAndSpec(left, right) 151 ret = net(1, 2) 152 print(ret) 153