test_pad_end_op.py 2.4 KB
Newer Older
X
xunxue 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""
Testing PadEnd op in DE
"""
import numpy as np
import pytest

import mindspore.dataset as ds
import mindspore.dataset.transforms.c_transforms as ops


N
nhussain 已提交
25 26
# Extensive testing of PadEnd is already done in batch with Pad test cases

X
xunxue 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
def pad_compare(array, pad_shape, pad_value, res):
    data = ds.NumpySlicesDataset([array])
    if pad_value is not None:
        data = data.map(operations=ops.PadEnd(pad_shape, pad_value))
    else:
        data = data.map(operations=ops.PadEnd(pad_shape))
    for d in data:
        np.testing.assert_array_equal(res, d[0])


def test_pad_end_basics():
    pad_compare([1, 2], [3], -1, [1, 2, -1])
    pad_compare([1, 2, 3], [3], -1, [1, 2, 3])
    pad_compare([1, 2, 3], [2], -1, [1, 2])
    pad_compare([1, 2, 3], [5], None, [1, 2, 3, 0, 0])


def test_pad_end_str():
    pad_compare([b"1", b"2"], [3], b"-1", [b"1", b"2", b"-1"])
    pad_compare([b"1", b"2", b"3"], [3], b"-1", [b"1", b"2", b"3"])
    pad_compare([b"1", b"2", b"3"], [2], b"-1", [b"1", b"2"])
    pad_compare([b"1", b"2", b"3"], [5], None, [b"1", b"2", b"3", b"", b""])


def test_pad_end_exceptions():
    with pytest.raises(RuntimeError) as info:
        pad_compare([1, 2], [3], "-1", [])
    assert "Source and pad_value tensors are not of the same type." in str(info.value)

    with pytest.raises(RuntimeError) as info:
        pad_compare([b"1", b"2", b"3", b"4", b"5"], [2], 1, [])
    assert "Source and pad_value tensors are not of the same type." in str(info.value)

N
nhussain 已提交
60 61 62 63
    with pytest.raises(TypeError) as info:
        pad_compare([3, 4, 5], ["2"], 1, [])
    assert "a value in the list is not an integer." in str(info.value)

X
xunxue 已提交
64 65 66 67 68

if __name__ == "__main__":
    test_pad_end_basics()
    test_pad_end_str()
    test_pad_end_exceptions()