aboutsummaryrefslogtreecommitdiff
path: root/pw_bloat/py/label_test.py
blob: 2e2236839f6aec14e11b99ef7c6ad0ae0ea42131 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/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
import os
import logging
import sys
from pw_bloat.label import DataSourceMap, Label

LIST_LABELS = [
    Label(name='main()', size=30, parents=tuple(['FLASH', '.code'])),
    Label(name='foo()', size=100, parents=tuple(['RAM', '.heap'])),
    Label(name='bar()', size=220, parents=tuple(['RAM', '.heap'])),
]

logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))


def get_test_map():
    pw_root = os.environ.get("PW ROOT")
    filename = f"{pw_root}/pigweed/pw_bloat/test_label.csv"
    with open(filename, 'r') as csvfile:
        ds_map = DataSourceMap.from_bloaty_tsv(csvfile)
        capacity_patterns = [("^__TEXT$", 459), ("^_", 920834)]
        for cap_pattern, cap_size in capacity_patterns:
            ds_map.add_capacity(cap_pattern, cap_size)
    return ds_map


class LabelStructTest(unittest.TestCase):
    """Testing class for the label structs."""

    def test_data_source_total_size(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        self.assertEqual(ds_map.get_total_size(), 0)

    def test_data_source_single_insert_total_size(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        ds_map.insert_label_hierachy(['FLASH', '.code', 'main()'], 30)
        self.assertEqual(ds_map.get_total_size(), 30)

    def test_data_source_multiple_insert_total_size(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        ds_map.insert_label_hierachy(['FLASH', '.code', 'main()'], 30)
        ds_map.insert_label_hierachy(['RAM', '.code', 'foo()'], 100)
        self.assertEqual(ds_map.get_total_size(), 130)

    def test_parsing_generator_three_datasource_names(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name], label.size
            )
        list_labels_three = [*LIST_LABELS, Label(name='total', size=350)]
        for label_hiearchy in ds_map.labels():
            self.assertIn(label_hiearchy, list_labels_three)
        self.assertEqual(ds_map.get_total_size(), 350)

    def test_parsing_generator_two_datasource_names(self):
        ds_map = DataSourceMap(['a', 'b'])
        ds_label_list = [
            Label(name='main()', size=30, parents=tuple(['FLASH'])),
            Label(name='foo()', size=100, parents=tuple(['RAM'])),
            Label(name='bar()', size=220, parents=tuple(['RAM'])),
        ]
        for label in ds_label_list:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.name], label.size
            )
        list_labels_two = [*ds_label_list, Label(name='total', size=350)]
        for label_hiearchy in ds_map.labels():
            self.assertIn(label_hiearchy, list_labels_two)
        self.assertEqual(ds_map.get_total_size(), 350)

    def test_parsing_generator_specified_datasource_1(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name], label.size
            )
        list_labels_ds_b = [
            Label(name='.code', size=30, parents=tuple(['FLASH'])),
            Label(name='.heap', size=320, parents=tuple(['RAM'])),
        ]
        list_labels_ds_b += [Label(name='total', size=350)]
        for label_hiearchy in ds_map.labels(1):
            self.assertIn(label_hiearchy, list_labels_ds_b)
        self.assertEqual(ds_map.get_total_size(), 350)

    def test_parsing_generator_specified_datasource_str_2(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name], label.size
            )
        list_labels_ds_a = [
            Label(name='FLASH', size=30, parents=tuple([])),
            Label(name='RAM', size=320, parents=tuple([])),
        ]
        list_labels_ds_a += [Label(name='total', size=350)]
        for label_hiearchy in ds_map.labels(0):
            self.assertIn(label_hiearchy, list_labels_ds_a)
        self.assertEqual(ds_map.get_total_size(), 350)

    def test_parsing_generator_specified_datasource_int(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name], label.size
            )
        list_labels_ds_a = [
            Label(name='FLASH', size=30, parents=tuple([])),
            Label(name='RAM', size=320, parents=tuple([])),
        ]
        list_labels_ds_a += [Label(name='total', size=350)]
        for label_hiearchy in ds_map.labels(0):
            self.assertIn(label_hiearchy, list_labels_ds_a)
        self.assertEqual(ds_map.get_total_size(), 350)

    def test_parsing_generator_specified_datasource_int_2(self):
        ds_map = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name], label.size
            )
        list_labels_ds_b = [
            Label(name='.code', size=30, parents=tuple(['FLASH'])),
            Label(name='.heap', size=320, parents=tuple(['RAM'])),
        ]
        list_labels_ds_b += [Label(name='total', size=350)]
        for label_hiearchy in ds_map.labels(1):
            self.assertIn(label_hiearchy, list_labels_ds_b)
        self.assertEqual(ds_map.get_total_size(), 350)

    def test_diff_same_ds_labels_diff_sizes(self):
        """Same map with different sizes."""
        ds_map = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name], label.size
            )

        ds_map2 = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map2.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name],
                label.size + 10,
            )

        list_labels_ds_b = [
            Label(
                name='main()',
                size=-10,
                exists_both=True,
                parents=tuple(['FLASH', '.code']),
            ),
            Label(
                name='foo()',
                size=-10,
                exists_both=True,
                parents=tuple(['RAM', '.heap']),
            ),
            Label(
                name='bar()',
                size=-10,
                exists_both=True,
                parents=tuple(['RAM', '.heap']),
            ),
        ]

        ds_map_diff = ds_map.diff(ds_map2)

        for label_hiearchy in ds_map_diff.labels():
            self.assertIn(label_hiearchy, list_labels_ds_b)

    def test_diff_missing_ds_labels_diff_sizes(self):
        """Different map with different sizes."""
        ds_map = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS:
            ds_map.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name], label.size
            )

        ds_map2 = DataSourceMap(['a', 'b', 'c'])
        for label in LIST_LABELS[:-1]:
            ds_map2.insert_label_hierachy(
                [label.parents[0], label.parents[1], label.name],
                label.size + 20,
            )
        ds_map2.insert_label_hierachy(
            [label.parents[0], label.parents[1], 'foobar()'], label.size + 20
        )

        ds_map2.insert_label_hierachy(["LOAD #5", 'random_load', 'func()'], 250)

        list_labels_ds_b = [
            Label(
                name='FLASH',
                size=20,
                capacity=None,
                exists_both=True,
                parents=(),
            ),
            Label(
                name='RAM',
                size=-80,
                capacity=None,
                exists_both=True,
                parents=(),
            ),
            Label(
                name='LOAD #5',
                size=250,
                capacity=None,
                exists_both=False,
                parents=(),
            ),
        ]

        ds_map_diff = ds_map2.diff(ds_map)

        for label_hiearchy in ds_map_diff.labels(0):
            self.assertIn(label_hiearchy, list_labels_ds_b)


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