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 16import numpy as np 17import pytest 18 19import mindspore.context as context 20import mindspore.nn as nn 21 22context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 23 24 25class OpNetWrapper(nn.Cell): 26 def __init__(self, op): 27 super(OpNetWrapper, self).__init__() 28 self.op = op 29 30 def construct(self, *inputs): 31 return self.op(*inputs) 32 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_cpu 36@pytest.mark.env_onecard 37def test_int(): 38 op = nn.Range(0, 100, 10) 39 op_wrapper = OpNetWrapper(op) 40 41 outputs = op_wrapper() 42 print(outputs) 43 assert outputs.shape == (10,) 44 assert np.allclose(outputs.asnumpy(), range(0, 100, 10)) 45 46 47@pytest.mark.level0 48@pytest.mark.platform_x86_cpu 49@pytest.mark.env_onecard 50def test_float(): 51 op = nn.Range(10., 100., 20.) 52 op_wrapper = OpNetWrapper(op) 53 54 outputs = op_wrapper() 55 print(outputs) 56 assert outputs.shape == (5,) 57 assert np.allclose(outputs.asnumpy(), [10., 30., 50., 70., 90.]) 58 59 60if __name__ == '__main__': 61 test_int() 62 test_float() 63