/
CompiledMethodTest.class.st
596 lines (443 loc) · 16.4 KB
/
CompiledMethodTest.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
"
This is the unit test for the class CompiledMethod. Unit tests are a good way to exercise the functionality of your system in a repeatable and automatic manner. They are therefore recommended if you plan to release anything. For more information, see:
- http://www.c2.com/cgi/wiki?UnitTest
- there is a chapter in the PharoByExample book (http://pharobyexample.org)
- the sunit class category
"
Class {
#name : #CompiledMethodTest,
#superclass : #ClassTestCase,
#instVars : [
'x',
'y'
],
#category : #'Kernel-Tests-Extended-Methods'
}
{ #category : #'tests - performing' }
CompiledMethodTest >> a1: a1 a2: a2 a3: a3 a4: a4 a5: a5 a6: a6 a7: a7 a8: a8 a9: a9 a10: a10 a11: a11 a12: a12 a13: a13 a14: a14 a15: a15 [
"I'm a method with the maximum size of arguments that can be executed via normal send but crash on perform :)"
^ a1 + a2 - a2
]
{ #category : #examples }
CompiledMethodTest >> abstractMethod [
"I am an abstract method"
^ self subclassResponsibility
]
{ #category : #'private - accessing' }
CompiledMethodTest >> categoryNameForTemporaryClasses [
"Answer the category where to classify temporarily created classes"
^'Dummy-Tests-Class'
]
{ #category : #coverage }
CompiledMethodTest >> classToBeTested [
^ CompiledMethod
]
{ #category : #examples }
CompiledMethodTest >> deprecatedMethod [
"Used to test sends of deprecation messages;
do not recategorize in one of the 'deprecated' categories."
self deprecated: 'example of a deprecated method'
]
{ #category : #examples }
CompiledMethodTest >> deprecatedMethod2 [
"Used to test sends of deprecation messages;
do not recategorize in one of the 'deprecated' categories."
self deprecated: 'example of a deprecated method' on: 'date' in: 'someversion'.
]
{ #category : #examples }
CompiledMethodTest >> deprecatedMethod3 [
"Used to test sends of deprecation messages;
do not recategorize in one of the 'deprecated' categories."
self
deprecated: 'Example of a deprecated method with transform'
transformWith: '`@receiver deprecatedMethod3'
-> '`@receiver deprecatedMethod3'.
]
{ #category : #examples }
CompiledMethodTest >> deprecatedMethod4 [
"Used to test sends of deprecation messages;
do not recategorize in one of the 'deprecated' categories."
self
deprecated: 'Example of a deprecated method with transform'
on: '01/01/1970'
in: #Pharo6
transformWith: '`@receiver deprecatedMethod4'
-> '`@receiver deprecatedMethod4'.
]
{ #category : #deprecated }
CompiledMethodTest >> deprecatedMethod5 [
]
{ #category : #'category - deprecated' }
CompiledMethodTest >> deprecatedMethod6 [
]
{ #category : #'deprecated - category' }
CompiledMethodTest >> deprecatedMethod7 [
]
{ #category : #examples }
CompiledMethodTest >> nonAbstractMethod [
"I am not an abstract method"
^ 4 + 5
]
{ #category : #examples }
CompiledMethodTest >> readX [
| tmp |
tmp := x.
^ tmp
]
{ #category : #examples }
CompiledMethodTest >> readXandY [
^ x + y
]
{ #category : #examples }
CompiledMethodTest >> returnPlusOne: anInteger [
^anInteger + 1.
]
{ #category : #examples }
CompiledMethodTest >> returnTrue [
^true
]
{ #category : #examples }
CompiledMethodTest >> shouldNotImplementMethod [
"I am not an abstract method"
^ self shouldNotImplement
]
{ #category : #running }
CompiledMethodTest >> tearDown [
RPackage organizer unregisterPackageNamed: self categoryNameForTemporaryClasses.
super tearDown
]
{ #category : #'tests - slots' }
CompiledMethodTest >> testAccessesSlot [
"Check the source code availability to do not fail on images without sources"
({ Point>>#x. Point>>#setX:setY: } allSatisfy: #hasSourceCode)
ifFalse: [ ^ self ].
self assert: ((Point>>#x) accessesSlot: (Point slotNamed: #x)).
self deny: ((Point>>#x) accessesSlot: (Point slotNamed: #y)).
self assert: ((Point>>#setX:setY:) accessesSlot: (Point slotNamed: #y)).
]
{ #category : #'tests - accessing' }
CompiledMethodTest >> testBytecode [
"The result of this test depends on the used bytecode set.
Because there are multiple versions of the encoders currently depending on the compiler used, we test the class name instead of the encoder class itself"
| method expectedResult |
method := Object>>#halt.
expectedResult := (method encoderClass name endsWith: 'SistaV1')
ifTrue: [#[16 129 216 88]]
ifFalse: [#[64 209 135 120]].
self assertCollection: (Object>>#halt) bytecode equals: expectedResult
]
{ #category : #'tests - accessing' }
CompiledMethodTest >> testComments [
self
assert: (CompiledMethod >> #comments) comments first
equals: 'Answer a string representing the comments in the method. Return an empty collection if the method''s source code does not contain a comment.'.
self assert: (CompiledMethod >> #compiledMethod) comments isEmpty
]
{ #category : #'tests - accessing' }
CompiledMethodTest >> testComparison [
| method1 method2 |
method1 := Float class >> #nan.
method2 := thisContext method.
self assert: method1 equals: method1.
self assert: method2 equals: method2.
self deny: method1 = method2.
self deny: method2 = method1.
Object methods do: [ :each |
self deny: method1 = each.
self deny: each = method1.
self deny: method2 = each.
self deny: each = method2 ]
]
{ #category : #'as yet unclassified' }
CompiledMethodTest >> testCompiledMethodAsString [
thisContext method asString
]
{ #category : #'as yet unclassified' }
CompiledMethodTest >> testContainsHalt [
| annonClass |
self
deny: (Object >> #halt) containsHalt;
deny: (Object >> #haltIfNil) containsHalt;
deny: (Halt class >> #once) containsHalt;
assert: (UndefinedObjectTest >> #testIfNil) containsHalt.
annonClass := Object newAnonymousSubclass.
annonClass
compile: 'm1 self halt';
compile: 'm2 self haltIfNil';
compile: 'm3 self yourself'.
self
assert: (annonClass >> #m1) containsHalt;
assert: (annonClass >> #m2) containsHalt;
deny: (annonClass >> #m3) containsHalt.
]
{ #category : #tests }
CompiledMethodTest >> testCopy [
<pragma: #pragma>
| method copy |
method := thisContext method.
self assert: method pragmas notEmpty.
copy := method copy.
self assert: (method equivalentTo: copy).
self assert: method header equals: copy header.
self assert: method equals: copy.
self assert: method ~~ copy.
self assert: copy penultimateLiteral method == copy.
self assert: method penultimateLiteral method == method.
method pragmas do:
[:p|
self assert: p method == method].
copy pragmas do:
[:p|
self assert: p method == copy]
]
{ #category : #tests }
CompiledMethodTest >> testCopyWithTrailerBytes [
<pragma: #pragma>
| method copy |
method := thisContext method.
self assert: method pragmas notEmpty.
copy := method copyWithTrailerBytes: method trailer.
self assert: (method equivalentTo: copy).
self deny: method == copy.
self assert: method symbolic equals: copy symbolic. "but their bytecode should be the same"
self assert: method ~~ copy.
self assert: copy penultimateLiteral method == copy.
self assert: method penultimateLiteral method == method.
method pragmas do:
[:p|
self assert: p method == method].
copy pragmas do:
[:p|
self assert: p method == copy]
]
{ #category : #'as yet unclassified' }
CompiledMethodTest >> testEqualityClassSideMethod [
| method1 method2 |
method1 := TestCase class compiler
compile: 'aMethod'.
method2 := TestCase class compiler
compile: 'aMethod'.
self assert: method1 equals: method2.
]
{ #category : #'as yet unclassified' }
CompiledMethodTest >> testEqualityInstanceSideMethod [
| method1 method2 |
method1 := TestCase compiler compile: 'aMethod'.
method2 := TestCase compiler compile: 'aMethod'.
self assert: (method1 literalAt: method1 numLiterals) == (method2 literalAt: method2 numLiterals).
self assert: method1 equals: method2.
]
{ #category : #'tests - instance variable' }
CompiledMethodTest >> testHasInstVarRef [
| method |
method := self class compiledMethodAt: #readX.
self assert: (method hasInstVarRef).
method := self class compiledMethodAt: #readXandY.
self assert: (method hasInstVarRef).
method := self class compiledMethodAt: #writeX.
self assert: (method hasInstVarRef).
method := self class compiledMethodAt: #writeXandY.
self assert: (method hasInstVarRef).
]
{ #category : #'tests - literals' }
CompiledMethodTest >> testHasLiteralSuchThat [
"#literals should not expose implementation hack that the last two literals are
used for name of method and class"
self deny: (Object >> #yourself hasLiteralSuchThat: [ :lit | lit == #yourself ]).
self assert: (Object >> #halt hasLiteralSuchThat: [ :lit | lit == #now ]).
]
{ #category : #'tests - abstract' }
CompiledMethodTest >> testIsAbstract [
self assert: (self class >> #abstractMethod) isAbstract.
self deny: (self class >> #nonAbstractMethod) isAbstract.
self deny: (self class >> #shouldNotImplementMethod) isAbstract
]
{ #category : #'tests - testing' }
CompiledMethodTest >> testIsDeprecated [
| deprecatedSelectors |
deprecatedSelectors := #(deprecatedMethod deprecatedMethod2 deprecatedMethod3 deprecatedMethod4 deprecatedMethod5 deprecatedMethod6 deprecatedMethod7)
asSet.
self class
selectorsDo: [ :each |
(deprecatedSelectors includes: each)
ifTrue: [ self assert: (self class >> each) isDeprecated ]
ifFalse: [ self deny: (self class >> each) isDeprecated ] ]
]
{ #category : #'as yet unclassified' }
CompiledMethodTest >> testIsInstalled [
| method cls |
method := (self class)>>#returnTrue.
self assert: method isInstalled.
Smalltalk removeClassNamed: #TUTU.
cls := Object subclass: #TUTU
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: self categoryNameForTemporaryClasses.
cls compile: 'foo ^ 10'.
method := cls >> #foo.
"now make an orphaned method by just deleting the method."
cls removeSelector: #foo.
self deny: method isInstalled.
Smalltalk removeClassNamed: #TUTU.
]
{ #category : #'tests - testing' }
CompiledMethodTest >> testIsQuick [
| method |
method := self class compiledMethodAt: #returnTrue.
self assert: (method isQuick).
method := self class compiledMethodAt: #returnPlusOne:.
self deny: (method isQuick).
]
{ #category : #'tests - literals' }
CompiledMethodTest >> testLiterals [
"#literals should not expose implementation hack that the last two literals are
used for name of method and class"
self assertEmpty: (Object >> #yourself) literals.
self assert: (Object >> #yourself) allLiterals size equals: 2.
self deny: (Object >> #yourself hasLiteral: #yourself)
]
{ #category : #'as yet unclassified' }
CompiledMethodTest >> testMethodClass [
| method cls |
method := self class >> #returnTrue.
self assert: method selector equals: #returnTrue.
"now make an orphaned method by just deleting the class.
old: #unknown
new semantics: return Absolete class"
Smalltalk removeClassNamed: #TUTU.
cls := Object
subclass: #TUTU
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: self categoryNameForTemporaryClasses.
cls compile: 'foo ^ 10'.
method := cls >> #foo.
Smalltalk removeClassNamed: #TUTU.
self assert: method methodClass equals: cls
]
{ #category : #'tests - accessing' }
CompiledMethodTest >> testOrigin [
| regularMethod methodFromTrait |
"Regular method"
regularMethod := Behavior>>#name.
"Method from a trait without alias "
methodFromTrait := Behavior>>#adoptInstance:.
self assert: regularMethod origin == regularMethod originMethod methodClass.
self assert: methodFromTrait origin == methodFromTrait originMethod methodClass.
]
{ #category : #'tests - performing' }
CompiledMethodTest >> testPerformCanExecutelongMethodWithTemps [
"the perform: primitive reuses the context of the method calling it. The primitive adds performed selector arguments to the context variables list. So this means that you can execute some methods but not performed them if the calling methods defined too many temps "
| temp1 temp2 temp3 |
temp1 := 33.
temp2 := 666.
temp3 := 42.
self assert: (self perform: #a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15: withArguments: #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)) equals: 1.
self assert: (self class>>#testPerformCanExecutelongMethodWithTemps) frameSize equals: CompiledMethod smallFrameSize.
self assert: (self class>>#a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15:) frameSize equals: CompiledMethod fullFrameSize.
]
{ #category : #'tests - performing' }
CompiledMethodTest >> testPerformInSuperclassCanExecutelongMethodWithTemps [
"the perform: primitive reuses the context of the method calling it. The primitive adds performed selector arguments to the context variables list. So this means that you can execute some methods but not performed them if the calling methods defined too many temps "
| temp1 temp2 temp3 |
temp1 := 33.
temp2 := 666.
temp3 := 42.
self assert: (self perform: #a1:a2:a3:a4:a5:a6:a7:a8:a9:a10:a11:a12:a13:a14:a15: withArguments: #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) inSuperclass: self class) equals: 1
]
{ #category : #'tests - instance variable' }
CompiledMethodTest >> testReadsField [
| method |
method := self class compiledMethodAt: #readX.
self assert: (method readsField: 3).
method := self class compiledMethodAt: #readXandY.
self assert: (method readsField: 4).
"read is not write"
method := self class compiledMethodAt: #writeX.
self deny: (method readsField: 3).
method := self class compiledMethodAt: #writeXandY.
self deny: (method readsField: 3).
method := self class compiledMethodAt: #writeXandY.
self deny: (method readsField: 4)
]
{ #category : #'tests - slots' }
CompiledMethodTest >> testReadsSlot [
"Check the source code availability to do not fail on images without sources"
({ Point>>#x. Point>>#setX:setY: } allSatisfy: #hasSourceCode)
ifFalse: [ ^ self ].
self assert: ((Point>>#x) readsSlot: (Point slotNamed: #x)).
self deny: ((Point>>#x) readsSlot: (Point slotNamed: #y)).
self deny: ((Point>>#setX:setY:) readsSlot: (Point slotNamed: #y)).
]
{ #category : #'as yet unclassified' }
CompiledMethodTest >> testSelector [
Author useAuthor: 'TUTU_TEST' during: [
| method cls |
method := (self class)>>#returnTrue.
self assert: method selector equals: #returnTrue.
"now make an orphaned method. new semantics: return corrent name"
Smalltalk removeClassNamed: #TUTU.
cls := Object subclass: #TUTU
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: self categoryNameForTemporaryClasses.
cls compile: 'foo ^ 10'.
method := cls >> #foo.
Smalltalk removeClassNamed: #TUTU.
self assert: method selector = #foo.
].
]
{ #category : #'tests - testing' }
CompiledMethodTest >> testSendsSelector [
self assert: ((CompiledCode >> #sendsSelector:) sendsSelector: #includes:).
self deny: ((CompiledCode >> #sendsSelector:) sendsSelector: #doBreakfastForMe)
]
{ #category : #'tests - evaluating' }
CompiledMethodTest >> testValueWithReceiverArguments [
| method value |
method := self class compiledMethodAt: #returnTrue.
value := method valueWithReceiver: nil arguments: #().
self assert: value equals: true.
method := self class compiledMethodAt: #returnPlusOne:.
value := method valueWithReceiver: nil arguments: #(1).
self assert: value equals: 2.
]
{ #category : #'tests - instance variable' }
CompiledMethodTest >> testWritesField [
| method |
method := self class compiledMethodAt: #writeX.
self assert: (method writesField: 3).
method := self class compiledMethodAt: #writeXandY.
self assert: (method writesField: 3).
method := self class compiledMethodAt: #writeXandY.
self assert: (method writesField: 4).
"write is not read"
method := self class compiledMethodAt: #readX.
self deny: (method writesField: 3).
method := self class compiledMethodAt: #readXandY.
self deny: (method writesField: 3).
method := self class compiledMethodAt: #readXandY.
self deny: (method writesField: 4).
]
{ #category : #'tests - slots' }
CompiledMethodTest >> testWritesSlot [
"Check the source code availability to do not fail on images without sources"
({ Point>>#x. Point>>#setX:setY: } allSatisfy: #hasSourceCode)
ifFalse: [ ^ self ].
self deny: ((Point>>#x) writesSlot: (Point slotNamed: #x)).
self deny: ((Point>>#x) writesSlot: (Point slotNamed: #y)).
self assert: ((Point>>#setX:setY:) writesSlot: (Point slotNamed: #y)).
self assert: ((Point>>#setX:setY:) writesSlot: (Point slotNamed: #x)).
]
{ #category : #examples }
CompiledMethodTest >> writeX [
x := 33
]
{ #category : #examples }
CompiledMethodTest >> writeXandY [
x := 33.
y := 66
]