• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    def test_map_segments_to_memory_regions(self) -> None:
25        """Ensures the mapping works correctly based on a real example."""
26        segments = {
27            3: (int(0x800f268), int(0x8100200)),
28            5: (int(0x20004650), int(0x20020650)),
29            6: (int(0x20020650), int(0x20030000)),
30            1: (int(0x8000200), int(0x800f060)),
31            4: (int(0x20000208), int(0x20004650)),
32            2: (int(0x20000000), int(0x20000208)),
33            0: (int(0x8000000), int(0x8000200)),
34        }
35        memory_regions = {
36            'FLASH': {
37                0: (int(0x8000200), int(0x8100200))
38            },
39            'RAM': {
40                0: (int(0x20000000), int(0x20030000))
41            },
42            'VECTOR_TABLE': {
43                0: (int(0x8000000), int(0x8000200))
44            },
45        }
46        expected = {
47            3: 'FLASH',
48            5: 'RAM',
49            6: 'RAM',
50            1: 'FLASH',
51            4: 'RAM',
52            2: 'RAM',
53            0: 'VECTOR_TABLE',
54        }
55        actual = bloaty_config.map_segments_to_memory_regions(
56            segments=segments, memory_regions=memory_regions)
57        self.assertEqual(expected, actual)
58
59    def test_generate_memoryregions_data_source(self) -> None:
60        """Ensures the formatted generation works correctly."""
61        segments_to_memory_regions = {
62            0: 'RAM',
63            1: 'RAM',
64            13: 'FLASH',
65        }
66        config = bloaty_config.generate_memoryregions_data_source(
67            segments_to_memory_regions)
68        expected = '\n'.join((
69            r'custom_data_source: {',
70            r'  name: "memoryregions"',
71            r'  base_data_source: "segments"',
72            r'  rewrite: {',
73            r'    pattern:"^LOAD #0 \\[.*\\]$"',
74            r'    replacement:"RAM"',
75            r'  }',
76            r'  rewrite: {',
77            r'    pattern:"^LOAD #1 \\[.*\\]$"',
78            r'    replacement:"RAM"',
79            r'  }',
80            r'  rewrite: {',
81            r'    pattern:"^LOAD #13 \\[.*\\]$"',
82            r'    replacement:"FLASH"',
83            r'  }',
84            r'  rewrite: {',
85            r'    pattern:".*"',
86            r'    replacement:"Not resident in memory"',
87            r'  }',
88            r'}',
89            r'',
90        ))
91        self.assertEqual(expected, config)
92
93    def test_generate_utilization_data_source(self) -> None:
94        config = bloaty_config.generate_utilization_data_source()
95        expected = '\n'.join((
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:".*"',
105            '    replacement:"Used space"',
106            '  }',
107            '}',
108            '',
109        ))
110        self.assertEqual(expected, config)
111
112
113if __name__ == '__main__':
114    unittest.main()
115