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_mod.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 mod"""
  16. import mindspore.nn as nn
  17. from mindspore import context
  18. context.set_context(mode=context.GRAPH_MODE)
  19. def test_positive_mod_positive():
  20. class Mod(nn.Cell):
  21. def __init__(self, x, y):
  22. super(Mod, self).__init__()
  23. self.x = x
  24. self.y = y
  25. def construct(self):
  26. return self.x % self.y
  27. x = 3.0
  28. y = 1.3
  29. mod_net = Mod(x, y)
  30. expect = x % y
  31. assert abs(mod_net() - expect) < 0.000001
  32. def test_positive_mod_negative():
  33. class Mod(nn.Cell):
  34. def __init__(self, x, y):
  35. super(Mod, self).__init__()
  36. self.x = x
  37. self.y = y
  38. def construct(self):
  39. return self.x % self.y
  40. x = 3.0
  41. y = -1.3
  42. mod_net = Mod(x, y)
  43. expect = x % y
  44. assert abs(mod_net() - expect) < 0.000001
  45. def test_negative_mod_positive():
  46. class Mod(nn.Cell):
  47. def __init__(self, x, y):
  48. super(Mod, self).__init__()
  49. self.x = x
  50. self.y = y
  51. def construct(self):
  52. return self.x % self.y
  53. x = -3.0
  54. y = 1.3
  55. mod_net = Mod(x, y)
  56. expect = x % y
  57. assert abs(mod_net() - expect) < 0.000001
  58. def test_negative_mod_negative():
  59. class Mod(nn.Cell):
  60. def __init__(self, x, y):
  61. super(Mod, self).__init__()
  62. self.x = x
  63. self.y = y
  64. def construct(self):
  65. return self.x % self.y
  66. x = -3.0
  67. y = -1.3
  68. mod_net = Mod(x, y)
  69. expect = x % y
  70. assert abs(mod_net() - expect) < 0.000001
  71. def test_int_mod_int():
  72. class Mod(nn.Cell):
  73. def __init__(self, x, y):
  74. super(Mod, self).__init__()
  75. self.x = x
  76. self.y = y
  77. def construct(self):
  78. return self.x % self.y
  79. x = 3
  80. y = 2
  81. mod_net = Mod(x, y)
  82. expect = x % y
  83. assert abs(mod_net() - expect) < 0.000001