Source

pypy / pypy / jit / metainterp / test / test_string.py

The branch 'remove-globals-in-jit' does not exist.
Samuele Pedroni 4804baf 
Alex Gaynor 1d95757 



Maciej Fijalkows… e7fbc95 

Alex Gaynor 1d95757 
Samuele Pedroni 4804baf 



Antonio Cuni 1728bcd 

Samuele Pedroni 4804baf 
Antonio Cuni 1728bcd 
Armin Rigo 4ca40f4 
Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 

Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 
Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 






Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 
Hakan Ardo 27c7364 
Samuele Pedroni 4804baf 

Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 
Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 

Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 
Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 






Antonio Cuni 1728bcd 
Samuele Pedroni 4804baf 
Hakan Ardo 27c7364 
Samuele Pedroni 4804baf 
Armin Rigo 4ca40f4 
Antonio Cuni 1728bcd 
Armin Rigo 4ca40f4 




Antonio Cuni 1728bcd 
Armin Rigo 4ca40f4 






Antonio Cuni 47af987 
Antonio Cuni 1728bcd 
















Hakan Ardo 27c7364 
Antonio Cuni 1728bcd 
Antonio Cuni 47af987 

Antonio Cuni 1728bcd 

















Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
















Hakan Ardo 27c7364 
Antonio Cuni 1728bcd 

Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 










Hakan Ardo 27c7364 
Antonio Cuni 1728bcd 









Antonio Cuni 47af987 








Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
Antonio Cuni 47af987 







Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Antonio Cuni 47af987 



Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 1728bcd 
Hakan Ardo 9021a2a 

Antonio Cuni 47af987 

Antonio Cuni 1728bcd 















Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
















Antonio Cuni 47af987 

Antonio Cuni 1728bcd 
















Hakan Ardo 9021a2a 
Antonio Cuni 47af987 
Hakan Ardo 5a92368 


Alex Gaynor fc7b6f5 
Hakan Ardo 5a92368 









Hakan Ardo dfaced0 




Alex Gaynor fc7b6f5 
Hakan Ardo dfaced0 

























Alex Gaynor fc7b6f5 
Hakan Ardo dfaced0 


















Hakan Ardo 31a39eb 
















Maciej Fijalkows… f38e5da 
Alex Gaynor fc7b6f5 
Hakan Ardo 941d81d 















Alex Gaynor fc7b6f5 
Hakan Ardo 941d81d 


















Alex Gaynor fc7b6f5 
Hakan Ardo 4b1dd6e 















Hakan Ardo 2e08608 


















Antonio Cuni 47af987 
Hakan Ardo 8ab3acc 





Alex Gaynor bf59d65 
Hakan Ardo 8ab3acc 











Hakan Ardo b2fbfc8 
Hakan Ardo 9021a2a 

Hakan Ardo 8ab3acc 
Maciej Fijalkows… e7fbc95 

Alex Gaynor bf59d65 
Maciej Fijalkows… e7fbc95 
Maciej Fijalkows… 4607670 
Maciej Fijalkows… e7fbc95 





Hakan Ardo 9021a2a 
Hakan Ardo 8ab3acc 
Antonio Cuni 1728bcd 


Samuele Pedroni 4804baf 

Armin Rigo 203b62f 
Samuele Pedroni 4804baf 
Antonio Cuni 1728bcd 























Hakan Ardo 9021a2a 

Armin Rigo 7f452cc 





Armin Rigo 17460c4 

Armin Rigo 7f452cc 








Hakan Ardo 9021a2a 


Alex Gaynor 760a2b7 




















Hakan Ardo b2fbfc8 
Hakan Ardo 9021a2a 
Alex Gaynor 1d95757 












Alex Gaynor 4d3c8a2 














Maciej Fijalkows… 0c27002 








  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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import py

from pypy.jit.codewriter.policy import StopAtXPolicy
from pypy.jit.metainterp.test.support import LLJitMixin, OOJitMixin
from pypy.rlib.debug import debug_print
from pypy.rlib.jit import JitDriver, dont_look_inside, we_are_jitted,\
     promote_string
from pypy.rlib.rstring import StringBuilder
from pypy.rpython.ootypesystem import ootype


class StringTests:
    _str, _chr = str, chr

    def test_eq_residual(self):
        _str = self._str
        jitdriver = JitDriver(greens = [], reds = ['n', 'i', 's'])
        global_s = _str("hello")
        def f(n, b, s):
            if b:
                s += _str("ello")
            else:
                s += _str("allo")
            i = 0
            while n > 0:
                jitdriver.can_enter_jit(s=s, n=n, i=i)
                jitdriver.jit_merge_point(s=s, n=n, i=i)
                n -= 1 + (s == global_s)
                i += 1
            return i
        res = self.meta_interp(f, [10, True, _str('h')], listops=True)
        assert res == 5
        self.check_resops(**{self.CALL: 1, self.CALL_PURE: 0})

    def test_eq_folded(self):
        _str = self._str
        jitdriver = JitDriver(greens = ['s'], reds = ['n', 'i'])
        global_s = _str("hello")
        def f(n, b, s):
            if b:
                s += _str("ello")
            else:
                s += _str("allo")
            i = 0
            while n > 0:
                jitdriver.can_enter_jit(s=s, n=n, i=i)
                jitdriver.jit_merge_point(s=s, n=n, i=i)
                n -= 1 + (s == global_s)
                i += 1
            return i
        res = self.meta_interp(f, [10, True, _str('h')], listops=True)
        assert res == 5
        self.check_resops(**{self.CALL: 0, self.CALL_PURE: 0})

    def test_newstr(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['n', 'm'])
        def f(n, m):
            while True:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                bytecode = _str('adlfkj') + _chr(n)
                res = bytecode[n]
                m -= 1
                if m < 0:
                    return ord(res)
        res = self.meta_interp(f, [6, 10])
        assert res == 6

    def test_char2string_pure(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['n'])
        @dont_look_inside
        def escape(x):
            pass
        def f(n):
            while n > 0:
                jitdriver.can_enter_jit(n=n)
                jitdriver.jit_merge_point(n=n)
                s = _chr(n)
                if not we_are_jitted():
                    s += s     # forces to be a string
                if n > 100:
                    escape(s)
                n -= 1
            return 42
        self.meta_interp(f, [6])
        self.check_resops(newstr=0, strsetitem=0, strlen=0,
                         newunicode=0, unicodesetitem=0, unicodelen=0)

    def test_char2string_escape(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['n', 'total'])
        @dont_look_inside
        def escape(x):
            return ord(x[0])
        def f(n):
            total = 0
            while n > 0:
                jitdriver.can_enter_jit(n=n, total=total)
                jitdriver.jit_merge_point(n=n, total=total)
                s = _chr(n)
                if not we_are_jitted():
                    s += s    # forces to be a string
                total += escape(s)
                n -= 1
            return total
        res = self.meta_interp(f, [6])
        assert res == 21

    def test_char2string2char(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['m', 'total'])
        def f(m):
            total = 0
            while m > 0:
                jitdriver.can_enter_jit(m=m, total=total)
                jitdriver.jit_merge_point(m=m, total=total)
                string = _chr(m)
                if m > 100:
                    string += string    # forces to be a string
                # read back the character
                c = string[0]
                total += ord(c)
                m -= 1
            return total
        res = self.meta_interp(f, [6])
        assert res == 21
        self.check_resops(newstr=0, strgetitem=0, strsetitem=0, strlen=0,
                         newunicode=0, unicodegetitem=0, unicodesetitem=0,
                         unicodelen=0)

    def test_strconcat_pure(self):
        _str = self._str
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        mylist = [_str("abc") + _str(i) for i in range(10)]
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = mylist[n] + mylist[m]
                if m > 100:
                    escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        self.check_resops(newstr=0, strsetitem=0,
                         newunicode=0, unicodesetitem=0,
                         call=0, call_pure=0)

    def test_strconcat_escape_str_str(self):
        _str = self._str
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        mylist = [_str("somestr") + _str(i) for i in range(10)]
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = mylist[n] + mylist[m]
                escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        if _str is str:
            self.check_resops(call_pure=0, copystrcontent=4,
                              strsetitem=0, call=2, newstr=2)
        else:
            self.check_resops(call_pure=0, unicodesetitem=0, call=2,
                              copyunicodecontent=4, newunicode=2)

    def test_strconcat_escape_str_char(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        mylist = [_str("somestr") + _str(i) for i in range(10)]
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = mylist[n] + _chr(m)
                escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        if _str is str:
            self.check_resops(call_pure=0, copystrcontent=2, strsetitem=2,
                              call=2, newstr=2)
        else:
            self.check_resops(call_pure=0, unicodesetitem=2, call=2,
                              copyunicodecontent=2, newunicode=2)

    def test_strconcat_escape_char_str(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        mylist = [_str("somestr") + _str(i) for i in range(10)]
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = _chr(n) + mylist[m]
                escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        if _str is str:
            self.check_resops(call_pure=0, copystrcontent=2,
                              strsetitem=2, call=2, newstr=2)
        else:
            self.check_resops(call_pure=0, unicodesetitem=2, call=2,
                              copyunicodecontent=2, newunicode=2)

    def test_strconcat_escape_char_char(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = _chr(n) + _chr(m)
                escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        if _str is str:
            self.check_resops(call_pure=0, copystrcontent=0,
                              strsetitem=4, call=2, newstr=2)
        else:
            self.check_resops(call_pure=0, unicodesetitem=4, call=2,
                              copyunicodecontent=0, newunicode=2)

    def test_strconcat_escape_str_char_str(self):
        _str, _chr = self._str, self._chr
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        mylist = [_str("somestr") + _str(i) for i in range(10)]
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = mylist[n] + _chr(n) + mylist[m]
                escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        if _str is str:
            self.check_resops(call_pure=0, copystrcontent=4, strsetitem=2,
                              call=2, newstr=2)
        else:
            self.check_resops(call_pure=0, unicodesetitem=2, call=2,
                              copyunicodecontent=4, newunicode=2)

    def test_strconcat_guard_fail(self):
        _str = self._str
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        mylist = [_str("abc") + _str(i) for i in range(12)]
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = mylist[n] + mylist[m]
                if m & 1:
                    escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 10])

    def test_strslice(self):
        _str = self._str
        longstring = _str("foobarbazetc")
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        def f(n, m):
            assert n >= 0
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = longstring[m:n]
                if m <= 5:
                    escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [10, 10])

    def test_streq_char(self):
        _str = self._str
        longstring = _str("?abcdefg")
        somechar = _str("?")
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        @dont_look_inside
        def escape(x):
            pass
        def f(n, m):
            assert n >= 0
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                s = longstring[:m]
                escape(s == somechar)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        self.check_resops(newunicode=0, newstr=0)

    def test_str_slice_len_surviving(self):
        _str = self._str
        longstring = _str("Unrolling Trouble")
        mydriver = JitDriver(reds = ['i', 'a', 'sa'], greens = [])
        def f(a):
            i = sa = a
            while i < len(longstring):
                mydriver.jit_merge_point(i=i, a=a, sa=sa)
                assert a >= 0 and i >= 0
                i = len(longstring[a:i+1])
                sa += i
            return sa
        assert self.meta_interp(f, [0]) == f(0)

    def test_virtual_strings_direct(self):
        _str = self._str
        fillers = _str("abcdefghijklmnopqrstuvwxyz")
        data = _str("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

        mydriver = JitDriver(reds = ['line', 'noise', 'res'], greens = [])
        def f():
            line = data
            noise = fillers
            ratio = len(line) // len(noise)
            res = data[0:0]
            while line and noise:
                mydriver.jit_merge_point(line=line, noise=noise, res=res)
                if len(line) // len(noise) > ratio:
                    c, line = line[0], line[1:]
                else:
                    c, noise = noise[0], noise[1:]
                res += c
            return res + noise + line
        s1 = self.meta_interp(f, [])
        s2 = f()
        for c1, c2 in zip(s1.chars, s2):
            assert c1==c2

    def test_virtual_strings_boxed(self):
        _str = self._str
        fillers = _str("abcdefghijklmnopqrstuvwxyz")
        data = _str("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        class Str(object):
            def __init__(self, value):
                self.value = value
        mydriver = JitDriver(reds = ['ratio', 'line', 'noise', 'res'],
                             greens = [])
        def f():
            line = Str(data)
            noise = Str(fillers)
            ratio = len(line.value) // len(noise.value)
            res = Str(data[0:0])
            while line.value and noise.value:
                mydriver.jit_merge_point(line=line, noise=noise, res=res,
                                         ratio=ratio)
                if len(line.value) // len(noise.value) > ratio:
                    c, line = line.value[0], Str(line.value[1:])
                else:
                    c, noise = noise.value[0], Str(noise.value[1:])
                res = Str(res.value + c)
            return res.value + noise.value + line.value
        s1 = self.meta_interp(f, [])
        s2 = f()
        for c1, c2 in zip(s1.chars, s2):
            assert c1==c2

    def test_string_in_virtual_state(self):
        _str = self._str
        s1 = _str("a")
        s2 = _str("AA")
        mydriver = JitDriver(reds = ['i', 'n', 'sa'], greens = [])
        def f(n):
            sa = s1
            i = 0
            while i < n:
                mydriver.jit_merge_point(i=i, n=n, sa=sa)
                if i&4 == 0:
                    sa += s1
                else:
                    sa += s2
                i += 1
            return len(sa)
        assert self.meta_interp(f, [16]) == f(16)

    def test_loop_invariant_string_slice(self):
        _str = self._str
        mydriver = JitDriver(reds = ['i', 'n', 'sa', 's', 's1'], greens = [])
        def f(n, c):
            s = s1 = _str(c*10)
            sa = i = 0
            while i < n:
                mydriver.jit_merge_point(i=i, n=n, sa=sa, s=s, s1=s1)
                sa += len(s)
                if i < n/2:
                    s = s1[1:3]
                else:
                    s = s1[2:3]
                i += 1
            return sa
        assert self.meta_interp(f, [16, 'a']) == f(16, 'a')

    def test_loop_invariant_string_slice_boxed(self):
        class Str(object):
            def __init__(self, value):
                self.value = value
        _str = self._str
        mydriver = JitDriver(reds = ['i', 'n', 'sa', 's', 's1'], greens = [])
        def f(n, c):
            s = s1 = Str(_str(c*10))
            sa = i = 0
            while i < n:
                mydriver.jit_merge_point(i=i, n=n, sa=sa, s=s, s1=s1)
                sa += len(s.value)
                if i < n/2:
                    s = Str(s1.value[1:3])
                else:
                    s = Str(s1.value[2:3])
                i += 1
            return sa
        assert self.meta_interp(f, [16, 'a']) == f(16, 'a')

    def test_loop_invariant_string_slice_in_array(self):
        _str = self._str
        mydriver = JitDriver(reds = ['i', 'n', 'sa', 's', 's1'], greens = [])
        def f(n, c):
            s = s1 = [_str(c*10)]
            sa = i = 0
            while i < n:
                mydriver.jit_merge_point(i=i, n=n, sa=sa, s=s, s1=s1)
                sa += len(s[0])
                if i < n/2:
                    s = [s1[0][1:3]]
                else:
                    s = [s1[0][2:3]]
                i += 1
            return sa
        assert self.meta_interp(f, [16, 'a']) == f(16, 'a')

    def test_boxed_virtual_string_not_surviving(self):
        class StrBox(object):
            def __init__(self, val):
                self.val = val
        class IntBox(object):
            def __init__(self, val):
                self.val = val
        _str = self._str
        mydriver = JitDriver(reds = ['i', 'nt', 'sa'], greens = [])
        def f(c):
            nt = StrBox(_str(c*16))
            sa = StrBox(_str(''))
            i = IntBox(0)
            while i.val < len(nt.val):
                mydriver.jit_merge_point(i=i, nt=nt, sa=sa)
                sa = StrBox(sa.val + StrBox(nt.val[i.val]).val)
                i = IntBox(i.val + 1)
            return len(sa.val)
        assert self.meta_interp(f, ['a']) == f('a')

    def test_string_comepare_quasiimmutable(self):
        class Sys(object):
            _immutable_fields_ = ["defaultencoding?"]
            def __init__(self, s):
                self.defaultencoding = s
        _str = self._str
        sys = Sys(_str('ascii'))
        mydriver = JitDriver(reds = ['n', 'sa'], greens = [])
        def f(n):
            sa = 0
            sys.defaultencoding = _str('ascii')
            while n:
                mydriver.jit_merge_point(n=n, sa=sa)
                if sys.defaultencoding == _str('ascii'):
                    sa += 1
                n -= 1
            sys.defaultencoding = _str('utf-8')
            return sa
        assert self.meta_interp(f, [8]) == f(8)
        self.check_resops({'jump': 1, 'int_is_true': 2, 'int_add': 2,
                           'guard_true': 2, 'guard_not_invalidated': 2,
                           'int_sub': 2})

    def test_promote_string(self):
        driver = JitDriver(greens = [], reds = ['n'])

        def f(n):
            while n < 21:
                driver.jit_merge_point(n=n)
                promote_string(str(n % 3))
                n += 1
            return 0

        self.meta_interp(f, [0])
        self.check_resops(call=7)

#class TestOOtype(StringTests, OOJitMixin):
#    CALL = "oosend"
#    CALL_PURE = "oosend_pure"

class TestLLtype(StringTests, LLJitMixin):
    CALL = "call"
    CALL_PURE = "call_pure"

class TestLLtypeUnicode(TestLLtype):
    _str, _chr = unicode, unichr

    def test_str2unicode(self):
        _str = self._str
        jitdriver = JitDriver(greens = [], reds = ['m', 'n'])
        class Foo:
            pass
        @dont_look_inside
        def escape(x):
            assert x == _str("6y")
        def f(n, m):
            while m >= 0:
                jitdriver.can_enter_jit(m=m, n=n)
                jitdriver.jit_merge_point(m=m, n=n)
                foo = Foo()
                foo.y = chr(m)
                foo.y = "y"
                s = _str(str(n)) + _str(foo.y)
                escape(s)
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        self.check_resops(unicodesetitem=2, newunicode=2, call=4,
                          copyunicodecontent=2, unicodegetitem=0)

    def test_str2unicode_fold(self):
        _str = self._str
        jitdriver = JitDriver(greens = ['g'], reds = ['m'])
        @dont_look_inside
        def escape(x):
            # a plain "print" would call os.write() and release the gil
            debug_print(str(x))
        def f(g, m):
            g = str(g)
            while m >= 0:
                jitdriver.can_enter_jit(g=g, m=m)
                jitdriver.jit_merge_point(g=g, m=m)
                escape(_str(g))
                m -= 1
            return 42
        self.meta_interp(f, [6, 7])
        self.check_resops(call_pure=0, unicodesetitem=0, call=2,
                          newunicode=0, unicodegetitem=0,
                          copyunicodecontent=0)

    def test_join_chars(self):
        jitdriver = JitDriver(reds=['a', 'b', 'c', 'i'], greens=[])
        def f(a, b, c):
            i = 0
            while i < 10:
                jitdriver.jit_merge_point(a=a, b=b, c=c, i=i)
                x = []
                if a:
                    x.append("a")
                if b:
                    x.append("b")
                if c:
                    x.append("c")
                i += len("".join(x))
            return i
        res = self.meta_interp(f, [1, 1, 1])
        assert res == f(True, True, True)
        # The "".join should be unrolled, since the length of x is known since
        # it is virtual, ensure there are no calls to ll_join_chars, or
        # allocations.
        self.check_resops({'jump': 1, 'guard_true': 5, 'int_lt': 2,
                           'int_add': 2, 'int_is_true': 3})

    def test_virtual_copystringcontent(self):
        jitdriver = JitDriver(reds=['n', 'result'], greens=[])
        def main(n):
            result = 0
            while n >= 0:
                jitdriver.jit_merge_point(n=n, result=result)
                b = StringBuilder(6)
                b.append("Hello!")
                result += ord(b.build()[0])
                n -= 1
            return result
        res = self.meta_interp(main, [9])
        assert res == main(9)

    def test_virtual_copystringcontent2(self):
        jitdriver = JitDriver(reds=['n', 'result'], greens=[])
        def main(n):
            result = 0
            while n >= 0:
                jitdriver.jit_merge_point(n=n, result=result)
                b = StringBuilder(6)
                b.append("Hello!")
                result += ord((b.build() + "xyz")[0])
                n -= 1
            return result
        res = self.meta_interp(main, [9])
        assert res == main(9)

    def test_bytearray(self):
        py.test.skip("implement it")
        def f(i):
            b = bytearray("abc")
            b[1] = i
            return b[1]

        res = self.interp_operations(f, [13])
        assert res == 13
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.