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