xref: /aosp_15_r20/external/pigweed/pw_bloat/py/bloaty_config_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1#!/usr/bin/env python3
2# Copyright 2022 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""Tests for bloaty configuration tooling."""
16
17import unittest
18
19from pw_bloat import bloaty_config
20
21
22class BloatyConfigTest(unittest.TestCase):
23    """Tests that the bloaty config tool produces the expected config."""
24
25    def test_map_segments_to_memory_regions(self) -> None:
26        """Ensures the mapping works correctly based on a real example."""
27        segments = {
28            3: (int(0x800F268), int(0x8100200)),
29            5: (int(0x20004650), int(0x20020650)),
30            6: (int(0x20020650), int(0x20030000)),
31            1: (int(0x8000200), int(0x800F060)),
32            4: (int(0x20000208), int(0x20004650)),
33            2: (int(0x20000000), int(0x20000208)),
34            0: (int(0x8000000), int(0x8000200)),
35        }
36        memory_regions = {
37            'FLASH': {0: (int(0x8000200), int(0x8100200))},
38            'RAM': {0: (int(0x20000000), int(0x20030000))},
39            'VECTOR_TABLE': {0: (int(0x8000000), int(0x8000200))},
40        }
41        expected = {
42            3: 'FLASH',
43            5: 'RAM',
44            6: 'RAM',
45            1: 'FLASH',
46            4: 'RAM',
47            2: 'RAM',
48            0: 'VECTOR_TABLE',
49        }
50        actual = bloaty_config.map_segments_to_memory_regions(
51            segments=segments, memory_regions=memory_regions
52        )
53        self.assertEqual(expected, actual)
54
55    def test_generate_memoryregions_data_source(self) -> None:
56        """Ensures the formatted generation works correctly."""
57        segments_to_memory_regions = {
58            0: 'RAM',
59            1: 'RAM',
60            13: 'FLASH',
61        }
62        config = bloaty_config.generate_memoryregions_data_source(
63            segments_to_memory_regions
64        )
65        expected = '\n'.join(
66            (
67                r'custom_data_source: {',
68                r'  name: "memoryregions"',
69                r'  base_data_source: "segments"',
70                r'  rewrite: {',
71                r'    pattern:"^LOAD #0 \\[.*\\]$"',
72                r'    replacement:"RAM"',
73                r'  }',
74                r'  rewrite: {',
75                r'    pattern:"^LOAD #1 \\[.*\\]$"',
76                r'    replacement:"RAM"',
77                r'  }',
78                r'  rewrite: {',
79                r'    pattern:"^LOAD #13 \\[.*\\]$"',
80                r'    replacement:"FLASH"',
81                r'  }',
82                r'  rewrite: {',
83                r'    pattern:".*"',
84                r'    replacement:"Not resident in memory"',
85                r'  }',
86                r'}',
87                r'',
88            )
89        )
90        self.assertEqual(expected, config)
91
92    def test_generate_utilization_data_source(self) -> None:
93        config = bloaty_config.generate_utilization_data_source()
94        expected = '\n'.join(
95            (
96                'custom_data_source: {',
97                '  name:"utilization"',
98                '  base_data_source:"sections"',
99                '  rewrite: {',
100                '    pattern:"unused_space"',
101                '    replacement:"Free space"',
102                '  }',
103                '  rewrite: {',
104                '    pattern:"^\\\\[LOAD"',
105                '    replacement:"Padding"',
106                '  }',
107                '  rewrite: {',
108                '    pattern:".*"',
109                '    replacement:"Used space"',
110                '  }',
111                '}',
112                '',
113            )
114        )
115        self.assertEqual(expected, config)
116
117
118if __name__ == '__main__':
119    unittest.main()
120