Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: tools/grit/grit/format/data_pack_unittest.py

Issue 2969123002: Add deduplication logic to .pak files (Closed)
Patch Set: sizeof() Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/grit/grit/format/data_pack.py ('k') | tools/grit/pak_util.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 '''Unit tests for grit.format.data_pack''' 6 '''Unit tests for grit.format.data_pack'''
7 7
8 8
9 import os 9 import os
10 import sys 10 import sys
11 if __name__ == '__main__': 11 if __name__ == '__main__':
12 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) 12 sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
13 13
14 import unittest 14 import unittest
15 15
16 from grit.format import data_pack 16 from grit.format import data_pack
17 17
18 18
19 class FormatDataPackUnittest(unittest.TestCase): 19 class FormatDataPackUnittest(unittest.TestCase):
20 def testWriteDataPack(self): 20 def testReadDataPackV4(self):
21 expected = ( 21 expected_data = (
22 '\x04\x00\x00\x00' # header(version 22 '\x04\x00\x00\x00' # header(version
23 '\x04\x00\x00\x00' # no. entries, 23 '\x04\x00\x00\x00' # no. entries,
24 '\x01' # encoding) 24 '\x01' # encoding)
25 '\x01\x00\x27\x00\x00\x00' # index entry 1 25 '\x01\x00\x27\x00\x00\x00' # index entry 1
26 '\x04\x00\x27\x00\x00\x00' # index entry 4 26 '\x04\x00\x27\x00\x00\x00' # index entry 4
27 '\x06\x00\x33\x00\x00\x00' # index entry 6 27 '\x06\x00\x33\x00\x00\x00' # index entry 6
28 '\x0a\x00\x3f\x00\x00\x00' # index entry 10 28 '\x0a\x00\x3f\x00\x00\x00' # index entry 10
29 '\x00\x00\x3f\x00\x00\x00' # extra entry for the size of last 29 '\x00\x00\x3f\x00\x00\x00' # extra entry for the size of last
30 'this is id 4this is id 6') # data 30 'this is id 4this is id 6') # data
31 input = {1: '', 4: 'this is id 4', 6: 'this is id 6', 10: ''} 31 expected_resources = {
32 output = data_pack.WriteDataPackToString(input, data_pack.UTF8) 32 1: '',
33 self.failUnless(output == expected) 33 4: 'this is id 4',
34 6: 'this is id 6',
35 10: '',
36 }
37 expected_data_pack = data_pack.DataPackContents(
38 expected_resources, data_pack.UTF8)
39 loaded = data_pack.ReadDataPackFromString(expected_data)
40 self.assertEquals(loaded, expected_data_pack)
41
42 def testReadWriteDataPackV5(self):
43 expected_data = (
44 '\x05\x00\x00\x00' # version
45 '\x01\x00\x00\x00' # encoding & padding
46 '\x03\x00' # resource_count
47 '\x01\x00' # alias_count
48 '\x01\x00\x28\x00\x00\x00' # index entry 1
49 '\x04\x00\x28\x00\x00\x00' # index entry 4
50 '\x06\x00\x34\x00\x00\x00' # index entry 6
51 '\x00\x00\x40\x00\x00\x00' # extra entry for the size of last
52 '\x0a\x00\x01\x00' # alias table
53 'this is id 4this is id 6') # data
54 expected_resources = {
55 1: '',
56 4: 'this is id 4',
57 6: 'this is id 6',
58 10: 'this is id 4',
59 }
60 data = data_pack.WriteDataPackToString(expected_resources, data_pack.UTF8)
61 self.assertEquals(data, expected_data)
62
63 expected_data_pack = data_pack.DataPackContents(
64 expected_resources, data_pack.UTF8)
65 loaded = data_pack.ReadDataPackFromString(expected_data)
66 self.assertEquals(loaded, expected_data_pack)
34 67
35 def testRePackUnittest(self): 68 def testRePackUnittest(self):
36 expected_with_whitelist = { 69 expected_with_whitelist = {
37 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let', 70 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let',
38 30: 'you down', 40: 'Never', 50: 'gonna run around and', 71 30: 'you down', 40: 'Never', 50: 'gonna run around and',
39 60: 'desert you'} 72 60: 'desert you'}
40 expected_without_whitelist = { 73 expected_without_whitelist = {
41 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let', 65: 'Close', 74 1: 'Never gonna', 10: 'give you up', 20: 'Never gonna let', 65: 'Close',
42 30: 'you down', 40: 'Never', 50: 'gonna run around and', 4: 'click', 75 30: 'you down', 40: 'Never', 50: 'gonna run around and', 4: 'click',
43 60: 'desert you', 6: 'chirr', 32: 'oops, try again', 70: 'Awww, snap!'} 76 60: 'desert you', 6: 'chirr', 32: 'oops, try again', 70: 'Awww, snap!'}
44 inputs = [{1: 'Never gonna', 4: 'click', 6: 'chirr', 10: 'give you up'}, 77 inputs = [{1: 'Never gonna', 4: 'click', 6: 'chirr', 10: 'give you up'},
45 {20: 'Never gonna let', 30: 'you down', 32: 'oops, try again'}, 78 {20: 'Never gonna let', 30: 'you down', 32: 'oops, try again'},
46 {40: 'Never', 50: 'gonna run around and', 60: 'desert you'}, 79 {40: 'Never', 50: 'gonna run around and', 60: 'desert you'},
47 {65: 'Close', 70: 'Awww, snap!'}] 80 {65: 'Close', 70: 'Awww, snap!'}]
48 whitelist = [1, 10, 20, 30, 40, 50, 60] 81 whitelist = [1, 10, 20, 30, 40, 50, 60]
49 inputs = [data_pack.DataPackContents(input, data_pack.UTF8) for input 82 inputs = [data_pack.DataPackContents(input, data_pack.UTF8) for input
50 in inputs] 83 in inputs]
51 84
52 # RePack using whitelist 85 # RePack using whitelist
53 output, _ = data_pack.RePackFromDataPackStrings(inputs, whitelist) 86 output, _ = data_pack.RePackFromDataPackStrings(
87 inputs, whitelist, suppress_removed_key_output=True)
54 self.assertDictEqual(expected_with_whitelist, output, 88 self.assertDictEqual(expected_with_whitelist, output,
55 'Incorrect resource output') 89 'Incorrect resource output')
56 90
57 # RePack a None whitelist 91 # RePack a None whitelist
58 output, _ = data_pack.RePackFromDataPackStrings(inputs, None) 92 output, _ = data_pack.RePackFromDataPackStrings(
93 inputs, None, suppress_removed_key_output=True)
59 self.assertDictEqual(expected_without_whitelist, output, 94 self.assertDictEqual(expected_without_whitelist, output,
60 'Incorrect resource output') 95 'Incorrect resource output')
61 96
62 97
63 if __name__ == '__main__': 98 if __name__ == '__main__':
64 unittest.main() 99 unittest.main()
OLDNEW
« no previous file with comments | « tools/grit/grit/format/data_pack.py ('k') | tools/grit/pak_util.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698