1# Copyright 2020 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 check parameter """ 16import pytest 17import numpy as np 18from mindspore._checkparam import Validator, twice 19 20kernel_size = 5 21kernel_size1 = twice(kernel_size) 22assert kernel_size1 == (5, 5) 23 24def test_check_int1(): 25 a = np.random.randint(-100, 100) 26 assert Validator.check_is_int(a) == a 27 28def test_check_int2(): 29 with pytest.raises(TypeError): 30 Validator.check_is_int(3.3) 31 32def test_check_int3(): 33 with pytest.raises(TypeError): 34 Validator.check_is_int("str") 35 36def test_check_int4(): 37 with pytest.raises(TypeError): 38 Validator.check_is_int(True) 39 40def test_check_is_int5(): 41 with pytest.raises(TypeError): 42 Validator.check_is_int(True) 43 with pytest.raises(TypeError): 44 Validator.check_is_int(False) 45 46def test_check_positive_int1(): 47 a = np.random.randint(1, 100) 48 assert Validator.check_positive_int(a) == a 49 50def test_check_positive_int2(): 51 a = np.random.randint(-100, 0) 52 with pytest.raises(ValueError): 53 Validator.check_positive_int(a) 54 55def test_check_positive_int3(): 56 with pytest.raises(TypeError): 57 Validator.check_positive_int(3.3) 58 59def test_check_positive_int4(): 60 with pytest.raises(TypeError): 61 Validator.check_positive_int("str") 62 63def test_check_negative_int1(): 64 a = np.random.randint(-100, -1) 65 assert Validator.check_negative_int(a) == a 66 67def test_check_negative_int2(): 68 a = np.random.randint(0, 100) 69 with pytest.raises(ValueError): 70 Validator.check_negative_int(a) 71 72def test_check_negative_int3(): 73 with pytest.raises(TypeError): 74 Validator.check_negative_int(3.3) 75 76def test_check_negative_int4(): 77 with pytest.raises(TypeError): 78 Validator.check_negative_int("str") 79 80def test_check_non_positive_int1(): 81 a = np.random.randint(-100, 0) 82 assert Validator.check_non_positive_int(a) == a 83 84def test_check_non_positive_int2(): 85 a = np.random.randint(1, 100) 86 with pytest.raises(ValueError): 87 Validator.check_non_positive_int(a) 88 89def test_check_non_positive_int3(): 90 with pytest.raises(TypeError): 91 Validator.check_non_positive_int(3.3) 92 93def test_check_non_positive_int4(): 94 with pytest.raises(TypeError): 95 Validator.check_non_positive_int("str") 96 97def test_check_bool_1(): 98 assert Validator.check_bool(True) 99 100 101def test_check_bool_2(): 102 assert Validator.check_bool(False) is not True 103 104 105def test_check_bool_3(): 106 with pytest.raises(TypeError): 107 Validator.check_bool("str") 108 109 110def test_check_bool_4(): 111 with pytest.raises(TypeError): 112 Validator.check_bool(1) 113 114 115def test_check_bool_5(): 116 with pytest.raises(TypeError): 117 Validator.check_bool(3.5) 118 119 120def test_twice_1(): 121 assert twice(3) == (3, 3) 122 123 124def test_twice_2(): 125 assert twice((3, 3)) == (3, 3) 126 127 128def test_twice_3(): 129 with pytest.raises(TypeError): 130 twice(0.5) 131 132 133def test_twice_4(): 134 with pytest.raises(TypeError): 135 twice("str") 136 137 138def test_twice_5(): 139 with pytest.raises(TypeError): 140 twice((1, 2, 3)) 141 142 143def test_twice_6(): 144 with pytest.raises(TypeError): 145 twice((3.3, 4)) 146