• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17import mindspore.nn as nn
18from mindspore import context
19
20context.set_context(mode=context.GRAPH_MODE)
21
22
23def test_positive_mod_positive():
24    class Mod(nn.Cell):
25        def __init__(self, x, y):
26            super(Mod, self).__init__()
27            self.x = x
28            self.y = y
29
30        def construct(self):
31            return self.x % self.y
32    x = 3.0
33    y = 1.3
34    mod_net = Mod(x, y)
35    expect = x % y
36    assert abs(mod_net() - expect) < 0.000001
37
38
39def test_positive_mod_negative():
40    class Mod(nn.Cell):
41        def __init__(self, x, y):
42            super(Mod, self).__init__()
43            self.x = x
44            self.y = y
45
46        def construct(self):
47            return self.x % self.y
48    x = 3.0
49    y = -1.3
50    mod_net = Mod(x, y)
51    expect = x % y
52    assert abs(mod_net() - expect) < 0.000001
53
54
55def test_negative_mod_positive():
56    class Mod(nn.Cell):
57        def __init__(self, x, y):
58            super(Mod, self).__init__()
59            self.x = x
60            self.y = y
61
62        def construct(self):
63            return self.x % self.y
64    x = -3.0
65    y = 1.3
66    mod_net = Mod(x, y)
67    expect = x % y
68    assert abs(mod_net() - expect) < 0.000001
69
70
71def test_negative_mod_negative():
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
78        def construct(self):
79            return self.x % self.y
80    x = -3.0
81    y = -1.3
82    mod_net = Mod(x, y)
83    expect = x % y
84    assert abs(mod_net() - expect) < 0.000001
85
86
87def test_int_mod_int():
88    class Mod(nn.Cell):
89        def __init__(self, x, y):
90            super(Mod, self).__init__()
91            self.x = x
92            self.y = y
93
94        def construct(self):
95            return self.x % self.y
96    x = 3
97    y = 2
98    mod_net = Mod(x, y)
99    expect = x % y
100    assert abs(mod_net() - expect) < 0.000001
101