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

Side by Side Diff: mojo/public/bindings/pylib/generate/mojom_pack_tests.py

Issue 226263002: Mojo: Move mojo/public/bindings to mojo/public/tools/bindings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 6 years, 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import mojom
6 import mojom_pack
7 import mojom_test
8 import sys
9
10 EXPECT_EQ = mojom_test.EXPECT_EQ
11 EXPECT_TRUE = mojom_test.EXPECT_TRUE
12 RunTest = mojom_test.RunTest
13
14
15 def TestOrdinalOrder():
16 errors = 0
17 struct = mojom.Struct('test')
18 struct.AddField('testfield1', mojom.INT32, 2)
19 struct.AddField('testfield2', mojom.INT32, 1)
20 ps = mojom_pack.PackedStruct(struct)
21
22 errors += EXPECT_EQ(2, len(ps.packed_fields))
23 errors += EXPECT_EQ('testfield2', ps.packed_fields[0].field.name)
24 errors += EXPECT_EQ('testfield1', ps.packed_fields[1].field.name)
25
26 return errors
27
28 def TestZeroFields():
29 errors = 0
30 struct = mojom.Struct('test')
31 ps = mojom_pack.PackedStruct(struct)
32 errors += EXPECT_EQ(0, len(ps.packed_fields))
33 return errors
34
35
36 def TestOneField():
37 errors = 0
38 struct = mojom.Struct('test')
39 struct.AddField('testfield1', mojom.INT8)
40 ps = mojom_pack.PackedStruct(struct)
41 errors += EXPECT_EQ(1, len(ps.packed_fields))
42 return errors
43
44 # Pass three tuples.
45 # |kinds| is a sequence of mojom.Kinds that specify the fields that are to
46 # be created.
47 # |fields| is the expected order of the resulting fields, with the integer
48 # "1" first.
49 # |offsets| is the expected order of offsets, with the integer "0" first.
50 def TestSequence(kinds, fields, offsets):
51 errors = 0
52 struct = mojom.Struct('test')
53 index = 1
54 for kind in kinds:
55 struct.AddField("%d" % index, kind)
56 index += 1
57 ps = mojom_pack.PackedStruct(struct)
58 num_fields = len(ps.packed_fields)
59 errors += EXPECT_EQ(len(kinds), num_fields)
60 for i in xrange(num_fields):
61 EXPECT_EQ("%d" % fields[i], ps.packed_fields[i].field.name)
62 EXPECT_EQ(offsets[i], ps.packed_fields[i].offset)
63
64 return errors
65
66
67 def TestPaddingPackedInOrder():
68 return TestSequence(
69 (mojom.INT8, mojom.UINT8, mojom.INT32),
70 (1, 2, 3),
71 (0, 1, 4))
72
73
74 def TestPaddingPackedOutOfOrder():
75 return TestSequence(
76 (mojom.INT8, mojom.INT32, mojom.UINT8),
77 (1, 3, 2),
78 (0, 1, 4))
79
80
81 def TestPaddingPackedOverflow():
82 kinds = (mojom.INT8, mojom.INT32, mojom.INT16, mojom.INT8, mojom.INT8)
83 # 2 bytes should be packed together first, followed by short, then by int.
84 fields = (1, 4, 3, 2, 5)
85 offsets = (0, 1, 2, 4, 8)
86 return TestSequence(kinds, fields, offsets)
87
88
89 def TestAllTypes():
90 struct = mojom.Struct('test')
91 array = mojom.Array()
92 return TestSequence(
93 (mojom.BOOL, mojom.INT8, mojom.STRING, mojom.UINT8,
94 mojom.INT16, mojom.DOUBLE, mojom.UINT16,
95 mojom.INT32, mojom.UINT32, mojom.INT64,
96 mojom.FLOAT, mojom.STRING, mojom.HANDLE,
97 mojom.UINT64, mojom.Struct('test'), mojom.Array()),
98 (1, 2, 4, 5, 7, 3, 6, 8, 9, 10, 11, 13, 12, 14, 15, 16, 17),
99 (0, 1, 2, 4, 6, 8, 16, 24, 28, 32, 40, 44, 48, 56, 64, 72, 80))
100
101
102 def TestPaddingPackedOutOfOrderByOrdinal():
103 errors = 0
104 struct = mojom.Struct('test')
105 struct.AddField('testfield1', mojom.INT8)
106 struct.AddField('testfield3', mojom.UINT8, 3)
107 struct.AddField('testfield2', mojom.INT32, 2)
108 ps = mojom_pack.PackedStruct(struct)
109 errors += EXPECT_EQ(3, len(ps.packed_fields))
110
111 # Second byte should be packed in behind first, altering order.
112 errors += EXPECT_EQ('testfield1', ps.packed_fields[0].field.name)
113 errors += EXPECT_EQ('testfield3', ps.packed_fields[1].field.name)
114 errors += EXPECT_EQ('testfield2', ps.packed_fields[2].field.name)
115
116 # Second byte should be packed with first.
117 errors += EXPECT_EQ(0, ps.packed_fields[0].offset)
118 errors += EXPECT_EQ(1, ps.packed_fields[1].offset)
119 errors += EXPECT_EQ(4, ps.packed_fields[2].offset)
120
121 return errors
122
123
124 def TestBools():
125 errors = 0
126 struct = mojom.Struct('test')
127 struct.AddField('bit0', mojom.BOOL)
128 struct.AddField('bit1', mojom.BOOL)
129 struct.AddField('int', mojom.INT32)
130 struct.AddField('bit2', mojom.BOOL)
131 struct.AddField('bit3', mojom.BOOL)
132 struct.AddField('bit4', mojom.BOOL)
133 struct.AddField('bit5', mojom.BOOL)
134 struct.AddField('bit6', mojom.BOOL)
135 struct.AddField('bit7', mojom.BOOL)
136 struct.AddField('bit8', mojom.BOOL)
137 ps = mojom_pack.PackedStruct(struct)
138 errors += EXPECT_EQ(10, len(ps.packed_fields))
139
140 # First 8 bits packed together.
141 for i in xrange(8):
142 pf = ps.packed_fields[i]
143 errors += EXPECT_EQ(0, pf.offset)
144 errors += EXPECT_EQ("bit%d" % i, pf.field.name)
145 errors += EXPECT_EQ(i, pf.bit)
146
147 # Ninth bit goes into second byte.
148 errors += EXPECT_EQ("bit8", ps.packed_fields[8].field.name)
149 errors += EXPECT_EQ(1, ps.packed_fields[8].offset)
150 errors += EXPECT_EQ(0, ps.packed_fields[8].bit)
151
152 # int comes last.
153 errors += EXPECT_EQ("int", ps.packed_fields[9].field.name)
154 errors += EXPECT_EQ(4, ps.packed_fields[9].offset)
155
156 return errors
157
158
159 def Main(args):
160 errors = 0
161 errors += RunTest(TestZeroFields)
162 errors += RunTest(TestOneField)
163 errors += RunTest(TestPaddingPackedInOrder)
164 errors += RunTest(TestPaddingPackedOutOfOrder)
165 errors += RunTest(TestPaddingPackedOverflow)
166 errors += RunTest(TestAllTypes)
167 errors += RunTest(TestPaddingPackedOutOfOrderByOrdinal)
168 errors += RunTest(TestBools)
169
170 return errors
171
172
173 if __name__ == '__main__':
174 sys.exit(Main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « mojo/public/bindings/pylib/generate/mojom_pack.py ('k') | mojo/public/bindings/pylib/generate/mojom_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698