AttachmentDownloader implementation.
[chromium-blink-merge.git] / tools / binary_size / explain_binary_size_delta_unittest.py
blob6a004e25a6cd3d1a66968c8d217055c0a57801c6
1 #!/usr/bin/env python
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."""
8 import cStringIO
9 import sys
10 import unittest
12 import explain_binary_size_delta
15 class ExplainBinarySizeDeltaTest(unittest.TestCase):
17 def testCompare(self):
18 # List entries have form: symbol_name, symbol_type, symbol_size, file_path
19 symbol_list1 = (
20 # File with one symbol, left as-is.
21 ( 'unchanged', 't', 1000, '/file_unchanged' ),
22 # File with one symbol, changed.
23 ( 'changed', 't', 1000, '/file_all_changed' ),
24 # File with one symbol, deleted.
25 ( 'removed', 't', 1000, '/file_all_deleted' ),
26 # File with two symbols, one unchanged, one changed, same bucket
27 ( 'unchanged', 't', 1000, '/file_pair_unchanged_changed' ),
28 ( 'changed', 't', 1000, '/file_pair_unchanged_changed' ),
29 # File with two symbols, one unchanged, one deleted, same bucket
30 ( 'unchanged', 't', 1000, '/file_pair_unchanged_removed' ),
31 ( 'removed', 't', 1000, '/file_pair_unchanged_removed' ),
32 # File with two symbols, one unchanged, one added, same bucket
33 ( 'unchanged', 't', 1000, '/file_pair_unchanged_added' ),
34 # File with two symbols, one unchanged, one changed, different bucket
35 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_changed' ),
36 ( 'changed', '@', 1000, '/file_pair_unchanged_diffbuck_changed' ),
37 # File with two symbols, one unchanged, one deleted, different bucket
38 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_removed' ),
39 ( 'removed', '@', 1000, '/file_pair_unchanged_diffbuck_removed' ),
40 # File with two symbols, one unchanged, one added, different bucket
41 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_added' ),
42 # File with four symbols, one added, one removed,
43 # one changed, one unchanged
44 ( 'size_changed', 't', 1000, '/file_tetra' ),
45 ( 'removed', 't', 1000, '/file_tetra' ),
46 ( 'unchanged', 't', 1000, '/file_tetra' ),
49 symbol_list2 = (
50 # File with one symbol, left as-is.
51 ( 'unchanged', 't', 1000, '/file_unchanged' ),
52 # File with one symbol, changed.
53 ( 'changed', 't', 2000, '/file_all_changed' ),
54 # File with two symbols, one unchanged, one changed, same bucket
55 ( 'unchanged', 't', 1000, '/file_pair_unchanged_changed' ),
56 ( 'changed', 't', 2000, '/file_pair_unchanged_changed' ),
57 # File with two symbols, one unchanged, one deleted, same bucket
58 ( 'unchanged', 't', 1000, '/file_pair_unchanged_removed' ),
59 # File with two symbols, one unchanged, one added, same bucket
60 ( 'unchanged', 't', 1000, '/file_pair_unchanged_added' ),
61 ( 'added', 't', 1000, '/file_pair_unchanged_added' ),
62 # File with two symbols, one unchanged, one changed, different bucket
63 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_changed' ),
64 ( 'changed', '@', 2000, '/file_pair_unchanged_diffbuck_changed' ),
65 # File with two symbols, one unchanged, one deleted, different bucket
66 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_removed' ),
67 # File with two symbols, one unchanged, one added, different bucket
68 ( 'unchanged', 't', 1000, '/file_pair_unchanged_diffbuck_added' ),
69 ( 'added', '@', 1000, '/file_pair_unchanged_diffbuck_added' ),
70 # File with four symbols, one added, one removed,
71 # one changed, one unchanged
72 ( 'size_changed', 't', 2000, '/file_tetra' ),
73 ( 'unchanged', 't', 1000, '/file_tetra' ),
74 ( 'added', 't', 1000, '/file_tetra' ),
75 # New file with one symbol added
76 ( 'added', 't', 1000, '/file_new' ),
79 # Here we go
80 (added, removed, changed, unchanged) = \
81 explain_binary_size_delta.Compare(symbol_list1, symbol_list2)
83 # File with one symbol, left as-is.
84 assert ('/file_unchanged', 't', 'unchanged', 1000, 1000) in unchanged
85 # File with one symbol, changed.
86 assert ('/file_all_changed', 't', 'changed', 1000, 2000) in changed
87 # File with one symbol, deleted.
88 assert ('/file_all_deleted', 't', 'removed', 1000, None) in removed
89 # New file with one symbol added
90 assert ('/file_new', 't', 'added', None, 1000) in added
91 # File with two symbols, one unchanged, one changed, same bucket
92 assert ('/file_pair_unchanged_changed',
93 't', 'unchanged', 1000, 1000) in unchanged
94 assert ('/file_pair_unchanged_changed',
95 't', 'changed', 1000, 2000) in changed
96 # File with two symbols, one unchanged, one removed, same bucket
97 assert ('/file_pair_unchanged_removed',
98 't', 'unchanged', 1000, 1000) in unchanged
99 assert ('/file_pair_unchanged_removed',
100 't', 'removed', 1000, None) in removed
101 # File with two symbols, one unchanged, one added, same bucket
102 assert ('/file_pair_unchanged_added',
103 't', 'unchanged', 1000, 1000) in unchanged
104 assert ('/file_pair_unchanged_added',
105 't', 'added', None, 1000) in added
106 # File with two symbols, one unchanged, one changed, different bucket
107 assert ('/file_pair_unchanged_diffbuck_changed',
108 't', 'unchanged', 1000, 1000) in unchanged
109 assert ('/file_pair_unchanged_diffbuck_changed',
110 '@', 'changed', 1000, 2000) in changed
111 # File with two symbols, one unchanged, one removed, different bucket
112 assert ('/file_pair_unchanged_diffbuck_removed',
113 't', 'unchanged', 1000, 1000) in unchanged
114 assert ('/file_pair_unchanged_diffbuck_removed',
115 '@', 'removed', 1000, None) in removed
116 # File with two symbols, one unchanged, one added, different bucket
117 assert ('/file_pair_unchanged_diffbuck_added',
118 't', 'unchanged', 1000, 1000) in unchanged
119 assert ('/file_pair_unchanged_diffbuck_added',
120 '@', 'added', None, 1000) in added
121 # File with four symbols, one added, one removed, one changed, one unchanged
122 assert ('/file_tetra', 't', 'size_changed', 1000, 2000) in changed
123 assert ('/file_tetra', 't', 'unchanged', 1000, 1000) in unchanged
124 assert ('/file_tetra', 't', 'added', None, 1000) in added
125 assert ('/file_tetra', 't', 'removed', 1000, None) in removed
127 # Now check final stats.
128 orig_stdout = sys.stdout
129 output_collector = cStringIO.StringIO()
130 sys.stdout = output_collector
131 try:
132 explain_binary_size_delta.CrunchStats(added, removed, changed,
133 unchanged, True, True)
134 finally:
135 sys.stdout = orig_stdout
136 result = output_collector.getvalue()
138 expected_output = """\
139 Symbol statistics:
140 4 added, totalling 4000 bytes across 4 sources
141 4 removed, totalling 4000 bytes removed across 4 sources
142 4 changed, resulting in a net change of 4000 bytes \
143 (4000 bytes before, 8000 bytes after) across 4 sources
144 8 unchanged, totalling 8000 bytes
145 Source stats:
146 11 sources encountered.
147 1 completely new.
148 1 removed completely.
149 8 partially changed.
150 1 completely unchanged.
151 Per-source Analysis:
152 Source: /file_all_changed
153 Change: 1000 bytes (gained 1000, lost 0)
154 Changed symbols:
155 changed type=t, delta=1000 bytes (was 1000 bytes, now 2000 bytes)
156 Source: /file_all_deleted
157 Change: -1000 bytes (gained 0, lost 1000)
158 Removed symbols:
159 removed type=t, size=1000 bytes
160 Source: /file_new
161 Change: 1000 bytes (gained 1000, lost 0)
162 New symbols:
163 added type=t, size=1000 bytes
164 Source: /file_pair_unchanged_added
165 Change: 1000 bytes (gained 1000, lost 0)
166 New symbols:
167 added type=t, size=1000 bytes
168 Source: /file_pair_unchanged_changed
169 Change: 1000 bytes (gained 1000, lost 0)
170 Changed symbols:
171 changed type=t, delta=1000 bytes (was 1000 bytes, now 2000 bytes)
172 Source: /file_pair_unchanged_diffbuck_added
173 Change: 1000 bytes (gained 1000, lost 0)
174 New symbols:
175 added type=@, size=1000 bytes
176 Source: /file_pair_unchanged_diffbuck_changed
177 Change: 1000 bytes (gained 1000, lost 0)
178 Changed symbols:
179 changed type=@, delta=1000 bytes (was 1000 bytes, now 2000 bytes)
180 Source: /file_pair_unchanged_diffbuck_removed
181 Change: -1000 bytes (gained 0, lost 1000)
182 Removed symbols:
183 removed type=@, size=1000 bytes
184 Source: /file_pair_unchanged_removed
185 Change: -1000 bytes (gained 0, lost 1000)
186 Removed symbols:
187 removed type=t, size=1000 bytes
188 Source: /file_tetra
189 Change: 1000 bytes (gained 2000, lost 1000)
190 New symbols:
191 added type=t, size=1000 bytes
192 Removed symbols:
193 removed type=t, size=1000 bytes
194 Changed symbols:
195 size_changed type=t, delta=1000 bytes (was 1000 bytes, now 2000 bytes)
198 self.maxDiff = None
199 self.assertMultiLineEqual(expected_output, result)
200 print "explain_binary_size_delta_unittest: All tests passed"
203 if __name__ == '__main__':
204 unittest.main()