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 3.8 kB

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