]> gitweb.factorcode.org Git - factor.git/blob - basis/locals/locals-tests.factor
d472a8b22b79a28b365e881a95d8ce53ec6139b9
[factor.git] / basis / locals / locals-tests.factor
1 USING: locals math sequences tools.test hashtables words kernel
2 namespaces arrays strings prettyprint io.streams.string parser
3 accessors generic eval combinators combinators.short-circuit
4 combinators.short-circuit.smart math.order math.functions
5 definitions compiler.units fry lexer words.symbol see multiline ;
6 IN: locals.tests
7
8 :: foo ( a b -- a a ) a a ;
9
10 [ 1 1 ] [ 1 2 foo ] unit-test
11
12 :: add-test ( a b -- c ) a b + ;
13
14 [ 3 ] [ 1 2 add-test ] unit-test
15
16 :: sub-test ( a b -- c ) a b - ;
17
18 [ -1 ] [ 1 2 sub-test ] unit-test
19
20 :: map-test ( a b -- seq ) a [ b + ] map ;
21
22 [ { 5 6 7 } ] [ { 1 2 3 } 4 map-test ] unit-test
23
24 :: map-test-2 ( seq inc -- seq ) seq [| elt | elt inc + ] map ;
25
26 [ { 5 6 7 } ] [ { 1 2 3 } 4 map-test-2 ] unit-test
27
28 :: let-test ( c -- d )
29     [let | a [ 1 ] b [ 2 ] | a b + c + ] ;
30
31 [ 7 ] [ 4 let-test ] unit-test
32
33 :: let-test-2 ( a -- a )
34     a [let | a [ ] | [let | b [ a ] | a ] ] ;
35
36 [ 3 ] [ 3 let-test-2 ] unit-test
37
38 :: let-test-3 ( a -- a )
39     a [let | a [ ] | [let | b [ [ a ] ] | [let | a [ 3 ] | b ] ] ] ;
40
41 :: let-test-4 ( a -- b )
42     a [let | a [ 1 ] b [ ] | a b 2array ] ;
43
44 [ { 1 2 } ] [ 2 let-test-4 ] unit-test
45
46 :: let-test-5 ( a -- b )
47     a [let | a [ ] b [ ] | a b 2array ] ;
48
49 [ { 2 1 } ] [ 1 2 let-test-5 ] unit-test
50
51 :: let-test-6 ( a -- b )
52     a [let | a [ ] b [ 1 ] | a b 2array ] ;
53
54 [ { 2 1 } ] [ 2 let-test-6 ] unit-test
55
56 [ -1 ] [ -1 let-test-3 call ] unit-test
57
58 [ 5 ] [
59     [let | a [ 3 ] | [wlet | func [ a + ] | 2 func ] ]
60 ] unit-test
61
62 :: wlet-test-2 ( a b -- seq )
63     [wlet | add-b [ b + ] |
64         a [ add-b ] map ] ;
65
66
67 [ { 4 5 6 } ] [ { 2 3 4 } 2 wlet-test-2 ] unit-test
68     
69 :: wlet-test-3 ( a -- b )
70     [wlet | add-a [ a + ] | [ add-a ] ]
71     [let | a [ 3 ] | a swap call ] ;
72
73 [ 5 ] [ 2 wlet-test-3 ] unit-test
74
75 :: wlet-test-4 ( a -- b )
76     [wlet | sub-a [| b | b a - ] |
77         3 sub-a ] ;
78
79 [ -7 ] [ 10 wlet-test-4 ] unit-test
80
81 :: write-test-1 ( n! -- q )
82     [| i | n i + dup n! ] ;
83
84 0 write-test-1 "q" set
85
86 { 1 1 } "q" get must-infer-as
87
88 [ 1 ] [ 1 "q" get call ] unit-test
89
90 [ 2 ] [ 1 "q" get call ] unit-test
91
92 [ 3 ] [ 1 "q" get call ] unit-test
93
94 [ 5 ] [ 2 "q" get call ] unit-test
95
96 :: write-test-2 ( -- q )
97     [let | n! [ 0 ] |
98         [| i | n i + dup n! ] ] ;
99
100 write-test-2 "q" set
101
102 [ 1 ] [ 1 "q" get call ] unit-test
103
104 [ 2 ] [ 1 "q" get call ] unit-test
105
106 [ 3 ] [ 1 "q" get call ] unit-test
107
108 [ 5 ] [ 2 "q" get call ] unit-test
109
110 [ 10 20 ]
111 [
112     20 10 [| a! | [| b! | a b ] ] call call
113 ] unit-test
114
115 :: write-test-3 ( a! -- q ) [| b | b a! ] ;
116
117 [ ] [ 1 2 write-test-3 call ] unit-test
118
119 :: write-test-4 ( x! -- q ) [ [let | y! [ 0 ] | f x! ] ] ;
120
121 [ ] [ 5 write-test-4 drop ] unit-test
122
123 ! Not really a write test; just enforcing consistency
124 :: write-test-5 ( x -- y )
125     [wlet | fun! [ x + ] | 5 fun! ] ;
126
127 [ 9 ] [ 4 write-test-5 ] unit-test
128
129 SYMBOL: a
130
131 :: use-test ( a b c -- a b c )
132     USE: kernel ;
133
134 [ t ] [ a symbol? ] unit-test
135
136 :: let-let-test ( n -- n ) [let | n [ n 3 + ] | n ] ;
137
138 [ 13 ] [ 10 let-let-test ] unit-test
139
140 GENERIC: lambda-generic ( a b -- c )
141
142 GENERIC# lambda-generic-1 1 ( a b -- c )
143
144 M:: integer lambda-generic-1 ( a b -- c ) a b * ;
145
146 M:: string lambda-generic-1 ( a b -- c )
147     a b CHAR: x <string> lambda-generic ;
148
149 M:: integer lambda-generic ( a b -- c ) a b lambda-generic-1 ;
150
151 GENERIC# lambda-generic-2 1 ( a b -- c )
152
153 M:: integer lambda-generic-2 ( a b -- c )
154     a CHAR: x <string> b lambda-generic ;
155
156 M:: string lambda-generic-2 ( a b -- c ) a b append ;
157
158 M:: string lambda-generic ( a b -- c ) a b lambda-generic-2 ;
159
160 [ 10 ] [ 5 2 lambda-generic ] unit-test
161
162 [ "abab" ] [ "aba" "b" lambda-generic ] unit-test
163
164 [ "abaxxx" ] [ "aba" 3 lambda-generic ] unit-test
165
166 [ "xaba" ] [ 1 "aba" lambda-generic ] unit-test
167
168 [ ] [ \ lambda-generic-1 see ] unit-test
169
170 [ ] [ \ lambda-generic-2 see ] unit-test
171
172 [ ] [ \ lambda-generic see ] unit-test
173
174 :: unparse-test-1 ( a -- ) [let | a! [ ] | ] ;
175
176 [ "[let | a! [ ] | ]" ] [
177     \ unparse-test-1 "lambda" word-prop body>> first unparse
178 ] unit-test
179
180 :: unparse-test-2 ( -- ) [wlet | a! [ ] | ] ;
181
182 [ "[wlet | a! [ ] | ]" ] [
183     \ unparse-test-2 "lambda" word-prop body>> first unparse
184 ] unit-test
185
186 :: unparse-test-3 ( -- b ) [| a! | ] ;
187
188 [ "[| a! | ]" ] [
189     \ unparse-test-3 "lambda" word-prop body>> first unparse
190 ] unit-test
191
192 DEFER: xyzzy
193
194 [ ] [
195     "IN: locals.tests USE: math GENERIC: xyzzy ( a -- b ) M: integer xyzzy ;"
196     <string-reader> "lambda-generic-test" parse-stream drop
197 ] unit-test
198
199 [ 10 ] [ 10 xyzzy ] unit-test
200
201 [ ] [
202     "IN: locals.tests USE: math USE: locals GENERIC: xyzzy ( a -- b ) M:: integer xyzzy ( n -- x ) 5 ;"
203     <string-reader> "lambda-generic-test" parse-stream drop
204 ] unit-test
205
206 [ 5 ] [ 10 xyzzy ] unit-test
207
208 :: let*-test-1 ( a -- b )
209     [let* | b [ a 1+ ]
210             c [ b 1+ ] |
211         a b c 3array ] ;
212
213 [ { 1 2 3 } ] [ 1 let*-test-1 ] unit-test
214
215 :: let*-test-2 ( a -- b )
216     [let* | b [ a 1+ ]
217             c! [ b 1+ ] |
218         a b c 3array ] ;
219
220 [ { 1 2 3 } ] [ 1 let*-test-2 ] unit-test
221
222 :: let*-test-3 ( a -- b )
223     [let* | b [ a 1+ ]
224             c! [ b 1+ ] |
225         c 1+ c!  a b c 3array ] ;
226
227 [ { 1 2 4 } ] [ 1 let*-test-3 ] unit-test
228
229 :: let*-test-4 ( a b -- c d )
230     [let | a [ b ]
231            b [ a ] |
232         [let* | a'  [ a  ]
233                 a'' [ a' ]
234                 b'  [ b  ]
235                 b'' [ b' ] |
236             a'' b'' ] ] ;
237
238 [ "xxx" "yyy" ] [ "yyy" "xxx" let*-test-4 ] unit-test
239
240 GENERIC: next-method-test ( a -- b )
241
242 M: integer next-method-test 3 + ;
243
244 M:: fixnum next-method-test ( a -- b ) a call-next-method 1 + ;
245
246 [ 5 ] [ 1 next-method-test ] unit-test
247
248 : no-with-locals-test ( -- seq ) { 1 2 3 } [| x | x 3 + ] map ;
249
250 [ { 4 5 6 } ] [ no-with-locals-test ] unit-test
251
252 { 3 0 } [| a b c | ] must-infer-as
253
254 [ ] [ 1 [let | a [ ] | ] ] unit-test
255
256 [ 3 ] [ 1 [let | a [ ] | 3 ] ] unit-test
257
258 [ ] [ 1 2 [let | a [ ] b [ ] | ] ] unit-test
259
260 :: a-word-with-locals ( a b -- ) ;
261
262 CONSTANT: new-definition "USING: math ;\nIN: locals.tests\n: a-word-with-locals ( -- x ) 2 3 + ;\n"
263
264 [ ] [ new-definition eval( -- ) ] unit-test
265
266 [ t ] [
267     [ \ a-word-with-locals see ] with-string-writer
268     new-definition =
269 ] unit-test
270
271 CONSTANT: method-definition "USING: locals locals.tests sequences ;\nM:: sequence method-with-locals ( a -- y ) a reverse ;\n"
272
273 GENERIC: method-with-locals ( x -- y )
274
275 M:: sequence method-with-locals ( a -- y ) a reverse ;
276
277 [ t ] [
278     [ \ sequence \ method-with-locals method see ] with-string-writer
279     method-definition =
280 ] unit-test
281
282 :: cond-test ( a b -- c )
283     {
284         { [ a b < ] [ 3 ] }
285         { [ a b = ] [ 4 ] }
286         { [ a b > ] [ 5 ] }
287     } cond ;
288
289 \ cond-test must-infer
290
291 [ 3 ] [ 1 2 cond-test ] unit-test
292 [ 4 ] [ 2 2 cond-test ] unit-test
293 [ 5 ] [ 3 2 cond-test ] unit-test
294
295 :: 0&&-test ( a -- ? )
296     { [ a integer? ] [ a even? ] [ a 10 > ] } 0&& ;
297
298 \ 0&&-test must-infer
299
300 [ f ] [ 1.5 0&&-test ] unit-test
301 [ f ] [ 3 0&&-test ] unit-test
302 [ f ] [ 8 0&&-test ] unit-test
303 [ t ] [ 12 0&&-test ] unit-test
304
305 :: &&-test ( a -- ? )
306     { [ a integer? ] [ a even? ] [ a 10 > ] } && ;
307
308 \ &&-test must-infer
309
310 [ f ] [ 1.5 &&-test ] unit-test
311 [ f ] [ 3 &&-test ] unit-test
312 [ f ] [ 8 &&-test ] unit-test
313 [ t ] [ 12 &&-test ] unit-test
314
315 :: let-and-cond-test-1 ( -- a )
316     [let | a [ 10 ] |
317         [let | a [ 20 ] |
318             {
319                 { [ t ] [ [let | c [ 30 ] | a ] ] }
320             } cond
321         ]
322     ] ;
323
324 \ let-and-cond-test-1 must-infer
325
326 [ 20 ] [ let-and-cond-test-1 ] unit-test
327
328 :: let-and-cond-test-2 ( -- pair )
329     [let | A [ 10 ] |
330         [let | B [ 20 ] |
331             { { [ t ] [ { A B } ] } } cond
332         ]
333     ] ;
334
335 \ let-and-cond-test-2 must-infer
336
337 [ { 10 20 } ] [ let-and-cond-test-2 ] unit-test
338
339 [ { 10       } ] [ 10       [| a     | { a     } ] call ] unit-test
340 [ { 10 20    } ] [ 10 20    [| a b   | { a b   } ] call ] unit-test
341 [ { 10 20 30 } ] [ 10 20 30 [| a b c | { a b c } ] call ] unit-test
342
343 [ { 10 20 30 } ] [ [let | a [ 10 ] b [ 20 ] c [ 30 ] | { a b c } ] ] unit-test
344
345 [ V{ 10 20 30 } ] [ 10 20 30 [| a b c | V{ a b c } ] call ] unit-test
346
347 [ H{ { 10 "a" } { 20 "b" } { 30 "c" } } ]
348 [ 10 20 30 [| a b c | H{ { a "a" } { b "b" } { c "c" } } ] call ] unit-test
349
350 [ T{ slice f 0 3 "abc" } ]
351 [ 0 3 "abc" [| from to seq | T{ slice f from to seq } ] call ] unit-test
352
353 { 3 1 } [| from to seq | T{ slice f from to seq } ] must-infer-as
354
355 ERROR: punned-class x ;
356
357 [ T{ punned-class f 3 } ] [ 3 [| a | T{ punned-class f a } ] call ] unit-test
358
359 :: literal-identity-test ( -- a b )
360     { 1 } V{ } ;
361
362 [ t t ] [
363     literal-identity-test
364     literal-identity-test
365     [ eq? ] [ eq? ] bi-curry* bi*
366 ] unit-test
367
368 :: mutable-local-in-literal-test ( a! -- b ) a 1 + a! { a } ;
369
370 [ { 4 } ] [ 3 mutable-local-in-literal-test ] unit-test
371
372 :: compare-case ( obj1 obj2 lt-quot eq-quot gt-quot -- )
373     obj1 obj2 <=> {
374         { +lt+ [ lt-quot call ] }
375         { +eq+ [ eq-quot call ] }
376         { +gt+ [ gt-quot call ] }
377     } case ; inline
378
379 [ [ ] [ ] [ ] compare-case ] must-infer
380
381 :: big-case-test ( a -- b )
382     a {
383         { 0 [ a 1 + ] }
384         { 1 [ a 1 - ] }
385         { 2 [ a 1 swap / ] }
386         { 3 [ a dup * ] }
387         { 4 [ a sqrt ] }
388         { 5 [ a a ^ ] }
389     } case ;
390
391 \ big-case-test must-infer
392
393 [ 9 ] [ 3 big-case-test ] unit-test
394
395 ! Dan found this problem
396 : littledan-case-problem-1 ( a -- b )
397     {
398         { t [ 3 ] }
399         { f [ 4 ] }
400         [| x | x 12 + { "howdy" } nth ]
401     } case ;
402
403 \ littledan-case-problem-1 must-infer
404
405 [ "howdy" ] [ -12 \ littledan-case-problem-1 def>> call ] unit-test
406 [ "howdy" ] [ -12 littledan-case-problem-1 ] unit-test
407
408 :: littledan-case-problem-2 ( a -- b )
409     a {
410         { t [ a not ] }
411         { f [ 4 ] }
412         [| x | x a - { "howdy" } nth ]
413     } case ;
414
415 \ littledan-case-problem-2 must-infer
416
417 [ "howdy" ] [ -12 \ littledan-case-problem-2 def>> call ] unit-test
418 [ "howdy" ] [ -12 littledan-case-problem-2 ] unit-test
419
420 :: littledan-cond-problem-1 ( a -- b )
421     a {
422         { [ dup 0 < ] [ drop a not ] }
423         { [| y | y y 0 > ] [ drop 4 ] }
424         [| x | x a - { "howdy" } nth ]
425     } cond ;
426
427 \ littledan-cond-problem-1 must-infer
428
429 [ f ] [ -12 \ littledan-cond-problem-1 def>> call ] unit-test
430 [ 4 ] [ 12 \ littledan-cond-problem-1 def>> call ] unit-test
431 [ "howdy" ] [ 0 \ littledan-cond-problem-1 def>> call ] unit-test
432 [ f ] [ -12 littledan-cond-problem-1 ] unit-test
433 [ 4 ] [ 12 littledan-cond-problem-1 ] unit-test
434 [ "howdy" ] [ 0 littledan-cond-problem-1 ] unit-test
435
436 /*
437 :: littledan-case-problem-3 ( a quot -- b )
438     a {
439         { t [ a not ] }
440         { f [ 4 ] }
441         quot
442     } case ; inline
443
444 [ f ] [ t [ ] littledan-case-problem-3 ] unit-test
445 [ 144 ] [ 12 [ sq ] littledan-case-problem-3 ] unit-test
446 [| | [| a | a ] littledan-case-problem-3 ] must-infer
447
448 : littledan-case-problem-4 ( a -- b )
449     [ 1 + ] littledan-case-problem-3 ;
450
451 \ littledan-case-problem-4 must-infer
452 */
453
454 GENERIC: lambda-method-forget-test ( a -- b )
455
456 M:: integer lambda-method-forget-test ( a -- b ) ;
457
458 [ ] [ [ M\ integer lambda-method-forget-test forget ] with-compilation-unit ] unit-test
459
460 [ 10 ] [ 10 [| A | { [ A ] } ] call first call ] unit-test
461
462 [
463     "USING: locals fry math ; 1 '[ [let | A [ 10 ] | A _ + ] ]"
464     eval( -- ) call
465 ] [ error>> >r/r>-in-fry-error? ] must-fail-with
466     
467 :: (funny-macro-test) ( obj quot -- ? ) obj { quot } 1&& ; inline
468 : funny-macro-test ( n -- ? ) [ odd? ] (funny-macro-test) ;
469
470 \ funny-macro-test must-infer
471
472 [ t ] [ 3 funny-macro-test ] unit-test
473 [ f ] [ 2 funny-macro-test ] unit-test
474
475 ! Some odd parser corner cases
476 [ "USE: locals [let" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
477 [ "USE: locals [let |" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
478 [ "USE: locals [let | a" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
479 [ "USE: locals [|" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
480
481 [ 25 ] [ 5 [| a | { [ a sq ] } cond ] call ] unit-test
482 [ 25 ] [ 5 [| | { [| a | a sq ] } ] call first call ] unit-test
483
484 :: FAILdog-1 ( -- b ) { [| c | c ] } ;
485
486 \ FAILdog-1 must-infer
487
488 :: FAILdog-2 ( a -- b ) a { [| c | c ] } cond ;
489
490 \ FAILdog-2 must-infer
491
492 [ 3 ] [ 3 [| a | \ a ] call ] unit-test
493
494 [ "USE: locals [| | { [let | a [ 0 ] | a ] } ]" eval( -- ) ] must-fail
495
496 [ "USE: locals [| | { [wlet | a [ 0 ] | a ] } ]" eval( -- ) ] must-fail
497
498 [ "USE: locals [| | { [let* | a [ 0 ] | a ] } ]" eval( -- ) ] must-fail
499
500 [ "USE: locals [| | [let | a! [ 0 ] | { a! } ] ]" eval( -- ) ] must-fail
501
502 [ "USE: locals [| | [wlet | a [ 0 ] | { a } ] ]" eval( -- ) ] must-fail
503
504 [ "USE: locals [| | { :> a } ]" eval( -- ) ] must-fail
505
506 [ "USE: locals 3 :> a" eval( -- ) ] must-fail
507
508 [ 3 ] [ 3 [| | :> a a ] call ] unit-test
509
510 [ 3 ] [ 3 [| | :> a! a ] call ] unit-test
511
512 [ 3 ] [ 2 [| | :> a! a 1+ a! a ] call ] unit-test
513
514 :: wlet-&&-test ( a -- ? )
515     [wlet | is-integer? [ a integer? ]
516             is-even? [ a even? ]
517             >10? [ a 10 > ] |
518         { [ is-integer? ] [ is-even? ] [ >10? ] } &&
519     ] ;
520
521 \ wlet-&&-test must-infer
522 [ f ] [ 1.5 wlet-&&-test ] unit-test
523 [ f ] [ 3 wlet-&&-test ] unit-test
524 [ f ] [ 8 wlet-&&-test ] unit-test
525 [ t ] [ 12 wlet-&&-test ] unit-test
526
527 : fry-locals-test-1 ( -- n )
528     [let | | 6 '[ [let | A [ 4 ] | A _ + ] ] call ] ;
529
530 \ fry-locals-test-1 must-infer
531 [ 10 ] [ fry-locals-test-1 ] unit-test
532
533 :: fry-locals-test-2 ( -- n )
534     [let | | 6 '[ [let | A [ 4 ] | A _ + ] ] call ] ;
535
536 \ fry-locals-test-2 must-infer
537 [ 10 ] [ fry-locals-test-2 ] unit-test
538
539 [ 1 ] [ 3 4 [| | '[ [ _ swap - ] call ] call ] call ] unit-test
540 [ -1 ] [ 3 4 [| | [| a | a - ] call ] call ] unit-test
541 [ -1 ] [ 3 4 [| | [| a | a - ] curry call ] call ] unit-test
542 [ -1 ] [ 3 4 [| a | a - ] curry call ] unit-test
543 [ 1 ] [ 3 4 [| | '[ [| a | _ a - ] call ] call ] call ] unit-test
544 [ -1 ] [ 3 4 [| | '[ [| a | a _ - ] call ] call ] call ] unit-test
545
546 [ { 1 2 3 4 } ] [
547     1 3 2 4
548     [| | '[ [| a b | a _ b _ 4array ] call ] call ] call
549 ] unit-test
550
551 [ 10 ] [
552     [| | 0 '[ [let | A [ 10 ] | A _ + ] ] call ] call
553 ] unit-test
554
555 ! littledan found this problem
556 [ "bar" ] [ [let | a [ [let | foo [ "bar" ] | foo ] ] | a ] ] unit-test
557 [ 10 ] [ [let | a [ 10 ] | [let | b [ a ] | b ] ] ] unit-test
558
559 [ { \ + } ] [ [let | x [ \ + ] | { \ x } ] ] unit-test
560
561 [ { \ + 3 } ] [ [let | a [ 3 ] | { \ + a } ] ] unit-test
562
563 [ 3 ] [ [let | a [ \ + ] | 1 2 [ \ a execute ] ] call ] unit-test
564
565 ! erg found this problem
566 :: erg's-:>-bug ( n ? -- n ) ? [ n :> n n ] [ n :> b b ] if ;
567
568 [ 3 ] [ 3 f erg's-:>-bug ] unit-test
569     
570 [ 3 ] [ 3 t erg's-:>-bug ] unit-test
571
572 :: erg's-:>-bug-2 ( n ? -- n ) ? n '[ _ :> n n ] [ n :> b b ] if ;
573
574 [ 3 ] [ 3 f erg's-:>-bug-2 ] unit-test
575     
576 [ 3 ] [ 3 t erg's-:>-bug-2 ] unit-test
577
578 ! dharmatech found this problem
579 GENERIC: ed's-bug ( a -- b )
580
581 M: string ed's-bug reverse ;
582 M: integer ed's-bug neg ;
583
584 :: ed's-test-case ( a -- b )
585    { [ a ed's-bug ] } && ;
586
587 [ t ] [ \ ed's-test-case optimized>> ] unit-test