2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Check that explain_binary_size_delta seems to work."""
12 import explain_binary_size_delta
15 class ExplainBinarySizeDeltaTest(unittest
.TestCase
):
17 def testCompare(self
):
18 # List entries have form:
19 # symbol_name, symbol_type, symbol_size, file_path, memory_address
21 # File with one symbol, left as-is.
22 ( 'unchanged', 't', 1000, '/file_unchanged', 0x1 ),
23 # File with one symbol, changed.
24 ( 'changed', 't', 1000, '/file_all_changed', 0x2 ),
25 # File with one symbol, deleted.
26 ( 'removed', 't', 1000, '/file_all_deleted', 0x3 ),
27 # File with two symbols, one unchanged, one changed, same bucket
28 ( 'unchanged', 't', 1000, '/file_pair_unchanged_changed', 0x4 ),
29 ( 'changed', 't', 1000, '/file_pair_unchanged_changed', 0x5 ),
30 # File with two symbols, one unchanged, one deleted, same bucket
31 ( 'unchanged', 't', 1000, '/file_pair_unchanged_removed', 0x6 ),
32 ( 'removed', 't', 1000, '/file_pair_unchanged_removed', 0x7 ),
33 # File with two symbols, one unchanged, one added, same bucket
34 ( 'unchanged', 't', 1000, '/file_pair_unchanged_added', 0x8 ),
35 # File with two symbols, one unchanged, one changed, different bucket
36 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_changed', 0x9 ),
37 ( 'changed', '@', 1000, '/file_pair_unchanged_diffbuck_changed', 0xa ),
38 # File with two symbols, one unchanged, one deleted, different bucket
39 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_removed', 0xb ),
40 ( 'removed', '@', 1000, '/file_pair_unchanged_diffbuck_removed', 0xc ),
41 # File with two symbols, one unchanged, one added, different bucket
42 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_added', 0xd ),
43 # File with four symbols, one added, one removed,
44 # one changed, one unchanged
45 ( 'size_changed', 't', 1000, '/file_tetra', 0xe ),
46 ( 'removed', 't', 1000, '/file_tetra', 0xf ),
47 ( 'unchanged', 't', 1000, '/file_tetra', 0x10 ),
51 # File with one symbol, left as-is.
52 ( 'unchanged', 't', 1000, '/file_unchanged', 0x1 ),
53 # File with one symbol, changed.
54 ( 'changed', 't', 2000, '/file_all_changed', 0x2 ),
55 # File with two symbols, one unchanged, one changed, same bucket
56 ( 'unchanged', 't', 1000, '/file_pair_unchanged_changed', 0x3 ),
57 ( 'changed', 't', 2000, '/file_pair_unchanged_changed', 0x4 ),
58 # File with two symbols, one unchanged, one deleted, same bucket
59 ( 'unchanged', 't', 1000, '/file_pair_unchanged_removed', 0x5 ),
60 # File with two symbols, one unchanged, one added, same bucket
61 ( 'unchanged', 't', 1000, '/file_pair_unchanged_added', 0x6 ),
62 ( 'added', 't', 1000, '/file_pair_unchanged_added', 0x7 ),
63 # File with two symbols, one unchanged, one changed, different bucket
64 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_changed', 0x8 ),
65 ( 'changed', '@', 2000, '/file_pair_unchanged_diffbuck_changed', 0x9 ),
66 # File with two symbols, one unchanged, one deleted, different bucket
67 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_removed', 0xa ),
68 # File with two symbols, one unchanged, one added, different bucket
69 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_added', 0xb ),
70 ( 'added', '@', 1000, '/file_pair_unchanged_diffbuck_added', 0xc ),
71 # File with four symbols, one added, one removed,
72 # one changed, one unchanged
73 ( 'size_changed', 't', 2000, '/file_tetra', 0xd ),
74 ( 'unchanged', 't', 1000, '/file_tetra', 0xe ),
75 ( 'added', 't', 1000, '/file_tetra', 0xf ),
76 # New file with one symbol added
77 ( 'added', 't', 1000, '/file_new', 0x10 ),
81 (added
, removed
, changed
, unchanged
) = \
82 explain_binary_size_delta
.Compare(symbol_list1
, symbol_list2
)
84 def delta(file_path
, symbol_type
, symbol_name
, old_size
, new_size
):
85 delta_info
= explain_binary_size_delta
.DeltaInfo(
86 file_path
, symbol_type
, symbol_name
, False)
87 delta_info
.old_size
= old_size
88 delta_info
.new_size
= new_size
91 # File with one symbol, left as-is.
92 assert delta('/file_unchanged', 't', 'unchanged', 1000, 1000) in unchanged
93 # File with one symbol, changed.
94 assert delta('/file_all_changed', 't', 'changed', 1000, 2000) in changed
95 # File with one symbol, deleted.
96 assert delta('/file_all_deleted', 't', 'removed', 1000, None) in removed
97 # New file with one symbol added
98 assert delta('/file_new', 't', 'added', None, 1000) in added
99 # File with two symbols, one unchanged, one changed, same bucket
100 assert delta('/file_pair_unchanged_changed',
101 't', 'unchanged', 1000, 1000) in unchanged
102 assert delta('/file_pair_unchanged_changed',
103 't', 'changed', 1000, 2000) in changed
104 # File with two symbols, one unchanged, one removed, same bucket
105 assert delta('/file_pair_unchanged_removed',
106 't', 'unchanged', 1000, 1000) in unchanged
107 assert delta('/file_pair_unchanged_removed',
108 't', 'removed', 1000, None) in removed
109 # File with two symbols, one unchanged, one added, same bucket
110 assert delta('/file_pair_unchanged_added',
111 't', 'unchanged', 1000, 1000) in unchanged
112 assert delta('/file_pair_unchanged_added',
113 't', 'added', None, 1000) in added
114 # File with two symbols, one unchanged, one changed, different bucket
115 assert delta('/file_pair_unchanged_diffbuck_changed',
116 't', 'unchanged', 1000, 1000) in unchanged
117 assert delta('/file_pair_unchanged_diffbuck_changed',
118 '@', 'changed', 1000, 2000) in changed
119 # File with two symbols, one unchanged, one removed, different bucket
120 assert delta('/file_pair_unchanged_diffbuck_removed',
121 't', 'unchanged', 1000, 1000) in unchanged
122 assert delta('/file_pair_unchanged_diffbuck_removed',
123 '@', 'removed', 1000, None) in removed
124 # File with two symbols, one unchanged, one added, different bucket
125 assert delta('/file_pair_unchanged_diffbuck_added',
126 't', 'unchanged', 1000, 1000) in unchanged
127 assert delta('/file_pair_unchanged_diffbuck_added',
128 '@', 'added', None, 1000) in added
129 # File with four symbols, one added, one removed, one changed, one unchanged
130 assert delta('/file_tetra', 't', 'size_changed', 1000, 2000) in changed
131 assert delta('/file_tetra', 't', 'unchanged', 1000, 1000) in unchanged
132 assert delta('/file_tetra', 't', 'added', None, 1000) in added
133 assert delta('/file_tetra', 't', 'removed', 1000, None) in removed
135 # Now check final stats.
136 orig_stdout
= sys
.stdout
137 output_collector
= cStringIO
.StringIO()
138 sys
.stdout
= output_collector
140 explain_binary_size_delta
.CrunchStats(added
, removed
, changed
,
141 unchanged
, True, True)
143 sys
.stdout
= orig_stdout
144 result
= output_collector
.getvalue()
146 expected_output
= """\
147 Total change: +4000 bytes
148 =========================
149 4 added, totalling +4000 bytes across 4 sources
150 4 removed, totalling -4000 bytes across 4 sources
151 4 grown, for a net change of +4000 bytes \
152 (4000 bytes before, 8000 bytes after) across 4 sources
153 8 unchanged, totalling 8000 bytes
155 11 sources encountered.
157 1 removed completely.
159 1 completely unchanged.
162 --------------------------------------------------
163 +1000 - Source: /file_new - (gained 1000, lost 0)
164 --------------------------------------------------
166 +1000: added type=t, size=1000 bytes
168 ---------------------------------------------------------------------
169 +1000 - Source: /file_pair_unchanged_changed - (gained 1000, lost 0)
170 ---------------------------------------------------------------------
172 +1000: changed type=t, (was 1000 bytes, now 2000 bytes)
174 ----------------------------------------------------------------------------
175 +1000 - Source: /file_pair_unchanged_diffbuck_added - (gained 1000, lost 0)
176 ----------------------------------------------------------------------------
178 +1000: added type=@, size=1000 bytes
180 -------------------------------------------------------------------
181 +1000 - Source: /file_pair_unchanged_added - (gained 1000, lost 0)
182 -------------------------------------------------------------------
184 +1000: added type=t, size=1000 bytes
186 ------------------------------------------------------------------------------
187 +1000 - Source: /file_pair_unchanged_diffbuck_changed - (gained 1000, lost 0)
188 ------------------------------------------------------------------------------
190 +1000: changed type=@, (was 1000 bytes, now 2000 bytes)
192 ----------------------------------------------------------
193 +1000 - Source: /file_all_changed - (gained 1000, lost 0)
194 ----------------------------------------------------------
196 +1000: changed type=t, (was 1000 bytes, now 2000 bytes)
198 -------------------------------------------------------
199 +1000 - Source: /file_tetra - (gained 2000, lost 1000)
200 -------------------------------------------------------
202 +1000: added type=t, size=1000 bytes
204 -1000: removed type=t, size=1000 bytes
206 +1000: size_changed type=t, (was 1000 bytes, now 2000 bytes)
208 ------------------------------------------------------------------------------
209 -1000 - Source: /file_pair_unchanged_diffbuck_removed - (gained 0, lost 1000)
210 ------------------------------------------------------------------------------
212 -1000: removed type=@, size=1000 bytes
214 ----------------------------------------------------------
215 -1000 - Source: /file_all_deleted - (gained 0, lost 1000)
216 ----------------------------------------------------------
218 -1000: removed type=t, size=1000 bytes
220 ---------------------------------------------------------------------
221 -1000 - Source: /file_pair_unchanged_removed - (gained 0, lost 1000)
222 ---------------------------------------------------------------------
224 -1000: removed type=t, size=1000 bytes
228 self
.assertMultiLineEqual(expected_output
, result
)
231 def testCompareStringEntries(self
):
232 # List entries have form:
233 # symbol_name, symbol_type, symbol_size, file_path, memory_address
235 # File with one string.
236 ( '.L.str107', 'r', 8, '/file_with_strs', 0x1 ),
240 # Two files with one string each, same name.
241 ( '.L.str107', 'r', 8, '/file_with_strs', 0x1 ),
242 ( '.L.str107', 'r', 7, '/other_file_with_strs', 0x2 ),
246 (added
, removed
, changed
, unchanged
) = \
247 explain_binary_size_delta
.Compare(symbol_list1
, symbol_list2
)
250 # Now check final stats.
251 orig_stdout
= sys
.stdout
252 output_collector
= cStringIO
.StringIO()
253 sys
.stdout
= output_collector
255 explain_binary_size_delta
.CrunchStats(added
, removed
, changed
,
256 unchanged
, True, True)
258 sys
.stdout
= orig_stdout
259 result
= output_collector
.getvalue()
261 expected_output
= """\
262 Total change: +7 bytes
263 ======================
264 1 added, totalling +7 bytes across 1 sources
265 1 unchanged, totalling 8 bytes
267 2 sources encountered.
269 0 removed completely.
271 1 completely unchanged.
274 --------------------------------------------------------
275 +7 - Source: /other_file_with_strs - (gained 7, lost 0)
276 --------------------------------------------------------
278 +7: .L.str107 type=r, size=7 bytes
282 self
.assertMultiLineEqual(expected_output
, result
)
284 def testCompareStringEntriesWithNoFile(self
):
285 # List entries have form:
286 # symbol_name, symbol_type, symbol_size, file_path, memory_address
288 ( '.L.str104', 'r', 21, '??', 0x1 ), # Will change size.
289 ( '.L.str105', 'r', 17, '??', 0x2 ), # Same.
290 ( '.L.str106', 'r', 13, '??', 0x3 ), # Will be removed.
291 ( '.L.str106', 'r', 3, '??', 0x4 ), # Same.
292 ( '.L.str106', 'r', 3, '??', 0x5 ), # Will be removed.
293 ( '.L.str107', 'r', 8, '??', 0x6 ), # Will be removed (other sizes).
297 # Two files with one string each, same name.
298 ( '.L.str104', 'r', 19, '??', 0x1 ), # Changed.
299 ( '.L.str105', 'r', 11, '??', 0x2 ), # New size for multi-symbol.
300 ( '.L.str105', 'r', 17, '??', 0x3 ), # New of same size for multi-symbol.
301 ( '.L.str105', 'r', 17, '??', 0x4 ), # Same.
302 ( '.L.str106', 'r', 3, '??', 0x5 ), # Same.
303 ( '.L.str107', 'r', 5, '??', 0x6 ), # New size for symbol.
304 ( '.L.str107', 'r', 7, '??', 0x7 ), # New size for symbol.
305 ( '.L.str108', 'r', 8, '??', 0x8 ), # New symbol.
309 (added
, removed
, changed
, unchanged
) = \
310 explain_binary_size_delta
.Compare(symbol_list1
, symbol_list2
)
313 # Now check final stats.
314 orig_stdout
= sys
.stdout
315 output_collector
= cStringIO
.StringIO()
316 sys
.stdout
= output_collector
318 explain_binary_size_delta
.CrunchStats(added
, removed
, changed
,
319 unchanged
, True, True)
321 sys
.stdout
= orig_stdout
322 result
= output_collector
.getvalue()
324 expected_output
= """\
325 Total change: +22 bytes
326 =======================
327 5 added, totalling +48 bytes across 1 sources
328 3 removed, totalling -24 bytes across 1 sources
329 1 shrunk, for a net change of -2 bytes (21 bytes before, 19 bytes after) \
331 2 unchanged, totalling 20 bytes
333 1 sources encountered.
335 0 removed completely.
337 0 completely unchanged.
340 ----------------------------------------
341 +22 - Source: ?? - (gained 48, lost 26)
342 ----------------------------------------
344 +17: .L.str105 type=r, size=17 bytes
345 +11: .L.str105 type=r, size=11 bytes
346 +8: .L.str108 type=r, size=8 bytes
347 +7: .L.str107 type=r, size=7 bytes
348 +5: .L.str107 type=r, size=5 bytes
350 -3: .L.str106 type=r, size=3 bytes
351 -8: .L.str107 type=r, size=8 bytes
352 -13: .L.str106 type=r, size=13 bytes
354 -2: .L.str104 type=r, (was 21 bytes, now 19 bytes)
358 self
.assertMultiLineEqual(expected_output
, result
)
360 def testCompareSharedSpace(self
):
361 # List entries have form:
362 # symbol_name, symbol_type, symbol_size, file_path, memory_address
364 # File with two symbols, same address.
365 ( 'sym1', 'r', 8, '/file', 0x1 ),
366 ( 'sym2', 'r', 8, '/file', 0x1 ),
370 # File with two symbols, same address.
371 ( 'sym1', 'r', 4, '/file', 0x1 ),
372 ( 'sym2', 'r', 4, '/file', 0x1 ),
376 (added
, removed
, changed
, unchanged
) = \
377 explain_binary_size_delta
.Compare(symbol_list1
, symbol_list2
)
380 # Now check final stats.
381 orig_stdout
= sys
.stdout
382 output_collector
= cStringIO
.StringIO()
383 sys
.stdout
= output_collector
385 explain_binary_size_delta
.CrunchStats(added
, removed
, changed
,
386 unchanged
, True, True)
388 sys
.stdout
= orig_stdout
389 result
= output_collector
.getvalue()
391 expected_output
= """\
392 Total change: -4 bytes
393 ======================
394 2 shrunk, for a net change of -4 bytes (8 bytes before, 4 bytes after) \
396 0 unchanged, totalling 0 bytes
398 1 sources encountered.
400 0 removed completely.
402 0 completely unchanged.
405 ----------------------------------------
406 -4 - Source: /file - (gained 0, lost 4)
407 ----------------------------------------
409 -2: sym1 type=r, (was 4 bytes, now 2 bytes) (adjusted sizes because \
411 -2: sym2 type=r, (was 4 bytes, now 2 bytes) (adjusted sizes because \
416 self
.assertMultiLineEqual(expected_output
, result
)
419 def testCompareSharedSpaceDuplicateSymbols(self
):
420 # List entries have form:
421 # symbol_name, symbol_type, symbol_size, file_path, memory_address
423 # File with two symbols, same address.
424 ( 'sym1', 'r', 7, '/file', 0x2 ),
425 ( 'sym1', 'r', 8, '/file', 0x1 ),
426 ( 'sym2', 'r', 8, '/file', 0x1 ),
430 # File with two symbols, same address.
431 ( 'sym1', 'r', 7, '/file', 0x2 ),
432 ( 'sym1', 'r', 4, '/file', 0x1 ),
433 ( 'sym2', 'r', 4, '/file', 0x1 ),
437 (added
, removed
, changed
, unchanged
) = \
438 explain_binary_size_delta
.Compare(symbol_list1
, symbol_list2
)
441 # Now check final stats.
442 orig_stdout
= sys
.stdout
443 output_collector
= cStringIO
.StringIO()
444 sys
.stdout
= output_collector
446 explain_binary_size_delta
.CrunchStats(added
, removed
, changed
,
447 unchanged
, True, True)
449 sys
.stdout
= orig_stdout
450 result
= output_collector
.getvalue()
452 expected_output
= """\
453 Total change: -4 bytes
454 ======================
455 1 added, totalling +2 bytes across 1 sources
456 1 removed, totalling -4 bytes across 1 sources
457 1 shrunk, for a net change of -2 bytes (4 bytes before, 2 bytes after) \
459 1 unchanged, totalling 7 bytes
461 1 sources encountered.
463 0 removed completely.
465 0 completely unchanged.
468 ----------------------------------------
469 -4 - Source: /file - (gained 2, lost 6)
470 ----------------------------------------
472 +2: sym1 type=r, size=2 bytes (adjusted sizes because of memory \
475 -4: sym1 type=r, size=4 bytes (adjusted sizes because of memory \
478 -2: sym2 type=r, (was 4 bytes, now 2 bytes) (adjusted sizes because \
483 self
.assertMultiLineEqual(expected_output
, result
)
485 def testCompareSharedSpaceBecomingUnshared(self
):
486 # List entries have form:
487 # symbol_name, symbol_type, symbol_size, file_path, memory_address
489 # File with two symbols, same address.
490 ( 'sym1', 'r', 8, '/file', 0x1 ),
491 ( 'sym2', 'r', 8, '/file', 0x1 ),
495 # File with two symbols, not the same address.
496 ( 'sym1', 'r', 8, '/file', 0x1 ),
497 ( 'sym2', 'r', 6, '/file', 0x2 ),
501 (added
, removed
, changed
, unchanged
) = \
502 explain_binary_size_delta
.Compare(symbol_list1
, symbol_list2
)
505 # Now check final stats.
506 orig_stdout
= sys
.stdout
507 output_collector
= cStringIO
.StringIO()
508 sys
.stdout
= output_collector
510 explain_binary_size_delta
.CrunchStats(added
, removed
, changed
,
511 unchanged
, True, True)
513 sys
.stdout
= orig_stdout
514 result
= output_collector
.getvalue()
516 expected_output
= """\
517 Total change: +6 bytes
518 ======================
519 2 grown, for a net change of +6 bytes (8 bytes before, 14 bytes after) \
521 0 unchanged, totalling 0 bytes
523 1 sources encountered.
525 0 removed completely.
527 0 completely unchanged.
530 ----------------------------------------
531 +6 - Source: /file - (gained 6, lost 0)
532 ----------------------------------------
534 +4: sym1 type=r, (was 4 bytes, now 8 bytes) (adjusted sizes because \
536 +2: sym2 type=r, (was 4 bytes, now 6 bytes) (adjusted sizes because \
541 self
.assertMultiLineEqual(expected_output
, result
)
543 def testCompareSymbolsBecomingUnshared(self
):
544 # List entries have form:
545 # symbol_name, symbol_type, symbol_size, file_path, memory_address
547 # File with two symbols, not the same address.
548 ( 'sym1', 'r', 8, '/file', 0x1 ),
549 ( 'sym2', 'r', 6, '/file', 0x2 ),
553 # File with two symbols, same address.
554 ( 'sym1', 'r', 8, '/file', 0x1 ),
555 ( 'sym2', 'r', 8, '/file', 0x1 ),
559 (added
, removed
, changed
, unchanged
) = \
560 explain_binary_size_delta
.Compare(symbol_list1
, symbol_list2
)
563 # Now check final stats.
564 orig_stdout
= sys
.stdout
565 output_collector
= cStringIO
.StringIO()
566 sys
.stdout
= output_collector
568 explain_binary_size_delta
.CrunchStats(added
, removed
, changed
,
569 unchanged
, True, True)
571 sys
.stdout
= orig_stdout
572 result
= output_collector
.getvalue()
574 expected_output
= """\
575 Total change: -6 bytes
576 ======================
577 2 shrunk, for a net change of -6 bytes (14 bytes before, 8 bytes after) \
579 0 unchanged, totalling 0 bytes
581 1 sources encountered.
583 0 removed completely.
585 0 completely unchanged.
588 ----------------------------------------
589 -6 - Source: /file - (gained 0, lost 6)
590 ----------------------------------------
592 -2: sym2 type=r, (was 6 bytes, now 4 bytes) (adjusted sizes because \
594 -4: sym1 type=r, (was 8 bytes, now 4 bytes) (adjusted sizes because \
599 self
.assertMultiLineEqual(expected_output
, result
)
601 def testDeltaInfo(self
):
602 x
= explain_binary_size_delta
.DeltaInfo("path", "t", "sym_name", False)
604 y
= explain_binary_size_delta
.DeltaInfo("path", "t", "sym_name", False)
613 z
= explain_binary_size_delta
.DeltaInfo("path", "t", "sym_name", True)
617 w
= explain_binary_size_delta
.DeltaInfo("other_path", "t", "sym_name", True)
620 if __name__
== '__main__':