Chromium Code Reviews| Index: tools/binary_size/explain_binary_size_delta_unittest.py |
| diff --git a/tools/binary_size/explain_binary_size_delta_unittest.py b/tools/binary_size/explain_binary_size_delta_unittest.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..58da34eac97e48812ac16c731f3a5afc3ef80652 |
| --- /dev/null |
| +++ b/tools/binary_size/explain_binary_size_delta_unittest.py |
| @@ -0,0 +1,210 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Check that explain_binary_size_delta seems to work.""" |
| + |
| +import cStringIO |
| +import sys |
|
Primiano Tucci (use gerrit)
2014/05/22 09:30:38
you can import unittest and use it, as all the oth
|
| + |
| +import explain_binary_size_delta |
| + |
| + |
| +def TestCompare(): |
|
Primiano Tucci (use gerrit)
2014/05/22 09:30:38
This should be
class TestCompare(unittest.TestCase
|
| + # List entries have form: symbol_name, symbol_type, symbol_size, file_path |
| + symbol_list1 = ( |
| + # File with one symbol, left as-is. |
| + ( 'unchanged', 't', 1000, '/file_unchanged' ), |
| + # File with one symbol, changed. |
| + ( 'changed', 't', 1000, '/file_all_changed' ), |
| + # File with one symbol, deleted. |
| + ( 'removed', 't', 1000, '/file_all_deleted' ), |
| + # File with two symbols, one unchanged, one changed, same bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_changed' ), |
| + ( 'changed', 't', 1000, '/file_pair_unchanged_changed' ), |
| + # File with two symbols, one unchanged, one deleted, same bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_removed' ), |
| + ( 'removed', 't', 1000, '/file_pair_unchanged_removed' ), |
| + # File with two symbols, one unchanged, one added, same bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_added' ), |
| + # File with two symbols, one unchanged, one changed, different bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_changed' ), |
| + ( 'changed', '@', 1000, '/file_pair_unchanged_diffbuck_changed' ), |
| + # File with two symbols, one unchanged, one deleted, different bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_removed' ), |
| + ( 'removed', '@', 1000, '/file_pair_unchanged_diffbuck_removed' ), |
| + # File with two symbols, one unchanged, one added, different bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_added' ), |
| + # File with four symbols, one added, one removed, one changed, one unchanged |
| + ( 'size_changed', 't', 1000, '/file_tetra' ), |
| + ( 'removed', 't', 1000, '/file_tetra' ), |
| + ( 'unchanged', 't', 1000, '/file_tetra' ), |
| + ) |
| + |
| + symbol_list2 = ( |
| + # File with one symbol, left as-is. |
| + ( 'unchanged', 't', 1000, '/file_unchanged' ), |
| + # File with one symbol, changed. |
| + ( 'changed', 't', 2000, '/file_all_changed' ), |
| + # File with two symbols, one unchanged, one changed, same bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_changed' ), |
| + ( 'changed', 't', 2000, '/file_pair_unchanged_changed' ), |
| + # File with two symbols, one unchanged, one deleted, same bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_removed' ), |
| + # File with two symbols, one unchanged, one added, same bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_added' ), |
| + ( 'added', 't', 1000, '/file_pair_unchanged_added' ), |
| + # File with two symbols, one unchanged, one changed, different bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_changed' ), |
| + ( 'changed', '@', 2000, '/file_pair_unchanged_diffbuck_changed' ), |
| + # File with two symbols, one unchanged, one deleted, different bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_removed' ), |
| + # File with two symbols, one unchanged, one added, different bucket |
| + ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_added' ), |
| + ( 'added', '@', 1000, '/file_pair_unchanged_diffbuck_added' ), |
| + # File with four symbols, one added, one removed, one changed, one unchanged |
| + ( 'size_changed', 't', 2000, '/file_tetra' ), |
| + ( 'unchanged', 't', 1000, '/file_tetra' ), |
| + ( 'added', 't', 1000, '/file_tetra' ), |
| + # New file with one symbol added |
| + ( 'added', 't', 1000, '/file_new' ), |
| + ) |
| + |
| + # Here we go |
| + (added, removed, changed, unchanged) = \ |
| + explain_binary_size_delta.Compare(symbol_list1, symbol_list2) |
| + |
| + # File with one symbol, left as-is. |
| + assert ('/file_unchanged', 't', 'unchanged', 1000, 1000) in unchanged |
|
Primiano Tucci (use gerrit)
2014/05/22 09:30:38
When you switch to python unittests, you could tak
|
| + # File with one symbol, changed. |
| + assert ('/file_all_changed', 't', 'changed', 1000, 2000) in changed |
| + # File with one symbol, deleted. |
| + assert ('/file_all_deleted', 't', 'removed', 1000, None) in removed |
| + # New file with one symbol added |
| + assert ('/file_new', 't', 'added', None, 1000) in added |
| + # File with two symbols, one unchanged, one changed, same bucket |
| + assert ('/file_pair_unchanged_changed', |
| + 't', 'unchanged', 1000, 1000) in unchanged |
| + assert ('/file_pair_unchanged_changed', |
| + 't', 'changed', 1000, 2000) in changed |
| + # File with two symbols, one unchanged, one removed, same bucket |
| + assert ('/file_pair_unchanged_removed', |
| + 't', 'unchanged', 1000, 1000) in unchanged |
| + assert ('/file_pair_unchanged_removed', |
| + 't', 'removed', 1000, None) in removed |
| + # File with two symbols, one unchanged, one added, same bucket |
| + assert ('/file_pair_unchanged_added', |
| + 't', 'unchanged', 1000, 1000) in unchanged |
| + assert ('/file_pair_unchanged_added', |
| + 't', 'added', None, 1000) in added |
| + # File with two symbols, one unchanged, one changed, different bucket |
| + assert ('/file_pair_unchanged_diffbuck_changed', |
| + 't', 'unchanged', 1000, 1000) in unchanged |
| + assert ('/file_pair_unchanged_diffbuck_changed', |
| + '@', 'changed', 1000, 2000) in changed |
| + # File with two symbols, one unchanged, one removed, different bucket |
| + assert ('/file_pair_unchanged_diffbuck_removed', |
| + 't', 'unchanged', 1000, 1000) in unchanged |
| + assert ('/file_pair_unchanged_diffbuck_removed', |
| + '@', 'removed', 1000, None) in removed |
| + # File with two symbols, one unchanged, one added, different bucket |
| + assert ('/file_pair_unchanged_diffbuck_added', |
| + 't', 'unchanged', 1000, 1000) in unchanged |
| + assert ('/file_pair_unchanged_diffbuck_added', |
| + '@', 'added', None, 1000) in added |
| + # File with four symbols, one added, one removed, one changed, one unchanged |
| + assert ('/file_tetra', 't', 'size_changed', 1000, 2000) in changed |
| + assert ('/file_tetra', 't', 'unchanged', 1000, 1000) in unchanged |
| + assert ('/file_tetra', 't', 'added', None, 1000) in added |
| + assert ('/file_tetra', 't', 'removed', 1000, None) in removed |
| + |
| + # Now check final stats. |
| + orig_stdout = sys.stdout |
| + output_collector = cStringIO.StringIO() |
| + sys.stdout = output_collector |
| + try: |
| + explain_binary_size_delta.CrunchStats(added, removed, changed, |
| + unchanged, True, True) |
| + finally: |
| + sys.stdout = orig_stdout |
| + result = output_collector.getvalue() |
| + |
| + expected_output = """\ |
| +Symbol statistics: |
| + 4 added, totalling 4000 bytes across 4 sources |
| + 4 removed, totalling 4000 bytes removed across 4 sources |
| + 4 changed, resulting in a net change of 4000 bytes \ |
| +(4000 bytes before, 8000 bytes after) across 4 sources |
| + 8 unchanged, totalling 8000 bytes |
| +Source stats: |
| + 11 sources encountered. |
| + 1 completely new. |
| + 1 removed completely. |
| + 8 partially changed. |
| + 1 completely unchanged. |
| +Per-source Analysis: |
| + Source: /file_all_changed |
| + Change: 1000 bytes (gained 1000, lost 0) |
| + Changed symbols: |
| + changed type=t, delta=1000 bytes (was 1000 bytes, now 2000 bytes) |
| + Source: /file_all_deleted |
| + Change: -1000 bytes (gained 0, lost 1000) |
| + Removed symbols: |
| + removed type=t, size=1000 bytes |
| + Source: /file_new |
| + Change: 1000 bytes (gained 1000, lost 0) |
| + New symbols: |
| + added type=t, size=1000 bytes |
| + Source: /file_pair_unchanged_added |
| + Change: 1000 bytes (gained 1000, lost 0) |
| + New symbols: |
| + added type=t, size=1000 bytes |
| + Source: /file_pair_unchanged_changed |
| + Change: 1000 bytes (gained 1000, lost 0) |
| + Changed symbols: |
| + changed type=t, delta=1000 bytes (was 1000 bytes, now 2000 bytes) |
| + Source: /file_pair_unchanged_diffbuck_added |
| + Change: 1000 bytes (gained 1000, lost 0) |
| + New symbols: |
| + added type=@, size=1000 bytes |
| + Source: /file_pair_unchanged_diffbuck_changed |
| + Change: 1000 bytes (gained 1000, lost 0) |
| + Changed symbols: |
| + changed type=@, delta=1000 bytes (was 1000 bytes, now 2000 bytes) |
| + Source: /file_pair_unchanged_diffbuck_removed |
| + Change: -1000 bytes (gained 0, lost 1000) |
| + Removed symbols: |
| + removed type=@, size=1000 bytes |
| + Source: /file_pair_unchanged_removed |
| + Change: -1000 bytes (gained 0, lost 1000) |
| + Removed symbols: |
| + removed type=t, size=1000 bytes |
| + Source: /file_tetra |
| + Change: 1000 bytes (gained 2000, lost 1000) |
| + New symbols: |
| + added type=t, size=1000 bytes |
| + Removed symbols: |
| + removed type=t, size=1000 bytes |
| + Changed symbols: |
| + size_changed type=t, delta=1000 bytes (was 1000 bytes, now 2000 bytes) |
| +""" |
| + |
| + if result != expected_output: |
|
Primiano Tucci (use gerrit)
2014/05/22 09:30:38
Lines 193-198 can be simply rewritten as:
self.ass
|
| + import difflib |
| + differ = difflib.Differ() |
| + assert False, "Unexpected output:\n" + \ |
| + "".join(differ.compare(expected_output.splitlines(1), |
| + result.splitlines(1))) |
| + |
| + print "explain_binary_size_delta_unittest: All tests passed" |
| + return 0 |
| + |
| + |
| +def main(): |
|
Primiano Tucci (use gerrit)
2014/05/22 09:30:38
remove this.
Python unittests can already auto-dis
|
| + """Main entry point.""" |
| + return TestCompare() |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |
|
Primiano Tucci (use gerrit)
2014/05/22 09:30:38
s/sys.exit(main())/unittest.main()/
|