You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_checkparameter.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 checkparameter """
  16. import pytest
  17. import numpy as np
  18. from mindspore._checkparam import check_input_format, Validator, twice, Rel
  19. kernel_size = 5
  20. kernel_size1 = twice(kernel_size)
  21. assert kernel_size1 == (5, 5)
  22. def test_check_integer1():
  23. with pytest.raises(TypeError):
  24. Validator.check_integer("input", 0, Rel.GE, "number")
  25. def test_check_integer2():
  26. with pytest.raises(ValueError):
  27. Validator.check_integer(-1, 0, Rel.GE, "number")
  28. def test_check_integer3():
  29. input = np.random.randint(0, 100)
  30. assert Validator.check_integer(input, 0, Rel.GE, "number") == input
  31. def test_check_int1():
  32. input = np.random.randint(-100, 100)
  33. assert Validator.check_is_int(input) == input
  34. def test_check_int2():
  35. with pytest.raises(TypeError):
  36. Validator.check_is_int(3.3)
  37. def test_check_int3():
  38. with pytest.raises(TypeError):
  39. Validator.check_is_int("str")
  40. def test_check_int4():
  41. with pytest.raises(TypeError):
  42. Validator.check_is_int(True)
  43. def test_check_is_int5():
  44. with pytest.raises(TypeError):
  45. Validator.check_is_int(True)
  46. with pytest.raises(TypeError):
  47. Validator.check_is_int(False)
  48. def test_check_positive_int1():
  49. input = np.random.randint(0, 100)
  50. assert Validator.check_positive_int(input) == input
  51. def test_check_positive_int2():
  52. input = np.random.randint(-100, 0)
  53. with pytest.raises(ValueError):
  54. Validator.check_positive_int(input)
  55. def test_check_positive_int3():
  56. with pytest.raises(ValueError):
  57. Validator.check_positive_int(3.3)
  58. def test_check_positive_int4():
  59. with pytest.raises(TypeError):
  60. Validator.check_positive_int("str")
  61. def test_check_negative_int1():
  62. input = np.random.randint(-100, -1)
  63. assert Validator.check_negative_int(input) == input
  64. def test_check_negative_int2():
  65. input = np.random.randint(0, 100)
  66. with pytest.raises(ValueError):
  67. Validator.check_negative_int(input)
  68. def test_check_negative_int3():
  69. with pytest.raises(ValueError):
  70. Validator.check_negative_int(3.3)
  71. def test_check_negative_int4():
  72. with pytest.raises(TypeError):
  73. Validator.check_negative_int("str")
  74. def test_check_non_positive_int1():
  75. input = np.random.randint(-100, 0)
  76. assert Validator.check_non_positive_int(input) == input
  77. def test_check_non_positive_int2():
  78. input = np.random.randint(1, 100)
  79. with pytest.raises(ValueError):
  80. Validator.check_non_positive_int(input)
  81. def test_check_non_positive_int3():
  82. with pytest.raises(ValueError):
  83. Validator.check_non_positive_int(3.3)
  84. def test_check_non_positive_int4():
  85. with pytest.raises(TypeError):
  86. Validator.check_non_positive_int("str")
  87. def test_check_bool_1():
  88. assert Validator.check_bool(True)
  89. def test_check_bool_2():
  90. assert Validator.check_bool(False) is not True
  91. def test_check_bool_3():
  92. with pytest.raises(TypeError):
  93. Validator.check_bool("str")
  94. def test_check_bool_4():
  95. with pytest.raises(TypeError):
  96. Validator.check_bool(1)
  97. def test_check_bool_5():
  98. with pytest.raises(TypeError):
  99. Validator.check_bool(3.5)
  100. def test_twice_1():
  101. assert twice(3) == (3, 3)
  102. def test_twice_2():
  103. assert twice((3, 3)) == (3, 3)
  104. def test_twice_3():
  105. with pytest.raises(TypeError):
  106. twice(0.5)
  107. def test_twice_4():
  108. with pytest.raises(TypeError):
  109. twice("str")
  110. def test_twice_5():
  111. with pytest.raises(TypeError):
  112. twice((1, 2, 3))
  113. def test_twice_6():
  114. with pytest.raises(TypeError):
  115. twice((3.3, 4))