1# Copyright 2019 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# ============================================================================== 15import numpy as np 16 17import mindspore._c_dataengine as cde 18 19 20def test_shape(): 21 x = [2, 3] 22 s = cde.TensorShape(x) 23 assert s.as_list() == x 24 assert s.is_known() 25 26 27def test_basic(): 28 x = np.array([1, 2, 3, 4, 5]) 29 n = cde.Tensor(x) 30 arr = np.array(n, copy=False) 31 arr[0] = 0 32 x = np.array([0, 2, 3, 4, 5]) 33 34 np.testing.assert_array_equal(x, arr) 35 assert n.type() == cde.DataType("int64") 36 37 arr2 = n.as_array() 38 arr[0] = 2 39 x = np.array([2, 2, 3, 4, 5]) 40 np.testing.assert_array_equal(x, arr2) 41 assert n.type() == cde.DataType("int64") 42 assert arr.__array_interface__['data'] == arr2.__array_interface__['data'] 43 44 45def test_strides(): 46 x = np.array([[1, 2, 3], [4, 5, 6]]) 47 n1 = cde.Tensor(x[:, 1]) 48 arr = np.array(n1, copy=False) 49 50 np.testing.assert_array_equal(x[:, 1], arr) 51 52 n2 = cde.Tensor(x.transpose()) 53 arr = np.array(n2, copy=False) 54 55 np.testing.assert_array_equal(x.transpose(), arr) 56 57 58if __name__ == '__main__': 59 test_shape() 60 test_strides() 61 test_basic() 62