blob: d087dd99d75dfd959ec5e8de8d58b0b318825ace [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
23
24
25class TestLiveRange:
26 def test_instantiate_live_range_with_tensor(self):
27 tens = MagicMock()
28 tens.storage_size.return_value = 4
29 tens.name = "test"
30
31 live_range = LiveRange(tens=tens)
32 assert live_range.size == 4
33 assert live_range.name == "test"
34 assert live_range.tensors == [tens]
35
36 def test_add_tensor_valid_size(self):
37 tens = MagicMock()
38 # When storage_size() is called twice, it returns 4 and then 3
39 tens.storage_size.side_effect = [4, 3]
40 tens.name = "test"
41
42 live_range = LiveRange(tens=tens)
43 live_range.add_tensor(tens)
44
45 assert live_range.size == 4
46 assert live_range.name == "test"
47 assert live_range.tensors == [tens, tens]
48
49 def test_add_tensor_invalid_size(self):
50 tens = MagicMock()
51 # When storage_size() is called twice, it returns 4 and then 5
52 tens.storage_size.side_effect = [4, 5]
53 tens.name = "test"
54
55 live_range = LiveRange(tens=tens)
56 # Expect an AssertionError with a message
57 with pytest.raises(AssertionError, match=r".* to the same LiveRange .*"):
58 live_range.add_tensor(tens)
59
60 # Check that the interal status of the object didn't change
61 assert live_range.size == 4
62 assert live_range.name == "test"
63 assert live_range.tensors == [tens]