11package gork
22
3- import "testing"
3+ import (
4+ "encoding/binary"
5+ "testing"
6+ )
47
58// attributes, parent, sibling, child
69// DO NOT STORE propPos it will be calculated!
@@ -13,10 +16,13 @@ var zobjectData [][]byte = [][]byte{
1316// nameLength, optional name, props
1417var zobjectProps [][]byte = [][]byte {
1518[]byte {0x00 , 0xB2 , 0x46 , 0xDC , 0x42 , 0xC2 , 0x42 , 0xB4 , 0x10 , 0x82 , 0x00 },
16- []byte {0x04 , 0x7E , 0x97 , 0xC0 , 0xA5 , 0xB2 , 0x46 , 0xDC , 0x42 , 0xC2 , 0x42 , 0xB4 , 0x10 , 0x82 , 0x00 },
17- []byte {0x04 , 0x23 , 0xC8 , 0xC6 , 0x95 , 0xB2 , 0x46 , 0xDC , 0x42 , 0xC2 , 0x42 , 0xB4 , 0x00 },
19+ []byte {0x02 , 0x7E , 0x97 , 0xC0 , 0xA5 , 0xB2 , 0x46 , 0xDC , 0x42 , 0xC2 , 0x42 , 0xB4 , 0x30 , 0x82 , 0x21 , 0x00 },
20+ []byte {0x02 , 0x23 , 0xC8 , 0xC6 , 0x95 , 0xB2 , 0x46 , 0xDC , 0x42 , 0xC2 , 0x42 , 0xB4 , 0x00 },
1821}
1922
23+ const defaultPropByte byte = 0xFF
24+ const defaultPropWord uint16 = uint16 (defaultPropByte )<< 8 | uint16 (defaultPropByte )
25+
2026// TODO generate automatically propertiesPos
2127var zobjectExpected []ZObject = []ZObject {
2228ZObject {
@@ -42,7 +48,7 @@ var zobjectExpected []ZObject = []ZObject{
4248propertiesPos : 0x0064 ,
4349properties : map [byte ][]byte {
445018 : []byte {0x46 , 0xDC , 0x42 , 0xC2 , 0x42 , 0xB4 },
45- 16 : []byte {0x82 },
51+ 16 : []byte {0x82 , 0x21 },
4652},
4753},
4854ZObject {
@@ -52,7 +58,7 @@ var zobjectExpected []ZObject = []ZObject{
5258sibling : 2 ,
5359child : 0 ,
5460name : "cyclop" ,
55- propertiesPos : 0x0073 ,
61+ propertiesPos : 0x0074 ,
5662properties : map [byte ][]byte {
576318 : []byte {0x46 , 0xDC , 0x42 , 0xC2 , 0x42 , 0xB4 },
5864},
@@ -75,8 +81,8 @@ func createZObjectBuf() []byte {
7581ret := make ([]byte , 31 * 2 )
7682
7783for i := range ret {
78- // default properties are all 0s in this case
79- ret [i ] = 0x00
84+ // default properties are all 0xFFs in this case
85+ ret [i ] = defaultPropByte
8086}
8187
8288firstPropPos := uint16 (len (ret )) + uint16 (len (zobjectData ))* uint16 (zobjectSize )
@@ -97,17 +103,28 @@ func createZObjectBuf() []byte {
97103return ret
98104}
99105
100- func TestZObject ( t * testing. T ) {
106+ func prelude () ( * ZMemory , * ZHeader , uint8 ) {
101107mem := ZMemory (createZObjectBuf ())
102108header := & ZHeader {objTblPos : 0x00 }
103109
104110count := ZObjectsCount (& mem , header )
111+
112+ return & mem , header , count
113+ }
114+
115+ func TestZObjectCount (t * testing.T ) {
116+ _ , _ , count := prelude ()
117+
105118if count != uint8 (len (zobjectExpected )) {
106119t .Fail ()
107120}
121+ }
122+
123+ func TestZObject (t * testing.T ) {
124+ mem , header , count := prelude ()
108125
109126for i := uint8 (0 ); i < count ; i ++ {
110- obj := NewZObject (& mem , i + 1 , header )
127+ obj := NewZObject (mem , i + 1 , header )
111128expected := zobjectExpected [i ]
112129
113130if obj .number != expected .number ||
@@ -117,7 +134,7 @@ func TestZObject(t *testing.T) {
117134obj .child != expected .child ||
118135obj .name != expected .name ||
119136obj .propertiesPos != expected .propertiesPos ||
120- obj .mem != & mem {
137+ obj .mem != mem {
121138t .Fail ()
122139}
123140
@@ -145,6 +162,152 @@ func TestZObject(t *testing.T) {
145162}
146163}
147164
165+ func TestZObjectGetProperty (t * testing.T ) {
166+ mem , header , count := prelude ()
167+
168+ for i := byte (0 ); i < count ; i ++ {
169+ obj := NewZObject (mem , i + 1 , header )
170+ good := zobjectExpected [i ]
171+
172+ for key , prop := range good .properties {
173+ if len (prop ) <= 2 {
174+ rProp := obj .GetProperty (key )
175+
176+ if len (prop ) == 1 {
177+ if rProp != uint16 (prop [0 ]) {
178+ t .Fail ()
179+ }
180+ } else if rProp != binary .BigEndian .Uint16 (prop ) {
181+ t .Fail ()
182+ }
183+ }
184+ }
185+
186+ // should return default property
187+ if _ , ok := obj .properties [31 ]; ! ok && obj .GetProperty (31 ) != defaultPropWord {
188+ t .Fail ()
189+ }
190+ }
191+ }
192+
193+ func TestZObjectSetProperty (t * testing.T ) {
194+ mem , header , count := prelude ()
195+
196+ for i := byte (0 ); i < count ; i ++ {
197+ obj := NewZObject (mem , i + 1 , header )
198+
199+ var expected uint16
200+
201+ for id , prop := range obj .properties {
202+ ok := true
203+ switch len (prop ) {
204+ case 1 :
205+ expected = uint16 (defaultPropByte )
206+ case 2 :
207+ expected = defaultPropWord
208+ default :
209+ ok = false
210+ }
211+
212+ if ok {
213+ obj .SetProperty (id , expected )
214+ if obj .GetProperty (id ) != expected {
215+ t .Fail ()
216+ }
217+ }
218+ }
219+ }
220+ }
221+
222+ func TestZObjectPropertyLen (t * testing.T ) {
223+ mem , header , count := prelude ()
224+
225+ for i := byte (0 ); i < count ; i ++ {
226+ obj := NewZObject (mem , i + 1 , header )
227+
228+ seq := mem .GetSequential (uint32 (obj .propertiesPos ))
229+ if seq .ReadByte () != 0 {
230+ // skip name
231+ seq .DecodeZString (header )
232+ }
233+ // skip dataSize
234+ seq .ReadByte ()
235+
236+ propertyPos := uint16 (seq .pos )
237+
238+ for _ , k := range obj .PropertiesIds () {
239+ prop := obj .properties [k ]
240+
241+ if GetPropertyLen (mem , uint32 (propertyPos )) != uint16 (len (prop )) {
242+ t .Fail ()
243+ }
244+
245+ // skip propData and next dataSize
246+ propertyPos += uint16 (len (prop )) + 1
247+ }
248+ }
249+ }
250+
251+ func TestZObjectGetFirstPropertyAddr (t * testing.T ) {
252+ mem , header , count := prelude ()
253+
254+ for i := byte (0 ); i < count ; i ++ {
255+ obj := NewZObject (mem , i + 1 , header )
256+
257+ seq := mem .GetSequential (uint32 (obj .propertiesPos ))
258+
259+ if seq .ReadByte () != 0 {
260+ // skip name
261+ seq .DecodeZString (header )
262+ }
263+
264+ if obj .GetFirstPropertyAddr () != seq .pos {
265+ t .Fail ()
266+ }
267+ }
268+ }
269+
270+ func TestZObjectGetPropertyAddr (t * testing.T ) {
271+ mem , header , count := prelude ()
272+
273+ for i := byte (0 ); i < count ; i ++ {
274+ obj := NewZObject (mem , i + 1 , header )
275+
276+ seq := mem .GetSequential (uint32 (obj .propertiesPos ))
277+
278+ if seq .ReadByte () != 0 {
279+ // skip name
280+ seq .DecodeZString (header )
281+ }
282+ propPos := seq .pos
283+
284+ keys := obj .PropertiesIds ()
285+
286+ if len (keys ) > 0 && obj .GetPropertyAddr (keys [0 ]) != obj .GetFirstPropertyAddr () {
287+ t .Fail ()
288+ }
289+
290+ // v3
291+ keyIdx := 0
292+ for propId := byte (31 ); propId > 0 ; propId -- {
293+ expected := uint32 (0 )
294+ if keyIdx < len (keys ) && keys [keyIdx ] == propId {
295+ expected = propPos
296+ keyIdx ++
297+
298+ // skip size
299+ propPos ++
300+ propPos += uint32 (len (zobjectExpected [i ].properties [propId ]))
301+ }
302+
303+ if obj .GetPropertyAddr (byte (propId )) != expected {
304+ t .Fail ()
305+ }
306+
307+ }
308+ }
309+ }
310+
148311func TestZObjectId (t * testing.T ) {
149312header := & ZHeader {objTblPos : 0 }
150313for i := byte (0 ); i < 255 ; i ++ {
0 commit comments