blob: 2a99da5411d849f99483c327736b543c286feb22 [file] [log] [blame]
Diego Russo286bd5e2020-04-23 19:53:00 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
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.
16# Description:
17# Contains unit tests for live ranges
18from unittest.mock import MagicMock
19
20import pytest
Louis Verhaard0b8268a2020-08-05 16:11:29 +020021
Diego Russo286bd5e2020-04-23 19:53:00 +010022from ethosu.vela.live_range import LiveRange
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020023from ethosu.vela.tensor import Tensor
Diego Russo286bd5e2020-04-23 19:53:00 +010024
25
26class TestLiveRange:
27 def test_instantiate_live_range_with_tensor(self):
28 tens = MagicMock()
29 tens.storage_size.return_value = 4
30 tens.name = "test"
31
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020032 live_range = LiveRange(tens, Tensor.AllocationQuantum)
Diego Russo286bd5e2020-04-23 19:53:00 +010033 assert live_range.size == 4
34 assert live_range.name == "test"
35 assert live_range.tensors == [tens]
36
37 def test_add_tensor_valid_size(self):
38 tens = MagicMock()
39 # When storage_size() is called twice, it returns 4 and then 3
40 tens.storage_size.side_effect = [4, 3]
41 tens.name = "test"
42
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020043 live_range = LiveRange(tens, Tensor.AllocationQuantum)
Diego Russo286bd5e2020-04-23 19:53:00 +010044 live_range.add_tensor(tens)
45
46 assert live_range.size == 4
47 assert live_range.name == "test"
48 assert live_range.tensors == [tens, tens]
49
50 def test_add_tensor_invalid_size(self):
51 tens = MagicMock()
52 # When storage_size() is called twice, it returns 4 and then 5
53 tens.storage_size.side_effect = [4, 5]
54 tens.name = "test"
55
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020056 live_range = LiveRange(tens, Tensor.AllocationQuantum)
Diego Russo286bd5e2020-04-23 19:53:00 +010057 # Expect an AssertionError with a message
58 with pytest.raises(AssertionError, match=r".* to the same LiveRange .*"):
59 live_range.add_tensor(tens)
60
61 # Check that the interal status of the object didn't change
62 assert live_range.size == 4
63 assert live_range.name == "test"
64 assert live_range.tensors == [tens]