aboutsummaryrefslogtreecommitdiff
path: root/ethosu/vela/test/test_live_range.py
blob: 2a99da5411d849f99483c327736b543c286feb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
60
61
62
63
64
# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# 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
#
# 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.
# Description:
# Contains unit tests for live ranges
from unittest.mock import MagicMock

import pytest

from ethosu.vela.live_range import LiveRange
from ethosu.vela.tensor import Tensor


class TestLiveRange:
    def test_instantiate_live_range_with_tensor(self):
        tens = MagicMock()
        tens.storage_size.return_value = 4
        tens.name = "test"

        live_range = LiveRange(tens, Tensor.AllocationQuantum)
        assert live_range.size == 4
        assert live_range.name == "test"
        assert live_range.tensors == [tens]

    def test_add_tensor_valid_size(self):
        tens = MagicMock()
        # When storage_size() is called twice, it returns 4 and then 3
        tens.storage_size.side_effect = [4, 3]
        tens.name = "test"

        live_range = LiveRange(tens, Tensor.AllocationQuantum)
        live_range.add_tensor(tens)

        assert live_range.size == 4
        assert live_range.name == "test"
        assert live_range.tensors == [tens, tens]

    def test_add_tensor_invalid_size(self):
        tens = MagicMock()
        # When storage_size() is called twice, it returns 4 and then 5
        tens.storage_size.side_effect = [4, 5]
        tens.name = "test"

        live_range = LiveRange(tens, Tensor.AllocationQuantum)
        # Expect an AssertionError with a message
        with pytest.raises(AssertionError, match=r".* to the same LiveRange .*"):
            live_range.add_tensor(tens)

        # Check that the interal status of the object didn't change
        assert live_range.size == 4
        assert live_range.name == "test"
        assert live_range.tensors == [tens]