This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
TFUFFICallbackTest.class.st
172 lines (113 loc) · 3.78 KB
/
TFUFFICallbackTest.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Class {
#name : #TFUFFICallbackTest,
#superclass : #TFUFFITestCase,
#category : #'ThreadedFFI-UFFI-Tests'
}
{ #category : #tests }
TFUFFICallbackTest >> callbackFromAnotherThread: callback [
^ self ffiCall: #(void callbackFromAnotherThread(void* callback))
]
{ #category : #tests }
TFUFFICallbackTest >> callbackInALoop: callback [
^ self ffiCall: #(int callbackInALoop(void* callback))
]
{ #category : #tests }
TFUFFICallbackTest >> getValue [
^ self ffiCall: #(int getValue())
]
{ #category : #tests }
TFUFFICallbackTest >> returnAnswer [
^ self ffiCall: #(int32 returnAnswer())
]
{ #category : #running }
TFUFFICallbackTest >> setUp [
super setUp.
]
{ #category : #tests }
TFUFFICallbackTest >> shortCallout [
^ self ffiCall: #(int shortCallout())
]
{ #category : #tests }
TFUFFICallbackTest >> singleCallToCallback: callback value: value [
^ self ffiCall: #(int singleCallToCallback(void* callback, int value))
]
{ #category : #tests }
TFUFFICallbackTest >> sumFloat: a andDouble: b [
^ self ffiCall: #(float sumAFloatAndADouble(float a, double b))
]
{ #category : #tests }
TFUFFICallbackTest >> sumInt:a andInt: b [
^ self ffiCall: #(int sumTwoNumbers(int a, int b))
]
{ #category : #tests }
TFUFFICallbackTest >> testCallbackAsFunction [
| callback fun definition runner |
runner := self ffiLibrary uniqueInstance runner.
callback := self
newCallbackWithSignature: #(float (int a, float b))
block: [ :a :b | a + b ].
definition := TFFunctionDefinition
parameterTypes: {TFBasicType sint. TFBasicType float.}
returnType: TFBasicType float.
fun := TFExternalFunction
fromAddress: callback getHandle
definition: definition.
self assert: (runner invokeFunction: fun withArguments: {1. 2.0}) equals: 3.0
]
{ #category : #tests }
TFUFFICallbackTest >> testCallbackInLoop [
| callback |
callback := self
newCallbackWithSignature: #(int (int a))
block: [ :a | a + 1 ].
self assert: (self callbackInALoop: callback) equals: 42
]
{ #category : #tests }
TFUFFICallbackTest >> testCallbackInLoopUsingSubclassOfFFICallback [
| callback |
callback := TFUFFIIncrementCallback new
ffiLibrary: self ffiLibrary;
yourself.
self assert: (self callbackInALoop: callback) equals: 42
]
{ #category : #tests }
TFUFFICallbackTest >> testCallbackInSingleFunction [
| callback |
callback := self newCallbackWithSignature: #(int (int a)) block: [ :a | a + 1 ].
self assert: (self singleCallToCallback: callback value: 3) equals: 5
]
{ #category : #tests }
TFUFFICallbackTest >> testCallbackInSingleFunctionUsingSubclassOfFFICallback [
| callback |
callback := TFUFFIIncrementCallback new
ffiLibrary: self ffiLibrary;
yourself.
self assert: (self singleCallToCallback: callback value: 3) equals: 5
]
{ #category : #tests }
TFUFFICallbackTest >> testCallbackWithErrorHanlded [
| callback fun definition runner |
runner := self ffiLibrary uniqueInstance runner.
callback := TFUFFIFailingCallbackWithDefault new
ffiLibrary: self ffiLibrary;
yourself.
definition := TFFunctionDefinition
parameterTypes: {TFBasicType sint.}
returnType: TFBasicType sint.
fun := TFExternalFunction
fromAddress: callback getHandle
definition: definition.
UIManager nonInteractiveDuring: [self assert: (runner invokeFunction: fun withArguments: {1}) equals: -1]
]
{ #category : #tests }
TFUFFICallbackTest >> testCallbacksInOtherThread [
| callback semaphore|
"Using same thread runner with callbacks from other threads produce a segmentation fault"
ffiLibrary = TFTestLibraryUsingSameThreadRunner ifTrue: [ ^ self skip ].
semaphore := Semaphore new.
callback := self newCallbackWithSignature: #(int (int a)) block: [ :a | semaphore signal. a + 1 ].
self callbackFromAnotherThread: callback.
semaphore wait.
1 second wait.
self assert: self getValue equals: 43
]