aboutsummaryrefslogtreecommitdiff
path: root/pw_bloat/py/bloaty_config_test.py
blob: 340ffe2f61d40763577dc21ae88e702d17ad4df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# Copyright 2022 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Tests for bloaty configuration tooling."""

import unittest

from pw_bloat import bloaty_config


class BloatyConfigTest(unittest.TestCase):
    """Tests that the bloaty config tool produces the expected config."""
    def test_map_segments_to_memory_regions(self) -> None:
        """Ensures the mapping works correctly based on a real example."""
        segments = {
            3: (int(0x800f268), int(0x8100200)),
            5: (int(0x20004650), int(0x20020650)),
            6: (int(0x20020650), int(0x20030000)),
            1: (int(0x8000200), int(0x800f060)),
            4: (int(0x20000208), int(0x20004650)),
            2: (int(0x20000000), int(0x20000208)),
            0: (int(0x8000000), int(0x8000200)),
        }
        memory_regions = {
            'FLASH': {
                0: (int(0x8000200), int(0x8100200))
            },
            'RAM': {
                0: (int(0x20000000), int(0x20030000))
            },
            'VECTOR_TABLE': {
                0: (int(0x8000000), int(0x8000200))
            },
        }
        expected = {
            3: 'FLASH',
            5: 'RAM',
            6: 'RAM',
            1: 'FLASH',
            4: 'RAM',
            2: 'RAM',
            0: 'VECTOR_TABLE',
        }
        actual = bloaty_config.map_segments_to_memory_regions(
            segments=segments, memory_regions=memory_regions)
        self.assertEqual(expected, actual)

    def test_generate_memoryregions_data_source(self) -> None:
        """Ensures the formatted generation works correctly."""
        segments_to_memory_regions = {
            0: 'RAM',
            1: 'RAM',
            13: 'FLASH',
        }
        config = bloaty_config.generate_memoryregions_data_source(
            segments_to_memory_regions)
        expected = '\n'.join((
            r'custom_data_source: {',
            r'  name: "memoryregions"',
            r'  base_data_source: "segments"',
            r'  rewrite: {',
            r'    pattern:"^LOAD #0 \\[.*\\]$"',
            r'    replacement:"RAM"',
            r'  }',
            r'  rewrite: {',
            r'    pattern:"^LOAD #1 \\[.*\\]$"',
            r'    replacement:"RAM"',
            r'  }',
            r'  rewrite: {',
            r'    pattern:"^LOAD #13 \\[.*\\]$"',
            r'    replacement:"FLASH"',
            r'  }',
            r'  rewrite: {',
            r'    pattern:".*"',
            r'    replacement:"Not resident in memory"',
            r'  }',
            r'}',
            r'',
        ))
        self.assertEqual(expected, config)

    def test_generate_utilization_data_source(self) -> None:
        config = bloaty_config.generate_utilization_data_source()
        expected = '\n'.join((
            'custom_data_source: {',
            '  name:"utilization"',
            '  base_data_source:"sections"',
            '  rewrite: {',
            '    pattern:"unused_space"',
            '    replacement:"Free space"',
            '  }',
            '  rewrite: {',
            '    pattern:".*"',
            '    replacement:"Used space"',
            '  }',
            '}',
            '',
        ))
        self.assertEqual(expected, config)


if __name__ == '__main__':
    unittest.main()