blob: 1cd036fe917e702a9e2ad7648fc53d46581d8e45 [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2020 Arm Limited and/or its affiliates <open-source-office@arm.com>
Diego Russo286bd5e2020-04-23 19:53:00 +01002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Diego Russo286bd5e2020-04-23 19:53:00 +010017# Description:
18# Contains unit tests for live ranges
19from unittest.mock import MagicMock
20
21import pytest
Louis Verhaard0b8268a2020-08-05 16:11:29 +020022
Diego Russo286bd5e2020-04-23 19:53:00 +010023from ethosu.vela.live_range import LiveRange
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020024from ethosu.vela.tensor import Tensor
Diego Russo286bd5e2020-04-23 19:53:00 +010025
26
27class TestLiveRange:
28 def test_instantiate_live_range_with_tensor(self):
29 tens = MagicMock()
30 tens.storage_size.return_value = 4
31 tens.name = "test"
32
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020033 live_range = LiveRange(tens, Tensor.AllocationQuantum)
Diego Russo286bd5e2020-04-23 19:53:00 +010034 assert live_range.size == 4
35 assert live_range.name == "test"
36 assert live_range.tensors == [tens]
37
38 def test_add_tensor_valid_size(self):
39 tens = MagicMock()
40 # When storage_size() is called twice, it returns 4 and then 3
41 tens.storage_size.side_effect = [4, 3]
42 tens.name = "test"
43
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020044 live_range = LiveRange(tens, Tensor.AllocationQuantum)
Diego Russo286bd5e2020-04-23 19:53:00 +010045 live_range.add_tensor(tens)
46
47 assert live_range.size == 4
48 assert live_range.name == "test"
49 assert live_range.tensors == [tens, tens]
50
51 def test_add_tensor_invalid_size(self):
52 tens = MagicMock()
53 # When storage_size() is called twice, it returns 4 and then 5
54 tens.storage_size.side_effect = [4, 5]
55 tens.name = "test"
56
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020057 live_range = LiveRange(tens, Tensor.AllocationQuantum)
Diego Russo286bd5e2020-04-23 19:53:00 +010058 # Expect an AssertionError with a message
59 with pytest.raises(AssertionError, match=r".* to the same LiveRange .*"):
60 live_range.add_tensor(tens)
61
62 # Check that the interal status of the object didn't change
63 assert live_range.size == 4
64 assert live_range.name == "test"
65 assert live_range.tensors == [tens]