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_padEnd_op.py 2.3 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. """
  16. Testing PadEnd op in DE
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.c_transforms as ops
  22. def pad_compare(array, pad_shape, pad_value, res):
  23. data = ds.NumpySlicesDataset([array])
  24. if pad_value is not None:
  25. data = data.map(operations=ops.PadEnd(pad_shape, pad_value))
  26. else:
  27. data = data.map(operations=ops.PadEnd(pad_shape))
  28. for d in data:
  29. np.testing.assert_array_equal(res, d[0])
  30. # Extensive testing of PadEnd is already done in batch with Pad test cases
  31. def test_pad_end_basics():
  32. pad_compare([1, 2], [3], -1, [1, 2, -1])
  33. pad_compare([1, 2, 3], [3], -1, [1, 2, 3])
  34. pad_compare([1, 2, 3], [2], -1, [1, 2])
  35. pad_compare([1, 2, 3], [5], None, [1, 2, 3, 0, 0])
  36. def test_pad_end_str():
  37. pad_compare([b"1", b"2"], [3], b"-1", [b"1", b"2", b"-1"])
  38. pad_compare([b"1", b"2", b"3"], [3], b"-1", [b"1", b"2", b"3"])
  39. pad_compare([b"1", b"2", b"3"], [2], b"-1", [b"1", b"2"])
  40. pad_compare([b"1", b"2", b"3"], [5], None, [b"1", b"2", b"3", b"", b""])
  41. def test_pad_end_exceptions():
  42. with pytest.raises(RuntimeError) as info:
  43. pad_compare([1, 2], [3], "-1", [])
  44. assert "Source and pad_value tensors are not of the same type." in str(info.value)
  45. with pytest.raises(RuntimeError) as info:
  46. pad_compare([b"1", b"2", b"3", b"4", b"5"], [2], 1, [])
  47. assert "Source and pad_value tensors are not of the same type." in str(info.value)
  48. if __name__ == "__main__":
  49. test_pad_end_basics()
  50. test_pad_end_str()
  51. test_pad_end_exceptions()