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 LogicNot(nn.Cell): 27 def __init__(self): 28 super(LogicNot, self).__init__() 29 self.m = 1 30 31 def construct(self, x): 32 not_v = not x 33 return not_v 34 35 36class LogicNotSpec(nn.Cell): 37 def __init__(self, x): 38 super(LogicNotSpec, self).__init__() 39 self.x = x 40 41 def construct(self, x): 42 not_v = not self.x 43 return not_v 44 45 46def test_ms_syntax_operator_logic_not_int(): 47 net = LogicNot() 48 ret = net(1) 49 print(ret) 50 51 52def test_ms_syntax_operator_logic_not_float(): 53 net = LogicNot() 54 ret = net(1.89) 55 print(ret) 56 57def test_ms_syntax_operator_logic_tensor_1_int(): 58 net = LogicNot() 59 x = Tensor(np.ones([1], np.int32)) 60 ret = net(x) 61 print(ret) 62 63def test_ms_syntax_operator_logic_not_tensor_1_float(): 64 net = LogicNot() 65 x = Tensor(np.ones([1], np.float)) 66 ret = net(x) 67 print(ret) 68 69 70def test_ms_syntax_operator_logic_not_tensor_2X2_int(): 71 net = LogicNot() 72 x = Tensor(np.ones([2, 2], np.int32)) 73 ret = net(x) 74 print(ret) 75 76 77def test_ms_syntax_operator_logic_not_tensor_2X2_float(): 78 net = LogicNot() 79 x = Tensor(np.ones([2, 2], np.float)) 80 ret = net(x) 81 print(ret) 82 83 84def test_ms_syntax_operator_logic_not_str(): 85 net = LogicNotSpec("cba") 86 ret = net(1) 87 print(ret) 88 89 90def test_ms_syntax_operator_logic_not_list_int(): 91 net = LogicNot() 92 ret = net([1, 2, 3]) 93 print(ret) 94 95 96def test_ms_syntax_operator_logic_not_list_float(): 97 net = LogicNot() 98 ret = net([1.0, 2.0, 3.0]) 99 print(ret) 100 101 102def test_ms_syntax_operator_logic_not_list_str(): 103 net = LogicNotSpec(["1", "2", "3"]) 104 ret = net(1) 105 print(ret) 106 107 108def test_ms_syntax_operator_logic_not_list_combine(): 109 net = LogicNotSpec([1, "2", 3]) 110 ret = net(1) 111 print(ret) 112 113 114def test_ms_syntax_operator_logic_not_tuple_int(): 115 net = LogicNot() 116 ret = net((1, 2, 3)) 117 print(ret) 118 119 120def test_ms_syntax_operator_logic_not_tuple_float(): 121 net = LogicNot() 122 ret = net((1.0, 2.0, 3.0)) 123 print(ret) 124 125 126def test_ms_syntax_operator_logic_not_tuple_str(): 127 net = LogicNotSpec(("1", "2", "3")) 128 ret = net(1) 129 print(ret) 130 131 132def test_ms_syntax_operator_logic_not_tuple_combine(): 133 net = LogicNotSpec((1, "2", 3)) 134 ret = net(1) 135 print(ret) 136