1
"""Custom types tests"""
6
from pykd import baseTypes
8
class CustomTypesTest(unittest.TestCase):
9
def testCommonStruct(self):
10
mySubStruct =pykd.createStruct("MySubCustomStruct")
11
mySubStruct.append( "m_uint1", baseTypes.UInt1B )
12
mySubStruct.append( "m_uint2", baseTypes.UInt2B )
14
mySubUnion = pykd.createUnion("MySubCustomUnion")
15
mySubUnion.append( "m_uint1", baseTypes.UInt1B )
16
mySubUnion.append( "m_uint2", baseTypes.UInt2B )
18
myType =pykd.createStruct("MyCustomStruct")
19
myType.append( "m_uint1", baseTypes.UInt1B )
20
myType.append( "m_uint4", baseTypes.UInt4B )
21
myType.append( "m_uint2", baseTypes.UInt2B )
22
myType.append( "m_struct", mySubStruct )
23
myType.append( "m_union", mySubUnion )
24
myType.append( "m_uint8", baseTypes.UInt8B )
26
self.assertTrue( myType.size() != 0 )
27
self.assertTrue( myType.size() >= myType.fieldOffset("m_uint8") + myType.m_uint8.size() )
29
self.assertTrue( myType.fieldOffset("m_uint1") == 0 )
31
self.assertTrue( myType.fieldOffset("m_uint1") < myType.fieldOffset("m_uint4") )
32
self.assertTrue( myType.fieldOffset("m_uint1") + myType.m_uint1.size() <= myType.fieldOffset("m_uint4") )
34
self.assertTrue( myType.fieldOffset("m_uint4") < myType.fieldOffset("m_uint2") )
35
self.assertTrue( myType.fieldOffset("m_uint4") + myType.m_uint4.size() <= myType.fieldOffset("m_uint2") )
37
self.assertTrue( myType.fieldOffset("m_uint2") < myType.fieldOffset("m_struct") )
38
self.assertTrue( myType.fieldOffset("m_uint2") + myType.m_uint2.size() <= myType.fieldOffset("m_struct") )
40
self.assertTrue( myType.fieldOffset("m_struct") < myType.fieldOffset("m_union") )
41
self.assertTrue( myType.fieldOffset("m_struct") + myType.m_struct.size() <= myType.fieldOffset("m_union") )
43
def testCommonUnion(self):
45
myType = pykd.createUnion("MyCustomStruct")
46
myType.append( "m_uint1", baseTypes.UInt1B )
47
myType.append( "m_uint4", baseTypes.UInt4B )
48
myType.append( "m_uint2", baseTypes.UInt2B )
50
self.assertFalse( myType.size() == 0 )
51
self.assertTrue( myType.fieldOffset("m_uint1") == 0 )
52
self.assertTrue( myType.fieldOffset("m_uint4") == 0 )
53
self.assertTrue( myType.fieldOffset("m_uint2") == 0 )
55
def testEmptyType(self):
57
myEmptyStruct1 = pykd.createStruct("EmptyStruct1")
58
self.assertEqual( 0, myEmptyStruct1.size() )
60
myEmptyStruct1.append("m_emptyStruct2", pykd.createStruct("EmptyStruct2"))
61
self.assertEqual( 0, myEmptyStruct1.size() )
63
myEmptyUnion1 = pykd.createUnion("EmptyUnion1")
64
self.assertEqual( 0, myEmptyUnion1.size() )
66
myEmptyStruct1.append("m_emptyUnion2", myEmptyUnion1)
67
self.assertEqual( 0, myEmptyStruct1.size() )
69
myEmptyUnion1.append("m_emptyStruct3", pykd.createStruct("EmptyStruct3"))
70
self.assertEqual( 0, myEmptyUnion1.size() )
72
def testDupFieldName(self):
74
myType = pykd.createStruct("MyCustomStruct")
75
exceptionRised = False
76
myType.append( "m_uint1", baseTypes.UInt1B )
78
myType.append( "m_uint1", baseTypes.UInt1B )
79
except pykd.TypeException:
81
self.assertTrue(exceptionRised)
83
myType = pykd.createUnion("MyCustomStruct")
84
exceptionRised = False
85
myType.append( "m_uint1", baseTypes.UInt1B )
87
myType.append( "m_uint1", baseTypes.UInt1B )
88
except pykd.TypeException:
90
self.assertTrue(exceptionRised)
92
def testPtrToCustomType(self):
93
mySubStruct =pykd.createStruct("MySubCustomStruct")
94
mySubStruct.append( "m_uint1", baseTypes.UInt1B )
95
mySubStruct.append( "m_uint2", baseTypes.UInt2B )
96
mySubStructPtr = mySubStruct.ptrTo()
97
self.assertEqual( pykd.ptrSize(), mySubStructPtr.size() )
100
struct = pykd.createStruct(name ="MyAlignStruct", align=4)
101
struct.append( "m_field1", baseTypes.UInt1B )
102
self.assertEqual( 1, struct.size() )
103
struct.append( "m_field2", baseTypes.UInt1B )
104
self.assertEqual( 2, struct.size() )
105
struct.append( "m_field3", baseTypes.UInt1B )
106
struct.append( "m_field4", baseTypes.UInt2B )
107
self.assertEqual( 6, struct.size() )
108
struct.append( "m_field5", baseTypes.UInt4B )
109
self.assertEqual( 12, struct.size() )
110
struct.append( "m_field6", baseTypes.UInt1B )
111
self.assertEqual( 16, struct.size() )
112
struct.append( "m_field7", baseTypes.UInt1B.arrayOf(5) )
113
self.assertEqual( 20, struct.size() )
115
struct = pykd.createStruct(name ="MyAlignStruct", align=1)
116
struct.append( "m_field1", baseTypes.UInt1B )
117
struct.append( "m_field2", baseTypes.UInt8B )
118
self.assertEqual( 9, struct.size() )
120
def testUnionAlignedSize(self):
121
union = pykd.createUnion("MyCustomUnion", align=4)
122
union.append( "m_field1", baseTypes.UInt2B )
123
self.assertEqual( 2, union.size() )
124
union.append( "m_field2", baseTypes.UInt1B.arrayOf(3) )
125
self.assertEqual( 4, union.size() )
127
def testWi12591(self):
128
struct = pykd.createStruct(name ="MyAlignStruct", align=4)
129
struct.append( "m_field1", baseTypes.UInt1B )
130
struct.append( "m_field2", baseTypes.UInt1B.arrayOf(2) )
131
self.assertEqual( struct.size(), 3 )
132
self.assertEqual( struct.fieldOffset("m_field2"), 1 )
134
def testWi12592(self):
135
struct = pykd.createStruct(name ="MyAlignStruct", align=4)
136
struct.append( "field1", baseTypes.UInt4B )
137
struct.append( "field2", baseTypes.UInt1B )
138
self.assertEqual( struct.size(), 8 )
139
self.assertEqual( struct.fieldOffset("field2"), 4 )
141
def testCustomFunction(self):
142
functype = pykd.defineFunction( baseTypes.UInt4B )
143
functype.append( "var1", baseTypes.WChar)
144
functype.append( "var2", baseTypes.UInt4B.ptrTo() )
145
self.assertEqual( "UInt4B(__cdecl)(WChar, UInt4B*)", functype.name() )