[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[S8Struct] instantiating

SwikiCodeRobot @> #testS8StructInstantiating

"type toString"
| type |
type := S8Struct type: #name -> #string.
type toString = '[StructType]' ifFalse: [ self error: 'Invalid result' ].


"should throw when the same field name is specified more than once"
| s |
s := S8Struct type: #a -> (S8Ref @ #int).
[	s #defineProperty: #a with: S8Ref @ #int
] on: Error do: [:ex| ^self print: 'ok - ' ,ex toString stream nextLine ].
self error: 'This should not happen'


"should throw when given an empty type"
[	S8Struct type: #v -> #void
] on: Error do: [:ex| ^self print: 'ok - ' ,ex toString stream nextLine ].
self error: 'This should not happen'


"should work in a simple case"
| type byte s |
byte := S8Ref type: #byte.
type := S8Struct type: #first -> byte ,(#second -> byte).
(type basicAt: #size) = 2 ifFalse: [ self error: 'Invalid size' ].
(type basicAt: #alignment) = 1 ifFalse: [ self error: 'Invalid alignment' ].
s := S8Struct type: type values:  #first -> 50 ,(#second -> 100).
s @ #first = 50 ifFalse: [ self error: 'Wrong first value'  ].
s @ #second = 100 ifFalse: [ self error: 'Wrong second value'  ].


"should work in another simple case"
| byte s |
byte := S8Ref type: #byte.
s := S8Struct
	type:  #first -> byte ,(#second -> byte)
	values:  #first -> 50 ,(#second -> 100).
s @ #first = 50 ifFalse: [ self error: 'Wrong first value'  ].
s @ #second = 100 ifFalse: [ self error: 'Wrong second value'  ].


"should work in a more complex case"
| s ptr values |
ptr := NodeBuffer size: 1.
values := #(
	#(byte 100)
	#(int8 -100)
	#(int16 -1000)
	#(uint16 1000)
	#(int32 -10000)
	#(uint32 10000)
	#(float 1.25)
	#(double 100.005)
	).
s := S8Struct
	type:  #byte -> (S8Ref type: #byte)
		,(#int8 -> (S8Ref type: #int8))
		,(#int16 -> (S8Ref type: #int16))
		,(#uint16 -> (S8Ref type: #uint16))
		,(#int32 -> (S8Ref type: #int32))
		,(#uint32 -> (S8Ref type: #uint32))
		,(#float -> (S8Ref type: #float))
		,(#double -> (S8Ref type: #double))
		,(#pointer -> (S8Ref refType: #void))
	values:  (ComposedAssociations withAll:
		(values collect: [:each| each first -> each last ]))
		,(#pointer -> ptr).
values do: [:tuple| | result |
	result := s @ tuple first.
	result = tuple last ifFalse: [ self error: 'Wrong value for ',tuple first  ].
].
(s @ #pointer) address = ptr address ifFalse: [ self error: 'Wrong pointer address' ].


"should allow Struct nesting"
| child s |
child := S8Struct type: #a -> (S8Ref type: #int) ,(#b -> (S8Ref type: #int)).
s := S8Struct
	type:  #childA -> child
		,(#childB -> child)
	values:
		 (#childA -> (#a -> 100 ,(#b -> 200)))
		,(#childB -> (#a -> 300 ,(#b -> 400))).
((s @ #childA) basicAt: #a) = 100 ifFalse: [ self error: 'Wrong value for childA@a'].
((s @ #childA) basicAt: #b) = 200 ifFalse: [ self error: 'Wrong value for childA@b'].
((s @ #childB) basicAt: #a) = 300 ifFalse: [ self error: 'Wrong value for childB@a'].
((s @ #childB) basicAt: #b) = 400 ifFalse: [ self error: 'Wrong value for childB@b'].