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 LogicOr(nn.Cell): 27 def __init__(self): 28 super(LogicOr, self).__init__() 29 self.m = 1 30 31 def construct(self, x, y): 32 or_v = x or y 33 return or_v 34 35 36class LogicOrSpec(nn.Cell): 37 def __init__(self, x, y): 38 super(LogicOrSpec, self).__init__() 39 self.x = x 40 self.y = y 41 42 def construct(self, x, y): 43 or_v = self.x or self.y 44 return or_v 45 46 47def test_ms_syntax_operator_logic_int_or_int(): 48 net = LogicOr() 49 ret = net(1, 2) 50 print(ret) 51 52 53def test_ms_syntax_operator_logic_float_or_float(): 54 net = LogicOr() 55 ret = net(1.89, 1.99) 56 print(ret) 57 58 59def test_ms_syntax_operator_logic_float_or_int(): 60 net = LogicOr() 61 ret = net(1.89, 1) 62 print(ret) 63 64 65def test_ms_syntax_operator_logic_tensor_1_int_or_tensor_1_int(): 66 net = LogicOr() 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_or_tensor_1_int(): 74 net = LogicOr() 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_2X2_int_or_tensor_2X2_int(): 82 net = LogicOr() 83 x = Tensor(np.ones([2, 2], np.int32)) 84 y = Tensor(np.zeros([2, 2], np.int32)) 85 ret = net(x, y) 86 print(ret) 87 88 89def test_ms_syntax_operator_logic_int_or_str(): 90 net = LogicOr() 91 ret = net(1, "cba") 92 print(ret) 93 94 95def test_ms_syntax_operator_logic_int_or_str_2(): 96 net = LogicOrSpec(1, "cba") 97 ret = net(1, 2) 98 print(ret) 99 100 101def test_ms_syntax_operator_logic_str_or_str(): 102 net = LogicOrSpec("abc", "cba") 103 ret = net(1, 2) 104 print(ret) 105 106 107def test_ms_syntax_operator_logic_list_int_or_list_int(): 108 net = LogicOr() 109 ret = net([1, 2, 3], [3, 2, 1]) 110 print(ret) 111 112 113def test_ms_syntax_operator_logic_list_int_or_int(): 114 net = LogicOr() 115 ret = net([1, 2, 3], 1) 116 print(ret) 117 118 119def test_ms_syntax_operator_logic_list_int_or_str(): 120 net = LogicOrSpec([1, 2, 3], "aaa") 121 ret = net(1, 2) 122 print(ret) 123 124 125def test_ms_syntax_operator_logic_list_int_or_list_str(): 126 net = LogicOrSpec([1, 2, 3], ["1", "2", "3"]) 127 ret = net(1, 2) 128 print(ret) 129 130 131def test_ms_syntax_operator_logic_list_int_or_list_str_var(): 132 left = [1, 2, 3] 133 right = ["1", "2", "3"] 134 net = LogicOrSpec(left, right) 135 ret = net(1, 2) 136 print(ret) 137